Cleanup - move discrete blocks of code into functions.

This commit is contained in:
Matt Low 2021-01-01 09:44:58 -07:00
parent 696976c52d
commit 6fe7895b95

View File

@ -13,7 +13,6 @@ INTERFACE_NAME=tun-${RAND}
SERVER_ADDR=10.99.255.1/24 SERVER_ADDR=10.99.255.1/24
CLIENT_ADDR=10.99.255.2/32 CLIENT_ADDR=10.99.255.2/32
DNS=8.8.8.8 DNS=8.8.8.8
#INTERFACE_NAME=${INTERFACE_PFX}-${RAND}
echo "Generating keys..." echo "Generating keys..."
SERVER_KEY=$(wg genkey) SERVER_KEY=$(wg genkey)
@ -84,9 +83,9 @@ cleanup() {
END END
) )
echo setup_server() {
echo "Starting server..." echo "Starting server..."
ssh -T ${HOST} /bin/bash << END ssh -T ${HOST} /bin/bash <<- END
# include SERVER_LIB # include SERVER_LIB
${SERVER_LIB} ${SERVER_LIB}
@ -131,15 +130,12 @@ enable_forwarding
echo echo
sudo wg show "$INTERFACE_NAME" sudo wg show "$INTERFACE_NAME"
END END
}
if [ "$?" -ne 0 ]; then setup_client() {
echo "Error starting server, aborting."
exit 1
fi
umask 0177 umask 0177
FILE="/tmp/${INTERFACE_NAME}.conf" FILE="/tmp/${INTERFACE_NAME}.conf"
cat << CONF > "${FILE}" cat <<- CONF > "${FILE}"
[Interface] [Interface]
Address = ${CLIENT_ADDR} Address = ${CLIENT_ADDR}
PrivateKey = ${CLIENT_KEY} PrivateKey = ${CLIENT_KEY}
@ -152,20 +148,16 @@ Endpoint = ${HOST_ADDR}:51820
AllowedIPs = 0.0.0.0/0, ::/0 AllowedIPs = 0.0.0.0/0, ::/0
CONF CONF
echo
echo "Starting client..." echo "Starting client..."
sudo wg-quick up "${FILE}" sudo wg-quick up "${FILE}"
RESULT=$?
sleep 1 sleep 1
echo echo
sudo wg show "${INTERFACE_NAME}" sudo wg show "${INTERFACE_NAME}"
echo
echo "Connected! SIGINT or Enter to disconnect and stop server."
# clear sensitive variables from memory return $RESULT
PSK= }
CLIENT_KEY=
SERVER_KEY=
cleanup() { cleanup() {
set -e set -e
@ -188,6 +180,20 @@ cleanup() {
exit 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 trap cleanup INT TERM
read var read var
cleanup cleanup