forked from ugurrdemirel/wireguard-oracle-cloud-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-peer.sh
executable file
·84 lines (68 loc) · 2.32 KB
/
add-peer.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
#!/bin/bash
echo 'Starting WireGuard peer configuration...'
while [[ $EUID != 0 ]];do
echo "This script must be run as root."
exit 1
done
hasWG=$(which wg-quick)
while [[ $hasWG == '' ]];do
echo 'WireGuard not installed. Run wireguard-autoconfig.sh first.'
exit 1
done
hasRC=$(which resolvconf)
while [[ $hasRC == '' ]];do
echo 'resolvconf not installed. Run wireguard-autoconfig.sh first.'
exit 1
done
hasQR=$(which qrencode)
while [[ $hasQR == '' ]];do
echo 'qrencode not installed. Run wireguard-autoconfig.sh first.'
exit 1
done
hasSettings=$(ls /etc/wireguard/settings/peer.next)
while [[ $hasSettings != '/etc/wireguard/settings/peer.next' ]];do
echo 'Script config not found. Run wireguard-autoconfig.sh first.'
exit 1
done
cd /etc/wireguard
peerNum=$(cat settings/peer.next)
echo $(($peerNum + 1)) > settings/peer.next
mkdir peer${peerNum}
cd peer${peerNum}
echo 'Generating keypair...'
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
cat << EOF > peer.conf
[Interface]
PrivateKey = REF_PEER_KEY
Address = REF_PEER_ADDRESS
DNS = 1.1.1.2, 1.0.0.2, 2606:4700:4700::1112, 2606:4700:4700::1002
[Peer]
PublicKey = REF_SERVER_PUBLIC_KEY
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = REF_SERVER_ENDPOINT
EOF
external_ip=$(curl ipinfo.io/ip)
server_endpoint="$external_ip:$(cat ../settings/port)"
ipv4_peer_addr="$(cat ../settings/ipv4)${peerNum}/24"
ipv6_peer_addr="$(cat ../settings/ipv6):${peerNum}/64"
#dns="$(cat ../settings/ipv4)1, $(cat ../settings/ipv6):1"
echo 'Setting peer configuration...'
sed -i "s;REF_PEER_KEY;$(cat privatekey);g" peer.conf
sed -i "s;REF_PEER_ADDRESS;$ipv4_peer_addr, $ipv6_peer_addr;g" peer.conf
#sed -i "s;REF_PEER_DNS;$dns;g" peer.conf
sed -i "s;REF_SERVER_PUBLIC_KEY;$(cat ../publickey);g" peer.conf
sed -i "s;REF_SERVER_ENDPOINT;$server_endpoint;g" peer.conf
wg-quick down wg0
echo 'Updating server configuration...'
cat << EOF >> ../wg0.conf
[Peer]
PublicKey = REF_PEER_PUBLIC_KEY
AllowedIPs = REF_PEER_IPS
EOF
allowed_ips="$(cat ../settings/ipv4)${peerNum}/32, $(cat ../settings/ipv6):${peerNum}/128"
sed -i "s;REF_PEER_PUBLIC_KEY;$(cat publickey);g" ../wg0.conf
sed -i "s;REF_PEER_IPS;$allowed_ips;g" ../wg0.conf
wg-quick up wg0
echo "You can connect using the config /etc/wireguard/peer${peerNum}/peer.conf -- or -- the QR code below:"
cat peer.conf | qrencode --type utf8