-
Notifications
You must be signed in to change notification settings - Fork 15
/
test-openvpn.ps1
193 lines (159 loc) · 5.69 KB
/
test-openvpn.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Param (
[string]$Openvpn,
[string]$Gui,
[string]$Config,
[array]$Ping,
[switch]$TestCmdexe,
[switch]$TestService,
[switch]$TestRespawn,
[switch]$TestGui,
[switch]$Help
)
Function Show-Usage {
Write-Host "Usage: Test-Openvpn.ps1 -Config <openvpn-config-file> -Ping <hosts> [-Openvpn <openvpn-exe>] [-Gui <openvpn-gui-exe>] [-TestCmdexe] [-TestService] [-TestRespawn] [-TestGui] [-Help]"
Write-Host
Write-Host "Parameters:"
Write-Host " -Openvpn Path to openvpn.exe (defaults to value in registry)"
Write-Host " -Gui Path to openvpn-gui.exe (as above, but openvpn.exe replaced with openvpn-gui.exe)"
Write-Host " -Config Path to the OpenVPN configuration file"
Write-Host " -Ping Target host(s) inside VPN to ping (should succeed). Separate multiple entries with commas."
Write-Host " -Suspend Test suspend and resume [UNIMPLEMENTED]"
Write-Host " -TestCmdexe Test connection from the command-line"
Write-Host " -TestGui Test OpenVPN-GUI"
Write-Host " -TestService Test openvpnserv2.exe"
Write-Host " -TestRespawn Test if openvpnserv2 is able to respawn a dead connection properly"
Write-Host " -Help Display this help"
Write-Host
Write-Host "Example: .\Test-Openvpn.ps1 -Config ""C:\Program Files\OpenVPN\config\company.ovpn"" -Ping 192.168.40.7 -TestCmdexe -TestService -TestGui"
}
Function Verify-Path ([string]$mypath) {
if (! (Test-Path $mypath -PathType leaf) ) {
Write-Host "ERROR: ${mypath} not found or not a file!"
Exit 1
}
}
$exe_path = (Get-ItemProperty 'HKLM:\SOFTWARE\OpenVPN' -Name 'exe_path').exe_path
if (! $Openvpn) {
$Openvpn = $exe_path
}
if (! $Gui) {
# Assume openvpn-gui.exe is in the same directory as openvpn.exe
$exe_dir = $exe_path.TrimEnd('openvpn.exe')
$Gui = (Join-Path $exe_dir 'openvpn-gui.exe')
}
if (! ($Config -and $Ping) -or $Help) {
Show-Usage
Exit 1
}
# Absolute directory from which this script is launched
$cwd = (Get-Item -Path ".\" -Verbose).FullName
# Separate OpenVPN configuration file path from the file name. This allows us
# to use relative certificate/key paths in OpenVPN configuration files.
$Configname = $Config.Split("\")[-1]
if ($Config -match '\\') {
$Configdir = $Config -replace $Configname, '' -Replace '\\$'
} else {
$Configdir = '.'
}
Verify-Path $Openvpn
Verify-Path $Gui
Verify-Path $Config
Function Stop-Generic ([string]$processname) {
$processes = (Get-Process|Where-Object { $_.ProcessName -eq "${processname}" })
foreach ($process in $processes) {
Stop-Process $process.Id -Force
}
}
Function Stop-Management {
$socket = New-Object System.Net.Sockets.TcpClient("127.0.0.1", "58581")
if ($socket) {
$Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
Start-Sleep -Seconds 1
$writer.WriteLine("signal SIGTERM")
$writer.Flush()
Start-Sleep -Seconds 5
} else {
Stop-Generic "openvpn"
}
}
Function Stop-Openvpn {
Stop-Generic "openvpn"
}
Function Stop-Gui {
& $Gui --command disconnect_all
}
Function Stop-Openvpnservice {
foreach ($service in 'OpenVPNService', 'OpenVPNServiceLegacy') {
if (Get-Service -Erroraction Ignore $service) {
Stop-Service $service
(Get-Service $service).WaitForStatus('Stopped')
}
}
}
# Stop all openvpn-related processes
Function Clean-Up {
# Kill all instances of (stock) OpenVPN
Stop-Openvpnservice
Stop-Generic "openvpn"
Stop-Gui
}
Function Check-Connectivity ([string]$test_type, [array]$ping) {
Start-Sleep -Seconds 10
foreach ($target in $ping) {
$connected = (Test-Connection -Computername $target -Quiet)
if ($connected) {
$result = "SUCCESS"
} else {
$result = "FAILURE"
}
Write-Host "${Configname} ${test_type} connection to ${target}: ${result}"
}
}
Function Test-Cmdexe {
# Create a .bat file dynamically for launching the OpenVPN connection in a
# separate cmd.exe windows. This approach was chosen as getting all the
# whitespace in openvpn.exe command-line quoted properly so that cmd.exe /c
# works properly was simply too nasty to deal with. Cmd.exe is used instead of
# Powershell as it allows sending signals (e.g. F4=EXIT) to the OpenVPN process.
$bat = "${Configname}.bat"
$pidfile = "${cwd}\${Configname}.pid"
Set-Content -Path "${bat}" -Value """${Openvpn}"" --management 127.0.0.1 58581 --config ""${config}"" --writepid ""${pidfile}"" --cd ""${Configdir}"" & exit"
Start-Process -FilePath $env:ComSpec -ArgumentList "/c", "start", "${bat}"
Check-Connectivity "cmdexe" $ping
Stop-Management
Remove-Item "${pidfile}"
Remove-Item "${bat}"
}
Function Test-Gui {
& $gui --connect "${configname}"
Check-Connectivity "gui" $ping
Stop-Gui
}
Function Test-Service {
$moved = @()
$configs = (Get-ChildItem "${configdir}" -Filter "*.ovpn")
foreach ($config in $configs) {
$current = $config.Name
if ( ! ($current -eq $configname) ) {
Move-Item "${configdir}\${current}" "${cwd}\${current}"
if ($?) {
$moved += ("${cwd}\${current}")
}
}
}
Start-Service OpenVPNService
Check-Connectivity "openvpnserv2" $ping
if ($TestRespawn) {
Stop-Openvpn
Check-connectivity "openvpnserv2-respawn" $ping
}
Stop-Service OpenVPNService
foreach ($move in $moved) {
Move-Item "${move}" "${configdir}\"
}
}
Clean-Up
if ($TestCmdExe) { Test-Cmdexe }
if ($TestGui) { Test-Gui }
if ($TestService) { Test-Service }