Cleanup - move discrete blocks of code into functions.
This commit is contained in:
parent
696976c52d
commit
6fe7895b95
38
vpnerator.sh
38
vpnerator.sh
@ -13,7 +13,6 @@ INTERFACE_NAME=tun-${RAND}
|
||||
SERVER_ADDR=10.99.255.1/24
|
||||
CLIENT_ADDR=10.99.255.2/32
|
||||
DNS=8.8.8.8
|
||||
#INTERFACE_NAME=${INTERFACE_PFX}-${RAND}
|
||||
|
||||
echo "Generating keys..."
|
||||
SERVER_KEY=$(wg genkey)
|
||||
@ -84,9 +83,9 @@ cleanup() {
|
||||
END
|
||||
)
|
||||
|
||||
echo
|
||||
setup_server() {
|
||||
echo "Starting server..."
|
||||
ssh -T ${HOST} /bin/bash << END
|
||||
ssh -T ${HOST} /bin/bash <<- END
|
||||
# include SERVER_LIB
|
||||
${SERVER_LIB}
|
||||
|
||||
@ -131,15 +130,12 @@ enable_forwarding
|
||||
echo
|
||||
sudo wg show "$INTERFACE_NAME"
|
||||
END
|
||||
}
|
||||
|
||||
if [ "$?" -ne 0 ]; then
|
||||
echo "Error starting server, aborting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
setup_client() {
|
||||
umask 0177
|
||||
FILE="/tmp/${INTERFACE_NAME}.conf"
|
||||
cat << CONF > "${FILE}"
|
||||
cat <<- CONF > "${FILE}"
|
||||
[Interface]
|
||||
Address = ${CLIENT_ADDR}
|
||||
PrivateKey = ${CLIENT_KEY}
|
||||
@ -152,20 +148,16 @@ Endpoint = ${HOST_ADDR}:51820
|
||||
AllowedIPs = 0.0.0.0/0, ::/0
|
||||
CONF
|
||||
|
||||
echo
|
||||
echo "Starting client..."
|
||||
sudo wg-quick up "${FILE}"
|
||||
RESULT=$?
|
||||
|
||||
sleep 1
|
||||
echo
|
||||
sudo wg show "${INTERFACE_NAME}"
|
||||
echo
|
||||
echo "Connected! SIGINT or Enter to disconnect and stop server."
|
||||
|
||||
# clear sensitive variables from memory
|
||||
PSK=
|
||||
CLIENT_KEY=
|
||||
SERVER_KEY=
|
||||
return $RESULT
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
set -e
|
||||
@ -188,6 +180,20 @@ cleanup() {
|
||||
exit
|
||||
}
|
||||
|
||||
clear_env() {
|
||||
# clear sensitive variables from memory
|
||||
unset PSK
|
||||
unset CLIENT_KEY
|
||||
unset SERVER_KEY
|
||||
}
|
||||
|
||||
echo
|
||||
setup_server || { echo "Error starting server, aborting."; exit 1; }
|
||||
echo
|
||||
setup_client || { echo "Error starting client, cleaning up..."; cleanup; exit 1; }
|
||||
clear_env
|
||||
echo -e "\nConnected! SIGINT or Enter to disconnect and stop server."
|
||||
|
||||
trap cleanup INT TERM
|
||||
read var
|
||||
cleanup
|
||||
|
Loading…
Reference in New Issue
Block a user