mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
11207186c8
- Modify buildWsURL to use web server port (18800) instead of gateway port (18790) - Add WebSocket proxy handler to forward /pico/ws to gateway - Gateway port is read from config (cfg.Gateway.Port), defaults to 18790 - This allows WebSocket connections through the same port as the web UI, avoiding the need to expose extra ports for Tailscale/Docker
179 lines
5.4 KiB
Go
179 lines
5.4 KiB
Go
package api
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
)
|
|
|
|
// registerPicoRoutes binds Pico Channel management endpoints to the ServeMux.
|
|
func (h *Handler) registerPicoRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/pico/token", h.handleGetPicoToken)
|
|
mux.HandleFunc("POST /api/pico/token", h.handleRegenPicoToken)
|
|
mux.HandleFunc("POST /api/pico/setup", h.handlePicoSetup)
|
|
|
|
// WebSocket proxy: forward /pico/ws to gateway
|
|
// This allows the frontend to connect via the same port as the web UI,
|
|
// avoiding the need to expose extra ports for WebSocket communication.
|
|
wsProxy := h.createWsProxy()
|
|
mux.HandleFunc("GET /pico/ws", h.handleWebSocketProxy(wsProxy))
|
|
}
|
|
|
|
// createWsProxy creates a reverse proxy to the gateway WebSocket endpoint.
|
|
// The gateway port is read from the configuration.
|
|
func (h *Handler) createWsProxy() *httputil.ReverseProxy {
|
|
cfg, err := config.LoadConfig(h.configPath)
|
|
gatewayPort := 18790 // default
|
|
if err == nil && cfg.Gateway.Port != 0 {
|
|
gatewayPort = cfg.Gateway.Port
|
|
}
|
|
gatewayURL, _ := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", gatewayPort))
|
|
wsProxy := httputil.NewSingleHostReverseProxy(gatewayURL)
|
|
wsProxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, "Gateway unavailable: "+err.Error(), http.StatusBadGateway)
|
|
}
|
|
return wsProxy
|
|
}
|
|
|
|
// handleWebSocketProxy wraps a reverse proxy to handle WebSocket connections.
|
|
// It ensures the Connection and Upgrade headers are properly forwarded.
|
|
func (h *Handler) handleWebSocketProxy(proxy *httputil.ReverseProxy) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Set headers for WebSocket upgrade
|
|
r.Header.Set("Connection", "upgrade")
|
|
r.Header.Set("Upgrade", "websocket")
|
|
proxy.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
// handleGetPicoToken returns the current WS token and URL for the frontend.
|
|
//
|
|
// GET /api/pico/token
|
|
func (h *Handler) handleGetPicoToken(w http.ResponseWriter, r *http.Request) {
|
|
cfg, err := config.LoadConfig(h.configPath)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
wsURL := h.buildWsURL(r, cfg)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"token": cfg.Channels.Pico.Token,
|
|
"ws_url": wsURL,
|
|
"enabled": cfg.Channels.Pico.Enabled,
|
|
})
|
|
}
|
|
|
|
// handleRegenPicoToken generates a new Pico WebSocket token and saves it.
|
|
//
|
|
// POST /api/pico/token
|
|
func (h *Handler) handleRegenPicoToken(w http.ResponseWriter, r *http.Request) {
|
|
cfg, err := config.LoadConfig(h.configPath)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
token := generateSecureToken()
|
|
cfg.Channels.Pico.Token = token
|
|
|
|
if err := config.SaveConfig(h.configPath, cfg); err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to save config: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
wsURL := h.buildWsURL(r, cfg)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"token": token,
|
|
"ws_url": wsURL,
|
|
})
|
|
}
|
|
|
|
// ensurePicoChannel enables the Pico channel with sane defaults if it isn't
|
|
// already configured. Returns true when the config was modified.
|
|
//
|
|
// callerOrigin is the Origin header from the setup request. If non-empty and
|
|
// no origins are configured yet, it's written as the allowed origin so the
|
|
// WebSocket handshake works for whatever host the caller is on (LAN, custom
|
|
// port, etc.). Pass "" when there's no request context.
|
|
func (h *Handler) ensurePicoChannel(callerOrigin string) (bool, error) {
|
|
cfg, err := config.LoadConfig(h.configPath)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
changed := false
|
|
|
|
if !cfg.Channels.Pico.Enabled {
|
|
cfg.Channels.Pico.Enabled = true
|
|
changed = true
|
|
}
|
|
|
|
if cfg.Channels.Pico.Token == "" {
|
|
cfg.Channels.Pico.Token = generateSecureToken()
|
|
changed = true
|
|
}
|
|
|
|
// Seed origins from the request instead of hardcoding ports.
|
|
if len(cfg.Channels.Pico.AllowOrigins) == 0 && callerOrigin != "" {
|
|
cfg.Channels.Pico.AllowOrigins = []string{callerOrigin}
|
|
changed = true
|
|
}
|
|
|
|
if changed {
|
|
if err := config.SaveConfig(h.configPath, cfg); err != nil {
|
|
return false, fmt.Errorf("failed to save config: %w", err)
|
|
}
|
|
}
|
|
|
|
return changed, nil
|
|
}
|
|
|
|
// handlePicoSetup automatically configures everything needed for the Pico Channel to work.
|
|
//
|
|
// POST /api/pico/setup
|
|
func (h *Handler) handlePicoSetup(w http.ResponseWriter, r *http.Request) {
|
|
changed, err := h.ensurePicoChannel(r.Header.Get("Origin"))
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
cfg, err := config.LoadConfig(h.configPath)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
wsURL := h.buildWsURL(r, cfg)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"token": cfg.Channels.Pico.Token,
|
|
"ws_url": wsURL,
|
|
"enabled": true,
|
|
"changed": changed,
|
|
})
|
|
}
|
|
|
|
// generateSecureToken creates a random 32-character hex string.
|
|
func generateSecureToken() string {
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
// Fallback to something pseudo-random if crypto/rand fails
|
|
return fmt.Sprintf("pico_%x", time.Now().UnixNano())
|
|
}
|
|
return hex.EncodeToString(b)
|
|
}
|