Skip to content

Instantly share code, notes, and snippets.

@VirtuBox
Last active November 17, 2023 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VirtuBox/c4c1667c8a71a217d74585f4efd5c53d to your computer and use it in GitHub Desktop.
Save VirtuBox/c4c1667c8a71a217d74585f4efd5c53d to your computer and use it in GitHub Desktop.
Script to install wsl-vpnkit with a systemd service for Debian/Ubuntu
#!/usr/bin/env bash
# -------------------------------------------------------------------------
# wsl-vpnkit install script
# -------------------------------------------------------------------------
# Check if user is root
[ "$(id -u)" != "0" ] && {
echo "Error: You must be root or use sudo to run this script"
exit 1
}
export DEBIAN_FRONTEND=noninteractive
unset LANG
export LANG='en_US.UTF-8'
export LC_ALL='C.UTF-8'
# Colors
CSI='\033['
CRED="${CSI}1;31m"
CGREEN="${CSI}1;32m"
CEND="${CSI}0m"
# update package list
apt-get update -qq
# check if a command exist
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# check if required packages are installed
required_packages="curl tar jq"
for package in $required_packages; do
if ! command_exists "$package"; then
apt-get install "$package" -qq >/dev/null 2>&1
fi
done
_install_dependencies() {
echo -ne ' Installing dependencies [..]\r'
if {
# install dependencies
apt-get install iproute2 iptables iputils-ping dnsutils wget -y
} >>/tmp/wslvpnkit.log 2>&1; then
echo -ne " Installing dependencies [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
else
echo -e " Installing dependencies [${CRED}FAIL${CEND}]"
echo -e '\n Please look at /tmp/wslvpnkit.log\n'
exit 1
fi
}
create_startup_script() {
echo -ne ' Creating startup script [..]\r'
if {
cat <<EOF >/usr/local/bin/start-vpn-kit.sh
#!/bin/sh
export WSL_INTEROP=
for socket in $(ls /run/WSL | sort -n); do
if ss -elx | grep "$socket"; then
export WSL_INTEROP=/run/WSL/$socket
else
rm $socket
fi
done
# also start wsl-vpnkit
/mnt/c/Windows/system32/wsl.exe -d wsl-vpnkit --cd /app wsl-vpnkit
EOF
chmod +x /usr/local/bin/start-vpn-kit.sh
} >>/tmp/wslvpnkit.log 2>&1; then
echo -ne " Creating startup script [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
else
echo -e " Creating startup script [${CRED}FAIL${CEND}]"
echo -e '\n Please look at /tmp/wslvpnkit.log\n'
exit 1
fi
}
create_systemd_service() {
echo -ne ' Creating systemd service [..]\r'
if {
cat <<EOF >/lib/systemd/system/wsl-vpnkit.service
[Unit]
Description=wsl-vpnkit
After=network.target
[Service]
# for wsl-vpnkit setup as a distro
ExecStart=/usr/local/bin/start-vpn-kit.sh
Restart=always
KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
} >>/tmp/wslvpnkit.log 2>&1; then
echo -ne " Creating systemd service [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
else
echo -e " Creating systemd service [${CRED}FAIL${CEND}]"
echo -e '\n Please look at /tmp/wslvpnkit.log\n'
exit 1
fi
}
_starting_wsl_vpnkit() {
echo -ne ' Starting wsl-vpnkit [..]\r'
if {
# activer le service
systemctl enable wsl-vpnkit.service
# démarrer le service
systemctl start wsl-vpnkit.service
} >>/tmp/wslvpnkit.log 2>&1; then
echo -ne " Starting wsl-vpnkit [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
else
echo -e " Starting wsl-vpnkit [${CRED}FAIL${CEND}]"
echo -e '\n Please look at /tmp/wslvpnkit.log\n'
exit 1
fi
}
###############
# Main
###############
_install_dependencies
create_startup_script
create_systemd_service
_starting_wsl_vpnkit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment