[CmdletBinding()]
param()

$machine_name = $env:COMPUTERNAME
$hw_id = (Get-WmiObject -Class Win32_ComputerSystemProduct).UUID
if (!$hw_id) { $hw_id = "UNKNOWN-HW-ID" }
$node_id = "$machine_name-$hw_id"

Write-Host "Node ID: $node_id"
$md5 = [System.Security.Cryptography.MD5]::Create()
$hash = $md5.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($node_id))
$node_secret = [BitConverter]::ToString($hash).Replace("-", "").ToLower()
Write-Host "Node Secret: $node_secret"

Write-Host "Fetching geolocation..."
try {
    $geo = Invoke-RestMethod -Uri "http://ip-api.com/json" -TimeoutSec 5
    $prov = $geo.regionName
    $city = $geo.city
    $public_ip = $geo.query
} catch {
    $prov = "Unknown"
    $city = "Unknown"
    $public_ip = "Unknown"
}
Write-Host "Location: $prov, $city ($public_ip)"

$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
$proxy_user = "n_" + (-join ((1..8) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] }))
$proxy_pass = -join ((1..16) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })

Write-Host "Generating config files..."

$agent_json = @{
    control_url = "http://api.zxiaoming.com"
    node_id = $node_id
    node_secret = $node_secret
    province = $prov
    city = $city
    public_ip = $public_ip
    proxy_user = $proxy_user
    proxy_pass = $proxy_pass
    frp_server_id = "frp-test-001"
    interval = 30
    timeout = 10
}
$agent_json | ConvertTo-Json | Out-File -Encoding ASCII "proxy-agent.json"

$frpc_toml = @"
serverAddr = "124.70.180.22"
serverPort = 7000

auth.method = "token"
auth.token = "ai-agent-frps-1000-frp-server-token"

[[proxies]]
name = "socks5-$node_id"
type = "tcp"
localIP = "127.0.0.1"
localPort = 1080
remotePort = 0
metadatas.node_id = "$node_id"
metadatas.province = "$prov"
metadatas.city = "$city"

[[proxies]]
name = "http-proxy-$node_id"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3128
remotePort = 0
metadatas.node_id = "$node_id"
metadatas.province = "$prov"
metadatas.city = "$city"
"@
Set-Content -Path "frpc.toml" -Value $frpc_toml -Encoding Ascii

Write-Host "Killing old background processes if running..."
Stop-Process -Name "proxy-agent", "frpc", "gost" -Force -ErrorAction SilentlyContinue

Write-Host "Starting processes in background (Hidden)..."
# Clear proxies just in case
$env:HTTP_PROXY=""
$env:HTTPS_PROXY=""

# Start the apps hiding the command window
Start-Process -FilePath ".\proxy-agent.exe" -ArgumentList "-config proxy-agent.json" -WindowStyle Hidden -RedirectStandardOutput "proxy-agent.log" -RedirectStandardError "proxy-agent-error.log"
Start-Process -FilePath ".\frpc.exe" -ArgumentList "-c frpc.toml" -WindowStyle Hidden -RedirectStandardOutput "frpc.log" -RedirectStandardError "frpc-error.log"

$http_listen = "http://$proxy_user`:$proxy_pass@:3128"
$socks_listen = "socks5://$proxy_user`:$proxy_pass@:1080"
Start-Process -FilePath ".\gost.exe" -ArgumentList "-L", $http_listen, "-L", $socks_listen -WindowStyle Hidden -RedirectStandardOutput "gost.log" -RedirectStandardError "gost-error.log"

Write-Host ""
Write-Host "Done! AI-Network node is running." -ForegroundColor Green
Write-Host "Use .\monitor-node.ps1 to view logs and status." -ForegroundColor Yellow

