Skip to content

Commit

Permalink
direct WSL2 host IP to VPNKit (#54)
Browse files Browse the repository at this point in the history
* direct WSL2 host IP to VPNKit

* add ways to access host machine
  • Loading branch information
sakai135 authored Oct 13, 2021
1 parent 5a52eef commit 1c73d46
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ wsl.exe -d wsl-vpnkit service wsl-vpnkit start

### Notes

* Services on the WSL 2 VM are accessible from the Windows host using `localhost`.
* Services on the Windows host are accessible from WSL 2 using `host.internal`.
* Ports on the WSL 2 VM are accessible from the Windows host using `localhost`.
* Ports on the Windows host are accessible from WSL 2 using `host.internal`, `192.168.67.2`, or [the IP address of the host machine](https://docs.microsoft.com/en-us/windows/wsl/networking#accessing-windows-networking-apps-from-linux-host-ip).

### Update

Expand Down
7 changes: 5 additions & 2 deletions wsl-vpnkit
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ ipconfig () {

if [ "$USE_IPTABLES" ]; then
echo "adding rules to iptables..."
iptables -t nat -A OUTPUT -d $WSL2_GATEWAY_IP -j DNAT --to-destination $VPNKIT_GATEWAY_IP
iptables -t nat -A PREROUTING -d $WSL2_GATEWAY_IP -j DNAT --to-destination $VPNKIT_GATEWAY_IP
iptables -t nat -A PREROUTING -d $WSL2_GATEWAY_IP/32 -p udp -m udp --dport 53 -j DNAT --to-destination $VPNKIT_GATEWAY_IP:53
iptables -t nat -A PREROUTING -d $WSL2_GATEWAY_IP/32 -j DNAT --to-destination $VPNKIT_HOST_IP
iptables -t nat -A OUTPUT -d $WSL2_GATEWAY_IP/32 -p udp -m udp --dport 53 -j DNAT --to-destination $VPNKIT_GATEWAY_IP:53
iptables -t nat -A OUTPUT -d $WSL2_GATEWAY_IP/32 -j DNAT --to-destination $VPNKIT_HOST_IP
iptables -t nat -A POSTROUTING -o $TAP_NAME -j MASQUERADE
echo "iptables done"
fi
Expand All @@ -100,6 +102,7 @@ close () {
if [ "$USE_IPTABLES" ]; then
echo "removing rules from iptables..."
iptables -t nat -S | grep $VPNKIT_GATEWAY_IP | cut -d " " -f 2- | tr '\n' '\0' | xargs -0 -r -n 1 sh -c 'iptables -t nat -D $1' argv0
iptables -t nat -S | grep $VPNKIT_HOST_IP | cut -d " " -f 2- | tr '\n' '\0' | xargs -0 -r -n 1 sh -c 'iptables -t nat -D $1' argv0
iptables -t nat -S | grep $TAP_NAME | cut -d " " -f 2- | tr '\n' '\0' | xargs -0 -r -n 1 sh -c 'iptables -t nat -D $1' argv0
echo "iptables done"
fi
Expand Down

0 comments on commit 1c73d46

Please sign in to comment.