修改配置项,新建日志模块,写入windows事件管理器。
This commit is contained in:
parent
c70f53bed3
commit
3f7602d51b
@ -1,5 +1,17 @@
|
||||
. $(-Join($(pwd).Path, '\config.ps1'))
|
||||
. $(-Join($(pwd).Path, '\proxy-test.ps1'))
|
||||
# $Global:TestDomain = "www.google.com" #测试代理是否可用
|
||||
# $Global:TestIP = "223.5.5.5" #测试是否连接网络
|
||||
# $Global:originalDns = "223.5.5.5" #默认DNS
|
||||
# $Global:originalGateway = "192.168.6.1" #默认网关
|
||||
# $Global:CustomGateway = "192.168.6.2" #代理网关
|
||||
# $Global:CustomIP = "192.168.6.4" #默认ip
|
||||
# $Global:TargetNetworkAdapter = {} #网络适配器实例
|
||||
# $Global:TargetNetworkAdapterKeyword = "Ethernet" #网络适配器名字通配符
|
||||
# $Global:isCustomGateway = $false #是否为自定义网关
|
||||
|
||||
. $(-Join($(Get-Location).Path, '\config.ps1'))
|
||||
. $(-Join($(Get-Location).Path, '\proxy-test.ps1'))
|
||||
. $(-Join($(Get-Location).Path, '\logger.ps1'))
|
||||
|
||||
|
||||
function isDhcpEnable(){
|
||||
return Get-NetIPConfiguration | select-object InterfaceAlias, IPv4Address, @{n="dhcp";e={$_.NetIPv4Interface.DHCP}} | Select-Object -ExpandProperty dhcp
|
||||
@ -10,10 +22,10 @@ function isForeignConnected {
|
||||
$result = Test-NetConnection -ComputerName $Global:TestDomain -Port 443
|
||||
|
||||
if ($result.TcpTestSucceeded) {
|
||||
Write-Host "Successfully connected to $($Global:TestDomain) via HTTPS. isCustomGateway:$($Global:isCustomGateway)"
|
||||
Write-Log -Message "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)"
|
||||
Write-Log -Message "Failed to connect to $($Global:TestDomain) via HTTPS. isCustomGateway:$($Global:isCustomGateway)"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
@ -22,10 +34,10 @@ 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)"
|
||||
Write-Log -Message "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)"
|
||||
Write-Log -Message "Failed to connect to $($Global:TestIP) via ICMP. isCustomGateway:$($Global:isCustomGateway)"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
@ -33,10 +45,10 @@ function isNetworkConnected {
|
||||
function setDhcp($enable){
|
||||
if($enable){
|
||||
Set-NetIPInterface -InterfaceAlias $Global:TargetNetworkAdapter.name -Dhcp Enabled
|
||||
Write-Host enable dhcp
|
||||
Write-Log -Message "enable dhcp"
|
||||
} else {
|
||||
Set-NetIPInterface -InterfaceAlias $Global:TargetNetworkAdapter.name -Dhcp Disabled
|
||||
Write-Host disable dhcp
|
||||
Write-Log -Message "disable dhcp"
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,20 +56,20 @@ function changeGateway{
|
||||
param(
|
||||
$adapter,$isCustomGateway
|
||||
)
|
||||
Write-Host "Changing gateway for adapter: $($adapter.Name) isCustomGateway $($isCustomGateway)"
|
||||
Write-Log -Message "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"
|
||||
if($currentGateway -eq $Global:CustomGateway){
|
||||
Write-Log -Message "current gateway is $currentGateway same as targetGateway $Global:TargetGateway, change gateway canceled"
|
||||
$Global:isCustomGateway = $true
|
||||
return
|
||||
}
|
||||
$result = $(Test-Connection $Global:TargetGateway -Count 1).Status
|
||||
$result = $(Test-Connection $Global:CustomGateway -Count 1).Status
|
||||
if (! ($result -eq 'Success')){
|
||||
Write-Host custom gateway $Global:TargetGateway is $result, refuse to change
|
||||
Write-Log -Message "custom gateway $Global:CustomGateway is $result, refuse to change"
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -67,14 +79,14 @@ function changeGateway{
|
||||
|
||||
# if have ip configuration, delete it
|
||||
if ($existingIP) {
|
||||
Write-Host "Removing existing IP address: $($existingIP.IPAddress)"
|
||||
Write-Log -Message "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..."
|
||||
Write-Log -Message "Removing existing default route..."
|
||||
Remove-NetRoute -InterfaceAlias $adapter.Name -Confirm:$false
|
||||
}
|
||||
|
||||
@ -84,20 +96,21 @@ function changeGateway{
|
||||
$Global:isCustomGateway = $true
|
||||
|
||||
# setting dns server
|
||||
Set-DnsClientServerAddress -InterfaceAlias $adapter.name -ServerAddresses ($Global:TargetGateway, $Global:TargetGateway)
|
||||
Set-DnsClientServerAddress -InterfaceAlias $adapter.name -ServerAddresses ($Global:CustomGateway, $Global:CustomGateway)
|
||||
# disable dhcp
|
||||
setDhcp($false)
|
||||
# setting gateway to customGateway
|
||||
New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress $Global:TargetIP -PrefixLength 24 -DefaultGateway $Global:TargetGateway
|
||||
New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress $Global:CustomIP -PrefixLength 24 -DefaultGateway $Global:CustomGateway
|
||||
} else {
|
||||
# config originalGateway
|
||||
$Global:isCustomGateway = $false
|
||||
|
||||
# setting dns server
|
||||
Set-DnsClientServerAddress -InterfaceAlias $adapter.name -ServerAddresses ($Global:originalDns, $Global:originalDns)
|
||||
|
||||
# disable dhcp
|
||||
setDhcp($false)
|
||||
# setting gateway to originalGateway
|
||||
New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress $Global:TargetIP -PrefixLength 24 -DefaultGateway $Global:originalGateway
|
||||
New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress $Global:CustomIP -PrefixLength 24 -DefaultGateway $Global:originalGateway
|
||||
}
|
||||
Start-Sleep 3
|
||||
}
|
||||
@ -125,25 +138,26 @@ function getNetworkAdapter(){
|
||||
break;
|
||||
}
|
||||
}
|
||||
Write-Host "getNetworkAdapter: " $Global:TargetNetworkAdapter
|
||||
Write-Log -Message "getNetworkAdapter: $Global:TargetNetworkAdapter"
|
||||
}
|
||||
|
||||
function init {
|
||||
Write-Log -Message "GateWay-KeepAliveForPowershell is start" -ToEventLog
|
||||
# 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..."
|
||||
Write-Log -Message "Requesting Administrator privileges..."
|
||||
Start-Process pwsh -ArgumentList "-NoExit", "-File", $PSCommandPath -Verb RunAs
|
||||
exit
|
||||
}
|
||||
|
||||
Write-Host "Running with Administrator privileges!"
|
||||
Write-Log -Message "Running with Administrator privileges!"
|
||||
|
||||
# get netAdapter instance
|
||||
getNetworkAdapter
|
||||
|
||||
Write-Host "current adapter: $($Global:TargetNetworkAdapter.name)"
|
||||
Write-Log -Message "current adapter: $($Global:TargetNetworkAdapter.name)"
|
||||
|
||||
# try to change to customGateway at start
|
||||
changeGateway $Global:TargetNetworkAdapter $true
|
||||
@ -153,12 +167,14 @@ function loop(){
|
||||
while($true){
|
||||
# if network offline and using customGateway, try to restore to originalGateway
|
||||
if((!$(isNetworkConnected)) -and $Global:isCustomGateway){
|
||||
Write-Log -Message "Network is down, change to original gateway" -ToEventLog
|
||||
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
|
||||
if($(Test-SocksProxy -ProxyHost $Global:CustomGateway -ProxyPort 1070) -and (!$Global:isCustomGateway)){
|
||||
Write-Log -Message "isCustomGateway $Global:isCustomGateway"
|
||||
Write-Log -Message "proxy host is up, change to custom gateway" -ToEventLog
|
||||
changeGateway $Global:TargetNetworkAdapter $true
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
$Global:TestDomain = "www.google.com" #测试代理是否可用
|
||||
$Global:TestIP = "223.5.5.5" #测试是否连接网络
|
||||
$Global:originalDns = "223.5.5.5" #默认DNS
|
||||
$Global:TargetGateway = "192.168.6.2" #代理网关
|
||||
$Global:TargetIP = "192.168.6.4" #默认ip
|
||||
$Global:originalGateway = "192.168.6.1" #默认网关
|
||||
$Global:CustomGateway = "192.168.6.2" #代理网关
|
||||
$Global:CustomIP = "192.168.6.4" #默认ip
|
||||
$Global:TargetNetworkAdapter = {} #网络适配器实例
|
||||
$Global:TargetNetworkAdapterKeyword = "Ethernet" #网络适配器名字通配符
|
||||
$Global:isCustomGateway = $false #是否为自定义网关
|
||||
$Global:isCustomGateway = $false #是否为自定义网关
|
||||
$Global:isDebugging = $false #是否为调试模式(日志输出到控制台)
|
||||
41
logger.ps1
Normal file
41
logger.ps1
Normal file
@ -0,0 +1,41 @@
|
||||
. $(-Join($(Get-Location).Path, '\config.ps1'))
|
||||
|
||||
function Write-Log {
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Message, # 日志内容
|
||||
[switch]$ToEventLog # 是否输出到事件管理器
|
||||
)
|
||||
|
||||
# 时间戳前缀
|
||||
$timestamp = (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
|
||||
$logEntry = "[$timestamp] $Message"
|
||||
|
||||
# 输出到控制台
|
||||
if ($Global:isDebugging) {
|
||||
Write-Host $logEntry
|
||||
}
|
||||
|
||||
if ($ToEventLog){
|
||||
# 写入到 Windows 事件日志
|
||||
$eventID = 1000
|
||||
|
||||
# 确保事件源存在
|
||||
$source = "GateWay-KeepAliveForPowershell"
|
||||
if (-not (Get-EventLog -LogName Application -Source $source -ErrorAction SilentlyContinue)) {
|
||||
try {
|
||||
New-EventLog -LogName Application -Source $source -ErrorAction Stop
|
||||
} catch {
|
||||
Write-Host "Error creating event source: $_" -ForegroundColor Red
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
# 写入事件日志
|
||||
try {
|
||||
Write-EventLog -LogName Application -Source $source -EventID $eventID -EntryType 'Info' -Message $Message
|
||||
} catch {
|
||||
Write-Host "Failed to write to event log: $_" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,6 @@
|
||||
. $(-Join($(Get-Location).Path, '\logger.ps1'))
|
||||
. $(-Join($(Get-Location).Path, '\config.ps1'))
|
||||
|
||||
function Test-SocksProxy {
|
||||
param (
|
||||
[string]$ProxyHost,
|
||||
@ -11,7 +14,7 @@ function Test-SocksProxy {
|
||||
# check if proxy is working
|
||||
$result = $(Test-Connection $ProxyHost -Count 1).Status
|
||||
if(!($result -eq "Success")){
|
||||
Write-Host proxy host $ProxyHost is $result
|
||||
Write-Log -Message "proxy host $ProxyHost is $result"
|
||||
return $false
|
||||
}
|
||||
|
||||
@ -55,14 +58,14 @@ function Test-SocksProxy {
|
||||
$stream.Read($connectResponse, 0, $connectResponse.Length)
|
||||
|
||||
if ($connectResponse[1] -eq 0x00) {
|
||||
Write-Host "Proxy is valid and can connect to ${TestHost}:$TestPort."
|
||||
Write-Log -Message "Proxy is valid and can connect to ${TestHost}:$TestPort."
|
||||
$result = $true
|
||||
} else {
|
||||
throw "SOCKS connection to ${TestHost}:$TestPort failed. Response code: $($connectResponse[1])"
|
||||
$result = $false
|
||||
}
|
||||
} catch {
|
||||
Write-Host "Proxy test failed: $_"
|
||||
Write-Log -Message "Proxy test failed: $_"
|
||||
$result = $false
|
||||
} finally {
|
||||
# 关闭连接
|
||||
|
||||
Loading…
Reference in New Issue
Block a user