reanchor.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package httpapi
  2. import (
  3. "net/http"
  4. "strings"
  5. "time"
  6. "github.com/gnacho/netpulse/server-go/internal/adapters"
  7. "github.com/gnacho/netpulse/server-go/internal/auth"
  8. )
  9. // handleReanchorRecommendations: GET /api/wifi-reanchor/recommendations
  10. // Devuelve la lista de clientes WiFi que estarían mejor en otro AP según
  11. // usteer (preferido) o DAWN. Requiere sesión de admin.
  12. func (s *server) handleReanchorRecommendations(w http.ResponseWriter, r *http.Request) {
  13. cfg := adapters.ReanchorConfig{
  14. MinRecommendedSignal: -65,
  15. MinDeltaDbm: 10,
  16. }
  17. recs, daemon, err := s.adapter.GetReanchorRecommendations(r.Context(), cfg)
  18. if err != nil {
  19. writeError(w, http.StatusInternalServerError, "internal_error")
  20. return
  21. }
  22. if recs == nil {
  23. recs = []adapters.ReanchorRecommendation{}
  24. }
  25. writeJSON(w, http.StatusOK, adapters.ReanchorResponse{Daemon: daemon, Recommendations: recs})
  26. }
  27. // handleReanchorMove: POST /api/wifi-reanchor/{mac}/move
  28. // Ejecuta del_client + ban_time en el AP donde está asociada la MAC,
  29. // usando el daemon activo para decidir el target. Requiere admin.
  30. func (s *server) handleReanchorMove(w http.ResponseWriter, r *http.Request) {
  31. mac, ok := validMAC(r.PathValue("mac"))
  32. if !ok {
  33. writeError(w, http.StatusBadRequest, "invalid_mac")
  34. return
  35. }
  36. mac = strings.ToUpper(mac)
  37. cfg := adapters.ReanchorConfig{
  38. MinRecommendedSignal: -65,
  39. MinDeltaDbm: 10,
  40. }
  41. recs, daemon, err := s.adapter.GetReanchorRecommendations(r.Context(), cfg)
  42. if err != nil {
  43. writeError(w, http.StatusInternalServerError, "internal_error")
  44. return
  45. }
  46. var rec *adapters.ReanchorRecommendation
  47. for i := range recs {
  48. if recs[i].MAC == mac {
  49. rec = &recs[i]
  50. break
  51. }
  52. }
  53. if rec == nil {
  54. writeError(w, http.StatusConflict, "no_recommendation", "No hay recomendación activa para este cliente")
  55. return
  56. }
  57. switch daemon {
  58. case adapters.RoamingDaemonUsteer:
  59. if err := s.adapter.KickUsteerClient(r.Context(), mac); err != nil {
  60. writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
  61. return
  62. }
  63. case adapters.RoamingDaemonDawn:
  64. if rec.CurrentHost == "" {
  65. writeError(w, http.StatusConflict, "router_unknown", "No se encontró el router del AP actual")
  66. return
  67. }
  68. script := adapters.ReanchorKickScript(rec.MAC, rec.CurrentIface)
  69. if _, err := s.pool.Run(rec.CurrentHost, script, 15*time.Second); err != nil {
  70. writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
  71. return
  72. }
  73. default:
  74. writeError(w, http.StatusServiceUnavailable, "unavailable", "No hay daemon de roaming disponible")
  75. return
  76. }
  77. writeJSON(w, http.StatusOK, map[string]any{"ok": true, "mac": mac, "from": rec.CurrentHostname})
  78. }
  79. // registerReanchorRoutes registra las rutas de re-anclaje WiFi.
  80. func (s *server) registerReanchorRoutes(mux *http.ServeMux) {
  81. mux.Handle("GET /api/wifi-reanchor/recommendations", auth.RequireAdmin(http.HandlerFunc(s.handleReanchorRecommendations)))
  82. mux.Handle("POST /api/wifi-reanchor/{mac}/move", auth.RequireAdmin(http.HandlerFunc(s.handleReanchorMove)))
  83. }