-
Notifications
You must be signed in to change notification settings - Fork 1
/
openvpn.sh
509 lines (480 loc) · 13.7 KB
/
openvpn.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/bin/bash
#OpenVPN server installer for Debian 9
clear
service apache2 stop
function rootako () {
if [ "$EUID" -ne 0 ]; then
return 1
fi
}
function checktuntap () {
if [ ! -e /dev/net/tun ]; then
return 1
fi
}
function checkdebian () {
if [[ -e /etc/debian_version ]]; then
OS="debian"
source /etc/os-release
if [[ "$ID" == "debian" || "$ID" == "raspbian" ]]; then
if [[ ! $VERSION_ID =~ (9) ]]; then
echo ' Your version of Debian is not supported.'
echo ""
echo "However, if you're using Debian >= 9 or unstable/testing then you can continue."
echo "Keep in mind they are not supported, though."
echo ""
until [[ $CONTINUE =~ (y|n) ]]; do
read -rp "Continue? [y/n]: " -e CONTINUE
done
if [[ "$CONTINUE" = "n" ]]; then
exit 1
fi
fi
fi
else
echo "Looks like you aren't running this installer on a Debian"
exit 1
fi
}
function initialCheck () {
if ! rootako; then
echo "Sorry, you need to run this as root"
exit 1
fi
if ! checktuntap; then
echo "TUN is not available"
exit 1
fi
checkdebian
}
function copymenu () {
cp menu/* /usr/local/sbin/
chmod +x /usr/local/sbin/*
}
function updatesoure () {
cat <<EOF >/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
EOF
chmod +x /etc/rc.local
systemctl start rc-local
sudo apt-get update
}
function BadVPN () {
wget -O /usr/bin/badvpn-udpgw "https://github.com/johndesu090/AutoScriptDebianStretch/raw/master/Files/Plugins/badvpn-udpgw"
if [ "$OS" == "x86_64" ]; then
wget -O /usr/bin/badvpn-udpgw "https://github.com/johndesu090/AutoScriptDebianStretch/raw/master/Files/Plugins/badvpn-udpgw64"
fi
sed -i '$ i\screen -AmdS badvpn badvpn-udpgw --listen-addr 127.0.0.1:7300' /etc/rc.local
chmod +x /usr/bin/badvpn-udpgw
screen -AmdS badvpn badvpn-udpgw --listen-addr 127.0.0.1:7300
}
function webmin () {
apt-get install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python -y
apt-get install libxml-parser-perl libexpat1-dev -y -f
sudo apt-get update
}
function dropssl () {
apt-get -y install stunnel4 dropbear &> /dev/null
openssl genrsa -out key.pem 4096
openssl req -new -x509 -key key.pem -out cert.pem -days 1095 -batch
cat key.pem cert.pem > /etc/stunnel/stunnel.pem
}
function endropstun () {
sed -i 's/NO_START=1/NO_START=0/g' /etc/default/dropbear
sed -i 's/DROPBEAR_PORT=22/DROPBEAR_PORT=442/g' /etc/default/dropbear
sed -i 's/DROPBEAR_EXTRA_ARGS=/DROPBEAR_EXTRA_ARGS="-p 109 -p 143"/g' /etc/default/dropbear
echo "/bin/false" >> /etc/shells
sed -i 's/ENABLED=0/ENABLED=1/g' /etc/default/stunnel4
}
function settime () {
ln -fs /usr/share/zoneinfo/Asia/Manila /etc/localtime
}
function certandkey () {
cp ~/wagov3/ca.crt /etc/openvpn/
cp ~/wagov3/server.key /etc/openvpn/
cp ~/wagov3/server.req /etc/openvpn/
cp ~/wagov3/server.crt /etc/openvpn/
cp ~/wagov3/dh.pem /etc/openvpn/
}
function serverconf () {
echo "port $PORT" > /etc/openvpn/server.conf
echo "proto $PROTOCOL" >> /etc/openvpn/server.conf
echo "dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
verify-client-cert none
username-as-common-name
plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so login
server 10.8.0.0 255.255.255.0
key-direction 0
ifconfig-pool-persist ipp.txt
push \"redirect-gateway def1 bypass-dhcp\"
push \"dhcp-option DNS 8.8.8.8\"
push \"dhcp-option DNS 8.8.4.4\"
push \"route-method exe\"
push \"route-delay 2\"
socket-flags TCP_NODELAY
push \"socket-flags TCP_NODELAY\"
keepalive 10 120
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
log openvpn.log
management 127.0.0.1 7505
verb 3
ncp-disable
cipher none
auth none" >> /etc/openvpn/server.conf
}
function disableipv6 () {
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
}
function setiptables () {
mkdir /etc/iptables
echo "#!/bin/sh
iptables -t nat -I POSTROUTING 1 -s 10.8.0.0/24 -o $NIC -j MASQUERADE
iptables -I INPUT 1 -i tun0 -j ACCEPT
iptables -I FORWARD 1 -i $NIC -o tun0 -j ACCEPT
iptables -I FORWARD 1 -i tun0 -o $NIC -j ACCEPT
iptables -I INPUT 1 -i $NIC -p $PROTOCOL --dport $PORT -j ACCEPT" > /etc/iptables/add-openvpn-rules.sh
echo "#!/bin/sh
iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o $NIC -j MASQUERADE
iptables -D INPUT -i tun0 -j ACCEPT
iptables -D FORWARD -i $NIC -o tun0 -j ACCEPT
iptables -D FORWARD -i tun0 -o $NIC -j ACCEPT
iptables -D INPUT -i $NIC -p $PROTOCOL --dport $PORT -j ACCEPT" > /etc/iptables/rm-openvpn-rules.sh
chmod +x /etc/iptables/add-openvpn-rules.sh
chmod +x /etc/iptables/rm-openvpn-rules.sh
ufw allow ssh
ufw allow $PORT/tcp
sed -i 's|DEFAULT_INPUT_POLICY="DROP"|DEFAULT_INPUT_POLICY="ACCEPT"|' /etc/default/ufw
sed -i 's|DEFAULT_FORWARD_POLICY="DROP"|DEFAULT_FORWARD_POLICY="ACCEPT"|' /etc/default/ufw
echo "[Unit]
Description=iptables rules for OpenVPN
Before=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/etc/iptables/add-openvpn-rules.sh
ExecStop=/etc/iptables/rm-openvpn-rules.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/iptables-openvpn.service
systemctl daemon-reload
systemctl enable iptables-openvpn
systemctl start iptables-openvpn
}
function clientovpn () {
echo "client" > /etc/openvpn/client-template.txt
if [[ "$PROTOCOL" = 'udp' ]]; then
echo "proto udp" >> /etc/openvpn/client-template.txt
elif [[ "$PROTOCOL" = 'tcp' ]]; then
echo "proto tcp" >> /etc/openvpn/client-template.txt
fi
echo "remote $IP $PORT
dev tun
auth-user-pass
persist-key
persist-tun
pull
resolv-retry infinite
nobind
user nobody
comp-lzo
remote-cert-tls server
verb 3
mute 2
connect-retry 5 5
connect-retry-max 8080
mute-replay-warnings
redirect-gateway def1
script-security 2
cipher none
setenv CLIENT_CERT 0
auth none" >> /etc/openvpn/client-template.txt
mkdir -p /home/panel/html
cp /etc/openvpn/client-template.txt /home/panel/html/client.ovpn
echo '<ca>' >> /home/panel/html/client.ovpn
cat /etc/openvpn/ca.crt >> /home/panel/html/client.ovpn
echo '</ca>' >> /home/panel/html/client.ovpn
}
function noload () {
echo "client" > /etc/openvpn/client-template1.txt
if [[ "$PROTOCOL" = 'udp' ]]; then
echo "proto udp" >> /etc/openvpn/client-template1.txt
elif [[ "$PROTOCOL" = 'tcp' ]]; then
echo "proto tcp" >> /etc/openvpn/client-template1.txt
fi
echo "remote $IP $PORT
dev tun
persist-key
persist-tun
dev tun
bind
float
lport 110
remote-cert-tls server
verb 0
auth-user-pass
redirect-gateway def1
cipher none
auth none
auth-nocache
setenv CLIENT_CERT 0
auth-retry interact
connect-retry 0 1
nice -20
reneg-sec 0
log /dev/null" >> /etc/openvpn/client-template1.txt
cp /etc/openvpn/client-template1.txt /home/panel/html/WagoVPN-SunNoLoad.ovpn
echo '<ca>' >> /home/panel/html/WagoVPN-SunNoLoad.ovpn
cat /etc/openvpn/ca.crt >> /home/panel/html/WagoVPN-SunNoLoad.ovpn
echo '</ca>' >> /home/panel/html/WagoVPN-SunNoLoad.ovpn
}
function stunconf () {
cat > /etc/stunnel/stunnel.conf <<-END
sslVersion = all
pid = /var/run/stunnel.pid
cert = /etc/stunnel/stunnel.pem
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
client = no
[dropbear]
accept = 445
connect = 127.0.0.1:442
END
}
function privoxconfig () {
rm -f /etc/privoxy/config
cat>>/etc/privoxy/config<<EOF
user-manual /usr/share/doc/privoxy/user-manual
confdir /etc/privoxy
logdir /var/log/privoxy
filterfile default.filter
logfile logfile
listen-address 0.0.0.0:$PORTS
listen-address 0.0.0.0:8888
listen-address 0.0.0.0:3128
toggle 1
enable-remote-toggle 0
enable-remote-http-toggle 0
enable-edit-actions 0
enforce-blocks 0
buffer-limit 4096
enable-proxy-authentication-forwarding 1
forwarded-connect-retries 1
accept-intercepted-requests 1
allow-cgi-request-crunching 1
split-large-forms 0
tolerate-pipelining 1
socket-timeout 300
permit-access 0.0.0.0/0 $IP
EOF
}
function restartall () {
service uwsgi restart
service nginx restart
service vnstat restart
service dropbear restart
service sshd restart
service privoxy restart
service openvpn restart
service stunnel4 restart
}
function setall () {
rm /etc/issue.net
cat ~/wagov3/bann3r > /etc/issue.net
cat ~/wagov3/banner > /etc/motd
cp ~/wagov3/banner /etc/
sed -i 's@#Banner[[:space:]]none@Banner /etc/banner@g' /etc/ssh/sshd_config
sed -i 's@PrintMotd[[:space:]]no@PrintMotd yes@g' /etc/ssh/sshd_config
sed -i 's@#PrintLastLog[[:space:]]yes@PrintLastLog no@g' /etc/ssh/sshd_config
sed -i 's@#PermitRootLogin[[:space:]]prohibit-password@PermitRootLogin yes@g' /etc/ssh/sshd_config
sed -i 's@#PubkeyAuthentication[[:space:]]yes@PubkeyAuthentication no@g' /etc/ssh/sshd_config
sed -i 's@PasswordAuthentication[[:space:]]no@PasswordAuthentication yes@g' /etc/ssh/sshd_config
sed -i 's@DROPBEAR_BANNER=""@DROPBEAR_BANNER="/etc/issue.net"@g' /etc/default/dropbear
sed -i 's@#Port[[:space:]]22@Port 22\nPort 222@g' /etc/ssh/sshd_config
sed -i 's@#AddressFamily[[:space:]]any@AddressFamily inet@g' /etc/ssh/sshd_config
sed -i 's@#ListenAddress[[:space:]]0@ListenAddress 0@g' /etc/ssh/sshd_config
service dropbear restart
}
function installQuestions () {
apt install -y sudo
EXT_INT=$(cut -d' ' -f5 <(ip -4 route ls default))
IP=$(ip -4 addr ls $EXT_INT | head -2 | tail -1 | cut -d' ' -f6 | cut -d'/' -f1)
if echo "$IP" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then
IP=$(curl https://ipinfo.io/ip)
fi
echo ""
echo 'Your IP is '"$IP" '.. What port do you want OpenVPN to listen to?'
echo " 1) Default: 443"
echo " 2) Custom"
echo " 3) Random [49152-65535]"
until [[ "$PORT_CHOICE" =~ ^[1-3]$ ]]; do
read -rp "Port choice [1-3]: " -e -i 1 PORT_CHOICE
done
case $PORT_CHOICE in
1)
PORT="443"
;;
2)
until [[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; do
read -rp "Custom port [1-65535]: " -e -i 443 PORT
done
;;
3)
PORT=$(shuf -i49152-65535 -n1)
echo "Random Port: $PORT"
;;
esac
echo ""
echo "What protocol do you want OpenVPN to use?"
echo "UDP is faster. Unless it is not available, you shouldn't use TCP."
echo " 1) UDP"
echo " 2) TCP"
until [[ "$PROTOCOL_CHOICE" =~ ^[1-2]$ ]]; do
read -rp "Protocol [1-2]: " -e -i 2 PROTOCOL_CHOICE
done
case $PROTOCOL_CHOICE in
1)
PROTOCOL="udp"
;;
2)
PROTOCOL="tcp"
;;
esac
echo ""
echo "What Privoxy port do you want?"
echo " 1) Default: 8080"
echo " 2) Custom"
echo " 3) Random [49152-65535]"
until [[ "$PORT_PRIVO" =~ ^[1-3]$ ]]; do
read -rp "Port choice [1-3]: " -e -i 1 PORT_PRIVO
done
case $PORT_PRIVO in
1)
PORTS="8080"
;;
2)
until [[ "$PORTS" =~ ^[0-9]+$ ]] && [ "$PORTS" -ge 1 ] && [ "$PORTS" -le 65535 ]; do
read -rp "Custom port [1-65535]: " -e -i 8080 PORTS
done
;;
3)
PORTS=$(shuf -i49152-65535 -n1)
echo "Random Port: $PORTS"
;;
esac
echo ""
echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now."
echo "You will be able to generate a client at the end of the installation."
APPROVE_INSTALL=${APPROVE_INSTALL:-n}
if [[ $APPROVE_INSTALL =~ n ]]; then
read -n1 -r -p "Press any key to continue..."
fi
}
function installall () {
NIC=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)
apt-get update
apt-get install openvpn iptables wget ca-certificates curl screenfetch gnupg telnet telnetd nginx privoxy squid3 vnstat ufw build-essential -y
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.d/20-openvpn.conf
sysctl --system
}
function monitoring () {
apt-get install -y gcc libgeoip-dev python-virtualenv python-dev geoip-database-extra uwsgi uwsgi-plugin-python
cd /srv
git clone https://github.com/furlongm/openvpn-monitor.git
cd openvpn-monitor
virtualenv .
. bin/activate
pip install -r requirements.txt
cp openvpn-monitor.conf.example openvpn-monitor.conf
sed -i "s@host=localhost@host=127.0.0.1@g" openvpn-monitor.conf
sed -i 's@port=5555@port=7505@g' openvpn-monitor.conf
cd ~/wagov3/
cp openvpn-monitor.ini /etc/uwsgi/apps-available/
ln -s /etc/uwsgi/apps-available/openvpn-monitor.ini /etc/uwsgi/apps-enabled/
cp ~/wagov3/openvpn-monitor.py /srv/openvpn-monitor/openvpn-monitor.py -f
}
initialCheck
installQuestions
installall
settime
copymenu
updatesoure
BadVPN
webmin
dropssl
certandkey
endropstun
serverconf
disableipv6
setiptables
clientovpn
noload
stunconf
privoxconfig
setall
monitoring
sed -i 's|LimitNPROC|#LimitNPROC|g' /lib/systemd/system/[email protected]
cp /lib/systemd/system/openvpn\@.service /etc/systemd/system/openvpn\@.service
if [[ ! -e /etc/nginx/nginx.conf ]]; then
mkdir -p /etc/nginx;
wget -qO /var/tmp/nginx.zip "https://raw.githubusercontent.com/rayvynlee/linux/master/nginx.zip";
unzip -qq /var/tmp/nginx.zip -d /etc/nginx/
fi
cd ~/wagov3
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
cp ~/wagov3/nginx.conf /etc/nginx/nginx.conf
rm /etc/nginx/conf.d/*.conf
cp ~/wagov3/ocs.conf /etc/nginx/conf.d/
cp ~/wagov3/monitoring.conf /etc/nginx/conf.d/
cp ~/wagov3/index.html /home/panel/html/
systemctl daemon-reload
systemctl restart openvpn@server
systemctl enable openvpn@server
vnstat -u -i eth0
restartall
clear
show_ports
apt-get install lolcat ruby bc -y
wget https://github.com/busyloop/lolcat/archive/master.zip
unzip master.zip
cd lolcat-master/bin
gem install lolcat
cd
#squid setup
SQUIDIP=$(wget -qO- ipv4.icanhazip.com);
SQUIDIP2="s/xxxxxxxxx/$SQUIDIP/g";
wget -O /etc/squid/squid.conf "https://raw.githubusercontent.com/omayanrey/wagov3/master/squid.conf"
sed -i $SQUIDIP2 /etc/squid/squid.conf;
service squid restart
#cron for daily reboot
echo "0 5 * * * root /sbin/reboot" > /etc/cron.d/reboot
#cron for expired users
echo "0 1 * * * root /usr/local/sbin/delete_expired" > /etc/cron.d/delete_expired
service cron restart
systemctl enable privoxy.service
systemctl enable squid.service
history -c
rm -Rf ~/wagov3/
userdel -r debian
# tail -f /var/log/syslog
reboot