Skip to content

Instantly share code, notes, and snippets.

@Daniel15
Last active August 29, 2023 21:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Daniel15/08921c969137dc3aff98f552c3ccf511 to your computer and use it in GitHub Desktop.
Save Daniel15/08921c969137dc3aff98f552c3ccf511 to your computer and use it in GitHub Desktop.
Routing scripts for Tinc

This Gist contains some scripts for Tinc, in order to configure the routing tables for a remote subnet on Windows (ie. if a remote system wants to expose an entire subnet over the VPN). Tinc does not configure the routing tables by default, which is why these scripts are needed. See the mailing list thread here: https://www.tinc-vpn.org/pipermail/tinc/2018-December/005340.html

Place the files in the Tinc network directory (eg. C:\Program Files\Tinc\netname\) and change $interface in functions.ps1 to match the name of the Tinc interface configured in tinc.conf

$interface = "Tinc" # Should match Interface setting in tinc.conf
# Gets the local IP address of the Tinc interface
function Get-LocalTincIP() {
$config = Get-NetIPConfiguration -InterfaceAlias $interface | Get-NetIPAddress -AddressFamily IPv4
return $config.IPAddress
}
# Converts CIDR (eg. "192.168.0.0/24") to IP and mask (eg. "192.168.0.0" with mask "255.255.255.0")
function Convert-CIDRToMask([Parameter(Mandatory)][String] $subnet) {
$parts = $subnet -split '/'
$ip = $parts[0]
$cidr = [Int32]$parts[1]
# Default to /32 if CIDR not provided
if ($cidr -eq 0) {
$cidr = 32
}
$mask = [IPAddress](([UInt32]::MaxValue) -shl (32 - $cidr) -shr (32 - $cidr))
return [PSCustomObject]@{
ip = $ip;
mask = [String]$mask;
}
}
cd %~dp0
powershell -ExecutionPolicy Bypass ./subnet-down.ps1
. ./functions.ps1
$local_ip = Get-LocalTincIP
$route = Convert-CIDRToMask $Env:SUBNET
Write-Output ('Removing route for {0}, subnet mask {1}' -f $route.ip, $route.mask)
route delete $route.ip mask $route.mask $local_ip
cd %~dp0
powershell -ExecutionPolicy Bypass ./subnet-up.ps1
. ./functions.ps1
$local_ip = Get-LocalTincIP
$route = Convert-CIDRToMask $Env:SUBNET
Write-Output ('Adding route for {0}, subnet mask {1}' -f $route.ip, $route.mask)
route add $route.ip mask $route.mask $local_ip
@hmoffatt
Copy link

Thanks, this is very helpful and works well.

I wonder if there is any way to speed it up? I have 20 nodes on my TINC network and I get a subnet-up for each of them, and it's taking quite long time to start PowerShell each time.

@Daniel15
Copy link
Author

@hmoffatt Not sure, sorry. I switched from Tinc to WireGuard once the Windows version was released.

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