-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg2ws-converter.ps1
177 lines (145 loc) · 7.69 KB
/
wg2ws-converter.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<#
.SYNOPSIS
Converts a standard WireGuard configuration file into a WireSock configuration file.
.DESCRIPTION
This script takes a WireGuard configuration file as input and generates a client side selective tunnel DNS leak proof WireSock configuration file. It assumes that the endpoint VPN server can serve DNS queries and is also DNS leak proof.
It prompts the user for custom routes and adds PostUp and PostDown scripts for DNS and routing management.
The PostUp script does the following:
1. Backs up current DNS settings for all active network adapters
2. Sets DNS to the specified server for all active adapters
3. Disables IPv6 on all active adapters
4. Adds routes for the VPN subnet and custom routes
The PostDown script does the following:
1. Restores original DNS settings for all network adapters
2. Re-enables IPv6 on all adapters
3. Removes the routes added by PostUp
.PARAMETER InputFile
The path to the input WireGuard configuration file. If not provided, the script will prompt for it.
.PARAMETER OutputFile
The path where the output WireSock configuration file will be saved. If not provided, the script will prompt for it.
.EXAMPLE
To run the script without changing the execution policy, use the following command in PowerShell:
powershell.exe -ExecutionPolicy Bypass -File .\WireSockConfigGenerator.ps1
.EXAMPLE
To run the script with parameters:
powershell.exe -ExecutionPolicy Bypass -File .\WireSockConfigGenerator.ps1 -InputFile "C:\path\to\wg0.conf" -OutputFile "C:\path\to\ws0.conf"
.NOTES
This script is designed for Windows environments. The -ExecutionPolicy Bypass parameter allows the script to run without changing the system-wide execution policy, which is more secure. Always ensure you trust the script before running it this way.
Author: Anton Luu
Date: September 7, 2024
License: GNU General Public License v2.0
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Full license text can be found at: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
#>
param(
[Parameter(Mandatory=$false)]
[string]$InputFile,
[Parameter(Mandatory=$false)]
[string]$OutputFile
)
# Function to display warnings with only "Warning:" and the faulty entry in red
function Write-ColorWarning {
param([string]$message, [string]$faultyEntry = "")
Write-Host "Warning: " -ForegroundColor Red -NoNewline
if ($faultyEntry) {
$parts = $message -split [regex]::Escape($faultyEntry)
Write-Host $parts[0] -NoNewline
Write-Host $faultyEntry -ForegroundColor Red -NoNewline
Write-Host $parts[1]
} else {
Write-Host $message
}
}
# Default file names
$defaultInputFile = "wg0.conf"
$defaultOutputFile = "ws0.conf"
# Prompt for input file
if (-not $InputFile) {
$InputFile = Read-Host "Enter the path to the input WireGuard configuration file (default: $defaultInputFile)"
if ([string]::IsNullOrWhiteSpace($InputFile)) {
$InputFile = $defaultInputFile
}
}
# Prompt for output file
if (-not $OutputFile) {
$OutputFile = Read-Host "Enter the path for the output WireSock configuration file (default: $defaultOutputFile)"
if ([string]::IsNullOrWhiteSpace($OutputFile)) {
$OutputFile = $defaultOutputFile
}
}
# Read the input WireGuard configuration file
$config = Get-Content $InputFile -Raw
# Parse the existing configuration
$interface = [regex]::Match($config, '\[Interface\](.*?)(?=\[Peer\]|\z)', [System.Text.RegularExpressions.RegexOptions]::Singleline).Groups[1].Value.Trim()
$peer = [regex]::Match($config, '\[Peer\](.*)', [System.Text.RegularExpressions.RegexOptions]::Singleline).Groups[1].Value.Trim()
# Extract DNS server from the WireGuard config
$dnsServer = [regex]::Match($interface, 'DNS\s*=\s*(.*)').Groups[1].Value.Trim()
if ([string]::IsNullOrWhiteSpace($dnsServer)) {
Write-ColorWarning "No DNS server found in the WireGuard config. Some features may not work correctly."
}
# Remove initial AllowedIPs
$peer = $peer -replace 'AllowedIPs\s*=\s*0\.0\.0\.0/0,\s*::/0\s*\n?', ''
# Prompt for allowed IPs and custom routes
$allowedIPs = @()
Write-Host "Enter IP ranges, websites, or IPs to route through the VPN."
Write-Host "Please enter only one item per line. Press Enter on an empty line to finish."
Write-Host "Examples:"
Write-Host " 10.0.0.0/24"
Write-Host " example.com"
Write-Host " 203.0.113.0"
do {
$route = Read-Host "Route"
if ($route -ne "") {
$allowedIPs += $route
}
} while ($route -ne "")
# Process allowed IPs and custom routes
$processedIPs = @()
foreach ($route in $allowedIPs) {
if ($route -match "^(\d{1,3}\.){3}\d{1,3}(\/\d{1,2})?$") {
# IPv4 address or range
$processedIPs += $route
} elseif ($route -match "^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$") {
# FQDN
try {
$ip = Resolve-DnsName $route -Type A -ErrorAction Stop | Select-Object -ExpandProperty IPAddress -First 1
if ($ip) {
$processedIPs += "$ip/32"
}
} catch {
Write-ColorWarning "Could not resolve FQDN $route. Skipping." $route
}
} elseif ($route -match ":") {
# IPv6 address or range
Write-ColorWarning "IPv6 address $route detected. Skipping as per requirements." $route
} else {
Write-ColorWarning "Invalid input $route. Skipping." $route
}
}
$allowedIPsString = $processedIPs -join ", "
# Prepare PostUp and PostDown scripts (without comments)
$postUp = "PostUp = powershell -Command `"`$adapters = Get-NetAdapter | Where-Object {`$_.Status -eq 'Up'}; `$global:dnsBackup = @{}; foreach (`$adapter in `$adapters) { `$dnsServers = (Get-DnsClientServerAddress -InterfaceAlias `$adapter.Name -AddressFamily IPv4).ServerAddresses; if (`$dnsServers) { `$global:dnsBackup[`$adapter.Name] = `$dnsServers; Set-DnsClientServerAddress -InterfaceAlias `$adapter.Name -ServerAddresses $dnsServer }; Disable-NetAdapterBinding -InterfaceAlias `$adapter.Name -ComponentID ms_tcpip6 }; `$global:dnsBackup | ConvertTo-Json | Set-Content 'C:\Windows\Temp\dns_backup.json'; $($processedIPs | ForEach-Object { "route add $_ mask 255.255.255.255 $dnsServer metric 5; " })`""
$postDown = "PostDown = powershell -Command `"if (Test-Path 'C:\Windows\Temp\dns_backup.json') { `$dnsBackup = Get-Content 'C:\Windows\Temp\dns_backup.json' | ConvertFrom-Json; foreach (`$adapter in `$dnsBackup.PSObject.Properties) { Set-DnsClientServerAddress -InterfaceAlias `$adapter.Name -ServerAddresses `$adapter.Value; Enable-NetAdapterBinding -InterfaceAlias `$adapter.Name -ComponentID ms_tcpip6 }; Remove-Item 'C:\Windows\Temp\dns_backup.json' }; $($processedIPs | ForEach-Object { "route delete $_ mask 255.255.255.255 $dnsServer; " })`""
# Construct the new WireSock configuration
$newConfig = @"
[Interface]
$interface
$postUp
$postDown
[Peer]
$peer
AllowedIPs = $allowedIPsString
"@
# Write the new configuration to the output file
$newConfig | Set-Content $OutputFile
Write-Host "WireSock configuration has been generated and saved to $OutputFile" -ForegroundColor Green