From 1bb1be36f87b4f436552c2f4b50c6a233015568b Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Sat, 17 Dec 2022 09:44:40 +0100 Subject: [PATCH] Change add command to use public key as optional param --- README.md | 18 ++++++++++++++++++ scripts/add-client.sh | 15 ++++++++++++--- scripts/entrypoint.sh | 5 +++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0d13837..13bdc84 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,24 @@ $ docker-compose run wg add This will create new client configuration and adds peer configuration to server config file. Restart service. +To add a client with existing public key run + +``` +$ docker-compose run wg add +``` + +and replace `` in command with public key created using `wg genkey`. +The created client config will contain a placeholder for clients secret key in interface config. + +``` +... +[Interface] +Address = 192.168.42.123/24 +ListenPort = 51820 +PrivateKey = +... +``` + ### Remove client Stop the service and run diff --git a/scripts/add-client.sh b/scripts/add-client.sh index f3cb1f7..5b06308 100755 --- a/scripts/add-client.sh +++ b/scripts/add-client.sh @@ -15,8 +15,13 @@ if [ -z $CLIENT_ID ]; then exit 1 fi -CLIENT_SEC_KEY=$(wg genkey) -CLIENT_PUB_KEY=$(echo $CLIENT_SEC_KEY | wg pubkey) +if [ -z $1 ]; then + CLIENT_SEC_KEY=$(wg genkey) + CLIENT_PUB_KEY=$(echo $CLIENT_SEC_KEY | wg pubkey) +else + CLIENT_SEC_KEY="" + CLIENT_PUB_KEY=$1 +fi # Add peer config cat << EOF >> $DEVICE.conf @@ -53,4 +58,8 @@ AllowedIPs = 0.0.0.0/0, ::/0 Endpoint = $SERVER_HOST:$SERVER_PORT EOF -echo "Added Client # $CLIENT_ID" \ No newline at end of file +if [ -z $1 ]; then + echo "Added Client # $CLIENT_ID" +else + echo "Added Client # $CLIENT_ID with existing public key" +fi diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 6ef07a4..a9fffe9 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -13,6 +13,11 @@ case "$1" in exit 0 ;; 'add-client' | 'add') + if [ ! -z $2 ]; then + cd /etc/wireguard + /scripts/add-client.sh $2 + exit 0 + fi cd /etc/wireguard /scripts/add-client.sh exit 0