Skip to content

Instantly share code, notes, and snippets.

@starkers
Created February 1, 2019 14:13
Show Gist options
  • Save starkers/98d8fc64380c58ae29b3d296f4da27d6 to your computer and use it in GitHub Desktop.
Save starkers/98d8fc64380c58ae29b3d296f4da27d6 to your computer and use it in GitHub Desktop.
simple script for wireguard
#!/usr/bin/env bash
## we know generating the client private here is bad.. but its quick n easy
FILE_MAIN=/etc/wireguard/wg0.conf
function list_existing_ips(){
grep ^AllowedIPs ${FILE_MAIN} | sed 's+\/32++g' | awk '{print $3}' | sort
}
function generate_conf(){
PRIVATE=$(wg genkey)
PUBLIC="$(echo "${PRIVATE}" | wg pubkey)"
}
echo "Pick an IP.. these ones are already taken.. (as is .1)"
list_existing_ips
read -p "??:: OK... so.. which IP? 172.11.22.X: " octet
echo ${octet}
read -p "??:: OK...please leave a comment (who/what is it?) " comment
generate_conf
TmpClient="$(mktemp)"
TmpServer="$(mktemp)"
cat >${TmpClient}<<EOF
[Interface]
Address = 172.11.22.${octet}
PrivateKey = ${PRIVATE}
#ListenPort = 21841
DNS = 172.11.22.1
[Peer]
PublicKey = UPaGGXW610PzbA7RvtYQ72tfvopJrxezUqaTiETJryU=
# vpn.srvbit.com
Endpoint = 136.243.248.154:443
AllowedIPs = 0.0.0.0/0
# This is for if you're behind a NAT and
# want the connection to be kept alive.
PersistentKeepalive = 25
EOF
DATE="$(date "+%Y-%m-%d %H:%M:%S")"
cat >${TmpServer}<<EOF
###########################
# ${comment} [added: ${DATE}]
[Peer]
PublicKey = ${PUBLIC}
AllowedIPs = 172.11.22.${octet}/32
EOF
echo "========= OK here is your client config... ========"
cat ${TmpClient}
echo "========= OK here is your client config... in QR format ========"
qrencode -t ansiutf8 < ${TmpClient}
read -p "=== Has that been added? r u sure u want to reconfigure WG server locally now? [yn] ====" yn
if [ X$yn == Xy ]; then
cat ${TmpServer} >> /etc/wireguard/wg0.conf
wg-quick down wg0
wg-quick up wg0
wg show wg0
else
echo doing NOTHING
fi
rm ${TmpClient}
rm ${TmpServer}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment