Gateway-keepaliveForPowershell/GateWay-KeepAlive.ps1
2024-12-19 00:14:14 +08:00

173 lines
6.1 KiB
PowerShell

. $(-Join($(pwd).Path, '\config.ps1'))
. $(-Join($(pwd).Path, '\proxy-test.ps1'))
function isDhcpEnable(){
return Get-NetIPConfiguration | select-object InterfaceAlias, IPv4Address, @{n="dhcp";e={$_.NetIPv4Interface.DHCP}} | Select-Object -ExpandProperty dhcp
}
function isForeignConnected {
# check foreign network availability
$result = Test-NetConnection -ComputerName $Global:TestDomain -Port 443
if ($result.TcpTestSucceeded) {
Write-Host "Successfully connected to $($Global:TestDomain) via HTTPS. isCustomGateway:$($Global:isCustomGateway)"
return $true
} else {
Write-Host "Failed to connect to $($Global:TestDomain) via HTTPS. isCustomGateway:$($Global:isCustomGateway)"
return $false
}
}
function isNetworkConnected {
# check network availability
$result = $(Test-Connection -ComputerName $Global:TestIP -Count 1).Status -eq 'Success'
if ($result) {
Write-Host "Successfully connected to $($Global:TestIP) via ICMP. isCustomGateway:$($Global:isCustomGateway)"
return $true
} else {
Write-Host "Failed to connect to $($Global:TestIP) via ICMP. isCustomGateway:$($Global:isCustomGateway)"
return $false
}
}
function setDhcp($enable){
if($enable){
Set-NetIPInterface -InterfaceAlias $Global:TargetNetworkAdapter.name -Dhcp Enabled
Write-Host enable dhcp
} else {
Set-NetIPInterface -InterfaceAlias $Global:TargetNetworkAdapter.name -Dhcp Disabled
Write-Host disable dhcp
}
}
function changeGateway{
param(
$adapter,$isCustomGateway
)
Write-Host "Changing gateway for adapter: $($adapter.Name) isCustomGateway $($isCustomGateway)"
# check gateway availability
if($isCustomGateway){
$currentGateway = $(Get-NetIPConfiguration).IPv4DefaultGateway.NextHop
if($currentGateway -eq $Global:TargetGateway){
Write-Host "current gateway is $currentGateway same as targetGateway $Global:TargetGateway, change gateway canceled"
$Global:isCustomGateway = $true
return
}
$result = $(Test-Connection $Global:TargetGateway -Count 1).Status
if (! ($result -eq 'Success')){
Write-Host custom gateway $Global:TargetGateway is $result, refuse to change
return
}
}
# check current ip configuration
$existingIP = Get-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4
# if have ip configuration, delete it
if ($existingIP) {
Write-Host "Removing existing IP address: $($existingIP.IPAddress)"
Remove-NetIPAddress -InterfaceAlias $adapter.Name -Confirm:$false
}
# delete current route(gateway)
$existingRoute = Get-NetRoute -InterfaceAlias $adapter.Name -DestinationPrefix "0.0.0.0/0"
if ($existingRoute) {
Write-Host "Removing existing default route..."
Remove-NetRoute -InterfaceAlias $adapter.Name -Confirm:$false
}
if ($isCustomGateway){ #setting gateway
# config customGateway
$Global:isCustomGateway = $true
# setting dns server
Set-DnsClientServerAddress -InterfaceAlias $adapter.name -ServerAddresses ($Global:TargetGateway, $Global:TargetGateway)
# disable dhcp
setDhcp($false)
# setting gateway to customGateway
New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress $Global:TargetIP -PrefixLength 24 -DefaultGateway $Global:TargetGateway
} else {
# config originalGateway
$Global:isCustomGateway = $false
# setting dns server
Set-DnsClientServerAddress -InterfaceAlias $adapter.name -ServerAddresses ($Global:originalDns, $Global:originalDns)
# setting gateway to originalGateway
New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress $Global:TargetIP -PrefixLength 24 -DefaultGateway $Global:originalGateway
}
Start-Sleep 3
}
function getIPAddress(){
$IP = Get-NetIPConfiguration | Select-Object -ExpandProperty IPV4Address | Select-Object -ExpandProperty IPAddress
return $IP
}
function getNetworkAdapter(){
$networkAdapters = Get-NetAdapter | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
Interface = $_.InterfaceDescription
Status = $_.Status
LinkSpeed = $_.LinkSpeed
MacAddress = $_.MacAddress
}
}
foreach ($networkAdapter in $networkAdapters){
if($networkAdapter.name.Contains($TargetNetworkAdapterKeyword)){
$Global:TargetNetworkAdapter = $networkAdapter
break;
}
}
Write-Host "getNetworkAdapter: " $Global:TargetNetworkAdapter
}
function init {
# check permission
$IsAdmin = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if (-not $IsAdmin.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
# if not admin permission, restart it with admin permission
Write-Host "Requesting Administrator privileges..."
Start-Process pwsh -ArgumentList "-NoExit", "-File", $PSCommandPath -Verb RunAs
exit
}
Write-Host "Running with Administrator privileges!"
# get netAdapter instance
getNetworkAdapter
Write-Host "current adapter: $($Global:TargetNetworkAdapter.name)"
# try to change to customGateway at start
changeGateway $Global:TargetNetworkAdapter $true
}
function loop(){
while($true){
# if network offline and using customGateway, try to restore to originalGateway
if((!$(isNetworkConnected)) -and $Global:isCustomGateway){
changeGateway $Global:TargetNetworkAdapter $false
}
# if customGateway proxy is working and using original gateway, try to change to customGateway
if($(Test-SocksProxy -ProxyHost $Global:TargetGateway -ProxyPort 1070) -and (!$Global:isCustomGateway)){
Write-Host isCustomGateway $Global:isCustomGateway
changeGateway $Global:TargetNetworkAdapter $true
}
# if customGateway proxy is not working but network still online, do nothing
Start-Sleep 1
}
}
init
loop