Files
AetherForge/server/internal/sys/firewall_windows.go
drjones b10d353a8b Stabilize Fusion builds and simplify optional modules.
Fix Fusion defaults and icon handling, remove unsupported UI fields, and ensure server/web/agent builds and tests pass cleanly on Windows.
2026-05-27 20:13:24 -07:00

34 lines
1.0 KiB
Go

//go:build windows
package sys
import (
"fmt"
"log"
"os/exec"
"strings"
)
// EnsureInboundTCPPort adds a Windows Firewall inbound allow rule for the control server port.
func EnsureInboundTCPPort(port int, ruleName string) error {
if port <= 0 {
return fmt.Errorf("invalid port")
}
if strings.TrimSpace(ruleName) == "" {
ruleName = "AetherForge Control Server"
}
nameEsc := strings.ReplaceAll(ruleName, `'`, `''`)
script := fmt.Sprintf(`
$name = '%s'
$port = %d
if (Get-NetFirewallRule -DisplayName $name -ErrorAction SilentlyContinue) { exit 0 }
New-NetFirewallRule -DisplayName $name -Direction Inbound -Protocol TCP -LocalPort $port -Action Allow -Profile Any | Out-Null
`, nameEsc, port)
cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", script)
if err := cmd.Run(); err != nil {
return fmt.Errorf("firewall rule: %w (run server once as Administrator or open port %d manually)", err, port)
}
log.Printf("[firewall] inbound TCP %d allowed (%s)", port, ruleName)
return nil
}