forked from xl-tech/OpenVPN-easy-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenvpnsetup.sh
More file actions
executable file
·483 lines (410 loc) · 11.5 KB
/
openvpnsetup.sh
File metadata and controls
executable file
·483 lines (410 loc) · 11.5 KB
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
#!/bin/bash
#
# Express setup of OpenVPN server
# for Debian 10.x and Ubuntu 19.x and Later
# Originally by xl-tech https://github.com/xl-tech
# Modified by r3bers https://github.com/r3bers
#
# Version 0.21 March 2020
#
# Use only on fresh installed machine! It rewrites your firewall rules
# and your current OpenVPN config (if you have it before).
# Change SSH Port in config params to prevent whole server denied from internet
#
# Script is licensed under the GNU General Public License v3.0
#
# Usage: just run openvpnsetup.sh :)
#
NET6="fd60:1:1:1::/64" #can generate yours at https://simpledns.plus/private-ipv6
NET4="192.168.100.0/24"
DNS1="8.8.8.8"
DNS2="8.8.4.4"
#DNS1="195.208.4.1" # a.res-nsdi.ru
#DNS2="195.208.5.1" # b.res-nsdi.ru
SSHPORT=22 #SSH Port for netfilter rules to allow connect without VPN
export OPENVPN_DIR=/etc/openvpn
export EASYRSA=${OPENVPN_DIR}/easy-rsa
export EASYRSA_PKI=${EASYRSA}/pki
#Uncomment some options for less asking from console
#IP="0.0.0.0"
#PORT="udp 1194"
#CIPHER=AES-256-GCM
#IPV6E=1
NO_PASS="nopass" # Generate CA key without password, comment this string to secure yours ca.key by password
#check for root
IAM=$(whoami)
if [[ ${IAM} != "root" ]]; then
echo "You must be root to use this script"
exit 1
fi
#check for tun/tap
if [[ -c /dev/net/tun ]]; then
echo TUN/TAP is enabled
else
echo TUN/TAP is disabled. Contact your VPS provider to enable it
exit 1
fi
#enable IPv4 forwarding
if sysctl net.ipv4.ip_forward | grep 0; then
sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
else
echo "IPv4 forwarding is already enabled"
fi
#package install
deb_packages="openssl openvpn easy-rsa iptables netfilter-persistent iptables-persistent curl"
if cat /etc/*release | grep ^NAME | grep "Debian\|Ubuntu"; then
apt-get install -y ${deb_packages}
if hash ufw 2>/dev/null; then
ufw disable
else
echo "UFW not installed"
fi
else
echo "Unsupported distro, sorry"
exit 1
fi
#server settings
#external IP
EIP=$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
#external IPv6
IIPv6=$(ip -6 addr | grep inet6 | awk -F '[ \t]+|/' '{print $3}' | grep -v ^::1 | grep -v "^fe80\|^fd60")
if [[ -z ${IP+x} ]]; then
#internal IP
IIP=$(hostname -I)
#internal IPv6 with mask
IIPv6=$(ip -6 addr | grep inet6 | awk -F '[ \t]+|/' '{print $3}' | grep -v ^::1 | grep -v "^fe80\|^fd60")
echo "Select server IP to listen on (only used for IPv4):
1) Internal IP - $IIP (in case you are behind NAT)
2) External IP - $EIP
3) External IPv6 - $IIPv6"
read -r n
case ${n} in
1) IP=${IIP} ;;
2) IP=${EIP} ;;
3) IP=${IIPv6} ;;
*) invalid option ;;
esac
fi
if [[ -z ${PORT+x} ]]; then
echo "Select server PORT to listen on:
1) tcp 443 (recommended)
2) udp 1194 (default)
3) Enter manually (proto (lowercase!) port)"
read -r n
case ${n} in
1) PORT="tcp 443" ;;
2) PORT="udp 1194" ;;
3)
echo -n "Enter proto and port (like tcp 80 or udp 53): " &
read -re PORT
;;
*) invalid option ;;
esac
fi
PORTN=$(echo ${PORT} | grep -o '[0-9]*')
PORTL=$(echo ${PORT} | grep -o '[a-z,A-Z]*')
PORTL6=${PORTL}6
if [[ -z ${CIPHER+x} ]]; then
echo "Select server cipher:
1) AES-256-GCM (default for OpenVPN 2.4.x, not supported by Ubuntu Server 16.x)
2) AES-256-CBC
3) AES-128-CBC
4) BF-CBC (insecure)"
read -r n
case ${n} in
1) CIPHER=AES-256-GCM ;;
2) CIPHER=AES-256-CBC ;;
3) CIPHER=AES-128-CBC ;;
4) CIPHER=BF-CBC ;;
*) invalid option ;;
esac
fi
if [[ -z ${IPV6E+x} ]]; then
echo "Enable IPv6? (ensure that your machine have IPv6 support):
1) Yes
2) No"
read -r n
case ${n} in
1) IPV6E=1 ;;
2) IPV6E=0 ;;
*) invalid option ;;
esac
fi
echo "Check your selection"
echo "Server will listen on ${IP}"
if [[ ${IPV6E} -eq 1 ]]; then
echo "Server will listen IPv6 on ${IIPv6}"
fi
echo "Server will listen on ${PORT}"
echo "Server will use ${CIPHER} cipher"
echo "IPv6 - ${IPV6E} (1 is enabled, 0 is disabled)"
read -rsp $'Press enter to continue...\n'
#create dirs and files
mkdir ${EASYRSA}
mkdir ${OPENVPN_DIR}/bundles
mkdir ${OPENVPN_DIR}/ccd
#copy easy-rsa
cp -a /usr/share/easy-rsa/* ${EASYRSA}
echo -e "set_var EASYRSA \"${EASYRSA}\"
set_var EASYRSA_PKI \"${EASYRSA_PKI}\"
set_var EASYRSA_CERT_EXPIRE 1825
set_var EASYRSA_CA_EXPIRE 3650
set_var EASYRSA_CRL_DAYS 180
set_var EASYRSA_DIGEST sha256
set_var EASYRSA_KEY_SIZE 2048
set_var EASYRSA_DN cn_only
set_var EASYRSA_REQ_COUNTRY \"RU\"
set_var EASYRSA_REQ_PROVINCE \"MSK\"
set_var EASYRSA_REQ_CITY \"Moscow\"
set_var EASYRSA_REQ_ORG \"MyVPN.org\"
set_var EASYRSA_REQ_OU \"MyVPN\"
set_var EASYRSA_REQ_CN \"MyVPN\"
set_var EASYRSA_REQ_EMAIL \"vpn@MyVPN.org\"
" >${EASYRSA}/vars
#issue certs and keys
#init
${EASYRSA}/easyrsa --batch init-pki
#ca
if ! ${EASYRSA}/easyrsa --batch build-ca ${NO_PASS}; then
echo "Build CA failed"
exit 1
fi
#crl
if ! ${EASYRSA}/easyrsa --batch gen-crl; then
echo "Build CRL failed"
exit 1
fi
#dh
${EASYRSA}/easyrsa --batch gen-dh
#server
${EASYRSA}/easyrsa --batch gen-req vpn-server nopass
if ! ${EASYRSA}/easyrsa --batch sign-req server vpn-server; then
echo "Sign server cert request failed"
exit 1
fi
#ta
openvpn --genkey --secret ${EASYRSA_PKI}/ta.key
#generate server config
#ipv6 part
if [[ ${IPV6E} -eq 1 ]]; then
#enable IPv6 forwarding
if sysctl net.ipv6.conf.all.forwarding | grep 0; then
sysctl -w net.ipv6.conf.all.forwarding=1
echo "net.ipv6.conf.all.forwarding = 1" >>/etc/sysctl.conf
else
echo "IPv6 forwarding is already enabled"
fi
echo -e "#IPv6 config
server-ipv6 ${NET6}
proto ${PORTL6}
tun-ipv6
push tun-ipv6
push \"route-ipv6 2000::/3\"
push \"redirect-gateway ipv6\"
" > ${OPENVPN_DIR}/server.conf
else
echo "local $IP" > ${OPENVPN_DIR}/server.conf
fi
#main part
echo -e "port ${PORTN}
proto ${PORTL}
dev tun
#for cert revoke check
crl-verify ${EASYRSA_PKI}/crl.pem
server ${NET4} 255.255.255.0
topology subnet
push \"redirect-gateway def1 bypass-dhcp\"
#duplicate-cn
push \"dhcp-option DNS ${DNS1}\"
push \"dhcp-option DNS ${DNS2}\"
comp-lzo adaptive
push \"comp-lzo adaptive\"
mtu-disc yes
#mtu-test only makes sense with --proto udp
#mtu-test
mssfix max
mute-replay-warnings
#management 0.0.0.0 7000 ${OPENVPN_DIR}/management-password
#duplicate-cn
keepalive 10 120
tls-timeout 160
hand-window 160
cipher ${CIPHER}
auth SHA256
#uncomment for 2.4.x feature to disable automatically negotiate in AES-256-GCM
ncp-disable
#max-clients 300
#user nobody
#group nobody
persist-key
persist-tun
status /var/log/openvpn-status.log
log-append /var/log/openvpn.log
verb 2
#reneg-sec 864000
mute 3
tls-server
#script-security 3
#tcp 443 better perfomance
txqueuelen 2000
#set ifconfig eth0 txqueuelen 2000
#buffers
sndbuf 0
rcvbuf 0
push \"sndbuf 0\"
push \"rcvbuf 0\"
ca ${EASYRSA_PKI}/ca.crt
cert ${EASYRSA_PKI}/issued/vpn-server.crt
key ${EASYRSA_PKI}/private/vpn-server.key
tls-crypt ${EASYRSA_PKI}/ta.key
dh ${EASYRSA_PKI}/dh.pem
" >> ${OPENVPN_DIR}/server.conf
#create iptables file
echo "*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:XL-Firewall-1-INPUT - [0:0]
-A INPUT -j XL-Firewall-1-INPUT
-A FORWARD -j XL-Firewall-1-INPUT
-A XL-Firewall-1-INPUT -p icmp --icmp-type any -s localhost -j ACCEPT
-A XL-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A XL-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport ${SSHPORT} -j ACCEPT
-A XL-Firewall-1-INPUT -m state --state NEW -m ${PORTL} -p ${PORTL} --dport ${PORTN} -j ACCEPT
-A XL-Firewall-1-INPUT -i tun+ -j ACCEPT
-A XL-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
*mangle
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
COMMIT
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -s ${NET4} -j SNAT --to-source ${EIP}
COMMIT" > /tmp/iptables
#create ip6tables file
echo "*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:XL-Firewall-1-INPUT - [0:0]
-A INPUT -j XL-Firewall-1-INPUT
-A FORWARD -j XL-Firewall-1-INPUT
-A XL-Firewall-1-INPUT -i lo -j ACCEPT
-A XL-Firewall-1-INPUT -p icmpv6 -j ACCEPT
-A XL-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A XL-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport ${SSHPORT} -j ACCEPT
-A XL-Firewall-1-INPUT -m state --state NEW -m ${PORTL} -p ${PORTL} --dport ${PORTN} -j ACCEPT
-A XL-Firewall-1-INPUT -i tun+ -j ACCEPT
-A XL-Firewall-1-INPUT -j REJECT --reject-with icmp6-adm-prohibited
COMMIT
*mangle
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
COMMIT
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -s ${NET6} -j SNAT --to-source ${IIPv6}
COMMIT" > /tmp/ip6tables
#start services
cp /tmp/ip6tables /etc/iptables/rules.v6
cp /tmp/iptables /etc/iptables/rules.v4
systemctl enable netfilter-persistent &
systemctl start netfilter-persistent
systemctl enable openvpn@server &
systemctl start openvpn@server
systemctl restart netfilter-persistent
#generate client config
echo -e "client
dev tun
dev-type tun
#bind to interface if needed
#dev-node \"Ethernet\"
remote-cert-tls server
setenv opt tls-version-min 1.0 or-highest
#block local dns
setenv opt block-outside-dns
nobind
remote ${EIP} ${PORTN} ${PORTL}
cipher ${CIPHER}
auth SHA256
resolv-retry infinite
persist-key
persist-tun
comp-lzo
auth-nocache
verb 3
ping 10
tls-client
float" > ${OPENVPN_DIR}/client.ovpn
#generate bash script to create one-file config for clients
echo -e "#! /bin/bash
# Script to automate creating new OpenVPN clients
#
# H Cooper - 05/02/11
# Y Frolov - 08/06/16 - bundle config added (unified format)
# M Solovev - 15/01/20 - Migrate from pkitool EasyRSA 2.x to EasyRSA 3.x
# Usage: newclient.sh <common-name>
echo \"Script to generate unified config for OpenVPN Apps\"
echo \"Usage: newclient.sh <common-name>\"
# Set vars
OPENVPN_DIR=${OPENVPN_DIR}
EASY_RSA=\${OPENVPN_DIR}/easy-rsa
EASYRSA_PKI=\${EASY_RSA}/pki
BUNDLE_DIR=\${OPENVPN_DIR}/bundles
# Either read the CN from \$1 or prompt for it
if [[ -z \"\$1\" ]]; then
echo -n \"Enter new client common name (CN): \"
read -er CN
else
CN=\$1
fi
# Ensure CN isn't blank
if [[ -z \${CN} ]]; then
echo \"You must provide a CN.\"
exit 1
fi
# Check the CN doesn't already exist
if [[ -f \${EASYRSA_PKI}/issued/\${CN}.crt ]]; then
echo \"Error: certificate with the CN \${CN} already exists!\"
echo \" \${EASYRSA_PKI}/issued/\${CN}.crt\"
exit 1
fi
# Generating Full Client package
\${EASY_RSA}/easyrsa build-client-full \${CN} nopass
# Add all certs to unified client config file
# Default config for client
cp \${OPENVPN_DIR}/client.ovpn \${BUNDLE_DIR}/\${CN}.ovpn
{
# CA
echo \"<ca>\"
cat \${EASYRSA_PKI}/ca.crt
echo \"</ca>\"
# Client cert
echo \"<cert>\"
cat \${EASYRSA_PKI}/issued/\$CN.crt
echo \"</cert>\"
# Client key
echo \"<key>\"
cat \${EASYRSA_PKI}/private/\$CN.key
echo \"</key>\"
# ta tls crypt OpenVPN 2.4.x
echo \"<tls-crypt>\"
cat \${EASYRSA_PKI}/ta.key
echo \"</tls-crypt>\"
} >> \${BUNDLE_DIR}/\$CN.ovpn
echo \"COMPLETE! Copy the new unified config from here: ${OPENVPN_DIR}/bundles/\$CN.ovpn\"" > ${OPENVPN_DIR}/newclient.sh
chmod +x ${OPENVPN_DIR}/newclient.sh
echo "Setup is complete. Happy VPNing!"
echo "Use ${OPENVPN_DIR}/newclient.sh to generate client config"