-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWG-VPNCheck.ps1
More file actions
79 lines (72 loc) · 2.43 KB
/
WG-VPNCheck.ps1
File metadata and controls
79 lines (72 loc) · 2.43 KB
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
#Check if VPN is running and restart if not
#Location of Wireguard program
[System.IO.DirectoryInfo]$WireguardDir = "$env:ProgramFiles\Wireguard\"
#Location of Wireguard config file(s)
[System.IO.DirectoryInfo]$ConfigDir = $WireguardDir.FullName + 'Data\ConfigFiles\'
#Locaiton of specific config file for this VPN check
[System.IO.FileInfo]$ConfigFile = $ConfigDir.FullName + 'VPN.conf'
#Whether to check if the IP Wireguard is connecting to is the same as what DNS resolves to
$DNSCheck = $true #or '$false'
#DNS name Wireguard is trying to connect to, will not use DNS cache on client
$VPNDNSName = Resolve-DnsName -DnsOnly -NoHostsFile -Type A -Name 'DOMAIN_NAME.myddns.me'
#------------
Clear-Host
Write-Host '================
VPN Status Check
================'
if (($DNSCheck -ne $true) -and ($DNSCheck -ne $false)) {
Write-Host '$DNSCheck needs to be $true or $false'
exit 1
}
if (($WireguardDir.Exists -ne $true) -or ($ConfigDir.Exists -ne $true) -or ($ConfigFile.Exists -ne $true)) {
Write-Host "
Missing file or folder
---------------------
WireguardDir = $($WireguardDir.Exists)
ConfigdDir = $($ConfigDir.Exists)
ConfigFile = $($ConfigFile.Exists)
"
exit 1
} else {
Write-Host ''
cd $WireguardDir
$VPNInfo = .\wg.exe show
if ($null -eq $VPNInfo) {
Write-Host 'VPN not running, starting...'
wireguard.exe /installtunnelservice $ConfigFile
Start-Sleep -Seconds 5
$VPNInfo = .\wg.exe show
if ($null -eq $VPNInfo) {
Write-Host 'Failed to restart VPN'
exit 1
} else {
Write-Host 'VPN back up'
if ($DNSCheck -ne $true) {
exit 0
}
}
} else {
Write-Host 'VPN running, exiting'
if ($DNSCheck -ne $true) {
exit 0
}
}
}
#DNS Check
if ($DNSCheck -eq $true) {
$VPNIP = (($VPNInfo | Select-String 'endpoint') -split ': ' -split ':')[1]
if ($VPNIP -ne $VPNDNSName.IPAddress) {
Write-Host 'DNS and VPN IP mismatch'
$WireguardProcs = Get-Process 'wireguard'
foreach ($Proc in $WireguardProcs) {
Write-Host "Stopping $($Proc.ProcessName) ($($Proc.Id))"
Stop-Process -Id $Proc.Id -Force
}
Write-Host 'Starting VPN again...'
Start-Sleep -Seconds 5
wireguard.exe /installtunnelservice $ConfigFile
}
} else {
Write-Host '$DNSCheck not $true, skipping'
exit 0
}