@@ -26,63 +26,66 @@ generate_confs () {
2626
2727DUDE"
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`
5962DUDE"
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}
6467PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
6568PresharedKey = $(cat /config/${PEER_ID}/presharedkey-${PEER_ID})
6669DUDE
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'`
7376DUDE"
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}
7881PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
7982DUDE
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
8689AllowedIPs = ${CLIENT_IP}/32,${!SERVER_ALLOWEDIPS}
8790
8891DUDE
9194AllowedIPs = ${CLIENT_IP}/32
9295
9396DUDE
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