Separate router discovery, evidence and authorized control from device runtime. Add Node/Core contracts and reusable operator surfaces.
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package node
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"nodedc.local/mission-core/node-agent/internal/routers"
|
|
)
|
|
|
|
func (p *Pairing) applyRouterCommands(raw json.RawMessage) {
|
|
if p.Routers == nil || len(raw) > 4096 {
|
|
return
|
|
}
|
|
var commands []routers.Command
|
|
if json.Unmarshal(raw, &commands) != nil || len(commands) > 2 {
|
|
return
|
|
}
|
|
for _, command := range commands {
|
|
_ = p.Routers.Submit(command)
|
|
}
|
|
}
|
|
|
|
func (s *Server) routerRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/routers", func(w http.ResponseWriter, r *http.Request) {
|
|
if !s.authorized(w, r) {
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if s.Pairing == nil || s.Pairing.Routers == nil {
|
|
json.NewEncoder(w).Encode(map[string]any{"available": false, "items": []any{}})
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(s.Pairing.Routers.Snapshot())
|
|
})
|
|
submit := func(control bool) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if !s.authorized(w, r) {
|
|
return
|
|
}
|
|
var command routers.Command
|
|
if !decode(w, r, &command) {
|
|
return
|
|
}
|
|
if (control != (command.Action != "")) || s.Pairing == nil || s.Pairing.Routers == nil || s.Pairing.Routers.Submit(command) != nil {
|
|
http.Error(w, "Router unavailable", http.StatusConflict)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(routers.Result{ID: command.ID, State: "queued"})
|
|
}
|
|
}
|
|
mux.HandleFunc("POST /api/routers/access", submit(false))
|
|
mux.HandleFunc("POST /api/routers/control", submit(true))
|
|
}
|