-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·71 lines (54 loc) · 1.87 KB
/
entrypoint.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
#!/bin/bash
set -e
on_signal () {
wg-quick down wg0
exit 0
}
#install on entry since wg is a kernel module
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y linux-headers-$(uname -r) wireguard
if [[ ! -e /etc/wireguard/wg0.conf ]]; then
echo "Generating configuration"
: ${server:=$(curl checkip.amazonaws.com)}
: ${server_port:=51820}
: ${clients:=2}
wg genkey | tee /etc/wireguard/server-privatekey | wg pubkey > /etc/wireguard/server-publickey
cat > /etc/wireguard/wg0.conf <<-EOF
[Interface]
Address = 192.168.99.254/24
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820
PrivateKey = $(cat /etc/wireguard/server-privatekey)
EOF
for (( client=1; client <= $clients; client+=1 )); do
wg genkey | tee /etc/wireguard/client${client}-privatekey | wg pubkey > "/etc/wireguard/client${client}-publickey"
cat >> /etc/wireguard/wg0.conf <<-EOF
[Peer]
PublicKey = $(cat /etc/wireguard/client${client}-publickey)
AllowedIPs = 192.168.99.${client}/32
EOF
cat > /etc/wireguard/client${client}.conf <<-EOF
[Interface]
PrivateKey = $(cat /etc/wireguard/client${client}-privatekey)
Address = 192.168.99.${client}/24
DNS = 1.1.1.1, 1.0.0.1
[Peer]
PublicKey = $(cat /etc/wireguard/server-publickey)
Endpoint = ${server}:${server_port}
AllowedIPs = 0.0.0.0/0
EOF
echo "QR code for client ${client}:"
qrencode -t ansiutf8 < /etc/wireguard/client${client}.conf
done
else
echo "Found existing configuration"
fi
wg-quick up wg0
trap on_signal SIGINT SIGTERM
while [ 1 ]; do
echo "**********************************************************************"
date
wg show
sleep 60
done