diff --git a/README.md b/README.md index 05817ae..9be36f3 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,12 @@ Create network namespace where all¹ network requests go via ssh connection. 1. Create a simple ssh config at `/etc/nnss//config`. This will be included with [other settings][0]. 2. Copy your ssh private key to `/etc/nnss//privatekey` -3. [Edit][1] your service file to include below properties +3. [Edit][1] your application's service file to include below properties ```systemd [Unit] -Requires=nnss-tunsocks@.service -After=nnss-tunsocks@.service +Requires=nnssA@.service +After=nnssA@.service [Service] NetworkNamespacePath=/run/netns/ns ``` @@ -43,8 +43,8 @@ Port 8822 # If the ssh server is not on default port 22 ❯ sudo systemd-run \ --property=NetworkNamespacePath=/run/netns/vps1ns \ --property=User=$USER \ - --property=Requires=nnss-tunsocks@vps1.service \ - --property=After=nnss-tunsocks@vps1.service \ + --property=Requires=nnssA@vps1.service \ + --property=After=nnssA@vps1.service \ --shell [sudo] password for balki: Running as unit: run-p233279-i233579.service diff --git a/nnss-tunsocks@.service b/nnssA@.service similarity index 90% rename from nnss-tunsocks@.service rename to nnssA@.service index f467663..b0a5a46 100644 --- a/nnss-tunsocks@.service +++ b/nnssA@.service @@ -9,7 +9,7 @@ StopWhenUnneeded=yes Type=notify NotifyAccess=all -RuntimeDirectory=nnss-tunsocks%i +RuntimeDirectory=nnssA%i ExecStart=/usr/lib/nnss/tunsocks.sh "%i" setup ExecStop=/usr/lib/nnss/tunsocks.sh "%i" cleanup diff --git a/nnssB@.service b/nnssB@.service new file mode 100644 index 0000000..bfba811 --- /dev/null +++ b/nnssB@.service @@ -0,0 +1,17 @@ +[Unit] +Description=Create network namespace and tun device to %I +StopWhenUnneeded=yes + +[Service] + +Type=notify +NotifyAccess=all + +RuntimeDirectory=nnssB%i + +# Add SOCKS_PROXY environment variable to below file. E.g. +# SOCKS_PROXY=socks5://127.0.0.1:9050 +EnvironmentFile=/etc/nnss/env_%i + +ExecStart=/usr/lib/nnss/tunsocks.sh "%i" use_env setup +ExecStop=/usr/lib/nnss/tunsocks.sh "%i" use_env cleanup diff --git a/tunsocks.sh b/tunsocks.sh index a594036..56a2e53 100755 --- a/tunsocks.sh +++ b/tunsocks.sh @@ -7,13 +7,19 @@ pidfile=${RUNTIME_DIRECTORY:-/tmp}/tunsocks.pid nsname="$1" device="tun${nsname}" ns="${nsname}ns" -socketpath="/run/nnss-${nsname}/sock" + +if [ "$2" = use_env ];then + proxy="$SOCKS_PROXY" + shift +else + proxy="socks5:///run/nnss-${nsname}/sock" +fi setup() { ip tuntap add mode tun dev "$device" - /usr/bin/tun2socks -device "$device" -proxy "socks5://$socketpath" & + /usr/bin/tun2socks -device "$device" -proxy "$proxy" & echo "$!" > "$pidfile" sleep 5