-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.sh
executable file
·309 lines (262 loc) · 9.2 KB
/
setup.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
#
# https://github.com/theroyalstudent/setupSimpleSyncthingRelay
#
clear
echo ""
echo "=================================================================="
echo "Simple Syncthing Relay Setup Script by @theroyalstudent (Edwin A.)"
echo "============ [ GitHub Source: https://git.io/vr2xt ] ============="
echo "=================================================================="
echo ""
if [[ $EUID -ne 0 ]]; then
echo ""
echo "You must be a root user" 2>&1
exit 1
fi
echo "Deleting old data and making sure no Syncthing Relay is running..."
killall relaysrv &> /dev/null
rm -rf /tmp/strelaysrv*.tar.gz /etc/relaysrv /home/relaysrv /usr/local/bin/relaysrv /etc/supervisor/conf.d/syncthingRelay.conf &> /dev/null
userdel relaysrv &> /dev/null
# input relay name
echo ""
read -rp "Please enter a relay name: " -e -i @ relayName
echo "You have entered '$relayName' as a relay name."
delimiter=' - '
if [ -z "$relayName" ]; then
delimiter=''
fi
# autodetect/input server geolocation
serverIPgeolocation="$(wget ipinfo.io/city -qO -), $(wget ipinfo.io/country -qO -)"
echo ""
echo "Your server IP geolocation is $serverIPgeolocation"
read -rp "Is this correct? [Y/n]: " -e -i y serverIPverification
if [[ "$serverIPverification" == [Nn] ]]; then
read -rp "Enter correct/preferred name: " serverIPgeolocation
elif [[ "$serverIPverification" == [Yy] ]] || [[ -z "$serverIPverification" ]]; then
echo "Nice, proceeding."
else
echo "User has not entered a valid response, unable to determine if autodetected location is accurate."
echo "Exiting."
exit 0;
fi
# inform user on relay name
displayName="$relayName$delimiter$serverIPgeolocation"
echo ""
echo "Thus, on the Syncthing Relay page at relays.syncthing.net, it will show as:"
echo "$(tput setaf 2)$displayName$(tput sgr0)"
# detect if user is behind a NAT
internalIP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
externalIP=$(wget -qO- ipv4.icanhazip.com)
if [[ $internalIP == "$externalIP" ]]; then
nat="no"
else
nat="yes"
fi
if [[ "$nat" == "yes" ]]; then
echo ""
echo "On commercial NAT VPS services like LowEndSpirit, the last octet of your local network would usually determine the ports open to you."
echo ""
echo "If the last octet of your IP is xxx, then expect ports from xxx01 to xxx20 to be open to you."
echo ""
echo "Here is your external IPv4 address:"
echo "$externalIP"
echo ""
echo "Here is your internal IPv4 address:"
echo "$internalIP"
echo ""
read -rp 'Enter port for daemon: ' daemonPort
echo "You have entered port $daemonPort as the port for the Syncthing relay daemon to listen on."
echo ""
read -rp 'Enter port for status: ' statusPort
echo "You have entered port $statusPort as the port for the Syncthing relay status to listen on."
echo ""
extDaemonPort="$externalIP:$daemonPort"
elif [[ "$nat" == "no" ]] || [[ -z "$nat" ]]; then
echo ""
echo "Assuming that ports 22067 (daemon) and 22068 (status) are readily available for usage."
echo ""
sleep 1
daemonPort=22067
extDaemonPort="$externalIP:$daemonPort"
statusPort=22068
else
echo "User has not entered a valid response, unable to determine the ports to listen on."
echo "Exiting."
exit 0;
fi
# start setup process
# Check if supervisor is installed first
defaultConfPath="/etc/supervisor/conf.d/syncthingRelay.conf"
supConfPath="$defaultConfPath"
newInstall=false
if [[ ! -e /usr/bin/supervisord ]]; then
newInstall=true
YUM_CMD=$(which yum)
APT_GET_CMD="/usr/bin/apt-get"
# detecting apt-get/yum
if [[ ! -z $YUM_CMD ]]; then
echo -n "Updating yum repositories..."
yum update-minimal --security -y &>/dev/null
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo -n "Installing sed, sudo and python-setuptools..."
yum install sed sudo python-setuptools -y &> /dev/null
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo -n "Downloading supervisor..."
cd /tmp
wget -q "https://pypi.python.org/packages/80/37/964c0d53cbd328796b1aeb7abea4c0f7b0e8c7197ea9b0b9967b7d004def/supervisor-3.3.1.tar.gz"
echo " $(tput setaf 2)DONE$(tput sgr0)"
#Extracting...
tar -xzf supervisor-3.3.1.tar.gz
echo -n "Building supervisor..."
cd supervisor-3.3.1
python setup.py install &> /dev/null
echo " $(tput setaf 2)DONE$(tput sgr0)"
# deleteing supervisor unneccessary files
cd /tmp
rm -rf supervisor*
#Setup startup
init=`cat /proc/1/comm`
if [[ "$init" == 'systemd' ]]; then
wget -q "https://raw.githubusercontent.com/theroyalstudent/setupSimpleSyncthingRelay/master/etc/supervisord.service" -O "/etc/systemd/system/supervisord.service"
systemctl enable supervisord
echo_supervisord_conf > /etc/supervisord.conf
else
wget -q "https://raw.githubusercontent.com/theroyalstudent/setupSimpleSyncthingRelay/master/etc/supervisord-yum.sh" -O "/etc/rc.d/init.d/supervisord"
chmod +x /etc/rc.d/init.d/supervisord
echo_supervisord_conf > /etc/supervisord.conf
chkconfig --add supervisord
chkconfig supervisord on
fi
elif [[ ! -z $APT_GET_CMD ]]; then
echo -n "Updating apt repositories..."
apt-get update -y &>/dev/null
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo -n "Installing packages: sed, sudo, supervisor if not installed yet..."
apt-get install sed sudo supervisor -y &>/dev/null
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo_supervisord_conf > /etc/supervisord.conf
else
echo ""
echo "unsupported or unknown architecture"
echo ""
exit;
fi
mkdir -p /var/run/supervisord/
mkdir -p /etc/supervisor/conf.d
# Modify it to include from conf.d by default
sed -i "s/\;\[include\]/[include]/" /etc/supervisord.conf
sed -i "s/\;files.*/files = \/etc\/supervisor\/conf.d\/*.conf/" /etc/supervisord.conf
sleep 2
service supervisord start
fi
# detect architecture
if uname -m | grep -q 64; then
cpubits="linux-amd64"
cpubitsname="for your 64-bit Linux system..."
elif uname -m | grep -q 86; then
cpubits="linux-386"
cpubitsname="for 32-bit Linux system..."
elif uname -m | grep -q "armv"; then
cpubits="linux-arm"
cpubitsname="for ARM Linux system..."
else
echo "unsupported or unknown architecture"
echo ""
exit;
fi
echo ""
echo -n "Downloading latest release of the relaysrv daemon $cpubitsname"
cd /tmp || exit
wget "$(wget https://api.github.com/repos/syncthing/relaysrv/releases/latest -qO - | grep 'browser_' | grep $cpubits | cut -d\" -f4)" &>/dev/null
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo -n "Extracting the relaysrv daemon..."
tar xzf strelaysrv-linux*.tar.gz
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo -n "Moving the relaysrv daemon to /usr/local/bin..."
cd strelaysrv-linux*
mv strelaysrv /usr/local/bin/relaysrv
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo -n "Clearing up the remains of the relaysrv daemon..."
cd /tmp
rm -rf strelaysrv-linux*.tar.gz
echo " $(tput setaf 2)DONE$(tput sgr0)"
# add user for relayserv
echo ""
echo -n "Adding a user for relaysrv, called relaysrv."
mkdir /etc/relaysrv
useradd -r -d /etc/relaysrv -s /bin/bash relaysrv &> /dev/null
chown -R relaysrv /etc/relaysrv
touch /etc/relaysrv/syncthingRelay.log
# download relay config
echo ""
echo -n "Copying Syncthing Relay supervisord configuration to the respective folder..."
if wget -q "https://raw.githubusercontent.com/theroyalstudent/setupSimpleSyncthingRelay/master/syncthingRelay.conf" -O "$supConfPath"; then
echo " $(tput setaf 2)DONE$(tput sgr0)"
else
echo " $(tput setaf 1)FAILED$(tput sgr0)"
echo ""
echo "Exiting."
echo ""
exit;
fi
echo ""
echo -n "Setting name of the Syncthing relay..."
sed -i s/RELAYNAME/"$displayName"/ $supConfPath
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo -n "Setting ports for the Syncthing relay to listen on..."
sed -i s/daemonPort/":$daemonPort"/ $supConfPath
sed -i s/extDaemonPort/"$extDaemonPort"/ $supConfPath
sed -i s/statusPort/"$statusPort"/ $supConfPath
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
echo "Restarting supervisord..."
echo ""
# Restarting supervisord also kills any running processes, which is bad
# Use supervisorctl update if supervisor was already installed
if "$newInstall" = "true"; then
# Check for both sysvinit & systemd
if [[ -e "/etc/rc.d/init.d/supervisord" || -e "/etc/systemd/system/supervisord.service" || -e "/usr/lib/systemd/system/supervisord.service" ]]; then
service supervisord restart
else
service supervisor restart
fi
else
supervisorctl update
fi
#Let the supervisord stabilize
echo "We would wait few seconds to let supervisord stabilize..."
secs=$((3 * 4))
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
echo ""
supervisorctl status syncthingRelay
echo ""
echo "And you should be up and running! (http://relays.syncthing.net)"
echo "If this script worked, feel free to give my script a star!"
echo "Exiting."
echo ""
exit 0
limit in GiB."
read bwlimit
echo "bandwidthlimit=$bwlimit" > /etc/syncthing_bw.conf
line1='1 * * * * /usr/bin/bandwidth_check'
line2='0 0 1 * * /usr/bin/bandwidth_check -r'
(crontab -u root -l; echo "$line1"; echo "$line2" ) | crontab -u root -
echo ""
supervisorctl status syncthingRelay
echo ""
echo "And you should be up and running! (http://relays.syncthing.net)"
echo "If this script worked, feel free to give my script a star!"
echo "Exiting."
echo ""
exit 0