Skip to content

Instantly share code, notes, and snippets.

@ExtraSettings
Last active October 24, 2019 23:20
Show Gist options
  • Select an option

  • Save ExtraSettings/6e5515c69b408a128b0f2afc21593fb9 to your computer and use it in GitHub Desktop.

Select an option

Save ExtraSettings/6e5515c69b408a128b0f2afc21593fb9 to your computer and use it in GitHub Desktop.
add.sh
#!/bin/bash
readonly INTERFACE="wg0"
# Generate peer keys
readonly PRIVATE_KEY=$(wg genkey)
readonly PUBLIC_KEY=$(echo ${PRIVATE_KEY} | wg pubkey)
readonly IPV4="$(curl http://checkip.amazonaws.com)"
# Read server key from interface
readonly SERVER_PUBLIC_KEY=$(wg show ${INTERFACE} public-key)
# Get next free peer IP (This will break after x.x.x.255)
readonly PEER_ADDRESS=$(wg show ${INTERFACE} allowed-ips | cut -f 2 | awk -F'[./]' '{print $1"."$2"."$3"."1+$4"/"$5}' | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -n | tail -n1 | tr " " ",\ ")
# Add peer
wg set ${INTERFACE} peer ${PUBLIC_KEY} allowed-ips ${PEER_ADDRESS}
# Logging
echo "Added peer ${PEER_ADDRESS} with public key ${PUBLIC_KEY}"
# Generate peer config QR code
cat << END_OF_CONFIG
[Interface]
Address = ${PEER_ADDRESS}
PrivateKey = ${PRIVATE_KEY}
DNS = 176.103.130.130,176.103.130.131
[Peer]
PublicKey = ${SERVER_PUBLIC_KEY}
AllowedIPs = 0.0.0.0/0
Endpoint = ${IPV4}:51820
END_OF_CONFIG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment