SYS 320 Week2 Lab - JadenGil/Jaden-Tech-Journal GitHub Wiki
Code for Peer and Server bash files
Peer.bash
#!/bin/bash
# Storyline: Create peer VPN configuration file
# What is the user / peer's name
echo -n "What is the peer's name? "
read the_client
# Filename variable
pFile="${the_client}-wg0.conf"
# Check for existing config file / if we want to override
if [ -f "${pFile}" ](/JadenGil/Jaden-Tech-Journal/wiki/--f-"${pFile}"-)
then
# Prompt if we need to overwrite the file
echo "The file ${pFile} already exists."
echo -n "Do you want to overwrite it? [y|N]"
read to_overwrite
`if [ "${to_overwrite}" == "N" ](/JadenGil/Jaden-Tech-Journal/wiki/|-"${to_overwrite}"-==-""-||-"${to_overwrite}"-==-"n"--)`
`then`
`echo "Exiting..."`
`exit 0`
`elif [ "${to_overwrite}" == "y" ](/JadenGil/Jaden-Tech-Journal/wiki/-"${to_overwrite}"-==-"y"-)`
`then`
`echo "Creating the wireguard configuration file..."`
`# If they don't specify y/N then error`
`else`
`echo "Invalid value"`
`exit 1`
`fi`
fi
# Generate private key
p="$(wg genkey)"
# Generate Public key
clientPub="$(echo ${p} | wg pubkey)"
# Generate preshared key (used for additional security for the client when establishing VPN tunnel)
pre="$(wg genpsk)"
# 10.254.132.0/24,172.16.28.0/24 192.199.97.163:4282 NH9qUERcppInDrMp8aT5Lx3gPdwf6s980Msa7y1x9nE= 8.8.8.8,1.1.1.1 1280 120 0.0.0.0/0
# Endpoint
end="$(head -1 wg0.conf | awk ' { print } ')"
# Server Public Key
pub="$(head -1 wg0.conf | awk ' { print } ')"
# DNS Servers
dns="$(head -1 wg0.conf | awk ' { print } ')"
# MTU
mtu="$(head -1 wg0.conf | awk ' { print } ')"
# KeepAlive
keep="$(head -1 wg0.conf | awk ' { print } ')"
# ListeningPort
lport="$(shuf -n1 -i 40000-50000)"
# Default routes for VPN
routes="$(head -1 wg0.conf | awk ' { print } ')"
# Create Client Configuration File
echo "[Interface]
Address = 10.254.132.100/24
DNS = ${dns}
ListenPort = ${lport}
MTU = ${mtu}
PrivateKey = ${p}
[Peer]
AllowedIPs = ${routes}
PersistentKeepalive = ${keep}
PresharedKey = ${pre}
PublicKey = ${pub}
Endpoint = ${end}
" > ${pFile}
# Add our peer configuration to the server config
echo "
# ${the_client} begin
[Peer]
PublicKey = ${clientPub}
PresharedKey = ${pre}
AllowedIPs = 10.254.132.100/32
# ${the_client} end" | tee -a wg0.conf
echo "
sudo cp wg0.conf /etc/wireguard
sudo wg addconf wg0 <(wg-quick strip wg0)
"
Server.bash
#!/bin/bash
# Storyline: Script to create a wireguard server
# Create a private key
p="$(wg genkey)"
# Create a public key
pub="$(echo ${p} | wg pubkey)"
# Set the addresses
address="10.254.132.0/24,172.16.28.0/24"
# Set Server IP Addresses
ServerAddress="10.254.132.1/24,172.16.28.1/24"
# Set a listening port
lport="4282"
# Create the format for the client configuration
peerInfo="# ${address} 192.199.97.163:4282 ${pub} 8.8.8.8,1.1.1.1 1280 120 0.0.0.0/0"
echo "${peerInfo}
[Interface]
Address = ${ServerAddress}
#PostUp = /etc/wireguard/wg-up.bash
#PostDown = /etc/wireguard/wg-down.bash
ListenPort = ${lport}
PrivateKey = ${p}
" > wg0.conf
wg0.conf
# 10.254.132.0/24,172.16.28.0/24 192.199.97.163:4282 aakZuNQZQaILZZZL7H3//Oz3dO+RQ0QBXR1jm+++XHc= 8.8.8.8,1.1.1.1 1280 120 0.0.0.0/0
[Interface]
Address = 10.254.132.1/24,172.16.28.1/24
#PostUp = /etc/wireguard/wg-up.bash
#PostDown = /etc/wireguard/wg-down.bash
ListenPort = 4282
PrivateKey = aEzzIpVpZ759nyesy0elZMspZHQSJyJ9hULFX6b8sGE=