adguard_detect.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // adguard_detect.go - POST /api/config/adguard/detect (#964): autodetección
  2. // de PRESENCIA de AdGuard Home (no credenciales; el usuario confirma la
  3. // password después). Sondea :3000/control/status primero en el gateway
  4. // (mode=glinet, source=gateway) y luego en la flota (mode=standard).
  5. package httpapi
  6. import (
  7. "context"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "strings"
  12. "time"
  13. "github.com/gnacho/netpulse/server-go/internal/routerstore"
  14. )
  15. // adguardDetectResponse es la forma JSON del detect: found=false si ningún
  16. // host de la flota sirve la API de AdGuard Home.
  17. type adguardDetectResponse struct {
  18. Found bool `json:"found"`
  19. Mode string `json:"mode,omitempty"` // "glinet" | "standard"
  20. Host string `json:"host,omitempty"`
  21. Port int `json:"port,omitempty"`
  22. Source string `json:"source,omitempty"` // "gateway" | "fleet"
  23. }
  24. // adguardStatusProbe pregunta a /control/status de un host (timeout 2s).
  25. // Variable de paquete para inyectar un fake en tests.
  26. var adguardStatusProbe = probeAdguardStatus
  27. // probeAdguardStatus detecta PRESENCIA: /control/status responde 200 sin
  28. // credenciales con un JSON que lleva dns_port/version. Cualquier otra cosa
  29. // (connection refused, timeout, 404 de otro servidor en :3000) = no está.
  30. func probeAdguardStatus(ctx context.Context, host string, port int) bool {
  31. ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
  32. defer cancel()
  33. req, err := http.NewRequestWithContext(ctx, http.MethodGet,
  34. fmt.Sprintf("http://%s:%d/control/status", host, port), nil)
  35. if err != nil {
  36. return false
  37. }
  38. res, err := http.DefaultClient.Do(req)
  39. if err != nil {
  40. return false
  41. }
  42. defer res.Body.Close()
  43. if res.StatusCode != http.StatusOK {
  44. return false
  45. }
  46. body, err := io.ReadAll(io.LimitReader(res.Body, 8192))
  47. if err != nil {
  48. return false
  49. }
  50. return strings.Contains(string(body), `"dns_port"`) || strings.Contains(string(body), `"version"`)
  51. }
  52. // handleDetectAdguard: gateway primero (GL.iNet lleva AdGuard Home propio en
  53. // :3000), luego el resto de la flota en orden. El primer hallazgo gana.
  54. func (s *server) handleDetectAdguard(w http.ResponseWriter, r *http.Request) {
  55. ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
  56. defer cancel()
  57. routers := routerstore.ListRouters(s.db.DB)
  58. const port = 3000
  59. for _, pass := range []struct {
  60. mode, source string
  61. gateway bool
  62. }{
  63. {"glinet", "gateway", true},
  64. {"standard", "fleet", false},
  65. } {
  66. for _, rt := range routers {
  67. if rt.IsGateway != pass.gateway || rt.Host == "" {
  68. continue
  69. }
  70. if adguardStatusProbe(ctx, rt.Host, port) {
  71. writeJSON(w, http.StatusOK, adguardDetectResponse{
  72. Found: true, Mode: pass.mode, Host: rt.Host, Port: port, Source: pass.source,
  73. })
  74. return
  75. }
  76. }
  77. }
  78. writeJSON(w, http.StatusOK, adguardDetectResponse{Found: false})
  79. }