Merge pull request #9 from ivanilves/feature/grim-reaper

Reap client afterwards
This commit is contained in:
Ivan Ilves 2017-02-20 07:16:26 +01:00 committed by GitHub
commit 4a623a5c86
9 changed files with 115 additions and 29 deletions

View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
#
# Update client's /etc/resolv.conf
#
if [[ ! -f /etc/resolv.conf.orig ]]; then
sudo cp /etc/resolv.conf /etc/resolv.conf.orig
fi
echo "--- resolv.conf ---"
echo "# Added by xiringuito" | sudo tee /etc/resolv.conf
sudo tee -a /etc/resolv.conf
if [[ ! $(grep "^nameserver 8.8.8.8$" /etc/resolv.conf) ]]; then
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
fi
echo "--- resolv.conf ---"

View File

@ -20,3 +20,11 @@ declare -r CLIENT_IP_ADDR=${IP_BASE}.${CLIENT_LAST_IP_ADDR_OCTET}
declare -r SERVER_IP_ADDR=${IP_BASE}.${SERVER_LAST_IP_ADDR_OCTET}
sudo ifconfig ${NETWORK_DEVICE} ${CLIENT_IP_ADDR} ${SERVER_IP_ADDR} netmask 255.255.255.255
set +e
NETWORK_SERVICE="$($(dirname ${0})/get-network-service-name.sh)"
DNS_SERVERS=$(networksetup -getdnsservers "${NETWORK_SERVICE}")
if [[ "${DNS_SERVERS:0:5}" != "There" ]]; then
echo ${DNS_SERVERS} | tee /tmp/xiringuito.dns.${LOCAL_TUNNEL_ID} >/dev/null
fi

View File

@ -0,0 +1,34 @@
#!/usr/bin/env bash
#
# Teardown client after disconnection [and main program exit] (MacOSX version)
#
if [[ ${#} -ne 3 ]]; then
echo "Usage: ${0} XIRINGUITO_PID SSH_PID LOCAL_TUNNEL_ID"
exit 1
fi
if [[ "${USER}" != "root" ]]; then
echo "Please run this script by root"
exit 77
fi
declare -r XIRINGUITO_PID=${1}
declare -r SSH_PID=${2}
declare -r LOCAL_TUNNEL_ID=${3}
while [[ $(ps -p ${XIRINGUITO_PID} | wc -l) -eq 2 ]]; do sleep 1; done
if [[ -f /etc/resolv.conf.orig ]]; then
cp /etc/resolv.conf.orig /etc/resolv.conf
fi
if [[ ${SSH_PID} -ne 0 ]]; then
kill ${SSH_PID}; sleep 1
fi
NETWORK_SERVICE="$($(dirname ${0})/get-network-service-name.sh)"
if [[ -f /tmp/xiringuito.dns.${LOCAL_TUNNEL_ID} ]]; then
DNS_SERVERS=$(cat /tmp/xiringuito.dns.${LOCAL_TUNNEL_ID})
sudo networksetup -setdnsservers "${NETWORK_SERVICE}" ${DNS_SERVERS}
rm /tmp/xiringuito.dns.${LOCAL_TUNNEL_ID}
fi

View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
#
# Update MacOSX DNS
#
if [[ ${#} != 1 ]]; then
echo "Usage: ${0} LOCAL_TUNNEL_ID"
exit 1
fi
if [[ -f /tmp/xiringuito.dns.${1} ]]; then
NETWORK_SERVICE="$($(dirname ${0})/get-network-service-name.sh)"
DNS_SERVERS=$(grep nameserver /etc/resolv.conf | awk '{print $2}' | tr '\n' ' ')
echo "* Setting DNS for \"${NETWORK_SERVICE}\": ${DNS_SERVERS}"
sudo networksetup -setdnsservers "${NETWORK_SERVICE}" ${DNS_SERVERS}
fi

View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
#
# Get name of the MacOSX network service (device connection)
#
networksetup -listnetworkserviceorder | grep '^(1) ' | sed 's/^(1) //'

View File

@ -1,10 +1,29 @@
#!/usr/bin/env bash
#
# Teardown client after disconnection
# Teardown client after disconnection [and main program exit]
#
if [[ ${#} != 1 ]]; then
echo "Usage: ${0} TUNNEL_ID"
if [[ ${#} -ne 3 ]]; then
echo "Usage: ${0} XIRINGUITO_PID SSH_PID TUNNEL_ID"
exit 1
fi
sudo ip tuntap del mode tun tun${1}
if [[ "${USER}" != "root" ]]; then
echo "Please run this script by root"
exit 77
fi
declare -r XIRINGUITO_PID=${1}
declare -r SSH_PID=${2}
declare -r TUNNEL_ID=${3}
while [[ -d /proc/${XIRINGUITO_PID} ]]; do sleep 1; done
if [[ -f /etc/resolv.conf.orig ]]; then
cp /etc/resolv.conf.orig /etc/resolv.conf
fi
if [[ ${SSH_PID} -ne 0 ]]; then
kill ${SSH_PID}; sleep 1
fi
ip tuntap del mode tun tun${TUNNEL_ID}

View File

@ -20,11 +20,11 @@ trap teardown EXIT
function teardown() {
sudo iptables -t nat -D POSTROUTING -s ${CLIENT_IP_ADDR} -j MASQUERADE
sleep 2
sudo ip tuntap del mode tun ${NETWORK_DEVICE}
}
echo "CONNECTED"
while true; do
sleep 60000 # do nothing until interrupted ;)
sleep 60000 # TODO: Maybe we need some heartbeat here
done

View File

@ -24,6 +24,8 @@ declare -r SSHD_RESTART_CMD="/etc/init.d/sshd reload"
# Ensure previous tunnels with the same ID are not running
set +e
pkill -f ${TUNNEL_ID}/server-execute.sh
if [[ ${?} -eq 0 ]]; then sleep 2; fi
sudo ip tuntap del mode tun ${NETWORK_DEVICE}
set -e
# Set up network device

View File

@ -56,26 +56,15 @@ else
declare -r LOCAL_TUNNEL_ID=$(./scripts/${KERNEL}/get-local-tunnel-id.sh)
fi
trap 'exit 130' INT
trap teardown EXIT
function teardown() {
set +e
echo "Tearing down tunnel..."
if [[ -f /etc/resolv.conf.orig ]]; then
sudo cp /etc/resolv.conf.orig /etc/resolv.conf
fi
if [[ ${SSH_PID} ]]; then
if [[ ${KERNEL} == linux ]]; then
kill ${SSH_PID}
else
sudo kill ${SSH_PID}
fi
ssh ${SSH_OPTS} ${SSH_SERVER} pkill -f ${REMOTE_PATH}/server-execute.sh
else
sudo ./scripts/${KERNEL}/client-teardown.sh ${$} 0 ${LOCAL_TUNNEL_ID}
fi
if [[ ${KERNEL} == linux ]]; then
./scripts/${KERNEL}/client-teardown.sh ${TUNNEL_ID}
fi
ssh ${SSH_OPTS} ${SSH_SERVER} pkill -f ${REMOTE_PATH}/server-execute.sh
echo "DONE"
}
echo "TUNNEL ID: ${TUNNEL_ID} (local: ${LOCAL_TUNNEL_ID})"
@ -97,6 +86,7 @@ else
fi
${SSH_TUNNEL_CMD} ${SSH_OPTS} -oStrictHostKeyChecking=no -w ${LOCAL_TUNNEL_ID}:${TUNNEL_ID} ${SSH_SERVER} ${REMOTE_PATH}/server-execute.sh ${TUNNEL_ID} ${IP_BASE} &
SSH_PID=${!}
sudo ./scripts/${KERNEL}/client-teardown.sh ${$} ${SSH_PID} ${LOCAL_TUNNEL_ID} &
if [[ ${KERNEL} == linux ]]; then
sleep 3
@ -115,20 +105,18 @@ for NETWORK in ${NETWORKS}; do
done
set -e
if [[ ! ${NO_DNS} && ! -z "${NETWORKS}" && ! "$(grep xiringuito /etc/resolv.conf)" ]]; then
if [[ ! ${NO_DNS} && ! -z "${NETWORKS}" ]]; then
echo
echo "* Will now replace your DNS config with one fetched from the SSH server."
echo "* Set enviromental variable 'NO_DNS', if you do not want this to happen."
REMOTE_RESOLV_CONF=$(ssh ${SSH_OPTS} ${SSH_SERVER} cat /etc/resolv.conf | grep -v "[#;]" )
sudo cp /etc/resolv.conf /etc/resolv.conf.orig
if [[ "${REMOTE_RESOLV_CONF}" =~ nameserver ]]; then
echo "--- resolv.conf ---"
echo "# Added by xiringuito" | sudo tee /etc/resolv.conf
echo "${REMOTE_RESOLV_CONF}" | sudo tee -a /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
echo "--- resolv.conf ---"
echo "${REMOTE_RESOLV_CONF}" | ./scripts/client-update-resolv-conf.sh
if [[ ${KERNEL} == darwin ]]; then
./scripts/${KERNEL}/client-update-macosx-dns.sh ${LOCAL_TUNNEL_ID}
fi
fi
fi