Last active
October 24, 2019 23:20
-
-
Save ExtraSettings/6e5515c69b408a128b0f2afc21593fb9 to your computer and use it in GitHub Desktop.
add.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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