This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathopenvpn.sh
153 lines (130 loc) · 3.67 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
#!/bin/bash
#
# Script to set up OpenVPN for routing all traffic.
# https://github.com/tinfoil/openvpn_autoconfig
#
set -e
if [[ $EUID -ne 0 ]]; then
echo "You must be a root user" 1>&2
exit 1
fi
apt-get update -q
debconf-set-selections <<EOF
iptables-persistent iptables-persistent/autosave_v4 boolean true
iptables-persistent iptables-persistent/autosave_v6 boolean true
EOF
apt-get install -qy openvpn curl iptables-persistent
cd /etc/openvpn
# Certificate Authority
>ca-key.pem openssl genrsa 2048
>ca-csr.pem openssl req -sha256 -new -key ca-key.pem -subj /CN=OpenVPN-CA/
>ca-cert.pem openssl x509 -req -sha256 -in ca-csr.pem -signkey ca-key.pem -days 365
>ca-cert.srl echo 01
# Server Key & Certificate
>server-key.pem openssl genrsa 2048
>server-csr.pem openssl req -sha256 -new -key server-key.pem -subj /CN=OpenVPN-Server/
>server-cert.pem openssl x509 -sha256 -req -in server-csr.pem -CA ca-cert.pem -CAkey ca-key.pem -days 365
# Client Key & Certificate
>client-key.pem openssl genrsa 2048
>client-csr.pem openssl req -sha256 -new -key client-key.pem -subj /CN=OpenVPN-Client/
>client-cert.pem openssl x509 -req -sha256 -in client-csr.pem -CA ca-cert.pem -CAkey ca-key.pem -days 365
# Diffie hellman parameters
>dh.pem openssl dhparam 2048
chmod 600 *-key.pem
# Set up IP forwarding and NAT for iptables
>>/etc/sysctl.conf echo net.ipv4.ip_forward=1
sysctl -p
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
>/etc/iptables/rules.v4 iptables-save
# Write configuration files for client and server
SERVER_IP=$(curl -s4 canhazip.com || echo "<insert server IP here>")
>udp80.conf cat <<EOF
server 10.8.0.0 255.255.255.0
verb 3
duplicate-cn
key server-key.pem
ca ca-cert.pem
cert server-cert.pem
dh dh.pem
keepalive 10 120
persist-key yes
persist-tun yes
comp-lzo yes
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
# Normally, the following command is sufficient.
# However, it doesn't assign a gateway when using
# VMware guest-only networking.
#
# push "redirect-gateway def1 bypass-dhcp"
push "redirect-gateway bypass-dhcp"
push "route-metric 512"
push "route 0.0.0.0 0.0.0.0"
user nobody
group nogroup
proto udp
port 80
dev tun80
status openvpn-status-80.log
EOF
>tcp443.conf cat <<EOF
server 10.8.0.0 255.255.255.0
verb 3
duplicate-cn
key server-key.pem
ca ca-cert.pem
cert server-cert.pem
dh dh.pem
keepalive 10 120
persist-key yes
persist-tun yes
comp-lzo yes
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
# Normally, the following command is sufficient.
# However, it doesn't assign a gateway when using
# VMware guest-only networking.
#
# push "redirect-gateway def1 bypass-dhcp"
push "redirect-gateway bypass-dhcp"
push "route-metric 512"
push "route 0.0.0.0 0.0.0.0"
user nobody
group nogroup
proto tcp
port 443
dev tun443
status openvpn-status-443.log
EOF
>client.ovpn cat <<EOF
client
nobind
dev tun
redirect-gateway def1 bypass-dhcp
remote $SERVER_IP 443 tcp
comp-lzo yes
<key>
$(cat client-key.pem)
</key>
<cert>
$(cat client-cert.pem)
</cert>
<ca>
$(cat ca-cert.pem)
</ca>
EOF
VERSION=$(lsb_release -rs)
case $VERSION in
"12.04"|"14.04")
service openvpn restart
;;
"16.04")
systemctl restart openvpn@tcp443 && systemctl enable openvpn@tcp443
systemctl restart openvpn@udp80 && systemctl enable openvpn@udp80
;;
*)
echo "Sorry, this is an unsupported version of Ubuntu. Please restart the OpenVPN service or box manually."
;;
esac
cat client.ovpn
cd -