-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
wg-start.sh
executable file
·90 lines (72 loc) · 2.05 KB
/
wg-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
#!/usr/bin/env bash
wireguard_port="${WIREGUARD_PORT:-51820}"
wireguard_mtu="${WIREGUARD_MTU:-1280}"
if [ ! -f /etc/wireguard/wg0.conf ]; then
server_private="$(wg genkey)"
server_public=$(echo -n "${server_private}" | wg pubkey)
cat >/etc/wireguard/wg0.conf <<EOF
[Interface]
PrivateKey = $server_private
Address = 10.0.0.254/32
ListenPort = $wireguard_port
MTU = $wireguard_mtu
SaveConfig = false
EOF
if [[ ${DOMAIN} && ${PEERS} ]]; then
count=${PEERS//[a-z]/}
for peer_number in $(seq $count); do
peer_private="$(wg genkey)"
peer_public=$(echo -n "${peer_private}" | wg pubkey)
cat >/etc/wireguard/peer$peer_number.conf <<EOF
[Interface]
PrivateKey = $peer_private
Address = 10.0.0.$peer_number/32
MTU = $wireguard_mtu
SaveConfig = false
[Peer]
PublicKey = $server_public
Endpoint = $DOMAIN:$wireguard_port
AllowedIPs = 10.0.0.254/32
PersistentKeepalive = 25
EOF
cat >>/etc/wireguard/wg0.conf <<EOF
[Peer]
PublicKey = $peer_public
AllowedIPs = 10.0.0.$peer_number/32
EOF
done
fi
fi
cp /etc/rinetd.conf.ori /etc/rinetd.conf
IFS=',' read -ra SERVICE <<<"$SERVICES"
for serv in "${SERVICE[@]}"; do
service_parts=(${serv//\:/ })
peer_number=${service_parts[0]//[a-z]/}
service_hostname=${service_parts[1]}
container_port=${service_parts[2]}
expose_port_as=${service_parts[3]}
if [[ ${DOMAIN} && ${PEERS} ]]; then
echo "0.0.0.0 $expose_port_as 10.0.0.$peer_number $expose_port_as" >>/etc/rinetd.conf
echo "0.0.0.0 $expose_port_as/udp 10.0.0.$peer_number $expose_port_as/udp" >>/etc/rinetd.conf
else
echo "0.0.0.0 $expose_port_as $service_hostname $container_port" >>/etc/rinetd.conf
echo "0.0.0.0 $expose_port_as/udp $service_hostname $container_port/udp" >>/etc/rinetd.conf
fi
done
echo "$(date): Starting Internet redirection server"
rinetd
echo "$(date): Starting Wireguard"
wg-quick up wg0
finish() {
echo "$(date): Shutting down Wireguard"
timeout 5 wg-quick down wg0
exit 0
}
trap finish TERM INT QUIT
wg
while :; do
if [ $(timeout 5 wg | wc -l) == 0 ]; then
exit 1
fi
sleep 10
done