-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwsl-vpnkit-start.sh
executable file
·114 lines (93 loc) · 2.8 KB
/
wsl-vpnkit-start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
WIN_BIN=$(wslpath "C:\bin")
SOCKET_PATH=/var/run/wsl-vpnkit.sock
PIPE_PATH="//./pipe/wsl-vpnkit"
VPNKIT_BACKLOG="32"
VPNKIT_PATH="${VPNKIT_PATH:-"${WIN_BIN}/wsl-vpnkit.exe"}"
VPNKIT_NPIPERELAY_PATH="${VPNKIT_NPIPERELAY_PATH:-"${WIN_BIN}/npiperelay.exe"}"
VPNKIT_GATEWAY_IP="192.168.67.1"
VPNKIT_HOST_IP="192.168.67.2"
VPNKIT_LOWEST_IP="192.168.67.3"
VPNKIT_HIGHEST_IP="192.168.67.14"
VPNKIT_DEBUG="${VPNKIT_DEBUG}"
WIN_PIPE_PATH="${PIPE_PATH//\//\\}"
TAP_NAME=eth1
IP_ROUTE=
relay()
{
socat "UNIX-LISTEN:${SOCKET_PATH},fork,umask=007" "EXEC:${VPNKIT_NPIPERELAY_PATH} -ep -s ${PIPE_PATH},nofork"
}
vpnkit()
{
"${WIN_BIN}/wsl-vpnkit.exe" \
--ethernet "${WIN_PIPE_PATH}" \
--listen-backlog "${VPNKIT_BACKLOG}" \
--gateway-ip "${VPNKIT_GATEWAY_IP}" \
--host-ip "${VPNKIT_HOST_IP}" \
--lowest-ip "${VPNKIT_LOWEST_IP}" \
--highest-ip "${VPNKIT_HIGHEST_IP}"
}
tap()
{
vpnkit-tap-vsockd --tap "${TAP_NAME}" --path "${SOCKET_PATH}"
}
ipconfig()
{
# Remove the default interface first
IP_ROUTE="$(ip route | grep default)"
ip route del ${IP_ROUTE} # No quotes, it needs to use the spaces
ETHERNET_DEVICE="${IP_ROUTE##* }"
local OLD_IFS="${IFS}"
local IFS=$'\n'
OTHER_ROUTES=($(ip route | grep "${ETHERNET_DEVICE}"))
IFS="${OLD_IFS}"
for route in ${OTHER_ROUTES[@]+"${OTHER_ROUTES[@]}"}; do
ip route del ${route} # No quotes
done
# plumb what will probably be eth1
ip a add "${VPNKIT_LOWEST_IP}/255.255.255.0" dev "${TAP_NAME}"
ip link set dev "${TAP_NAME}" up
# Set the new default route
ip route add default via "${VPNKIT_GATEWAY_IP}" dev "${TAP_NAME}"
}
close()
{
ip link set dev "${TAP_NAME}" down
# for some reason, you get this problem https://serverfault.com/a/978311/321910
# Adding onlink works, and will be remove when WSL restarts, so it seems harmless
if [[ ${IP_ROUTE} =~ onlink ]]; then
ip route add ${IP_ROUTE} # No quotes
else
ip route add ${IP_ROUTE} onlink # No quotes
fi
for route in ${OTHER_ROUTES[@]+"${OTHER_ROUTES[@]}"}; do
ip route add ${route} # No quotes
done
kill 0
}
if [ "${EUID:-"$(id -u)"}" -ne 0 ]; then
echo "Please run this script as root"
exit 1
fi
# Connect the windows named pipe to socket
relay &
# Wait for socket to be created
while [ ! -S "${SOCKET_PATH}" ]; do
sleep 0.001
done
# Connect to the windows side of the socket
vpnkit &
# Connect to the linux side of the socket, and tap it as an ethernet device
tap &
# Wait for the ethernet device to be tapped
while [ ! -e "/sys/class/net/${TAP_NAME}" ]; do
sleep 0.0001
done
# create eth1 and patch routing table
ipconfig
# Make sure routing table is restored when finished, or else wsl.exe --terminate
# will be needed to restore the routing table
trap close exit
trap exit int term
# Just wait for the service to be killed
wait