2024-09-28 19:53:01 +03:00
|
|
|
# Path to log file
|
2024-09-28 20:36:27 +03:00
|
|
|
$logFile = $args[3]
|
2024-09-28 19:53:01 +03:00
|
|
|
|
|
|
|
# Function to log messages
|
|
|
|
function Log-Message {
|
|
|
|
param (
|
|
|
|
[string]$message
|
|
|
|
)
|
|
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
|
|
"$timestamp - $message" | Out-File -Append -FilePath $logFile
|
|
|
|
}
|
|
|
|
|
2024-09-28 18:15:43 +03:00
|
|
|
if ($args.Count -lt 2) {
|
2024-09-28 20:31:25 +03:00
|
|
|
Log-Message "Usage: windows_task.ps1 <url> <sslocal_path> <comment> <log_file>"
|
2024-09-28 18:15:43 +03:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $args[0]
|
|
|
|
$sslocalPath = $args[1]
|
2024-09-28 19:32:30 +03:00
|
|
|
$localPort = $args[2]
|
2024-09-28 18:15:43 +03:00
|
|
|
|
|
|
|
$localAddr = "localhost"
|
|
|
|
$checkInterval = 60
|
|
|
|
$previousPassword = ""
|
|
|
|
|
|
|
|
# Function to start sslocal
|
|
|
|
function Start-SSLocal {
|
|
|
|
param (
|
|
|
|
[string]$method,
|
|
|
|
[string]$password,
|
|
|
|
[string]$server,
|
|
|
|
[int]$serverPort
|
|
|
|
)
|
|
|
|
|
|
|
|
# Form the Shadowsocks connection string
|
|
|
|
$credentials = "${method}:${password}@${server}:${serverPort}"
|
|
|
|
$encodedCredentials = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($credentials))
|
|
|
|
$ssUrl = "ss://$encodedCredentials"
|
|
|
|
|
|
|
|
# Kill any existing sslocal process
|
|
|
|
Get-Process sslocal -ErrorAction SilentlyContinue | Stop-Process -Force
|
|
|
|
|
2024-09-28 19:53:01 +03:00
|
|
|
# Log the sslocal restart
|
|
|
|
Log-Message "Starting sslocal with method: $method, server: $server, port: $serverPort"
|
|
|
|
|
2024-09-28 18:15:43 +03:00
|
|
|
# Start sslocal with the provided arguments
|
|
|
|
Start-Process -NoNewWindow -FilePath $sslocalPath -ArgumentList "--local-addr ${localAddr}:${localPort} --server-url $ssUrl"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Main loop
|
|
|
|
while ($true) {
|
|
|
|
try {
|
|
|
|
# Download and parse the JSON
|
|
|
|
$jsonContent = Invoke-WebRequest -Uri $url -UseBasicParsing | Select-Object -ExpandProperty Content
|
|
|
|
$json = $jsonContent | ConvertFrom-Json
|
|
|
|
|
|
|
|
# Extract the necessary fields
|
|
|
|
$method = $json.method
|
|
|
|
$password = $json.password
|
|
|
|
$server = $json.server
|
|
|
|
$serverPort = $json.server_port
|
|
|
|
|
2024-09-28 19:53:01 +03:00
|
|
|
# Log current password and server information
|
|
|
|
Log-Message "Checking server: $server, port: $serverPort"
|
|
|
|
|
2024-09-28 18:15:43 +03:00
|
|
|
# Check if the password has changed
|
|
|
|
if ($password -ne $previousPassword) {
|
|
|
|
# Start/restart sslocal
|
|
|
|
Start-SSLocal -method $method -password $password -server $server -serverPort $serverPort
|
|
|
|
$previousPassword = $password
|
2024-09-28 19:53:01 +03:00
|
|
|
Log-Message "Password changed, restarting sslocal."
|
|
|
|
} else {
|
|
|
|
Log-Message "Password has not changed."
|
2024-09-28 18:15:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch {
|
2024-09-28 19:53:01 +03:00
|
|
|
Log-Message "Error occurred: $_"
|
2024-09-28 18:15:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Wait for the next check
|
|
|
|
Start-Sleep -Seconds $checkInterval
|
|
|
|
}
|