From cd1968d191b30dc3d2c875bb44b8b3b8c79d5edf Mon Sep 17 00:00:00 2001 From: aptalca Date: Wed, 8 Apr 2020 10:46:14 -0400 Subject: [PATCH] update the scripts to use templates --- README.md | 1 + readme-vars.yml | 1 + root/app/add-peer | 30 ++++++++++++++---------------- root/app/show-peer | 5 ++++- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ac13b94..00310af 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64 ## Versions +* **08.04.20:** - Update the `add-peer`/`show-peer` scripts to utilize the templates and the `INTERNAL_SUBNET` var (previously missed, oops). * **05.04.20:** - Add `INTERNAL_SUBNET` variable to prevent subnet clashes. Add templates for server and peer confs. * **01.04.20:** - Add `show-peer` script and include info on host installed headers. * **31.03.20:** - Initial Release. diff --git a/readme-vars.yml b/readme-vars.yml index 8e1375c..36f54ad 100644 --- a/readme-vars.yml +++ b/readme-vars.yml @@ -74,6 +74,7 @@ app_setup_block: | # changelog changelogs: + - { date: "08.04.20:", desc: "Update the `add-peer`/`show-peer` scripts to utilize the templates and the `INTERNAL_SUBNET` var (previously missed, oops)." } - { date: "05.04.20:", desc: "Add `INTERNAL_SUBNET` variable to prevent subnet clashes. Add templates for server and peer confs." } - { date: "01.04.20:", desc: "Add `show-peer` script and include info on host installed headers." } - { date: "31.03.20:", desc: "Initial Release." } diff --git a/root/app/add-peer b/root/app/add-peer index f1bda6f..447527f 100755 --- a/root/app/add-peer +++ b/root/app/add-peer @@ -1,12 +1,20 @@ #!/usr/bin/with-contenv bash -if [ ! -f /config/wg0.conf ] || [ -z "$PEERS" ] || [ -z "$SERVERURL" ]; then +if [ ! -f /config/wg0.conf ] || [ -z "$PEERS" ]; then echo "Wireguard is not set up in server mode" exit 0 fi +INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0} +INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--') +if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then + SERVERURL=$(curl icanhazip.com) +fi +SERVERPORT=${SERVERPORT:-51820} +PEERDNS=${PEERDNS:-8.8.8.8} + for i in {1..254}; do - if grep -q "AllowedIPs = 10.13.13.$(( $i + 1 ))/32" /config/wg0.conf; then + if grep -q "AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32" /config/wg0.conf; then echo "Peer $i exists" else echo "Adding new Peer $i" @@ -15,24 +23,14 @@ for i in {1..254}; do umask 077 wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey > /config/peer${i}/publickey-peer${i} fi - SERVERPORT=${SERVERPORT:-51820} - PEERDNS=${PEERDNS:-8.8.8.8} + eval "`printf %s` cat < /config/peer${i}/peer${i}.conf -[Interface] -Address = 10.13.13.$(( $i + 1 )) -PrivateKey = $(cat /config/peer${i}/privatekey-peer${i}) -ListenPort = 51820 -DNS = ${PEERDNS} - -[Peer] -PublicKey = $(cat /config/server/publickey-server) -Endpoint = ${SERVERURL}:${SERVERPORT} -AllowedIPs = 0.0.0.0/0, ::/0 -DUDE +`cat /config/templates/peer.conf` +DUDE" cat <> /config/wg0.conf [Peer] PublicKey = $(cat /config/peer${i}/publickey-peer${i}) -AllowedIPs = 10.13.13.$(( $i + 1 ))/32 +AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32 DUDE echo "PEER ${i} QR code:" diff --git a/root/app/show-peer b/root/app/show-peer index 4264343..b283464 100755 --- a/root/app/show-peer +++ b/root/app/show-peer @@ -5,8 +5,11 @@ if [ ! $# -gt 0 ]; then exit 0 fi +INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0} +INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--') + for i in "$@"; do - if grep -q "AllowedIPs = 10.13.13.$(( $i + 1 ))/32" /config/wg0.conf; then + if grep -q "AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32" /config/wg0.conf; then echo "PEER $i QR code:" qrencode -t ansiutf8 < /config/peer${i}/peer${i}.conf else