Skip to content

Commit 83e9257

Browse files
committed
Allow to specify a list of names as PEERS and add ALLOWEDIPS environment variable
1 parent 082716d commit 83e9257

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

readme-vars.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ opt_param_usage_include_env: true
4444
opt_param_env_vars:
4545
- { env_var: "SERVERURL", env_value: "wireguard.domain.com", desc: "External IP or domain name for docker host. Used in server mode. If set to `auto`, the container will try to determine and set the external IP automatically"}
4646
- { env_var: "SERVERPORT", env_value: "51820", desc: "External port for docker host. Used in server mode."}
47-
- { env_var: "PEERS", env_value: "1", desc: "Number of peers to create confs for. Required for server mode."}
47+
- { env_var: "PEERS", env_value: "1", desc: "Number of peers to create confs for. Required for server mode. Can be a list of names too: myPC,myPhone,myTablet..."}
4848
- { env_var: "PEERDNS", env_value: "auto", desc: "DNS server set in peer/client configs (can be set as `8.8.8.8`). Used in server mode. Defaults to `auto`, which uses wireguard docker host's DNS via included CoreDNS forward."}
4949
- { env_var: "INTERNAL_SUBNET", env_value: "10.13.13.0", desc: "Internal subnet for the wireguard and server and peers (only change if it clashes). Used in server mode."}
50+
- { env_var: "ALLOWEDIPS", env_value: "192.168.1.0/24,192.168.2.0/24", desc: "The IPs/Ranges that the peers will be able to reach using the VPN connection. If not specified the default value is: '0.0.0.0/0, ::0/0'"}
5051

5152
optional_block_1: false
5253
optional_block_1_items: ""
@@ -63,13 +64,13 @@ app_setup_block: |
6364
This can be run as a server or a client, based on the parameters used.
6465
6566
## Server Mode
66-
If the environment variable `PEERS` is set to a number, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log. They will also be saved in text and png format under `/config/peerX`.
67+
If the environment variable `PEERS` is set to a number or a list of strings separated by comma, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log. They will also be saved in text and png format under `/config/peerX` in case `PEERS` is a variable and an integer or `/config/peer_X` in case a list of names was provided instead of an integer.
6768
6869
Variables `SERVERURL`, `SERVERPORT`, `INTERNAL_SUBNET` and `PEERDNS` are optional variables used for server mode. Any changes to these environment variables will trigger regeneration of server and peer confs. Peer/client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
6970
70-
To add more peers/clients later on, you increment the `PEERS` environment variable and recreate the container.
71+
To add more peers/clients later on, you increment the `PEERS` environment variable or add more elements to the list and recreate the container.
7172
72-
To display the QR codes of active peers again, you can use the following command and list the peer numbers as arguments: `docker exec -it wireguard /app/show-peer 1 4 5` (Keep in mind that the QR codes are also stored as PNGs in the config folder).
73+
To display the QR codes of active peers again, you can use the following command and list the peer numbers as arguments: `docker exec -it wireguard /app/show-peer 1 4 5` or `docker exec -it wireguard /app/show-peer myPC myPhone myTablet` (Keep in mind that the QR codes are also stored as PNGs in the config folder).
7374
7475
The templates used for server and peer confs are saved under `/config/templates`. Advanced users can modify these templates and force conf generation by deleting `/config/wg0.conf` and restarting the container.
7576
@@ -87,6 +88,7 @@ app_setup_block: |
8788
8889
# changelog
8990
changelogs:
91+
- { date: "19.08.20:", desc: "Allow to specify a list of names as PEERS and add ALLOWEDIPS environment variable. Also, add peer name/id to each one of the peer sections in wg0.conf. Important: Existing users need to delete `/config/templates/peer.conf` and restart" }
9092
- { date: "06.08.20:", desc: "Replace resolvconf with openresolv due to dns issues when a client based on this image is connected to a server also based on this image. Add IPv6 info to readme. Display kernel version in logs." }
9193
- { date: "29.07.20:", desc: "Update Coredns config to detect dns loops (existing users need to delete `/config/coredns/Corefile` and restart)." }
9294
- { date: "27.07.20:", desc: "Update Coredns config to prevent issues with non-user-defined bridge networks (existing users need to delete `/config/coredns/Corefile` and restart)." }

root/app/show-peer

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ if [ ! $# -gt 0 ]; then
55
exit 0
66
fi
77

8-
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
9-
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
10-
118
for i in "$@"; do
12-
if grep -q "AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32" /config/wg0.conf; then
13-
echo "PEER $i QR code:"
14-
qrencode -t ansiutf8 < /config/peer${i}/peer${i}.conf
9+
if [[ "${i}" =~ ^[0-9]+$ ]]; then
10+
PEER_ID="peer${i}"
11+
else
12+
PEER_ID="peer_${i//[^[:alnum:]_-]/}"
13+
fi
14+
15+
if grep -q "# ${PEER_ID}" /config/wg0.conf; then
16+
echo "PEER ${i} QR code:"
17+
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
1518
else
16-
echo "PEER $i is not active"
19+
echo "PEER ${i} is not active"
1720
fi
1821
done

root/defaults/peer.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[Interface]
2-
Address = ${INTERFACE}.$(( $i + 1 ))
3-
PrivateKey = $(cat /config/peer${i}/privatekey-peer${i})
2+
Address = ${INTERFACE}.${CLIENT_IP_IDX}
3+
PrivateKey = $(cat /config/${PEER_ID}/privatekey-${PEER_ID})
44
ListenPort = 51820
55
DNS = ${PEERDNS}
66

77
[Peer]
88
PublicKey = $(cat /config/server/publickey-server)
99
Endpoint = ${SERVERURL}:${SERVERPORT}
10-
AllowedIPs = 0.0.0.0/0, ::/0
10+
AllowedIPs = ${ALLOWEDIPS}

root/etc/cont-init.d/30-config

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,33 @@ generate_confs () {
172172
`cat /config/templates/server.conf`
173173

174174
DUDE"
175-
for i in $(seq 1 $PEERS); do
176-
mkdir -p /config/peer${i}
177-
if [ ! -f "/config/peer${i}/privatekey-peer${i}" ]; then
175+
CLIENT_IP_IDX=2
176+
for i in ${PEERS_ARRAY[@]}; do
177+
if [[ "${i}" =~ ^[0-9]+$ ]]; then
178+
PEER_ID="peer${i}"
179+
else
180+
PEER_ID="peer_${i//[^[:alnum:]_-]/}"
181+
fi
182+
mkdir -p /config/${PEER_ID}
183+
if [ ! -f "/config/${PEER_ID}/privatekey-${PEER_ID}" ]; then
178184
umask 077
179-
wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey > /config/peer${i}/publickey-peer${i}
185+
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey > /config/${PEER_ID}/publickey-${PEER_ID}
180186
fi
181187
eval "`printf %s`
182-
cat <<DUDE > /config/peer${i}/peer${i}.conf
188+
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
183189
`cat /config/templates/peer.conf`
184190
DUDE"
185191
cat <<DUDE >> /config/wg0.conf
186192
[Peer]
187-
PublicKey = $(cat /config/peer${i}/publickey-peer${i})
188-
AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32
193+
# ${PEER_ID}
194+
PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
195+
AllowedIPs = ${INTERFACE}.${CLIENT_IP_IDX}/32
189196

190197
DUDE
198+
CLIENT_IP_IDX=$(( $CLIENT_IP_IDX + 1 ))
191199
echo "PEER ${i} QR code:"
192-
qrencode -t ansiutf8 < /config/peer${i}/peer${i}.conf
193-
qrencode -o /config/peer${i}/peer${i}.png < /config/peer${i}/peer${i}.conf
200+
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
201+
qrencode -o /config/${PEER_ID}/${PEER_ID}.png < /config/${PEER_ID}/${PEER_ID}.conf
194202
done
195203
}
196204

@@ -201,15 +209,18 @@ ORIG_SERVERPORT=$SERVERPORT
201209
ORIG_PEERDNS=$PEERDNS
202210
ORIG_PEERS=$PEERS
203211
ORIG_INTERFACE=$INTERFACE
212+
ORIG_ALLOWEDIPS=$ALLOWEDIPS
204213
DUDE
205214
}
206215

207216
if [ -n "$PEERS" ]; then
208217
echo "**** Server mode is selected ****"
209-
if ! [[ "$PEERS" =~ ^[0-9]+$ ]]; then
210-
echo "**** PEERS is not set to an integer, setting it to 1 ****"
211-
PEERS="1"
218+
if [[ "$PEERS" =~ ^[0-9]+$ ]] && ! [[ "$PEERS" =~ *,* ]]; then
219+
PEERS_ARRAY=($(seq 1 $PEERS))
220+
else
221+
PEERS_ARRAY=($(echo "$PEERS" | tr ',' ' '))
212222
fi
223+
PEERS_COUNT=$(echo "${#PEERS_ARRAY[@]}")
213224
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
214225
SERVERURL=$(curl icanhazip.com)
215226
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
@@ -221,22 +232,24 @@ if [ -n "$PEERS" ]; then
221232
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
222233
echo "**** Internal subnet is set to $INTERNAL_SUBNET ****"
223234
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
235+
ALLOWEDIPS=${ALLOWEDIPS:-0.0.0.0/0, ::/0}
236+
echo "**** AllowedIPs for peers $ALLOWEDIPS ****"
224237
if [ -z "$PEERDNS" ] || [ "$PEERDNS" = "auto" ]; then
225238
PEERDNS="${INTERFACE}.1"
226239
echo "**** PEERDNS var is either not set or is set to \"auto\", setting peer DNS to ${INTERFACE}.1 to use wireguard docker host's DNS. ****"
227240
else
228241
echo "**** Peer DNS servers will be set to $PEERDNS ****"
229242
fi
230243
if [ ! -f /config/wg0.conf ]; then
231-
echo "**** No found wg0.conf found (maybe an initial install), generating 1 server and $PEERS peer/client confs ****"
244+
echo "**** No wg0.conf found (maybe an initial install), generating 1 server and ${PEERS} peer/client confs ****"
232245
generate_confs
233246
save_vars
234247
else
235248
echo "**** Server mode is selected ****"
236249
[[ -f /config/.donoteditthisfile ]] && \
237250
. /config/.donoteditthisfile
238-
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ]; then
239-
echo "**** Server related environment variables changed, regenerating 1 server and $PEERS peer/client confs ****"
251+
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ] || [ "$ALLOWEDIPS" != "$ORIG_ALLOWEDIPS" ]; then
252+
echo "**** Server related environment variables changed, regenerating 1 server and ${PEERS} peer/client confs ****"
240253
generate_confs
241254
save_vars
242255
else

0 commit comments

Comments
 (0)