Files
pfm-ocr/start-dev-tunnel.ps1
T

73 lines
3.4 KiB
PowerShell

# Get the active network interface IP address (excluding virtual and loopback adapters)
Write-Host "Checking active network adapters..." -ForegroundColor Cyan
$ipAddress = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
$_.IPAddress -notlike "127.*" -and
$_.IPAddress -notlike "169.254.*" -and
$_.InterfaceAlias -notlike "*vEthernet*" -and
$_.InterfaceAlias -notlike "*WSL*" -and
$_.IPAddress -notlike "192.168.56.*"
} | Select-Object -ExpandProperty IPAddress -First 1)
if (-not $ipAddress) {
Write-Host "[-] No active LAN IP address found! Falling back to 127.0.0.1" -ForegroundColor Yellow
$ipAddress = "127.0.0.1"
} else {
Write-Host "[+] Found active Host IP: $ipAddress" -ForegroundColor Green
}
# Update Flutter Configuration
$configFile = "lib/config/app_config.dart"
if (Test-Path $configFile) {
$content = Get-Content $configFile -Raw
$pattern = "(static const String _lanBaseUrl = 'http://)([^:]+)(:8000/api/v1';)"
if ($content -match $pattern) {
$content = $content -replace $pattern, ('${1}' + $ipAddress + '${3}')
Set-Content $configFile $content -NoNewline
Write-Host "[+] Synchronized local IP address ($ipAddress) to Flutter configuration." -ForegroundColor Green
} else {
Write-Host "[-] Warning: _lanBaseUrl pattern not found in $configFile." -ForegroundColor Yellow
}
} else {
Write-Host "[-] Warning: Flutter config file '$configFile' not found. Skip updating local IP." -ForegroundColor Yellow
}
# Start ngrok
Write-Host "`nStarting Ngrok tunnel..." -ForegroundColor Cyan
Write-Host "Exposing Secure Port: http://localhost:8001 (API v1 Only)" -ForegroundColor Cyan
Write-Host "Public URL: https://unlocated-waylon-potently.ngrok-free.dev" -ForegroundColor Cyan
# Check if ngrok is available
$ngrokCmd = Get-Command ngrok -ErrorAction SilentlyContinue
if ($ngrokCmd) {
Write-Host "[+] Launching ngrok in a new window..." -ForegroundColor Green
Start-Process ngrok -ArgumentList "http", "--domain=unlocated-waylon-potently.ngrok-free.dev", "8001"
Write-Host "[*] Waiting for API health check to become reachable (LAN & Tunnel)..." -ForegroundColor Yellow
$healthChecked = $false
$retryCount = 0
while (-not $healthChecked -and $retryCount -lt 30) {
try {
$lanHealth = Invoke-RestMethod -Uri "http://${ipAddress}:8001/api/v1/health" -Method GET -ErrorAction Stop
$ngrokHealth = Invoke-RestMethod -Uri "https://unlocated-waylon-potently.ngrok-free.dev/api/v1/health" -Method GET -ErrorAction Stop
if ($lanHealth.status -eq "ok" -and $ngrokHealth.status -eq "ok") {
$healthChecked = $true
Write-Host "[+] Tunnel and LAN endpoints are fully reachable and healthy!" -ForegroundColor Green
}
} catch {
Start-Sleep -Seconds 2
$retryCount++
}
}
if (-not $healthChecked) {
Write-Host "[-] Warning: Health check failed after 60 seconds. The API might not be fully reachable yet." -ForegroundColor Red
}
} else {
Write-Host "[-] Error: 'ngrok' command not found in your system PATH." -ForegroundColor Red
Write-Host " Please ensure ngrok is installed and added to your environment variables." -ForegroundColor Yellow
}
Write-Host "`nDone! Your Flutter app can now connect locally via WiFi ($ipAddress) or publicly via Ngrok." -ForegroundColor Green