Skip to content

Commit fdb377d

Browse files
authored
Merge pull request #204 from linuxserver/alpine-alnum
reject non-alnum peer names, log message
2 parents 899a840 + 8dd8e77 commit fdb377d

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
326326

327327
## Versions
328328

329+
* **26.10.22:** - Better handle unsupported peer names. Improve logging.
330+
* **12.10.22:** - Add Alpine branch. Optimize wg and coredns services.
329331
* **04.10.22:** - Rebase to Jammy. Upgrade to s6v3.
330332
* **16.05.22:** - Improve NAT handling in server mode when multiple ethernet devices are present.
331333
* **23.04.22:** - Add pre-shared key support. Automatically added to all new peer confs generated, existing ones are left without to ensure no breaking changes.

readme-vars.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ app_setup_block: |
121121
122122
# changelog
123123
changelogs:
124+
- { date: "26.10.22:", desc: "Better handle unsupported peer names. Improve logging." }
125+
- { date: "12.10.22:", desc: "Add Alpine branch. Optimize wg and coredns services." }
124126
- { date: "04.10.22:", desc: "Rebase to Jammy. Upgrade to s6v3." }
125127
- { date: "16.05.22:", desc: "Improve NAT handling in server mode when multiple ethernet devices are present." }
126128
- { date: "23.04.22:", desc: "Add pre-shared key support. Automatically added to all new peer confs generated, existing ones are left without to ensure no breaking changes." }

root/etc/s6-overlay/s6-rc.d/init-wireguard-confs/run

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,66 @@ generate_confs () {
2626

2727
DUDE"
2828
for i in ${PEERS_ARRAY[@]}; do
29-
if [[ "${i}" =~ ^[0-9]+$ ]]; then
30-
PEER_ID="peer${i}"
29+
if [[ ! "${i}" =~ ^[[:alnum:]]+$ ]]; then
30+
echo "**** Peer ${i} contains non-alphanumeric characters and thus will be skipped. No config for peer ${i} will be generated. ****"
3131
else
32-
PEER_ID="peer_${i//[^[:alnum:]_-]/}"
33-
fi
34-
mkdir -p /config/${PEER_ID}
35-
if [ ! -f "/config/${PEER_ID}/privatekey-${PEER_ID}" ]; then
36-
umask 077
37-
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey > /config/${PEER_ID}/publickey-${PEER_ID}
38-
wg genpsk > /config/${PEER_ID}/presharedkey-${PEER_ID}
39-
fi
40-
if [ -f "/config/${PEER_ID}/${PEER_ID}.conf" ]; then
41-
CLIENT_IP=$(cat /config/${PEER_ID}/${PEER_ID}.conf | grep "Address" | awk '{print $NF}')
42-
if [ -n "${ORIG_INTERFACE}" ] && [ "${INTERFACE}" != "${ORIG_INTERFACE}" ]; then
43-
CLIENT_IP=$(echo "${CLIENT_IP}" | sed "s|${ORIG_INTERFACE}|${INTERFACE}|")
32+
if [[ "${i}" =~ ^[0-9]+$ ]]; then
33+
PEER_ID="peer${i}"
34+
else
35+
PEER_ID="peer_${i}"
4436
fi
45-
else
46-
for idx in {2..254}; do
47-
PROPOSED_IP="${INTERFACE}.${idx}"
48-
if ! grep -q -R "${PROPOSED_IP}" /config/peer*/*.conf && ([ -z "${ORIG_INTERFACE}" ] || ! grep -q -R "${ORIG_INTERFACE}.${idx}" /config/peer*/*.conf); then
49-
CLIENT_IP="${PROPOSED_IP}"
50-
break
37+
mkdir -p /config/${PEER_ID}
38+
if [ ! -f "/config/${PEER_ID}/privatekey-${PEER_ID}" ]; then
39+
umask 077
40+
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey > /config/${PEER_ID}/publickey-${PEER_ID}
41+
wg genpsk > /config/${PEER_ID}/presharedkey-${PEER_ID}
42+
fi
43+
if [ -f "/config/${PEER_ID}/${PEER_ID}.conf" ]; then
44+
CLIENT_IP=$(cat /config/${PEER_ID}/${PEER_ID}.conf | grep "Address" | awk '{print $NF}')
45+
if [ -n "${ORIG_INTERFACE}" ] && [ "${INTERFACE}" != "${ORIG_INTERFACE}" ]; then
46+
CLIENT_IP=$(echo "${CLIENT_IP}" | sed "s|${ORIG_INTERFACE}|${INTERFACE}|")
5147
fi
52-
done
53-
fi
54-
if [ -f "/config/${PEER_ID}/presharedkey-${PEER_ID}" ]; then
55-
# create peer conf with presharedkey
56-
eval "`printf %s`
57-
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
48+
else
49+
for idx in {2..254}; do
50+
PROPOSED_IP="${INTERFACE}.${idx}"
51+
if ! grep -q -R "${PROPOSED_IP}" /config/peer*/*.conf 2>/dev/null && ([ -z "${ORIG_INTERFACE}" ] || ! grep -q -R "${ORIG_INTERFACE}.${idx}" /config/peer*/*.conf 2>/dev/null); then
52+
CLIENT_IP="${PROPOSED_IP}"
53+
break
54+
fi
55+
done
56+
fi
57+
if [ -f "/config/${PEER_ID}/presharedkey-${PEER_ID}" ]; then
58+
# create peer conf with presharedkey
59+
eval "`printf %s`
60+
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
5861
`cat /config/templates/peer.conf`
5962
DUDE"
60-
# add peer info to server conf with presharedkey
61-
cat <<DUDE >> /config/wg0.conf
63+
# add peer info to server conf with presharedkey
64+
cat <<DUDE >> /config/wg0.conf
6265
[Peer]
6366
# ${PEER_ID}
6467
PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
6568
PresharedKey = $(cat /config/${PEER_ID}/presharedkey-${PEER_ID})
6669
DUDE
67-
else
68-
echo "**** Existing keys with no preshared key found for ${PEER_ID}, creating confs without preshared key for backwards compatibility ****"
69-
# create peer conf without presharedkey
70-
eval "`printf %s`
71-
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
70+
else
71+
echo "**** Existing keys with no preshared key found for ${PEER_ID}, creating confs without preshared key for backwards compatibility ****"
72+
# create peer conf without presharedkey
73+
eval "`printf %s`
74+
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
7275
`cat /config/templates/peer.conf | sed '/PresharedKey/d'`
7376
DUDE"
74-
# add peer info to server conf without presharedkey
75-
cat <<DUDE >> /config/wg0.conf
77+
# add peer info to server conf without presharedkey
78+
cat <<DUDE >> /config/wg0.conf
7679
[Peer]
7780
# ${PEER_ID}
7881
PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
7982
DUDE
80-
fi
81-
SERVER_ALLOWEDIPS=SERVER_ALLOWEDIPS_PEER_${i}
82-
# add peer's allowedips to server conf
83-
if [ -n "${!SERVER_ALLOWEDIPS}" ]; then
84-
echo "Adding ${!SERVER_ALLOWEDIPS} to wg0.conf's AllowedIPs for peer ${i}"
85-
cat <<DUDE >> /config/wg0.conf
83+
fi
84+
SERVER_ALLOWEDIPS=SERVER_ALLOWEDIPS_PEER_${i}
85+
# add peer's allowedips to server conf
86+
if [ -n "${!SERVER_ALLOWEDIPS}" ]; then
87+
echo "Adding ${!SERVER_ALLOWEDIPS} to wg0.conf's AllowedIPs for peer ${i}"
88+
cat <<DUDE >> /config/wg0.conf
8689
AllowedIPs = ${CLIENT_IP}/32,${!SERVER_ALLOWEDIPS}
8790

8891
DUDE
@@ -91,14 +94,15 @@ DUDE
9194
AllowedIPs = ${CLIENT_IP}/32
9295

9396
DUDE
97+
fi
98+
if [ -z "${LOG_CONFS}" ] || [ "${LOG_CONFS}" = "true" ]; then
99+
echo "PEER ${i} QR code:"
100+
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
101+
else
102+
echo "PEER ${i} conf and QR code png saved in /config/${PEER_ID}"
103+
fi
104+
qrencode -o /config/${PEER_ID}/${PEER_ID}.png < /config/${PEER_ID}/${PEER_ID}.conf
94105
fi
95-
if [ -z "${LOG_CONFS}" ] || [ "${LOG_CONFS}" = "true" ]; then
96-
echo "PEER ${i} QR code:"
97-
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
98-
else
99-
echo "PEER ${i} conf and QR code png saved in /config/${PEER_ID}"
100-
fi
101-
qrencode -o /config/${PEER_ID}/${PEER_ID}.png < /config/${PEER_ID}/${PEER_ID}.conf
102106
done
103107
}
104108

0 commit comments

Comments
 (0)