From 28992229fedfa64979faa9ec84b1b4bcf5c8f449 Mon Sep 17 00:00:00 2001 From: David Beck Date: Fri, 17 Nov 2023 14:46:00 +0100 Subject: [PATCH 1/3] Adapt the wsl-vpnkit, so it makes use of WSL_INTEROP variable and make it self-healing when the socket would be closed for one reason or another --- wsl-vpnkit | 38 ++++++++++++++++++++++++++++++-------- wsl-vpnkit.service | 3 ++- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/wsl-vpnkit b/wsl-vpnkit index e610d7c..e1b60d3 100755 --- a/wsl-vpnkit +++ b/wsl-vpnkit @@ -27,12 +27,14 @@ fi WSL2_RESOLVCONF=${WSL2_RESOLVCONF:-$WSL2_RESOLVCONF_DEFAULT} WSL2_GATEWAY_IP_FROM_RESOLVCONF="$(cat $WSL2_RESOLVCONF | awk '/^nameserver/ {print $2}')" WSL2_GATEWAY_IP=${WSL2_GATEWAY_IP:-$WSL2_GATEWAY_IP_FROM_RESOLVCONF} +export WSL_INTEROP=${WSL_INTEROP:-"/run/WSL/$(ls /run/WSL | sort -n | tail -n 1)"} set -x # show values WSL2_TAP_NAME=$WSL2_TAP_NAME WSL2_GATEWAY_IP=$WSL2_GATEWAY_IP +WSL_INTEROP=$WSL_INTEROP if [ "$DEBUG" -eq 0 ]; then set +x @@ -120,6 +122,32 @@ check () { check_https "https://$CHECK_HOST" } +start () { + # run cleanup to restore any leftover configuration changes + cleanup + wsl2tap_down + run + iptables_set A + check +} + +monitor () { + while true + do + if ss -elx | grep -q $WSL_INTEROP; then + : #everything is going well, socket still valid, no action to do + else + echo "Something is going bad, socket $WSL_INTEROP broken, trying to repair..." + echo "killing all subprcesses from this wsl-vpnkit-script" + pstree -A -p $$ | grep -Eow "[0-9]+" | tail -n +2 | xargs kill + echo "setting new Interop-variable" + export WSL_INTEROP="/run/WSL/$(ls /run/WSL | sort -n | tail -n 1)" + start + fi + sleep 1 + done +} + cleanup () { iptables_set D 2>/dev/null wsl2tap_up @@ -158,11 +186,5 @@ fi trap close exit trap exit int term -# run cleanup to restore any leftover configuration changes -cleanup - -wsl2tap_down -run -iptables_set A -check -wait +start +monitor diff --git a/wsl-vpnkit.service b/wsl-vpnkit.service index dee9019..196f9d6 100644 --- a/wsl-vpnkit.service +++ b/wsl-vpnkit.service @@ -1,8 +1,9 @@ [Unit] Description=wsl-vpnkit -After=network.target [Service] +# Run as one of last services to start up +Type=idle # for wsl-vpnkit setup as a distro ExecStart=/mnt/c/Windows/system32/wsl.exe -d wsl-vpnkit --cd /app wsl-vpnkit From fab5742f7f10c28cef819f4e4488aa8d8acc76d7 Mon Sep 17 00:00:00 2001 From: David Beck Date: Mon, 20 Nov 2023 14:32:01 +0100 Subject: [PATCH 2/3] Adapt the wsl-vpnkit, so it makes use of WSL_INTEROP variable and make it self-healing when the socket would be closed for one reason or another --- wsl-vpnkit | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wsl-vpnkit b/wsl-vpnkit index e1b60d3..2e976fb 100755 --- a/wsl-vpnkit +++ b/wsl-vpnkit @@ -45,6 +45,7 @@ command -v iptables-legacy >/dev/null && alias iptables=iptables-legacy run () { echo "starting vm and gvproxy..." + printenv $VMEXEC_PATH \ -url="stdio:$GVPROXY_PATH?listen-stdio=accept&debug=$DEBUG" \ -iface="$TAP_NAME" \ @@ -134,12 +135,13 @@ start () { monitor () { while true do - if ss -elx | grep -q $WSL_INTEROP; then + if ss -elx | grep -q $WSL_INTEROP && + [ $WSL_INTEROP = "/run/WSL/$(ls /run/WSL | sort -n | tail -n 1)" ]; then : #everything is going well, socket still valid, no action to do else echo "Something is going bad, socket $WSL_INTEROP broken, trying to repair..." echo "killing all subprcesses from this wsl-vpnkit-script" - pstree -A -p $$ | grep -Eow "[0-9]+" | tail -n +2 | xargs kill + pstree -p $$ | grep -Eow "[0-9]+" | tail -n +2 | xargs kill echo "setting new Interop-variable" export WSL_INTEROP="/run/WSL/$(ls /run/WSL | sort -n | tail -n 1)" start From 6845578336b5bcc3484f23dce51c4f6ac37baba6 Mon Sep 17 00:00:00 2001 From: David Beck Date: Mon, 20 Nov 2023 15:33:59 +0100 Subject: [PATCH 3/3] Adapt the wsl-vpnkit, so it makes use of WSL_INTEROP variable and make it self-healing when the socket would be closed for one reason or another --- wsl-vpnkit | 1 - 1 file changed, 1 deletion(-) diff --git a/wsl-vpnkit b/wsl-vpnkit index 2e976fb..391a9fc 100755 --- a/wsl-vpnkit +++ b/wsl-vpnkit @@ -45,7 +45,6 @@ command -v iptables-legacy >/dev/null && alias iptables=iptables-legacy run () { echo "starting vm and gvproxy..." - printenv $VMEXEC_PATH \ -url="stdio:$GVPROXY_PATH?listen-stdio=accept&debug=$DEBUG" \ -iface="$TAP_NAME" \