-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzt.sh
executable file
·147 lines (121 loc) · 2.95 KB
/
zt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#
# script to install and manage Zerotier on a Synology NAS
# will also migrate settings from the Synology spkg
# based on https://docs.zerotier.com/devices/synology/
set -eEuo pipefail
RC_SCRIPT=/usr/local/etc/rc.d/tun.sh
TUN_DEV=/dev/net/tun
DOCKER_VOLUME=/volume1/docker/zerotier-one
DOCKER_IMAGE_VERSION=latest
DOCKER_IMAGE=zerotier/zerotier-synology:${DOCKER_IMAGE_VERSION:-latest}
DOCKER_CONTAINER=zerotier
function usage {
cat <<EOF >&2
$(basename -- "$0") <command> [command options]
Custom commands:
setup
start
stop
upgrade
shell [shell command] [shell args]
everything else is passed through to the zerotier-one cli inside the Docker container
eg.
info
status
join <network id>
-j listnetworks
EOF
exit 1
}
function invokeCommand {
local -r cmdArgs=("$@")
local -r cmd="cmd_${cmdArgs[0]}"
local -r cmdOpts=("${cmdArgs[@]:1}")
if [[ "$(type -t "$cmd")" == "function" ]] ; then
"$cmd" "${cmdOpts[@]}"
else
cmd_zt_cli "$@"
fi
}
function cmd_setup {
# ref: https://memoryleak.dev/post/fix-tun-tap-not-available-on-a-synology-nas/
if [ ! -f "$RC_SCRIPT" ] ; then
cat <<EOF > "$RC_SCRIPT"
#!/bin/sh -e
if ! lsmod | grep -qw tun ; then
insmod /lib/modules/tun.ko
fi
EOF
fi
if [ ! -x "$RC_SCRIPT" ] ; then
chmod a+x "$RC_SCRIPT"
fi
"$RC_SCRIPT"
if [ ! -r "$TUN_DEV" ] ; then
echo ERROR "$TUN_DEV" not found >&2
exit 1
fi
if ! command -v docker 2> /dev/null ; then
echo ERROR Docker not installed. Attempting install... >&2
if ! synopkg install_from_server Docker ; then
echo ERROR Docker failed to install. Install manually >&2
exit 2
fi
fi
if [ ! -d "$DOCKER_VOLUME" ] ; then
# TODO query Docker pkg volume root
# shellcheck disable=SC2174
mkdir -p -m 0700 "$DOCKER_VOLUME"
fi
if [ -d /var/lib/zerotier-one ] \
&& [ -r /var/lib/zerotier-one/identity.secret ] ; then
# we have config from the Synology package
if [ ! -r "$DOCKER_VOLUME/identity.secret" ] ; then
echo Found config in /var/lib/zerotier-one
echo Copying to Docker volume
cp -av /var/lib/zerotier-one/* "$DOCKER_VOLUME/"
fi
fi
cmd_start
}
function cmd_start {
docker run -d \
--name "$DOCKER_CONTAINER" \
--restart=always \
--device="$TUN_DEV" \
--net=host \
--cap-add=NET_ADMIN \
--cap-add=SYS_ADMIN \
-v "$DOCKER_VOLUME":/var/lib/zerotier-one "$DOCKER_IMAGE"
}
function cmd_stop {
local -r cid="$(docker ps -q -f name="$DOCKER_CONTAINER")"
if [ -n "$cid" ] ; then
docker stop "$cid"
fi
}
function cmd_upgrade {
# download first
docker pull "$DOCKER_IMAGE"
cmd_stop
docker container rm "$DOCKER_CONTAINER"
cmd_start
}
function cmd_in_docker {
docker exec -it "$DOCKER_CONTAINER" "$@"
}
function cmd_zt_cli {
cmd_in_docker zerotier-cli "$@"
}
function cmd_shell {
cmd_in_docker bash "$@"
}
case "${#@}" in
0)
usage
;;
*)
invokeCommand "$@"
;;
esac