2021-10-26 22:58:20 +08:00
|
|
|
#!/bin/sh
|
2021-03-30 06:12:30 +08:00
|
|
|
|
2021-10-26 22:58:20 +08:00
|
|
|
if [ $(id -u) -ne 0 ]; then
|
2021-11-02 19:01:26 +08:00
|
|
|
echo "This script must be run as root"
|
2021-06-03 12:55:39 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-10-26 22:58:20 +08:00
|
|
|
echo "checking dependencies..."
|
|
|
|
|
2021-11-16 22:51:37 +08:00
|
|
|
OS=$(uname)
|
|
|
|
|
2021-10-26 22:58:20 +08:00
|
|
|
if [ -f /etc/debian_version ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
dependencies="wireguard wireguard-tools"
|
|
|
|
update_cmd='apt update'
|
2021-10-26 22:58:20 +08:00
|
|
|
install_cmd='apt-get install -y'
|
|
|
|
elif [ -f /etc/alpine-release ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
dependencies="wireguard"
|
|
|
|
update_cmd='apk update'
|
2021-10-26 22:58:20 +08:00
|
|
|
install_cmd='apk --update add'
|
|
|
|
elif [ -f /etc/centos-release ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
dependencies="wireguard"
|
|
|
|
update_cmd='yum update'
|
2021-10-26 22:58:20 +08:00
|
|
|
install_cmd='yum install -y'
|
|
|
|
elif [ -f /etc/fedora-release ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
dependencies="wireguard"
|
|
|
|
update_cmd='dnf update'
|
2021-10-26 22:58:20 +08:00
|
|
|
install_cmd='dnf install -y'
|
2022-02-23 21:23:07 +08:00
|
|
|
elif [ -f /etc/redhat-release ]; then
|
|
|
|
dependencies="wireguard"
|
|
|
|
update_cmd='yum update'
|
|
|
|
install_cmd='yum install -y'
|
2022-03-20 21:50:41 +08:00
|
|
|
elif [ -f /etc/arch-release ]; then
|
2022-02-10 23:13:54 +08:00
|
|
|
dependecies="wireguard-tools"
|
|
|
|
update_cmd='pacman -Sy'
|
|
|
|
install_cmd='pacman -S --noconfirm'
|
2021-11-17 03:18:41 +08:00
|
|
|
elif [ "${OS}" = "FreeBSD" ]; then
|
2022-06-22 23:52:01 +08:00
|
|
|
dependencies="wireguard wget"
|
2021-11-22 20:54:01 +08:00
|
|
|
update_cmd='pkg update'
|
2021-11-16 22:51:37 +08:00
|
|
|
install_cmd='pkg install -y'
|
2022-10-15 20:41:41 +08:00
|
|
|
elif [ -f /etc/turris-version ]; then
|
|
|
|
dependencies="wireguard-tools bash"
|
|
|
|
OS="TurrisOS"
|
|
|
|
update_cmd='opkg update'
|
|
|
|
install_cmd='opkg install'
|
2021-11-22 20:54:01 +08:00
|
|
|
elif [ -f /etc/openwrt_release ]; then
|
2022-07-14 01:45:13 +08:00
|
|
|
dependencies="wireguard-tools bash"
|
2021-11-22 20:54:01 +08:00
|
|
|
OS="OpenWRT"
|
|
|
|
update_cmd='opkg update'
|
|
|
|
install_cmd='opkg install'
|
2021-10-26 22:58:20 +08:00
|
|
|
else
|
|
|
|
install_cmd=''
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${install_cmd}" ]; then
|
|
|
|
echo "OS unsupported for automatic dependency install"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-11-22 20:54:01 +08:00
|
|
|
|
2022-07-08 16:53:21 +08:00
|
|
|
${update_cmd}
|
|
|
|
|
2021-10-26 22:58:20 +08:00
|
|
|
set -- $dependencies
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
echo $1
|
2021-11-17 03:18:41 +08:00
|
|
|
if [ "${OS}" = "FreeBSD" ]; then
|
2021-11-17 09:56:22 +08:00
|
|
|
is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
|
|
|
|
if [ "$is_installed" != "" ]; then
|
2021-11-16 22:51:37 +08:00
|
|
|
echo " " $1 is installed
|
|
|
|
else
|
|
|
|
echo " " $1 is not installed. Attempting install.
|
|
|
|
${install_cmd} $1
|
|
|
|
sleep 5
|
2021-11-17 09:56:22 +08:00
|
|
|
is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
|
|
|
|
if [ "$is_installed" != "" ]; then
|
2021-11-16 22:51:37 +08:00
|
|
|
echo " " $1 is installed
|
|
|
|
elif [ -x "$(command -v $1)" ]; then
|
|
|
|
echo " " $1 is installed
|
|
|
|
else
|
|
|
|
echo " " FAILED TO INSTALL $1
|
|
|
|
echo " " This may break functionality.
|
|
|
|
fi
|
|
|
|
fi
|
2021-10-26 22:58:20 +08:00
|
|
|
else
|
2022-10-15 20:41:41 +08:00
|
|
|
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
is_installed=$(opkg list-installed $1 | grep $1)
|
|
|
|
else
|
|
|
|
is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
|
|
|
|
fi
|
|
|
|
if [ "${is_installed}" != "" ]; then
|
2021-10-26 22:58:20 +08:00
|
|
|
echo " " $1 is installed
|
|
|
|
else
|
2021-11-16 22:51:37 +08:00
|
|
|
echo " " $1 is not installed. Attempting install.
|
|
|
|
${install_cmd} $1
|
|
|
|
sleep 5
|
2022-10-15 20:41:41 +08:00
|
|
|
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
is_installed=$(opkg list-installed $1 | grep $1)
|
|
|
|
else
|
|
|
|
is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
|
|
|
|
fi
|
|
|
|
if [ "${is_installed}" != "" ]; then
|
2021-11-16 22:51:37 +08:00
|
|
|
echo " " $1 is installed
|
|
|
|
elif [ -x "$(command -v $1)" ]; then
|
|
|
|
echo " " $1 is installed
|
|
|
|
else
|
|
|
|
echo " " FAILED TO INSTALL $1
|
|
|
|
echo " " This may break functionality.
|
|
|
|
fi
|
2021-10-26 22:58:20 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2021-03-30 19:57:51 +08:00
|
|
|
[ -z "$KEY" ] && KEY=nokey;
|
2021-10-20 23:06:54 +08:00
|
|
|
[ -z "$VERSION" ] && echo "no \$VERSION provided, fallback to latest" && VERSION=latest;
|
2021-10-31 22:58:45 +08:00
|
|
|
[ "latest" != "$VERSION" ] && [ "v" != `echo $VERSION | cut -c1` ] && VERSION="v$VERSION"
|
2021-11-02 19:01:26 +08:00
|
|
|
[ -z "$NAME" ] && NAME="";
|
2021-03-30 06:12:30 +08:00
|
|
|
|
2021-10-20 23:06:54 +08:00
|
|
|
dist=netclient
|
|
|
|
|
2021-10-21 00:29:59 +08:00
|
|
|
echo "OS Version = $(uname)"
|
2021-10-20 23:06:54 +08:00
|
|
|
echo "Netclient Version = $VERSION"
|
|
|
|
|
2022-07-14 01:25:38 +08:00
|
|
|
case $(uname | tr A-Z a-z) in
|
2021-10-26 22:58:20 +08:00
|
|
|
linux*)
|
|
|
|
if [ -z "$CPU_ARCH" ]; then
|
|
|
|
CPU_ARCH=$(uname -m)
|
|
|
|
fi
|
|
|
|
case $CPU_ARCH in
|
|
|
|
amd64)
|
|
|
|
dist=netclient
|
|
|
|
;;
|
|
|
|
x86_64)
|
|
|
|
dist=netclient
|
|
|
|
;;
|
|
|
|
arm64)
|
|
|
|
dist=netclient-arm64
|
|
|
|
;;
|
|
|
|
aarch64)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-arm64
|
2021-10-26 22:58:20 +08:00
|
|
|
;;
|
2022-01-11 01:20:33 +08:00
|
|
|
armv6l)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-arm6
|
2022-01-11 01:20:33 +08:00
|
|
|
;;
|
2021-11-03 16:23:25 +08:00
|
|
|
armv7l)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-arm7
|
2021-11-03 16:23:25 +08:00
|
|
|
;;
|
2021-10-26 22:58:20 +08:00
|
|
|
arm*)
|
|
|
|
dist=netclient-$CPU_ARCH
|
2021-11-17 23:23:29 +08:00
|
|
|
;;
|
2022-10-23 15:36:16 +08:00
|
|
|
mipsle)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-mipsle
|
2022-10-23 15:36:16 +08:00
|
|
|
;;
|
2022-11-21 21:35:15 +08:00
|
|
|
mips)
|
|
|
|
#If binary in the below condition is not compatible with your hardware, retry with other netclient-mips* binaries.
|
|
|
|
if [[ `printf '\0\1' | hexdump -e '/2 "%04x"'` -eq 0100 ]]; then
|
|
|
|
#Little Endian, tested and confirmed in GL-MT1300 OS "OpenWrt 19.07.8"
|
|
|
|
dist=netclient-mipsle-softfloat
|
|
|
|
else
|
|
|
|
#Big Endian, tested and confirmed in DSL-2750U OS "OpenWrt 22.03.2"
|
|
|
|
dist=netclient-mips-softfloat
|
|
|
|
fi
|
2021-11-17 23:21:35 +08:00
|
|
|
;;
|
2021-10-26 22:58:20 +08:00
|
|
|
*)
|
|
|
|
fatal "$CPU_ARCH : cpu architecture not supported"
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
darwin)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-darwin
|
2021-10-26 22:58:20 +08:00
|
|
|
;;
|
2021-12-13 17:40:51 +08:00
|
|
|
Darwin)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-darwin
|
2021-12-13 17:40:51 +08:00
|
|
|
;;
|
2021-11-16 22:51:37 +08:00
|
|
|
freebsd*)
|
|
|
|
if [ -z "$CPU_ARCH" ]; then
|
|
|
|
CPU_ARCH=$(uname -m)
|
|
|
|
fi
|
|
|
|
case $CPU_ARCH in
|
|
|
|
amd64)
|
|
|
|
dist=netclient-freebsd
|
|
|
|
;;
|
|
|
|
x86_64)
|
|
|
|
dist=netclient-freebsd
|
|
|
|
;;
|
|
|
|
arm64)
|
|
|
|
dist=netclient-freebsd-arm64
|
|
|
|
;;
|
|
|
|
aarch64)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-freebsd-arm64
|
2021-11-16 22:51:37 +08:00
|
|
|
;;
|
|
|
|
armv7l)
|
2022-07-08 16:53:21 +08:00
|
|
|
dist=netclient-freebsd-arm7
|
2021-11-16 22:51:37 +08:00
|
|
|
;;
|
|
|
|
arm*)
|
|
|
|
dist=netclient-freebsd-$CPU_ARCH
|
2022-07-08 16:53:21 +08:00
|
|
|
;;
|
2021-11-16 22:51:37 +08:00
|
|
|
*)
|
|
|
|
fatal "$CPU_ARCH : cpu architecture not supported"
|
|
|
|
esac
|
|
|
|
;;
|
2021-10-26 22:58:20 +08:00
|
|
|
esac
|
2021-10-20 23:06:54 +08:00
|
|
|
|
|
|
|
echo "Binary = $dist"
|
|
|
|
|
2021-11-01 01:18:07 +08:00
|
|
|
url="https://github.com/gravitl/netmaker/releases/download/$VERSION/$dist"
|
2021-11-22 20:54:01 +08:00
|
|
|
curl_opts='-nv'
|
2022-10-15 20:41:41 +08:00
|
|
|
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
|
2021-11-22 20:54:01 +08:00
|
|
|
curl_opts='-q'
|
|
|
|
fi
|
|
|
|
|
2021-11-01 01:18:07 +08:00
|
|
|
if curl --output /dev/null --silent --head --fail "$url"; then
|
|
|
|
echo "Downloading $dist $VERSION"
|
2021-11-22 20:54:01 +08:00
|
|
|
wget $curl_opts -O netclient $url
|
2021-11-01 01:18:07 +08:00
|
|
|
else
|
|
|
|
echo "Downloading $dist latest"
|
2022-02-10 12:10:05 +08:00
|
|
|
wget $curl_opts -O netclient https://github.com/gravitl/netmaker/releases/latest/download/$dist
|
2021-11-01 01:18:07 +08:00
|
|
|
fi
|
2021-11-17 09:56:22 +08:00
|
|
|
|
2021-03-30 06:12:30 +08:00
|
|
|
chmod +x netclient
|
2021-11-05 21:59:42 +08:00
|
|
|
|
2021-11-17 03:18:41 +08:00
|
|
|
EXTRA_ARGS=""
|
2022-10-15 20:41:41 +08:00
|
|
|
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
|
2021-11-17 09:56:22 +08:00
|
|
|
EXTRA_ARGS="--daemon=off"
|
2021-11-16 22:51:37 +08:00
|
|
|
fi
|
|
|
|
|
2022-06-22 23:57:27 +08:00
|
|
|
if [ "${KEY}" != "nokey" ]; then
|
|
|
|
if [ -z "${NAME}" ]; then
|
|
|
|
./netclient join -t $KEY $EXTRA_ARGS
|
|
|
|
else
|
|
|
|
./netclient join -t $KEY --name $NAME $EXTRA_ARGS
|
|
|
|
fi
|
2021-11-05 21:59:42 +08:00
|
|
|
fi
|
|
|
|
|
2022-06-23 00:02:39 +08:00
|
|
|
if [ "${OS}" = "FreeBSD" ]; then
|
2022-06-23 01:03:04 +08:00
|
|
|
if ! [ -x /usr/sbin/netclient ]; then
|
2022-06-23 00:11:06 +08:00
|
|
|
echo "Moving netclient executable to \"/usr/sbin/netclient\""
|
|
|
|
mv netclient /usr/sbin
|
|
|
|
else
|
|
|
|
echo "Netclient already present."
|
|
|
|
fi
|
2022-06-23 00:02:39 +08:00
|
|
|
fi
|
2021-11-16 22:51:37 +08:00
|
|
|
|
2022-10-15 20:41:41 +08:00
|
|
|
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
|
2022-02-10 22:56:46 +08:00
|
|
|
mv ./netclient /sbin/netclient
|
2022-10-15 20:41:41 +08:00
|
|
|
|
|
|
|
if [ "${OS}" = "TurrisOS" ]; then
|
|
|
|
url="https://raw.githubusercontent.com/gravitl/netmaker/$VERSION/scripts/openwrt-daemon.sh"
|
|
|
|
if curl --output /dev/null --silent --head --fail $url; then
|
|
|
|
wget $curl_opts -O netclient.service.tmp $url
|
|
|
|
else
|
|
|
|
wget $curl_opts -O netclient.service.tmp https://raw.githubusercontent.com/gravitl/netmaker/master/scripts/openwrt-daemon.sh
|
|
|
|
fi
|
2022-10-23 15:36:16 +08:00
|
|
|
elif [ "${OS}" = "OpenWRT" ] && [ "$CPU_ARCH" = "mips" ]; then
|
|
|
|
wget $curl_opts -O netclient.service.tmp https://raw.githubusercontent.com/gravitl/netmaker/master/scripts/openwrt-daemon.sh
|
2022-10-15 20:41:41 +08:00
|
|
|
else
|
|
|
|
cat << 'END_OF_FILE' > ./netclient.service.tmp
|
2021-11-22 20:54:01 +08:00
|
|
|
#!/bin/sh /etc/rc.common
|
|
|
|
|
|
|
|
EXTRA_COMMANDS="status"
|
|
|
|
EXTRA_HELP=" status Check service is running"
|
|
|
|
START=99
|
|
|
|
|
|
|
|
LOG_FILE="/tmp/netclient.logs"
|
|
|
|
|
|
|
|
start() {
|
|
|
|
if [ ! -f "${LOG_FILE}" ];then
|
|
|
|
touch "${LOG_FILE}"
|
|
|
|
fi
|
2022-02-10 22:56:46 +08:00
|
|
|
local PID=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
|
2021-11-22 20:54:01 +08:00
|
|
|
if [ "${PID}" ];then
|
|
|
|
echo "service is running"
|
|
|
|
return
|
|
|
|
fi
|
2022-02-10 22:56:46 +08:00
|
|
|
bash -c "do /sbin/netclient daemon >> ${LOG_FILE} 2>&1;\
|
2021-11-22 20:54:01 +08:00
|
|
|
if [ $(ls -l ${LOG_FILE}|awk '{print $5}') -gt 10240000 ];then tar zcf "${LOG_FILE}.tar" -C / "tmp/netclient.logs" && > $LOG_FILE;fi;done &"
|
|
|
|
echo "start"
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2022-02-10 22:56:46 +08:00
|
|
|
pids=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
|
2021-11-22 20:54:01 +08:00
|
|
|
for i in "${pids[@]}"
|
|
|
|
do
|
|
|
|
if [ "${i}" ];then
|
|
|
|
kill "${i}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "stop"
|
|
|
|
}
|
|
|
|
|
|
|
|
status() {
|
2022-02-10 22:56:46 +08:00
|
|
|
local PID=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
|
2021-11-22 20:54:01 +08:00
|
|
|
if [ "${PID}" ];then
|
|
|
|
echo -e "netclient[${PID}] is running \n"
|
|
|
|
else
|
|
|
|
echo -e "netclient is not running \n"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
END_OF_FILE
|
2022-10-15 20:41:41 +08:00
|
|
|
fi
|
2021-11-22 20:54:01 +08:00
|
|
|
mv ./netclient.service.tmp /etc/init.d/netclient
|
|
|
|
chmod +x /etc/init.d/netclient
|
|
|
|
/etc/init.d/netclient enable
|
|
|
|
/etc/init.d/netclient start
|
|
|
|
else
|
2021-11-17 09:56:22 +08:00
|
|
|
rm -f netclient
|
2022-10-23 15:36:16 +08:00
|
|
|
fi
|