|
5 | 5 | An agent that automatically patches your WSL2 DNS configuration when using Cisco AnyConnect (or similar VPNs that block
|
6 | 6 | split-tunneling).
|
7 | 7 |
|
8 |
| -## Prerequisite #1 - WSL2 Internet Access |
| 8 | +## How it works |
9 | 9 |
|
10 |
| -First you need to ensure your WSL2 distributions can access the internet. Before connecting to the VPN your routes for |
11 |
| -WSL2 will look something like (using the `Get-NetAdapter` command in powershell): |
| 10 | +1. The agent detects when you connect/disconnect from a VPN. |
| 11 | +2. The agent finds the highest priority DNS servers being used by Windows. |
| 12 | +3. The agent detects your WSL2 distributions, for each distribution it ensures that `generateResolvConf` is disabled, |
| 13 | + and then writes the DNS servers to `/etc/resolv.conf`. |
12 | 14 |
|
13 |
| -``` |
14 |
| -ifIndex DestinationPrefix NextHop RouteMetric ifMetric PolicyStore |
15 |
| -------- ----------------- ------- ----------- -------- ----------- |
16 |
| -26 172.31.79.255/32 0.0.0.0 256 5000 ActiveStore |
17 |
| -26 172.31.64.1/32 0.0.0.0 256 5000 ActiveStore |
18 |
| -26 172.31.64.0/20 0.0.0.0 256 5000 ActiveStore |
19 |
| -``` |
20 |
| - |
21 |
| -But when you connect to the VPN, AnyConnect adds a non-functional route with a lower metric: |
22 |
| - |
23 |
| -``` |
24 |
| -26 172.31.79.255/32 0.0.0.0 256 5000 ActiveStore |
25 |
| -26 172.31.64.1/32 0.0.0.0 256 5000 ActiveStore |
26 |
| -56 172.31.64.0/20 10.17.104.1 1 1 ActiveStore |
27 |
| -26 172.31.64.0/20 0.0.0.0 256 5000 ActiveStore |
28 |
| -``` |
| 15 | +## Usage |
29 | 16 |
|
30 |
| -Unfortunately we cannot remove or modify this route because it will be automatically |
31 |
| -[replaced by AnyConnect](https://community.cisco.com/t5/vpn/enforcing-the-split-tunnel-only-access/m-p/4390557/highlight/true#M278089). |
32 |
| -However, Windows determines the best route by the lowest sum of interface metric + route metric. What we can do is |
33 |
| -increase the AnyConnect interface metric: |
| 17 | +**Ensure you have first fixed the route table for WSL2, and not broken the Windows DNS server priority in the process**. |
| 18 | +See the [guide](./docs/ROUTING.md) for how to do this. |
34 | 19 |
|
35 |
| -```powershell |
36 |
| -Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000 |
37 |
| -``` |
| 20 | +Simply download `wsl2-dns-agent.exe` from the [releases page](https://github.com/jacob-pro/wsl2-dns-agent/releases/latest) |
38 | 21 |
|
39 |
| -Now the route table will allow WSL2's NAT connection to the Internet, because 5256 is a lower metric than 6001: |
| 22 | +Save it to your startup folder (`%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup`). |
40 | 23 |
|
41 |
| -``` |
42 |
| -26 172.31.79.255/32 0.0.0.0 256 5000 ActiveStore |
43 |
| -26 172.31.64.1/32 0.0.0.0 256 5000 ActiveStore |
44 |
| -56 172.31.64.0/20 10.17.104.1 1 6000 ActiveStore |
45 |
| -26 172.31.64.0/20 0.0.0.0 256 5000 ActiveStore |
46 |
| -``` |
| 24 | +Launch the application. |
47 | 25 |
|
48 |
| -(Unfortunately we still cannot connect from Windows to WSL2 via its IP address because AnyConnect blocks this at the |
49 |
| -firewall level using Windows Filtering Platform) |
| 26 | +## Advanced options |
50 | 27 |
|
51 |
| -## Prerequisite #2 - Working Windows DNS |
| 28 | +For advanced use cases you can edit the config file in `%APPDATA%\WSL2 DNS Agent\config.toml` |
52 | 29 |
|
53 |
| -The above fix then leads to a problem for the Windows host, when we look at the routes to the internet the AnyConnect |
54 |
| -adapter (56) now has a higher metric than Wi-Fi (17) and Ethernet (13): |
| 30 | +Example config: |
55 | 31 |
|
56 | 32 | ```
|
57 |
| -56 0.0.0.0/0 10.17.104.1 1 6000 ActiveStore |
58 |
| -17 0.0.0.0/0 10.2.9.254 0 50 ActiveStore |
59 |
| -13 0.0.0.0/0 10.2.9.254 0 25 ActiveStore |
60 |
| -``` |
61 |
| - |
62 |
| -This will cause Windows to attempt to connect to the now inaccessible DNS servers on Ethernet and Wi-Fi first, causing |
63 |
| -up to a 10-second delay in DNS resolution. The solution is to manually update the network interfaces to have a higher |
64 |
| -metric than the AnyConnect interface. |
| 33 | +show_notifications = false |
65 | 34 |
|
66 |
| -Set the Ethernet and Wi-Fi metrics to 6025 and 6050 to ensure they have lower priority than the AnyConnect route (6001) |
67 |
| -(Control Panel -> Network and Sharing Center -> Change adapter settings -> Ethernet Properties -> Internet Protocol Version 4 -> Advanced) |
| 35 | +# Default options for distributions |
| 36 | +[defaults] |
| 37 | +apply_dns = true |
| 38 | +patch_wsl_conf = true |
| 39 | +# If the distribution was previously Stopped, then shutdown once the DNS update is complete |
| 40 | +# Note: This option is usually not needed on Windows 11 (because vmIdleTimeout will do it for you) |
| 41 | +shutdown = false |
68 | 42 |
|
69 |
| -``` |
70 |
| -56 0.0.0.0/0 10.17.104.1 1 6000 ActiveStore |
71 |
| -17 0.0.0.0/0 10.2.9.254 0 6050 ActiveStore |
72 |
| -13 0.0.0.0/0 10.2.9.254 0 6025 ActiveStore |
| 43 | +# Set options for a specific distribution |
| 44 | +[distributions.Ubuntu] |
| 45 | +apply_dns = false |
73 | 46 | ```
|
0 commit comments