Skip to content

Commit ceb7473

Browse files
authored
install as distro (sakai135#37)
* install as distro * add config option for forwarding * small edits to readme * move distro files to dir * use same dir to install distro as readme * build npiperelay.exe * verify Docker Desktop Installer file * add build ref to distro image * update readme * show installed package versions * update build * Create LICENSE
1 parent 4273cb7 commit ceb7473

11 files changed

+282
-197
lines changed

.dockerignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.git
2+
.github
3+
.dockerignore
4+
distro/Dockerfile
5+
README.md
6+
distro/test.sh

.github/workflows/ci.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
pull_request:
6+
branches: [ $default-branch ]
7+
8+
env:
9+
TAG_NAME: ${{ format('wslvpnkit:{0}-{1}', github.sha, github.run_number) }}
10+
11+
jobs:
12+
13+
build:
14+
runs-on: ubuntu-20.04
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Build
18+
run: |
19+
docker build -t $TAG_NAME -f ./distro/Dockerfile --build-arg REF=$TAG_NAME .
20+
- name: Package
21+
run: |
22+
CONTAINER_ID=$(docker create $TAG_NAME)
23+
docker export $CONTAINER_ID | gzip > wsl-vpnkit.tar.gz
24+
sha256sum wsl-vpnkit.tar.gz | tee wsl-vpnkit.tar.gz.sha256
25+
ls -la wsl-vpnkit.tar.gz wsl-vpnkit.tar.gz.sha256
26+
- name: Artifacts
27+
uses: actions/upload-artifact@v2
28+
with:
29+
name: wsl-vpnkit
30+
path: |
31+
wsl-vpnkit.tar.gz
32+
wsl-vpnkit.tar.gz.sha256

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Keiichi Shimamura
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+42-125
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,74 @@
11
# wsl-vpnkit
22

3-
Uses [VPNKit](https://github.com/moby/vpnkit) and [npiperelay](https://github.com/jstarks/npiperelay) to provide network connectivity to the WSL 2 VM. This requires no settings changes or admin privileges on the Windows host.
3+
The `wsl-vpnkit` script uses [VPNKit](https://github.com/moby/vpnkit) and [npiperelay](https://github.com/jstarks/npiperelay) to provide network connectivity to the WSL 2 VM while connected to VPNs on the Windows host. This requires no settings changes or admin privileges on the Windows host.
44

5-
## Setup
6-
7-
The following steps will use WSL to setup `wsl-vpnkit`. If you do not have connectivity in WSL 2, you can [switch your WSL version](https://docs.microsoft.com/en-us/windows/wsl/install-win10#set-your-distribution-version-to-wsl-1-or-wsl-2) to WSL 1 for setup and back to WSL 2 once done. Alternatively, you can refer to [this post to setup `wsl-vpnkit` from the Windows side](https://github.com/sakai135/wsl-vpnkit/issues/11#issuecomment-777806102).
8-
9-
### Install `vpnkit.exe` and `vpnkit-tap-vsockd`
10-
11-
This will download and extract `vpnkit.exe` and `vpnkit-tap-vsockd` from the [Docker Desktop for Windows installer](https://docs.docker.com/docker-for-windows/install/). Alternatively, build `vpnkit.exe` and `vpnkit-tap-vsockd` from [VPNKit](https://github.com/moby/vpnkit).
12-
13-
```sh
14-
sudo apt install p7zip-full
15-
```
16-
17-
```sh
18-
wget https://desktop.docker.com/win/stable/amd64/67351/Docker%20Desktop%20Installer.exe
19-
7z e Docker\ Desktop\ Installer.exe resources/vpnkit.exe resources/wsl/docker-for-wsl.iso
20-
7z e docker-for-wsl.iso containers/services/vpnkit-tap-vsockd/lower/sbin/vpnkit-tap-vsockd
21-
rm Docker\ Desktop\ Installer.exe docker-for-wsl.iso
22-
23-
mkdir -p /mnt/c/bin
24-
mv vpnkit.exe /mnt/c/bin/wsl-vpnkit.exe
25-
26-
chmod +x vpnkit-tap-vsockd
27-
sudo chown root:root vpnkit-tap-vsockd
28-
sudo mv vpnkit-tap-vsockd /usr/local/sbin/vpnkit-tap-vsockd
29-
```
30-
31-
### Install `npiperelay.exe`
5+
The releases bundle the script together with VPNKit and npiperelay in an [Alpine](https://alpinelinux.org/) distro.
326

33-
Download from [npiperelay](https://github.com/jstarks/npiperelay).
7+
## Setup
348

35-
```sh
36-
wget https://github.com/jstarks/npiperelay/releases/download/v0.1.0/npiperelay_windows_amd64.zip
37-
7z e npiperelay_windows_amd64.zip npiperelay.exe
38-
rm npiperelay_windows_amd64.zip
9+
Download the prebuilt file `wsl-vpnkit.tar.gz` from the [latest release](https://github.com/sakai135/wsl-vpnkit/releases/latest) and import the distro into WSL 2. Running the distro will show a short intro and exit.
3910

40-
mkdir -p /mnt/c/bin
41-
mv npiperelay.exe /mnt/c/bin/
11+
```pwsh
12+
wsl --import wsl-vpnkit $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz
13+
wsl -d wsl-vpnkit
4214
```
4315

44-
### Install `socat`
16+
Start `wsl-vpnkit` from Windows or your other WSL 2 distros. Add the command to your `.profile` or `.bashrc` to start `wsl-vpnkit` when you open your WSL terminal.
4517

4618
```sh
47-
sudo apt install socat
19+
wsl.exe -d wsl-vpnkit service wsl-vpnkit start
4820
```
4921

50-
### Configure DNS for WSL
22+
### Notes
5123

52-
Disable WSL from generating and overwriting `/etc/resolv.conf` with the [network options in `wsl.conf`](https://docs.microsoft.com/en-us/windows/wsl/wsl-config#network).
24+
* Services on the WSL 2 VM are accessible from the Windows host using `localhost`.
25+
* Services on the Windows host are accessible from WSL 2 using `host.internal`.
5326

54-
```sh
55-
sudo tee /etc/wsl.conf <<EOL
56-
[network]
57-
generateResolvConf = false
58-
EOL
59-
```
27+
### Uninstall
6028

61-
Shutdown the WSL2 VM and reopen your shell for `wsl.conf` to take effect.
29+
To uninstall, unregister the distro.
6230

63-
```sh
64-
wsl.exe --shutdown
31+
```pwsh
32+
wsl --unregister wsl-vpnkit
33+
rm -r $env:USERPROFILE\wsl-vpnkit
6534
```
6635

67-
Manually set DNS servers to use when not running `wsl-vpnkit`. [`1.1.1.1`](https://1.1.1.1/dns/) is provided here as an example.
68-
69-
```sh
70-
sudo tee /etc/resolv.conf <<EOL
71-
nameserver 1.1.1.1
72-
EOL
73-
```
36+
### Build
7437

75-
### Clone `wsl-vpnkit`
38+
This will build and import the distro.
7639

7740
```sh
7841
git clone https://github.com/sakai135/wsl-vpnkit.git
7942
cd wsl-vpnkit/
80-
```
81-
82-
## Run
83-
84-
```sh
85-
sudo ./wsl-vpnkit
86-
```
87-
88-
Keep this terminal open.
89-
90-
In some environments, explicitly pass the environment variable `WSL_INTEROP` to `sudo`.
91-
92-
```sh
93-
sudo --preserve-env=WSL_INTEROP ./wsl-vpnkit
94-
```
95-
96-
Services on the WSL 2 VM should be accessible from the Windows host using `localhost` through [the WSL networking integrations](https://devblogs.microsoft.com/commandline/whats-new-for-wsl-in-insiders-preview-build-18945/#use-localhost-to-connect-to-your-linux-applications-from-windows) which can be configured by the [`localhostForwarding` option in `.wslconfig`](https://docs.microsoft.com/en-us/windows/wsl/wsl-config#wsl-2-settings). Services on the Windows host should be accessible from WSL 2 using the IP from `VPNKIT_HOST_IP` (`192.168.67.2`).
97-
98-
## Run in the Background
99-
100-
This uses `wsl.exe` and `start-stop-daemon` to run `wsl-vpnkit` in the background. A log file will be created at `/var/log/wsl-vpnkit.log` with the output from `wsl-vpnkit`.
101-
102-
```sh
103-
sudo ./wsl-vpnkit.service start
104-
```
105-
106-
## Run as a Service
107-
108-
This is an example setup to run `wsl-vpnkit` as a service.
10943

110-
### Create Service
111-
112-
```sh
113-
sudo ln -s $(pwd)/wsl-vpnkit.service /etc/init.d/wsl-vpnkit
44+
./distro/test.sh
11445
```
11546

116-
### Setup Sudoers
117-
118-
This allows running the `wsl-vpnkit` service without entering a password every time.
47+
## Using `wsl-vpnkit` as a standalone script
11948

120-
This step can be dangerous. Read [Sudoers](https://help.ubuntu.com/community/Sudoers) before doing this step.
49+
The `wsl-vpnkit` script can be used as a normal script in your existing distro. This is an example setup script for Ubuntu.
12150

12251
```sh
123-
sudo visudo -f /etc/sudoers.d/wsl-vpnkit
124-
```
52+
USERPROFILE=$(wslpath "$(/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe -c '$env:USERPROFILE' | tr -d '\r')")
53+
mkdir -p "$USERPROFILE/wsl-vpnkit"
12554

126-
```
127-
yourusername ALL=(ALL) NOPASSWD: /usr/sbin/service wsl-vpnkit *
128-
```
55+
sudo apt install p7zip-full socat
12956

130-
### Run Automatically
57+
wget https://desktop.docker.com/win/stable/amd64/67351/Docker%20Desktop%20Installer.exe
58+
7z e Docker\ Desktop\ Installer.exe resources/vpnkit.exe resources/wsl/docker-for-wsl.iso
59+
7z e docker-for-wsl.iso containers/services/vpnkit-tap-vsockd/lower/sbin/vpnkit-tap-vsockd
60+
rm Docker\ Desktop\ Installer.exe docker-for-wsl.iso
61+
mv vpnkit.exe "$USERPROFILE/wsl-vpnkit/wsl-vpnkit.exe"
62+
chmod +x vpnkit-tap-vsockd
63+
sudo chown root:root vpnkit-tap-vsockd
64+
sudo mv vpnkit-tap-vsockd /usr/local/sbin/vpnkit-tap-vsockd
13165

132-
Add the following to your `.profile` or `.bashrc` to start `wsl-vpnkit` when you open your WSL terminal.
66+
wget https://github.com/jstarks/npiperelay/releases/download/v0.1.0/npiperelay_windows_amd64.zip
67+
7z e npiperelay_windows_amd64.zip npiperelay.exe
68+
rm npiperelay_windows_amd64.zip
69+
mv npiperelay.exe "$USERPROFILE/wsl-vpnkit/"
13370

134-
```sh
135-
sudo service wsl-vpnkit start
71+
sudo ./wsl-vpnkit
13672
```
13773

13874
## Troubleshooting
@@ -141,28 +77,9 @@ sudo service wsl-vpnkit start
14177

14278
If VS Code takes a long time to open your folder in WSL, [enable the setting "Connect Through Localhost"](https://github.com/microsoft/vscode-docs/blob/main/remote-release-notes/v1_54.md#fix-for-wsl-2-connection-issues-when-behind-a-proxy).
14379

144-
### Try shutting down WSL VM to reset
145-
146-
```sh
147-
wsl.exe --shutdown
148-
```
149-
150-
```powershell
151-
Stop-Process -Name wsl-vpnkit
152-
```
153-
154-
### Check for the required processes
155-
156-
```sh
157-
ps aux | grep wsl-vpnkit
158-
```
159-
160-
* `socat ... npiperelay.exe`
161-
* `wsl-vpnkit.exe`
162-
* `vpnkit-tap-vsockd`
80+
### Try shutting down WSL 2 VM to reset
16381

164-
### Run VPNKit with Debug
165-
166-
```sh
167-
sudo VPNKIT_DEBUG=1 ./wsl-vpnkit
82+
```pwsh
83+
wsl --shutdown
84+
kill -Name wsl-vpnkit
16885
```

distro/Dockerfile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM alpine:3.14.2 AS vpnkit
2+
WORKDIR /files
3+
COPY ./distro/sha256sums ./
4+
RUN apk add --no-cache p7zip && \
5+
wget https://desktop.docker.com/win/stable/amd64/67351/Docker%20Desktop%20Installer.exe && \
6+
sha256sum -c sha256sums && \
7+
7z e Docker%20Desktop%20Installer.exe resources/vpnkit.exe resources/wsl/docker-for-wsl.iso && \
8+
7z e docker-for-wsl.iso containers/services/vpnkit-tap-vsockd/lower/sbin/vpnkit-tap-vsockd && \
9+
chmod +x vpnkit-tap-vsockd && \
10+
rm Docker%20Desktop%20Installer.exe docker-for-wsl.iso && \
11+
wget https://raw.githubusercontent.com/moby/vpnkit/v0.5.0/LICENSE
12+
13+
FROM golang:1.17.1-alpine3.14 AS npiperelay
14+
ENV GOOS=windows
15+
WORKDIR /files
16+
RUN go mod init local/build && \
17+
go get -d -v github.com/jstarks/[email protected] && \
18+
GOOS=windows go build -o ./npiperelay.exe github.com/jstarks/npiperelay && \
19+
cp /go/pkg/mod/github.com/jstarks/[email protected]/LICENSE ./
20+
21+
FROM alpine:3.14.2
22+
ARG REF=0
23+
WORKDIR /app
24+
COPY --from=npiperelay /files /files/npiperelay
25+
COPY --from=vpnkit /files /files/vpnkit
26+
COPY ./ ./
27+
RUN apk add --no-cache socat openrc iptables && \
28+
apk list --installed && \
29+
echo "$REF" > /app/ref && \
30+
ln -s /files/vpnkit/vpnkit-tap-vsockd /app/wsl-vpnkit /usr/sbin/ && \
31+
ln -s /app/distro/wsl-vpnkit.service /etc/init.d/wsl-vpnkit && \
32+
ln -s /app/distro/startup.sh /etc/profile.d/

distro/sha256sums

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8f36a5b306442d03a87bb80a492558c81902fdabe497af44a39ca07c161c60a5 Docker%20Desktop%20Installer.exe

distro/startup.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /bin/sh
2+
3+
LOG_PATH="/var/log/wsl-vpnkit.log"
4+
USERPROFILE=$(wslpath "$(/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe -c '$env:USERPROFILE' | tr -d '\r')")
5+
6+
touch $LOG_PATH
7+
8+
echo "
9+
This distro is only intended to run wsl-vpnkit.
10+
11+
Run the following commands from Windows or other WSL 2 distros to use.
12+
13+
wsl.exe -d $WSL_DISTRO_NAME service wsl-vpnkit start
14+
wsl.exe -d $WSL_DISTRO_NAME service wsl-vpnkit stop
15+
16+
The following files will be copied if they do not already exist.
17+
18+
$USERPROFILE/wsl-vpnkit/wsl-vpnkit.exe
19+
$USERPROFILE/wsl-vpnkit/npiperelay.exe
20+
21+
Logs for wsl-vpnkit can be viewed here.
22+
23+
wsl.exe -d $WSL_DISTRO_NAME tail -f $LOG_PATH
24+
25+
Config for wsl-vpnkit can be edited here. See the wsl-vpnkit script for possible values.
26+
27+
$USERPROFILE/wsl-vpnkit/wsl-vpnkit.conf
28+
wsl.exe -d $WSL_DISTRO_NAME cat /usr/sbin/wsl-vpnkit
29+
30+
Press [enter] key to continue...
31+
"
32+
read _
33+
exit 0

distro/test.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /bin/sh
2+
3+
# run from repo root
4+
# ./distro/test.sh
5+
6+
USERPROFILE="$(/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe -c '$env:USERPROFILE' | tr -d '\r')"
7+
DUMP=wsl-vpnkit.tar.gz
8+
TAG_NAME=wslvpnkit
9+
10+
docker build -t $TAG_NAME -f ./distro/Dockerfile .
11+
CONTAINER_ID=$(docker create $TAG_NAME)
12+
docker export $CONTAINER_ID | gzip > $DUMP
13+
docker container rm $CONTAINER_ID
14+
ls -la $DUMP
15+
16+
wsl.exe --unregister wsl-vpnkit
17+
wsl.exe --import wsl-vpnkit "$USERPROFILE\\wsl-vpnkit" $DUMP
18+
rm $DUMP
19+
wsl.exe -d wsl-vpnkit

0 commit comments

Comments
 (0)