Skip to content

Instantly share code, notes, and snippets.

@Iksas
Last active November 17, 2024 20:38
Show Gist options
  • Save Iksas/de855339eb4cb6c61461ba3f1549b79c to your computer and use it in GitHub Desktop.
Save Iksas/de855339eb4cb6c61461ba3f1549b79c to your computer and use it in GitHub Desktop.
Auto-connecting multiple Wireguard tunnels in Windows

Wireguard multi-tunnel auto-connect in Windows

Set up multiple simultaneous Wireguard tunnels that auto-connect on boot in Windows.

Install the Wireguard tunnels

Install the Windows version of Wireguard and import the two tunnel .conf files.

Set required registry key

Set the following key to a DWORD with value 1:

`Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Wireguard\MultipleSimultaneousTunnels`

(docs)

Auto-start both tunnels

After the machine boots up, Wireguard still only activates one of the two tunnels.

To fix this, there are two solutions:

Variant 1: Add PreUp statements to the Wireguard tunnels

You can use the PreUp section of one tunnel to activate the other tunnel.

For this to work, set this additional registry key to a DWORD with value 1:

`Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Wireguard\DangerousScriptExecution`

Assuming the tunnels are called wg0 and wg1, edit them and add the following to their [Interface] sections:

Tunnel wg0 activates wg1:

[Interface]
...
PreUp = (wireguard /installtunnelservice "C:\Program Files\WireGuard\Data\Configurations\wg1.conf.dpapi") ^& exit 0

Tunnel wg1 activates wg0:

[Interface]
...
PreUp = (wireguard /installtunnelservice "C:\Program Files\WireGuard\Data\Configurations\wg0.conf.dpapi") ^& exit 0

Note how ^& exit 0 ignores errors that occur if the other tunnel is already running.

Variant 2: Start both tunnels with a .bat script

It's also possible to add the following .bat script to the computer's startup items to activate the tunnels:

rem Wait for 5 seconds
timeout /t 5 /nobreak > NUL

rem Connect only the tunnels that are not already connected
wg | findstr wg0 || (wireguard /installtunnelservice "C:\Program Files\WireGuard\Data\Configurations\wg0.conf.dpapi" > nul 2> nul)
wg | findstr wg1 || (wireguard /installtunnelservice "C:\Program Files\WireGuard\Data\Configurations\wg1.conf.dpapi" > nul 2> nul)

Install the script as a startup application, or use Task Scheduler to create a task that runs after the user logs in.

The script has to be run with administrator privileges. Make sure that the .bat file cannot be edited by normal users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment