Preview — /var/www/sites/splice/install-splicevpn.ps1

/var/www/sites/splice/install-splicevpn.ps1

[CmdletBinding()]
param([string]$Coordinator = "https://splicevpn.com.au/api")
$ErrorActionPreference = "Stop"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$id = [Security.Principal.WindowsIdentity]::GetCurrent()
if (-not (New-Object Security.Principal.WindowsPrincipal($id)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  Start-Process powershell -Verb RunAs -ArgumentList "-NoProfile","-ExecutionPolicy","Bypass","-File","`"$PSCommandPath`"","-Coordinator","`"$Coordinator`""
  return
}

function Info($m){ Write-Host "==> $m" -ForegroundColor Cyan }
function NewKey {
  $b = New-Object byte[] 18
  [Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($b)
  return "spl_" + (([Convert]::ToBase64String($b)) -replace '\+','-' -replace '/','_' -replace '=','')
}

$wg = Join-Path $env:ProgramFiles "WireGuard\wireguard.exe"
if (-not (Test-Path $wg)) {
  Info "Installing WireGuard for Windows (official)..."
  $arch = if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {"arm64"} elseif ([Environment]::Is64BitOperatingSystem) {"amd64"} else {"x86"}
  $idx = (Invoke-WebRequest -UseBasicParsing "https://download.wireguard.com/windows-client/").Content
  $msi = ([regex]::Matches($idx, "wireguard-$arch-[0-9.]+\.msi") | ForEach-Object {$_.Value} | Sort-Object -Unique | Sort-Object {[version]([regex]::Match($_,'[0-9]+(\.[0-9]+)+').Value)} | Select-Object -Last 1)
  if (-not $msi) { throw "No WireGuard MSI found for $arch" }
  $dst = Join-Path $env:TEMP $msi
  Invoke-WebRequest -UseBasicParsing "https://download.wireguard.com/windows-client/$msi" -OutFile $dst
  $p = Start-Process msiexec.exe -Wait -PassThru -ArgumentList "/i","`"$dst`"","/qn","DO_NOT_LAUNCH=1"
  if ($p.ExitCode -ne 0) { throw "WireGuard install failed (exit $($p.ExitCode))" }
  Info "WireGuard installed."
} else { Info "WireGuard already present." }

$dir = Join-Path $env:ProgramFiles "SpliceVPN"
New-Item -ItemType Directory -Force -Path $dir | Out-Null
$agent = Join-Path $dir "splice-agent.exe"
Info "Downloading SpliceVPN agent..."
Invoke-WebRequest -UseBasicParsing "https://splicevpn.com.au/splice-agent.exe" -OutFile $agent

function AddNetwork($key,$iface){
  $a = "-key $key -coordinator $Coordinator -name $env:COMPUTERNAME -iface $iface"
  $act = New-ScheduledTaskAction -Execute $agent -Argument $a
  $trg = New-ScheduledTaskTrigger -AtStartup
  $prn = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
  Register-ScheduledTask -TaskName "SpliceVPN-$iface" -Action $act -Trigger $trg -Principal $prn -Force | Out-Null
  Start-ScheduledTask -TaskName "SpliceVPN-$iface"
}

$n = 0
do {
  Write-Host ""
  Write-Host "Network setup:" -ForegroundColor Green
  Write-Host "  [G] Generate a NEW network (gives you a key to share with your other devices)"
  Write-Host "  [P] Paste an existing key to join a network"
  $c = Read-Host "Choose G or P"
  if ($c -match '^[Pp]') {
    $key = (Read-Host "Paste network key").Trim()
  } else {
    $key = NewKey
    Write-Host ""
    Write-Host "  NEW NETWORK KEY - copy this to your other devices:" -ForegroundColor Yellow
    Write-Host "      $key" -ForegroundColor White
    Write-Host ""
  }
  if ([string]::IsNullOrWhiteSpace($key)) { Write-Host "No key, skipping." -ForegroundColor Red; break }
  AddNetwork $key "splice$n"
  Info "Connected on splice$n."
  $n++
  $more = Read-Host "Add another network? (y/N)"
} while ($more -match '^[Yy]')

Write-Host ""
Write-Host "Done. SpliceVPN is installed and reconnects automatically on boot." -ForegroundColor Green
Write-Host "Manage tunnels in the WireGuard app; networks live under Task Scheduler (SpliceVPN-*)."