From 22eb5ec7aff4a1fa6d454e7230f967ce0137082c Mon Sep 17 00:00:00 2001 From: Alexandr Bogomyakov Date: Sat, 28 Sep 2024 18:15:43 +0300 Subject: [PATCH] Create windows_service.ps1 --- windows_service.ps1 | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 windows_service.ps1 diff --git a/windows_service.ps1 b/windows_service.ps1 new file mode 100644 index 0000000..8c50689 --- /dev/null +++ b/windows_service.ps1 @@ -0,0 +1,62 @@ +if ($args.Count -lt 2) { + Write-Host "Usage: script.ps1 " + exit 1 +} + +$url = $args[0] +$sslocalPath = $args[1] + +$localAddr = "localhost" +$localPort = 1080 +$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 + + # 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 + + # 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 + Write-Host "Password changed, restarting sslocal." + } + + } catch { + Write-Host "Error occurred: $_" + } + + # Wait for the next check + Start-Sleep -Seconds $checkInterval +}