|
[CmdletBinding(DefaultParameterSetName = "PreDown")] |
|
param ( |
|
[Parameter(ParameterSetName = "Setup")] |
|
[switch] |
|
$Setup, |
|
[Parameter(ParameterSetName = "Setup")] |
|
[switch] |
|
$RestartWGService, |
|
# WireGuard Interface |
|
[Parameter(Position = 0)] |
|
[string] |
|
$WireGuardInterfaceName = $env:WIREGUARD_TUNNEL_NAME, |
|
# Post Up Switch |
|
[Parameter(ParameterSetName = "PostUp")] |
|
[switch] |
|
$PostUp, |
|
# Post Up Switch |
|
[Parameter(ParameterSetName = "PreDown")] |
|
[switch] |
|
$PreDown, |
|
# No Default Route |
|
[Parameter(ParameterSetName = "PostUp")] |
|
[Parameter(ParameterSetName = "PreDown")] |
|
[switch] |
|
$NoDefaultRoute, |
|
# Use Route One |
|
[Parameter(ParameterSetName = "PostUp")] |
|
[Parameter(ParameterSetName = "PreDown")] |
|
[switch] |
|
$RouteOne |
|
) |
|
$InformationPreference = "Continue" |
|
|
|
# $ErrorActionPreference = "SilentlyContinue" |
|
function Invoke-WireGuardExternalRoutingSetup { |
|
[CmdletBinding()] |
|
param ( |
|
# Restart the Wireguard service if demanded |
|
[Parameter()] |
|
[switch] |
|
$RestartWGService |
|
) |
|
|
|
begin { |
|
|
|
} |
|
|
|
process { |
|
if ($PSCmdlet.ShouldContinue("DangerousScriptExecution", "Activating")) { |
|
|
|
$ActivateDangerousScriptExecutionSplat = @{ |
|
Path = "hklm:\Software\WireGuard" |
|
Name = "DangerousScriptExecution" |
|
PropertyType = 'DWord' |
|
Value = 1 |
|
ErrorAction = 'SilentlyContinue' |
|
} |
|
|
|
New-ItemProperty @ActivateDangerousScriptExecutionSplat |
|
|
|
if ($RestartWGService) { |
|
Write-Information "Restarting the Wireguard Service" |
|
Get-Service WireGuardManager | Restart-Service -Verbose |
|
} |
|
else { |
|
Write-Warning "You have to restart the wireguard service to apply the registry change" |
|
} |
|
} |
|
} |
|
|
|
end { |
|
|
|
} |
|
} |
|
|
|
if ($Setup) { |
|
Invoke-WireGuardExternalRoutingSetup -RestartWGService:$RestartWGService |
|
} else { |
|
$WireGuardInterface = Get-NetAdapter -Name $WireGuardInterfaceName |
|
} |
|
|
|
if (-not $NoDefaultRoute) { |
|
|
|
$DefaultNetRouteSplat = @{ |
|
InterfaceAlias = $WireGuardInterface.InterfaceAlias |
|
DestinationPrefix = "0.0.0.0/0" |
|
RouteMetric = 35 |
|
Confirm = $false |
|
} |
|
Write-Information -MessageData "Taking care of Default Route" |
|
switch ($PSCmdlet.ParameterSetName) { |
|
"PostUp" { New-NetRoute @DefaultNetRouteSplat | Out-Null } |
|
"PreDown" { Remove-NetRoute @DefaultNetRouteSplat | Out-Null } |
|
Default {} |
|
} |
|
|
|
|
|
} |
|
|
|
if ($RouteOne) { |
|
$RouteOneSplat = @{ |
|
InterfaceAlias = $WireGuardInterface.InterfaceAlias |
|
DestinationPrefix = "192.168.0.0/24" |
|
Confirm = $false |
|
} |
|
Write-Information -MessageData "Taking care of Route One" |
|
switch ($PSCmdlet.ParameterSetName) { |
|
"PostUp" { New-NetRoute @RouteOneSplat | Out-Null } |
|
"PreDown" { Remove-NetRoute @RouteOneSplat | Out-Null } |
|
Default {} |
|
} |
|
} |
|
|
|
# Bonus DNS Snippet |
|
# Set to $true to enable |
|
$SetupDNS = $false |
|
if ($SetupDNS){ |
|
|
|
$setDnsClientServerAddressSplat = @{ |
|
InterfaceAlias = $WireGuardInterface.InterfaceAlias |
|
} |
|
Write-Information -MessageData "Taking care of DNS" |
|
switch ($PSCmdlet.ParameterSetName) { |
|
"PostUp" { |
|
Set-DnsClientServerAddress @setDnsClientServerAddressSplat -ServerAddresses "192.168.0.1" |
|
} |
|
"PreDown" { |
|
Set-DnsClientServerAddress @setDnsClientServerAddressSplat -ResetServerAddresses |
|
} |
|
Default {} |
|
} |
|
} |
This is great, now that I've got it working.
Thanks so much for automating it!
That said, I got really jammed up at least 3 times, trying to get it up and running. First, because I was relying on Win10's baked-in PowerShell to execute the script -- which it doesn't. So I needed to install PowerShell 7.0 and let the installer give me a real PATH registration for the new pwsh.
Then, before I realized that powershell was blocking execution because it's a foreign, unsigned script, I ended up adding the needed:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WireGuard\DangerousScriptExecution = DWORD32(1)by hand.Then when the WireGuard log showed that it was being blocked for lack of a signature, I figured I'd just lower my execution policy threshold to 'unrestricted' to get it through. But somehow that wasn't enough! And I had to go find out that you can just do this instead:
Unblock-File -Path "<your-path-to>\Invoke-WireGuardRoutingHelper.ps1"And THEN!... It worked. And I now have a discreet, always-on VPN tunnel that runs silently in the background, while I innocently check my email over the unmasked public connection from the ISP. It's a real thing of beauty, this! 👍