Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template peer info appended to the server config #345

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme-vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ app_setup_block: |

# changelog
changelogs:
- { date: "12.07.24:", desc: "Peer info that is appended to the server config is now templated." }
- { date: "24.05.24:", desc: "Rebase to Alpine 3.20, install wireguard-tools from Alpine repo." }
- { date: "10.03.24:", desc: "Use iptables-legacy on Alpine 3.19." }
- { date: "05.03.24:", desc: "Rebase master to Alpine 3.19." }
Expand Down
5 changes: 5 additions & 0 deletions root/defaults/server-peer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Peer]
# friendly_name = ${PEER_ID}
PublicKey = $(cat "/config/${PEER_ID}/publickey-${PEER_ID}")
$([[ -f "/config/${PEER_ID}/presharedkey-${PEER_ID}" ]] && echo "PresharedKey = $(cat /config/${PEER_ID}/presharedkey-${PEER_ID})" || echo)
$([[ -n "${!SERVER_ALLOWEDIPS}" ]] && echo "AllowedIPs = ${CLIENT_IP}/32,${!SERVER_ALLOWED_IPS}" || echo "AllowedIPs = ${CLIENT_IP}/32")
29 changes: 8 additions & 21 deletions root/etc/s6-overlay/s6-rc.d/init-wireguard-confs/run
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ fi
if [[ ! -f /config/templates/peer.conf ]]; then
cp /defaults/peer.conf /config/templates/peer.conf
fi
if [[ ! -f /config/templates/server-peer.conf ]]; then
cp /defaults/server-peer.conf /config/templates/server-peer.conf
fi
# add preshared key to user templates (backwards compatibility)
if ! grep -q 'PresharedKey' /config/templates/peer.conf; then
sed -i 's|^Endpoint|PresharedKey = \$\(cat /config/\${PEER_ID}/presharedkey-\${PEER_ID}\)\nEndpoint|' /config/templates/peer.conf
Expand Down Expand Up @@ -69,39 +72,23 @@ DUDE"
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
$(cat /config/templates/peer.conf)
DUDE"
# add peer info to server conf with presharedkey
cat <<DUDE >> /config/wg_confs/wg0.conf
[Peer]
# ${PEER_ID}
PublicKey = $(cat "/config/${PEER_ID}/publickey-${PEER_ID}")
PresharedKey = $(cat "/config/${PEER_ID}/presharedkey-${PEER_ID}")
DUDE
else
echo "**** Existing keys with no preshared key found for ${PEER_ID}, creating confs without preshared key for backwards compatibility ****"
# create peer conf without presharedkey
eval "$(printf %s)
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
$(sed '/PresharedKey/d' "/config/templates/peer.conf")
DUDE"
# add peer info to server conf without presharedkey
cat <<DUDE >> /config/wg_confs/wg0.conf
[Peer]
# ${PEER_ID}
PublicKey = $(cat "/config/${PEER_ID}/publickey-${PEER_ID}")
DUDE
fi
SERVER_ALLOWEDIPS=SERVER_ALLOWEDIPS_PEER_${i}
# add peer's allowedips to server conf
if [[ -n "${!SERVER_ALLOWEDIPS}" ]]; then
echo "Adding ${!SERVER_ALLOWEDIPS} to wg0.conf's AllowedIPs for peer ${i}"
cat <<DUDE >> /config/wg_confs/wg0.conf
AllowedIPs = ${CLIENT_IP}/32,${!SERVER_ALLOWEDIPS}
DUDE
else
cat <<DUDE >> /config/wg_confs/wg0.conf
AllowedIPs = ${CLIENT_IP}/32
DUDE
fi
# add peer info to server conf
eval "$(printf %s)
cat <<DUDE >> /config/wg_confs/wg0.conf
$(cat /config/templates/server-peer.conf)
DUDE"
# add PersistentKeepalive if the peer is specified
if [[ -n "${PERSISTENTKEEPALIVE_PEERS_ARRAY}" ]] && ([[ "${PERSISTENTKEEPALIVE_PEERS_ARRAY[0]}" = "all" ]] || printf '%s\0' "${PERSISTENTKEEPALIVE_PEERS_ARRAY[@]}" | grep -Fxqz -- "${i}"); then
cat <<DUDE >> /config/wg_confs/wg0.conf
Expand Down