quick-wireguard/new-client.sh

40 lines
990 B
Bash
Raw Permalink Normal View History

2023-11-05 09:40:53 +00:00
#!/bin/bash
TEMP_CONF=$(mktemp)
2023-11-05 11:21:07 +00:00
SERVER_NAME="cs2"
SERVER_CONF_FILE="./server/${SERVER_NAME}.conf"
SERVER_KEYFILE="./server/private.key"
export ENDPOINT="wg.oliveratkinson.net:51820"
# Make server
if [[ ! -e "${SERVER_KEYFILE}" ]]; then
mkdir server 2> /dev/null
umask 077
wg genkey > $SERVER_KEYFILE
if [[ ! -e "${SERVER_CONF_FILE}" ]]; then
export SERVER_PRIVATE_KEY=$(cat $SERVER_KEYFILE)
envsubst < server.template > $SERVER_CONF_FILE
fi
fi
export SERVER_PUBLIC_KEY=$(wg pubkey < $SERVER_KEYFILE)
2023-11-05 09:40:53 +00:00
2023-11-05 11:21:07 +00:00
create_client() {
mkdir clients 2> /dev/null
umask 077
2023-11-05 11:22:42 +00:00
CPRK="/tmp/cprk" # client private key
CPUK="/tmp/cpuk" # client public key
2023-11-05 11:21:07 +00:00
wg genkey | tee $CPRK | wg pubkey > $CPUK
export CLIENT_PRIVATE_KEY=$(cat $CPRK)
export CLIENT_PUBLIC_KEY=$(cat $CPUK)
export IP_ADDR=$1
2023-11-05 09:40:53 +00:00
2023-11-05 11:21:07 +00:00
envsubst < client.template > clients/client${IP_ADDR}.$SERVER_NAME.conf
envsubst < peer.template >> $SERVER_CONF_FILE
rm $CPUK $CPRK
}
# create clients
for i in {2..5}; do
create_client $i
done
2023-11-05 09:40:53 +00:00