-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart.sh
298 lines (260 loc) · 8.46 KB
/
start.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
#!/bin/bash
set -e
EASY_RSA_PATH="/etc/openvpn/server/easy-rsa"
READY_FILE="/etc/openvpn/server/.ready"
CRL_BLACKLIST_DIR="/etc/openvpn/server/crl-blacklist"
function assert_variable {
var_name="$1"
var_value="${!var_name}"
if [ -z "$var_value" ]; then
echo "Error: $var_name is not set"
exit 3
fi
}
assert_cidr() {
local cidr="$1"
# Check if the CIDR has a mask, add /24 if not
if [[ ! $cidr =~ / ]]; then
cidr="$cidr/24"
fi
# Inline is_valid_cidr logic
if [[ $cidr =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/([0-9]{1,2})$ ]]; then
local ip="${cidr%/*}"
local mask="${cidr#*/}"
# Validate IP address
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
IFS='.' read -r -a octets <<< "$ip"
for octet in "${octets[@]}"; do
if ((octet < 0 || octet > 255)); then
echo "Error: Invalid CIDR notation."
exit 1
fi
done
else
echo "Error: Invalid CIDR notation."
exit 1
fi
# Validate mask
if ! ((mask >= 0 && mask <= 32)); then
echo "Error: Invalid CIDR notation."
exit 1
fi
else
echo "Error: Invalid CIDR notation."
exit 1
fi
# Check if the mask is less than /16
if ((mask < 16)); then
echo "Error: CIDR mask is less than /16."
exit 1
fi
}
wait_for_tun() {
local TUN_DEVICE=$1
while ! ip link show "$TUN_DEVICE" > /dev/null 2>&1; do
echo "Waiting for $TUN_DEVICE to be created..."
sleep 1
done
echo "$TUN_DEVICE exists!"
}
function initialize {
echo "Initializing openvpn for the first time"
mkdir -p /etc/openvpn/server
mkdir -p "$EASY_RSA_PATH"
rm -rf /etc/openvpn/server/*
rm -rf "$EASY_RSA_PATH"
EASYRSA_REQ_CN="$OPENVPN_EXTERNAL_HOSTNAME"
cp -r /usr/share/easy-rsa "$EASY_RSA_PATH"
cd "$EASY_RSA_PATH"
./easyrsa init-pki
openssl rand -writerand "$EASY_RSA_PATH/pki/.rnd"
./easyrsa build-ca nopass
./easyrsa gen-dh
./easyrsa build-server-full server nopass
openvpn --genkey --secret "$EASY_RSA_PATH/pki/ta.key"
./easyrsa gen-crl
cd "$EASY_RSA_PATH/pki"
cp -rp ca.crt dh.pem ta.key crl.pem issued private /etc/openvpn/server/
touch "$READY_FILE"
}
function create_client {
client="$1"
mkdir -p /etc/openvpn/clients
client_file="/etc/openvpn/clients/$client.ovpn"
if [ ! -f "./pki/private/$client.key" ]; then
echo "Creating certificate and keys for client $client"
./easyrsa build-client-full "$client" nopass
else
echo "Skipping certificate and key generation for client $client"
fi
echo "" > "$client_file"
echo "client" >> "$client_file"
echo "dev tun" >> "$client_file"
echo "nobind" >> "$client_file"
echo "key-direction 1" >> "$client_file"
echo "auth SHA256" >> "$client_file"
echo "resolv-retry infinite" >> "$client_file"
echo "persist-key" >> "$client_file"
echo "persist-tun" >> "$client_file"
echo "mute-replay-warnings" >> "$client_file"
echo "remote-cert-tls server" >> "$client_file"
echo "verb 3" >> "$client_file"
echo "<key>" >> "$client_file"
cat "./pki/private/$client.key" >> "$client_file"
echo "</key>" >> "$client_file"
echo "<cert>" >> "$client_file"
cat "./pki/issued/$client.crt" >> "$client_file"
echo "</cert>" >> "$client_file"
echo "<ca>" >> "$client_file"
cat /etc/openvpn/server/ca.crt >> "$client_file"
echo "</ca>" >> "$client_file"
echo "<tls-auth>" >> "$client_file"
cat /etc/openvpn/server/ta.key >> "$client_file"
echo "</tls-auth>" >> "$client_file"
if [ ! -z "$OPENVPN_PORT_UDP" ]; then
echo "<connection>" >> "$client_file"
echo "proto udp" >> "$client_file"
echo "remote $OPENVPN_EXTERNAL_HOSTNAME $OPENVPN_PORT_UDP" >> "$client_file"
echo "</connection>" >> "$client_file"
fi
if [ ! -z "$OPENVPN_PORT_TCP" ]; then
echo "<connection>" >> "$client_file"
echo "proto tcp" >> "$client_file"
echo "remote $OPENVPN_EXTERNAL_HOSTNAME $OPENVPN_PORT_TCP" >> "$client_file"
echo "</connection>" >> "$client_file"
fi
}
function create_clients {
mkdir -p /etc/openvpn/clients
find /etc/openvpn/clients -name "*.ovpn" | xargs -r rm
cd "$EASY_RSA_PATH"
for client in $OPENVPN_CLIENTS; do
create_client "$client"
done
}
function blacklist_unlisted_clients {
mkdir -p "$CRL_BLACKLIST_DIR"
cd "$EASY_RSA_PATH"
touch pki/crl.pem
for cert_file in $(ls pki/issued/*.crt); do
client_name=$(basename "$cert_file" .crt)
if [[ ! " $OPENVPN_CLIENTS " =~ " $client_name " && "$client_name" != "server" ]]; then
echo "Blacklisting client $client_name found on file $cert_file"
./easyrsa revoke "$client_name"
./easyrsa gen-crl
fi
done
cp -f pki/crl.pem /etc/openvpn/server/crl.pem
}
function run_openvpn {
local port="$1"
local proto="$2"
local tun="$3"
local net="$4"
local net_ip=$(ipcalc -n "$net" | grep 'NETWORK' | awk -F '=' '{print $2}')
local net_mask=$(ipcalc -n -m "$net" | grep 'NETMASK' | awk -F '=' '{print $2}')
openvpn \
--server "$net_ip" "$net_mask" \
--dev "$tun" \
--dev-type "tun" \
--mode server \
--local 0.0.0.0 \
--port "$port" \
--proto "$proto" \
--keepalive "$OPENVPN_PING" "$OPENVPN_PING_RESTART" \
--bind \
--dh /etc/openvpn/server/dh.pem \
--ca /etc/openvpn/server/ca.crt \
--cert /etc/openvpn/server/issued/server.crt \
--key /etc/openvpn/server/private/server.key \
--tls-auth /etc/openvpn/server/ta.key \
--key-direction 0 \
--auth SHA256 \
--crl-verify /etc/openvpn/server/crl.pem \
$OPENVPN_CIPHER \
--persist-tun \
--persist-key \
--topology subnet \
--push "redirect-gateway def1" \
--push "block-outside-dns" \
--push "topology subnet" \
--push "dhcp-option DNS $OPENVPN_DNS" \
--sndbuf 524288 \
--rcvbuf 524288 \
--push "sndbuf 524288" \
--push "rcvbuf 524288" \
--tun-mtu $OPENVPN_TUN_MTU \
$OPENVPN_FASTIO
}
function start {
echo "Creating tun devices..."
mkdir -p /dev/net
if [ ! -c /dev/net/tun ]; then
mknod /dev/net/tun c 10 200
fi
if [ ! -z "$OPENVPN_PORT_UDP" ]; then
echo "Starting openvpn with udp..."
run_openvpn "$OPENVPN_PORT_UDP" "udp" "ovpnsetun0" "$OPENVPN_NETWORK_UDP" &
wait_for_tun "ovpnsetun0"
iptables -t nat -A POSTROUTING -s "$OPENVPN_NETWORK_UDP" -o "$OPENVPN_ROUTE_DEV" -j MASQUERADE
fi
if [ ! -z "$OPENVPN_PORT_TCP" ]; then
echo "Starting openvpn with tcp..."
run_openvpn "$OPENVPN_PORT_TCP" "tcp" "ovpnsetun1" "$OPENVPN_NETWORK_TCP" &
wait_for_tun "ovpnsetun1"
iptables -t nat -A POSTROUTING -s "$OPENVPN_NETWORK_TCP" -o "$OPENVPN_ROUTE_DEV" -j MASQUERADE
fi
wait -n
}
if [ "$OPENVPN_PORT_UDP" == "off" ]; then
OPENVPN_PORT_UDP=""
fi
if [ "$OPENVPN_PORT_TCP" == "off" ]; then
OPENVPN_PORT_TCP=""
fi
if [ -z "$OPENVPN_PORT_UDP" ] && [ -z "$OPENVPN_PORT_TCP" ]; then
echo "Error: OPENVPN_PORT_UDP and/or OPENVPN_PORT_TCP must be set"
exit 2
fi
if [ "$OPENVPN_CIPHER" != "" ]; then
OPENVPN_CIPHER="--data-ciphers $OPENVPN_CIPHER --cipher $OPENVPN_CIPHER"
fi
if [ "$OPENVPN_FASTIO" == "true" ] || [ "$OPENVPN_FASTIO" == "1" ]; then
echo "Notice: --fast-io is enabled"
OPENVPN_FASTIO="--fast-io"
else
OPENVPN_FASTIO=""
fi
# Check if the CIDR has a mask, add /24 if not
if [[ ! $OPENVPN_NETWORK_UDP =~ / ]]; then
OPENVPN_NETWORK_UDP="$OPENVPN_NETWORK_UDP/24"
fi
if [[ ! $OPENVPN_NETWORK_TCP =~ / ]]; then
OPENVPN_NETWORK_TCP="$OPENVPN_NETWORK_TCP/24"
fi
# Validate the CIDR
assert_cidr $OPENVPN_NETWORK_UDP
assert_cidr $OPENVPN_NETWORK_TCP
assert_variable "OPENVPN_EXTERNAL_HOSTNAME"
assert_variable "OPENVPN_CLIENTS"
if [ ! -f "$READY_FILE" ]; then
initialize
else
echo "Existing configuration detected... skipping openvpn initialization"
fi
COMMAND=${1:-serve}
case "$COMMAND" in
"serve")
create_clients
blacklist_unlisted_clients
start
;;
"setup")
create_clients
blacklist_unlisted_clients
;;
*)
echo "Error: Invalid command. Use 'serve' or 'prepare'."
exit 1
;;
esac