forked from xl-tech/OpenVPN-easy-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openvpnsetup.sh
503 lines (433 loc) · 13.1 KB
/
openvpnsetup.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
#! /bin/bash
#
# Express setup of OpenVPN server
# for CentOS 7.x and Ubuntu Server 16.x / 17.x
# by xl-tech https://github.com/xl-tech
#
# Version 0.1 12 August 2017
#
# Use only on fresh installed machine! It can rewrite your firewall rules
# or your current OpenVPN config (if you have it before).
#
# Script is licensed under the GNU General Public License v3.0
#
# Usage: just run openvpnsetup.sh :)
#
#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
yum_packages="openssl openvpn easy-rsa iptables iptables-services curl"
deb_packages="openssl openvpn easy-rsa iptables netfilter-persistent iptables-persistent curl"
if cat /etc/*release | grep ^NAME | grep CentOS; then
yum -y install epel-release
yum -y install $yum_packages
systemctl disable firewalld & systemctl stop firewalld
elif cat /etc/*release | grep ^NAME | grep Ubuntu; then
apt-get install -y $deb_packages
ufw disable
else
echo "Unsupported distro, sorry"
exit 1;
fi
#server settings
#internal IP
IIP=`hostname -I`
#external IP
EIP=`curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
#internal IPv6 with mask
IIPv6=`ip -6 addr|grep inet6|grep fe80|awk -F '[ \t]+|' '{print $3}'`
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
"
read n
case $n in
1) IP=$IIP;;
2) IP=$EIP;;
*) invalid option;;
esac
echo "Select server PORT to listen on:
1) tcp 443 (recommended)
2) udp 1194 (default)
3) Enter manually (proto (lowercase!) port)
"
read 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 -e PORT;;
*) invalid option;;
esac
PORTN=`echo $PORT|grep -o '[0-9]*'`
PORTL=`echo $PORT|grep -o '[a-z,A-Z]*'`
PORTL6=$PORTL"6"
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 (default for OpenVPN 2.3.x)
4) BF-CBC (insecure)
"
read 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
echo "Enable IPv6? (ensure that your machine have IPv6 support):
1) Yes
2) No
"
read n
case $n in
1) IPV6E=1;;
2) IPV6E=0;;
*) invalid option;;
esac
echo "Check your selection"
echo "Server will listen on $IP"
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 /etc/openvpn/easy-rsa
mkdir /etc/openvpn/easy-rsa/keys
mkdir /etc/openvpn/logs
mkdir /etc/openvpn/bundles
mkdir /etc/openvpn/ccd
touch /etc/openvpn/easy-rsa/keys/index.txt
touch /etc/openvpn/easy-rsa/keys/serial
echo 00 >> /etc/openvpn/easy-rsa/keys/serial
#copy easy-rsa
if cat /etc/*release | grep ^NAME | grep CentOS; then
cp /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa
elif cat /etc/*release | grep ^NAME | grep Ubuntu; then
cp /usr/share/easy-rsa/* /etc/openvpn/easy-rsa
fi
#vars for certs
export EASY_RSA="/etc/openvpn/easy-rsa"
export OPENSSL="openssl"
export PKCS11TOOL="pkcs11-tool"
export GREP="grep"
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`
export KEY_DIR="$EASY_RSA/keys"
export PKCS11_MODULE_PATH="dummy"
export PKCS11_PIN="dummy"
export KEY_SIZE=2048
export CA_EXPIRE=3650
export KEY_EXPIRE=1825
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="[email protected]"
export KEY_OU="MyVPN"
export KEY_NAME="EasyRSA"
#issue certs and keys
#ca
export EASY_RSA="${EASY_RSA:-.}"
"$EASY_RSA/pkitool" --batch --initca $*
#server
export EASY_RSA="${EASY_RSA:-.}"
"$EASY_RSA/pkitool" --batch --server server-cert
#dh
$OPENSSL dhparam -out ${KEY_DIR}/dh.pem ${KEY_SIZE}
#ta
openvpn --genkey --secret ${KEY_DIR}/ta.key
#issue and revoke client cert to generate list of revoked certs
"$EASY_RSA/pkitool" --batch revoked
"$EASY_RSA/revoke-full" revoked
echo "Error 23 indicates that revoke is successful"
#generate server config
#ipv6 part
if (( "$IPV6E" == 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 fd6c:62d9:eb8c::/112
proto $PORTL6
tun-ipv6
push tun-ipv6
push \042route-ipv6 2000::/3\042
push \042redirect-gateway ipv6\042
" > /etc/openvpn/server.conf
else
echo "local $IP" > /etc/openvpn/server.conf
fi
#main part
echo -e "port $PORTN
proto $PORTL
dev tun
#for cert revoke check
crl-verify /etc/openvpn/easy-rsa/keys/crl.pem
server 10.1.0.0 255.255.255.0
topology subnet
push \042redirect-gateway def1 bypass-dhcp\042
#duplicate-cn
push \042dhcp-option DNS 8.8.8.8\042
push \042dhcp-option DNS 8.8.4.4\042
comp-lzo adaptive
push \042comp-lzo adaptive\042
mssfix 0
push \042mssfix 0\042
#management 0.0.0.0 7000 /etc/openvpn/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 /etc/openvpn/logs/openvpn-status.log
log-append /etc/openvpn/logs/openvpn.log
verb 2
#reneg-sec 864000
mute 3
tls-server
#script-security 3
#buffers
sndbuf 393216
rcvbuf 393216
push \042sndbuf 393216\042
push \042rcvbuf 393216\042
" >> /etc/openvpn/server.conf
echo "<ca>" >> /etc/openvpn/server.conf
cat $KEY_DIR/ca.crt >> /etc/openvpn/server.conf
echo "</ca>" >> /etc/openvpn/server.conf
echo "<cert>" >> /etc/openvpn/server.conf
cat $KEY_DIR/server-cert.crt >> /etc/openvpn/server.conf
echo "</cert>" >> /etc/openvpn/server.conf
echo "<key>" >> /etc/openvpn/server.conf
cat $KEY_DIR/server-cert.key >> /etc/openvpn/server.conf
echo "</key>" >> /etc/openvpn/server.conf
if openvpn --version | grep 2.3; then
# ta tls auth OpenVPN 2.3.x
echo "key-direction 0" >> /etc/openvpn/server.conf
echo "<tls-auth>" >> /etc/openvpn/server.conf
cat $KEY_DIR/ta.key >> /etc/openvpn/server.conf
echo "</tls-auth>" >> /etc/openvpn/server.conf
else
# ta tls crypt OpenVPN 2.4.x
echo "<tls-crypt>" >> /etc/openvpn/server.conf
cat $KEY_DIR/ta.key >> /etc/openvpn/server.conf
echo "</tls-crypt>" >> /etc/openvpn/server.conf
fi
echo "<dh>" >> /etc/openvpn/server.conf
cat $KEY_DIR/dh.pem >> /etc/openvpn/server.conf
echo "</dh>" >> /etc/openvpn/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 22 -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 10.1.0.0/24 -j MASQUERADE
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 $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 fd6c:62d9:eb8c::/112 -j MASQUERADE
COMMIT" > /tmp/ip6tables
#start services
if cat /etc/*release | grep ^NAME | grep CentOS; then
cp /tmp/ip6tables /etc/sysconfig/ip6tables
cp /tmp/iptables /etc/sysconfig/iptables
systemctl enable iptables & systemctl start iptables
systemctl enable ip6tables & systemctl start ip6tables
systemctl enable openvpn@server & systemctl start openvpn@server
systemctl restart iptables & systemctl restart ip6tables
elif cat /etc/*release | grep ^NAME | grep Ubuntu; then
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
fi
#generate client config
echo -e "client
dev tun
dev-type tun
#bind to interface if needed
#dev-node \042Ethernet 4\042
ns-cert-type 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
mssfix 0
verb 3
ping 10
tls-client
float" >> /etc/openvpn/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)
# Usage: newclient.sh <common-name>
echo \042Script to generate unified config for Windows App\042
echo \042sage: newclient.sh <common-name>\042
# Set vars
OPENVPN_DIR=/etc/openvpn
OPENVPN_RSA_DIR=/etc/openvpn/easy-rsa
OPENVPN_KEYS=\044OPENVPN_RSA_DIR/keys
BUNDLE_DIR=/etc/openvpn/bundles
# Either read the CN from \0441 or prompt for it
if [ -z \042\0441\042 ]
then echo -n \042Enter new client common name (CN): \042
read -e CN
else
CN=\u00241
fi
# Ensure CN isn't blank
if [ -z \042\044CN\042 ]
then echo \042You must provide a CN.\042
exit
fi
# Check the CN doesn't already exist
if [ -f \044OPENVPN_KEYS/\044CN.crt ]
then echo \042Error: certificate with the CN \044CN alread exists!\042
echo \042 \044OPENVPN_KEYS/\044CN.crt\042
exit
fi
# Establish the default variables
export EASY_RSA=\042/etc/openvpn/easy-rsa\042
export OPENSSL=\042openssl\042
export PKCS11TOOL=\042pkcs11-tool\042
export GREP=\042grep\042
export KEY_CONFIG=\x60\044EASY_RSA/whichopensslcnf \044EASY_RSA\x60
export KEY_DIR=\042\044EASY_RSA/keys\042
export PKCS11_MODULE_PATH=\042dummy\042
export PKCS11_PIN=\042dummy\042
export KEY_SIZE=2048
export CA_EXPIRE=3650
export KEY_EXPIRE=1825
export KEY_COUNTRY=\042US\042
export KEY_PROVINCE=\042CA\042
export KEY_CITY=\042SanFrancisco\042
export KEY_ORG=\042Fort-Funston\042
export KEY_EMAIL=\[email protected]\042
export KEY_OU=\042MyVPN\042
export KEY_NAME=\042EasyRSA\042
# Copied from build-key script (to ensure it works!)
export EASY_RSA=\042\044{EASY_RSA:-.}\042
\042\044EASY_RSA/pkitool\042 --batch \044CN
# Add all certs to unified client config file
# Default config for client
cp \044OPENVPN_DIR/client.ovpn \044BUNDLE_DIR/\044CN.ovpn
# CA
echo \042<ca>\042 >> \044BUNDLE_DIR/\044CN.ovpn
cat \044OPENVPN_KEYS/ca.crt >> \044BUNDLE_DIR/\044CN.ovpn
echo \042</ca>\042 >> \044BUNDLE_DIR/\044CN.ovpn
# Client cert
echo \042<cert>\042 >> \044BUNDLE_DIR/\044CN.ovpn
cat \044OPENVPN_KEYS/\044CN.crt >> \044BUNDLE_DIR/\044CN.ovpn
echo \042</cert>\042 >> \044BUNDLE_DIR/\044CN.ovpn
# Client key
echo \042<key>\042 >> \044BUNDLE_DIR/\044CN.ovpn
cat \044OPENVPN_KEYS/\044CN.key >> \044BUNDLE_DIR/\044CN.ovpn
echo \042</key>\042 >> \044BUNDLE_DIR/\044CN.ovpn
if openvpn --version | grep 2.3; then
# ta tls auth OpenVPN 2.3.x
echo \042key-direction 1\042 >> \044BUNDLE_DIR/\044CN.ovpn
echo \042<tls-auth>\042 >> \044BUNDLE_DIR/\044CN.ovpn
cat \044OPENVPN_KEYS/ta.key >> \044BUNDLE_DIR/\044CN.ovpn
echo \042</tls-auth>\042 >> \044BUNDLE_DIR/\044CN.ovpn
else
# ta tls crypt OpenVPN 2.4.x
echo \042<tls-crypt>\042 >> \044BUNDLE_DIR/\044CN.ovpn
cat \044OPENVPN_KEYS/ta.key >> \044BUNDLE_DIR/\044CN.ovpn
echo \042</tls-crypt>\042 >> \044BUNDLE_DIR/\044CN.ovpn
fi
# DH key
echo \042<dh>\042 >> \044BUNDLE_DIR/\044CN.ovpn
cat \044OPENVPN_KEYS/dh.pem >> \044BUNDLE_DIR/\044CN.ovpn
echo \042</dh>\042 >> \044BUNDLE_DIR/\044CN.ovpn
#echo \042\042
echo \042COMPLETE! Copy the new unified config from here: /etc/openvpn/bundles/\044CN.ovpn\042" > /etc/openvpn/newclient.sh
chmod +x /etc/openvpn/newclient.sh
echo "Setup is complete. Happy VPNing!"
echo "Use /etc/openvpn/newclient.sh to generate client config"
exit 1