Skip to content

Commit

Permalink
adds vpn script to start in background incl. cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elonderin committed Apr 7, 2021
1 parent b91a5a2 commit f39dcc6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
52 changes: 52 additions & 0 deletions vpn
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# starts the wsl-vpnkit in a tmux sessions and cleanup beforehand, based on https://github.com/sakai135/wsl-vpnkit/issues/13

echo $BASH_SOURCE
echo $0

ME=$(readlink -f "$BASH_SOURCE")

if [ ! "$(id -u)" = "0" ]; then
echo "Not root, calling self as root..."
sudo -E sh -c $ME
exit "$?"
else
echo "Running as root..."
fi

set -x


function kill_vpnkit(){
SIG=$2
MSG=$1

if ps -ef | grep -v grep | grep "bash ./wsl-vpnkit" >/dev/null; then
echo $MSG
for i in $(ps -ef | grep "bash ./wsl-vpnkit" | grep -v grep | awk '{print $2}'); do kill $SIG $i; done
fi
}


kill_vpnkit "Graceful kill of older processes..."

#first graceful kill

if ps -ef | grep -v grep | grep "bash ./wsl-vpnkit" >/dev/null; then
echo "Wait 60 sec for graceful death..."
sleep 20
fi


kill_vpnkit "Not so graceful kill of older processes..." -9


#startup
echo "Starting up..."

cd ~/bin/wsl-vpnkit
if [ -f "$ME.out" ]; then
mv $ME.out $ME.out.$(date +'%Y%m%d%H%M%S')
fi

tmux new -s wsl-vpnkit -d "./wsl-vpnkit > ./wsl-vpnkit.out 2>&1"
12 changes: 10 additions & 2 deletions wsl-vpnkit
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#! /bin/sh
#!/usr/bin/env bash

set -x

SOCKET_PATH=/var/run/wsl-vpnkit.sock
PIPE_PATH="//./pipe/wsl-vpnkit"
Expand Down Expand Up @@ -69,7 +71,13 @@ close () {
"$POWERSHELL_PATH" -Command "Stop-Process -Id $PID_VPNKIT"
ip link set dev $TAP_NAME down
ip route add $IP_ROUTE
echo "$RESOLV_CONF" > /etc/resolv.conf
if [ -f /etc/resolv.conf~ ] ; then
# using a manually copied backup of the config is more stable,
# eg. when `wsl --shutdown` `close()` will not be executed and then the setting cannot be restored
cp /etc/resolv.conf~ /etc/resolv.conf
else
echo "$RESOLV_CONF" > /etc/resolv.conf
fi
kill 0
}

Expand Down

0 comments on commit f39dcc6

Please sign in to comment.