# setup.ps1
$repo = "https://git.help-d.ru/helmut/auto-turning/raw/branch/main"
$temp = "$env:TEMP\ssh_setup"

Write-Host "1. Скачиваю установщик..." -ForegroundColor Cyan
Remove-Item $temp -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $temp -Force | Out-Null
Invoke-WebRequest -Uri "$repo/installers/ssh.msi" -OutFile "$temp\ssh.msi"

Write-Host "2. Устанавливаю SSH..." -ForegroundColor Cyan
msiexec /i "$temp\ssh.msi" /quiet /norestart
Start-Sleep -Seconds 3

Write-Host "3. Настраиваю авторизацию..." -ForegroundColor Cyan
$sshPath = "$env:USERPROFILE\.ssh"
New-Item -ItemType Directory -Path $sshPath -Force | Out-Null
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHceolzaC/nnN14/lVqeXzcXcIANtQgJcIaFczLIchVo efgufk\admin@ISERVER" | Out-File "$sshPath\authorized_keys" -Encoding utf8

Write-Host "4. Настраиваю sshd_config..." -ForegroundColor Cyan
$config = "C:\ProgramData\ssh\sshd_config"
$content = Get-Content $config
$content = $content -replace '^#Port 22', 'Port 2222'
$content = $content -replace '^Match Group administrators', '#Match Group administrators'
$content = $content -replace '^       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys', '#       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys'
$content | Set-Content $config

Write-Host "5. Перезапускаю службу..." -ForegroundColor Cyan
Restart-Service sshd -Force

Write-Host "6. Чистка..." -ForegroundColor Cyan
Remove-Item $temp -Recurse -Force

Write-Host "`n========== ГОТОВО ==========" -ForegroundColor Green
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -notlike "*Loopback*" -and $_.PrefixOrigin -ne "WellKnown"}).IPAddress
Write-Host "Подключайтесь: ssh -p 2222 $env:USERNAME@$ip" -ForegroundColor Yellow
Write-Host "============================" -ForegroundColor Green