File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 214214 - name : Start the wg0 wireguard tunnel
215215 command : wg-quick up wg0
216216 tags : [ wireguard ]
217+
218+ - name : Template add-vpn-user.sh to {{ wireguard_dir }}
219+ ansible.builtin.template :
220+ src : add-vpn-user.sh
221+ dest : " {{ wireguard_dir }}add-vpn-user.sh"
222+ owner : root
223+ group : root
224+ mode : ' 0700'
225+ tags : [ wireguard ]
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ set -euxo pipefail
4+
5+ # Configuration parameters
6+ SERVER_PUBLIC_IP=$1
7+ SERVER_PORT=$2
8+ SERVER_PUBLIC_KEY=$( wg pubkey < router-private.key)
9+ DNS=$3
10+
11+ # Generate client keys
12+ CLIENT_PRIVATE_KEY=$( wg genkey)
13+ CLIENT_PUBLIC_KEY=$( wg pubkey <<< " ${CLIENT_PRIVATE_KEY}" )
14+
15+ # Assign the next available IP. This assumes you're using a /24 subnet.
16+ # You'll have to adjust the logic if your setup is different.
17+ LAST_IP=$( grep -oE ' 10\.10\.11\.[0-9]{1,3}' wg0.conf | tail -n1)
18+ NEXT_IP_INT=$( echo " ${LAST_IP} " | awk -F. ' {print $4+1}' )
19+ CLIENT_IP=" 10.10.11.${NEXT_IP_INT} "
20+
21+ # Append the new client to the server configuration
22+ cat << EOF >> wg0.conf
23+
24+ [Peer]
25+ PublicKey = ${CLIENT_PUBLIC_KEY}
26+ AllowedIPs = ${CLIENT_IP} /32
27+ EOF
28+
29+ # Generate the client configuration
30+ cat << EOF > "client_${CLIENT_IP} .conf"
31+ [Interface]
32+ PrivateKey = ${CLIENT_PRIVATE_KEY}
33+ Address = ${CLIENT_IP} /32
34+ DNS = ${DNS}
35+
36+ [Peer]
37+ PublicKey = ${SERVER_PUBLIC_KEY}
38+ Endpoint = ${SERVER_PUBLIC_IP} :${SERVER_PORT}
39+ AllowedIPs = 0.0.0.0/0
40+ PersistentKeepalive = 25
41+ EOF
42+
43+ echo " Client configuration saved as client_${CLIENT_IP} .conf"
44+
You can’t perform that action at this time.
0 commit comments