Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve service init script and logs #147

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ Start `wsl-vpnkit` from your other WSL 2 distros. Add the command to your `.prof
wsl.exe -d wsl-vpnkit service wsl-vpnkit start
```

You can also check service status to start service only if needed.

```sh
wsl.exe -d wsl-vpnkit service wsl-vpnkit status >/dev/null || \
wsl.exe -d wsl-vpnkit service wsl-vpnkit start
```

### Notes

* Ports on the WSL 2 VM are accessible from the Windows host using `localhost`.
Expand Down Expand Up @@ -57,8 +64,8 @@ This will build and import the distro.
```sh
git clone https://github.com/sakai135/wsl-vpnkit.git
cd wsl-vpnkit/

./distro/test.sh
./build.sh
./test.sh
```

## Using `wsl-vpnkit` as a standalone script
Expand Down Expand Up @@ -113,3 +120,12 @@ wsl.exe -d wsl-vpnkit wsl-vpnkit
wsl --shutdown
kill -Name wsl-gvproxy
```

### Run service with debug

If you set DEBUG variable before calling service you can see more debug information

Example:
```sh
wsl.exe -d wsl-vpnkit DEBUG=1 service wsl-vpnkit restart
```
7 changes: 4 additions & 3 deletions distro/test.sh → build.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#! /bin/sh -e
#! /bin/sh -xe

# run from repo root
# ./distro/test.sh
# ./build.sh

USERPROFILE="$(powershell.exe -c 'Write-Host -NoNewline $env:USERPROFILE')"
DUMP=wsl-vpnkit.tar.gz
TAG_NAME=wslvpnkit

# build
docker build -t $TAG_NAME -f ./distro/Dockerfile .
CONTAINER_ID=$(docker create $TAG_NAME)
docker export $CONTAINER_ID | gzip > $DUMP
docker container rm $CONTAINER_ID
ls -la $DUMP

# reinstall
wsl.exe --unregister wsl-vpnkit || :
wsl.exe --import wsl-vpnkit "$USERPROFILE\\wsl-vpnkit" $DUMP --version 2
rm $DUMP
wsl.exe -d wsl-vpnkit
8 changes: 6 additions & 2 deletions distro/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ touch $LOG_PATH

echo "
wsl-vpnkit $VERSION
This distro is only intended for running wsl-vpnkit.
This distro is only intended for running wsl-vpnkit.

Run the following commands from Windows or other WSL 2 distros to use.

wsl.exe -d $WSL_DISTRO_NAME service wsl-vpnkit start
wsl.exe -d $WSL_DISTRO_NAME service wsl-vpnkit stop
wsl.exe -d $WSL_DISTRO_NAME service wsl-vpnkit start

The following file will be copied if it does not already exist.

Expand All @@ -23,6 +23,10 @@ Logs for wsl-vpnkit can be viewed here.

wsl.exe -d $WSL_DISTRO_NAME tail -f $LOG_PATH

To see only the most recent logs

wsl.exe -d $WSL_DISTRO_NAME sh -c \"tac $LOG_PATH | awk '{print}; /starting wsl-vpnkit/{exit}' | tac\"

Config for wsl-vpnkit can be edited here.

$USERPROFILE/wsl-vpnkit/wsl-vpnkit.conf
Expand Down
49 changes: 45 additions & 4 deletions distro/scripts/wsl-vpnkit.service
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
#! /bin/sh

PID_PATH="/var/run/wsl-vpnkit.pid"
LOG_PATH="/var/log/wsl-vpnkit.log"
EXECUTABLE="wsl-vpnkit" # starting point
SUB_PROCESS="wsl-vm" # forked sub processes
PID_PATH="/var/run/$EXECUTABLE.pid"
LOG_PATH="/var/log/$EXECUTABLE.log"

ret=0

_debug_check() {
if [ -n "$DEBUG" ]; then
set -x
test -f $PID_PATH && cat $PID_PATH
ps
fi
}

start() {
_debug_check
start-stop-daemon \
--pidfile $PID_PATH \
--make-pidfile \
--background \
--stdout $LOG_PATH \
--stderr $LOG_PATH \
--exec wsl-vpnkit \
--exec $EXECUTABLE \
--wait 1000 --progress \
${DEBUG+--verbose} \
--start
ret=$?
}

stop() {
_debug_check
start-stop-daemon \
--pidfile $PID_PATH \
--retry 2 \
${DEBUG+--verbose} \
--stop
ret=$?
# kill sub processes if still running
pgrep $SUB_PROCESS >/dev/null && pkill -9 $SUB_PROCESS
}

status() {
_debug_check
test -f $PID_PATH && pgrep -P $(cat $PID_PATH) &>/dev/null
ret=$?
if [ $ret = 0 ]; then
echo Service $EXECUTABLE is running
else
echo Service $EXECUTABLE is not running
fi
}

restart() {
stop
start
ret=$?
}

case "$1" in
Expand All @@ -31,8 +66,14 @@ case "$1" in
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: wsl-vpnkit {start|stop}"
echo "Usage: $EXECUTABLE {start|stop|restart|status}"
exit 1
esac

Expand Down
49 changes: 49 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#! /bin/sh -xe

# ensuring distro is stopped before running tests
if wsl.exe -d wsl-vpnkit service wsl-vpnkit status; then
wsl.exe -d wsl-vpnkit service wsl-vpnkit stop || \
wsl.exe -t wsl-vpnkit
fi

# tests
for debug_value in '1' '2' ''; do
if [ -n "${debug_value}" ]; then
debug_str="DEBUG=${debug_value}"
else
debug_str=""
fi
echo "####### Test Round with debug_str [${debug_str}] #######" | grep --colour=always .

# start service
wsl.exe -d wsl-vpnkit ${debug_str} service wsl-vpnkit start
output=$(wsl.exe -d wsl-vpnkit ${debug_str} service wsl-vpnkit status)
echo "$output" | grep --colour=always "Service wsl-vpnkit is running"

# check latest log
sleep 5
output=$(wsl.exe -d wsl-vpnkit sh -c "tac /var/log/wsl-vpnkit.log | awk '{print}; /starting wsl-vpnkit/{exit}' | tac")

( set +x # avoid clutter during output checks
# check for working ping
echo "$output" | grep --colour=always "ping success"

# check for working dns
echo "$output" | grep --colour=always "nslookup success"
)

# restart service
wsl.exe -d wsl-vpnkit ${debug_str} service wsl-vpnkit restart
output=$(wsl.exe -d wsl-vpnkit ${debug_str} service wsl-vpnkit status)
echo "$output" | grep --colour=always "Service wsl-vpnkit is running"

# stop service
wsl.exe -d wsl-vpnkit ${debug_str} service wsl-vpnkit stop
output=$(wsl.exe -d wsl-vpnkit ${debug_str} service wsl-vpnkit status)||echo "ignoring exit code"
echo "$output" | grep --colour=always "Service wsl-vpnkit is not running"

# check welcome screen
wsl.exe -d wsl-vpnkit sh -c 'echo 1 | source /etc/profile'
done

echo "$0 Finished" | grep --colour=always .