repeater/internal/api/router.go
Pierre-Olivier Mercier 07f8673f2f Harden API surface and station/wifi backends
Bind to localhost by default and stop echoing backend errors (which can
embed credentials or low-level details) back over the API and log
broadcast. Validate hotspot SSID/passphrase/channel before writing
hostapd.conf and tighten its mode to 0600 since it stores the WPA PSK.
Restrict WebSocket upgrades to same-origin so a LAN browser can't be
turned into a proxy for the API.

Guard shared state: status reads/writes go through StatusMutex (the
periodic updater races with the toggle and status handlers otherwise),
broadcastToWebSockets no longer mutates the client map under RLock, and
station-event callbacks now run under SafeGo so a panic in app code can't
take down the daemon. Stop channels in hostapd, dhcp, and iwd signal
monitors are now closed under sync.Once to survive concurrent Stop calls.

App.Shutdown is idempotent and waits for the periodic loops before
closing backends, so signal-driven and deferred shutdowns no longer race.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 21:56:50 +08:00

70 lines
1.7 KiB
Go

package api
import (
"embed"
"io/fs"
"net/http"
"sync"
"github.com/gin-gonic/gin"
"github.com/nemunaire/repeater/internal/api/handlers"
"github.com/nemunaire/repeater/internal/config"
"github.com/nemunaire/repeater/internal/models"
)
// SetupRouter creates and configures the Gin router
func SetupRouter(status *models.SystemStatus, statusMu *sync.RWMutex, cfg *config.Config, assets embed.FS) *gin.Engine {
// Set Gin to release mode (can be overridden with GIN_MODE env var)
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// API routes
api := r.Group("/api")
{
// WiFi endpoints
wifi := api.Group("/wifi")
{
wifi.GET("/networks", handlers.GetWiFiNetworks)
wifi.GET("/scan", handlers.ScanWiFi)
wifi.POST("/connect", handlers.ConnectWiFi)
wifi.POST("/disconnect", handlers.DisconnectWiFi)
}
// Hotspot endpoints
hotspot := api.Group("/hotspot")
{
hotspot.POST("/config", handlers.ConfigureHotspot)
hotspot.POST("/toggle", func(c *gin.Context) {
handlers.ToggleHotspot(c, status, statusMu)
})
}
// Device endpoints
api.GET("/devices", func(c *gin.Context) {
handlers.GetDevices(c, cfg)
})
// Status endpoint
api.GET("/status", func(c *gin.Context) {
handlers.GetStatus(c, status, statusMu)
})
// Log endpoints
api.GET("/logs", handlers.GetLogs)
api.DELETE("/logs", handlers.ClearLogs)
}
// WebSocket endpoints
r.GET("/ws/logs", handlers.WebSocketLogs)
r.GET("/ws/wifi", handlers.WebSocketWifi)
// Serve static files
sub, err := fs.Sub(assets, "static")
if err != nil {
panic("Unable to access static directory: " + err.Error())
}
r.NoRoute(gin.WrapH(http.FileServer(http.FS(sub))))
return r
}