Skip to content

Commit c5a43f2

Browse files
authored
Merge pull request #59 from cristiangauma/master
Allow to specify a list of names as PEERS and add ALLOWEDIPS environment variable
2 parents 74d4bb9 + 1cd7c25 commit c5a43f2

File tree

4 files changed

+61
-34
lines changed

4 files changed

+61
-34
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
@@ -102,6 +103,7 @@ app_setup_block: |
102103
103104
# changelog
104105
changelogs:
106+
- { date: "04.10.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" }
105107
- { date: "27.09.20:", desc: "Cleaning service binding example to have accurate PreDown script." }
106108
- { 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." }
107109
- { date: "29.07.20:", desc: "Update Coredns config to detect dns loops (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 = ${CLIENT_IP}
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: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,44 +172,64 @@ 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+
for i in ${PEERS_ARRAY[@]}; do
176+
if [[ "${i}" =~ ^[0-9]+$ ]]; then
177+
PEER_ID="peer${i}"
178+
else
179+
PEER_ID="peer_${i//[^[:alnum:]_-]/}"
180+
fi
181+
mkdir -p /config/${PEER_ID}
182+
if [ ! -f "/config/${PEER_ID}/privatekey-${PEER_ID}" ]; then
178183
umask 077
179-
wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey > /config/peer${i}/publickey-peer${i}
184+
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey > /config/${PEER_ID}/publickey-${PEER_ID}
185+
fi
186+
if [ -f "/config/${PEER_ID}/${PEER_ID}.conf" ]; then
187+
CLIENT_IP=$(cat /config/${PEER_ID}/${PEER_ID}.conf | grep "Address" | awk '{print $NF}')
188+
else
189+
for idx in {2..254}; do
190+
PROPOSED_IP="${INTERFACE}.${idx}"
191+
if ! grep -q -R "${PROPOSED_IP}" /config/peer*/*.conf; then
192+
CLIENT_IP="${PROPOSED_IP}"
193+
break
194+
fi
195+
done
180196
fi
181197
eval "`printf %s`
182-
cat <<DUDE > /config/peer${i}/peer${i}.conf
198+
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
183199
`cat /config/templates/peer.conf`
184200
DUDE"
185201
cat <<DUDE >> /config/wg0.conf
186202
[Peer]
187-
PublicKey = $(cat /config/peer${i}/publickey-peer${i})
188-
AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32
203+
# ${PEER_ID}
204+
PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
205+
AllowedIPs = ${CLIENT_IP}/32
189206

190207
DUDE
191208
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
209+
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
210+
qrencode -o /config/${PEER_ID}/${PEER_ID}.png < /config/${PEER_ID}/${PEER_ID}.conf
194211
done
195212
}
196213

197214
save_vars () {
198215
cat <<DUDE > /config/.donoteditthisfile
199-
ORIG_SERVERURL=$SERVERURL
200-
ORIG_SERVERPORT=$SERVERPORT
201-
ORIG_PEERDNS=$PEERDNS
202-
ORIG_PEERS=$PEERS
203-
ORIG_INTERFACE=$INTERFACE
216+
ORIG_SERVERURL="$SERVERURL"
217+
ORIG_SERVERPORT="$SERVERPORT"
218+
ORIG_PEERDNS="$PEERDNS"
219+
ORIG_PEERS="$PEERS"
220+
ORIG_INTERFACE="$INTERFACE"
221+
ORIG_ALLOWEDIPS="$ALLOWEDIPS"
204222
DUDE
205223
}
206224

207225
if [ -n "$PEERS" ]; then
208226
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"
227+
if [[ "$PEERS" =~ ^[0-9]+$ ]] && ! [[ "$PEERS" =~ *,* ]]; then
228+
PEERS_ARRAY=($(seq 1 $PEERS))
229+
else
230+
PEERS_ARRAY=($(echo "$PEERS" | tr ',' ' '))
212231
fi
232+
PEERS_COUNT=$(echo "${#PEERS_ARRAY[@]}")
213233
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
214234
SERVERURL=$(curl icanhazip.com)
215235
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
@@ -221,22 +241,24 @@ if [ -n "$PEERS" ]; then
221241
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
222242
echo "**** Internal subnet is set to $INTERNAL_SUBNET ****"
223243
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
244+
ALLOWEDIPS=${ALLOWEDIPS:-0.0.0.0/0, ::/0}
245+
echo "**** AllowedIPs for peers $ALLOWEDIPS ****"
224246
if [ -z "$PEERDNS" ] || [ "$PEERDNS" = "auto" ]; then
225247
PEERDNS="${INTERFACE}.1"
226248
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. ****"
227249
else
228250
echo "**** Peer DNS servers will be set to $PEERDNS ****"
229251
fi
230252
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 ****"
253+
echo "**** No wg0.conf found (maybe an initial install), generating 1 server and ${PEERS} peer/client confs ****"
232254
generate_confs
233255
save_vars
234256
else
235257
echo "**** Server mode is selected ****"
236258
[[ -f /config/.donoteditthisfile ]] && \
237259
. /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 ****"
260+
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ] || [ "$ALLOWEDIPS" != "$ORIG_ALLOWEDIPS" ]; then
261+
echo "**** Server related environment variables changed, regenerating 1 server and ${PEERS} peer/client confs ****"
240262
generate_confs
241263
save_vars
242264
else

0 commit comments

Comments
 (0)