2022-10-04 14:02:55 +01:00
|
|
|
// Package websvc contains the AdGuard Home HTTP API service.
|
|
|
|
//
|
|
|
|
// NOTE: Packages other than cmd must not import this package, as it imports
|
|
|
|
// most other packages.
|
2022-04-26 18:50:09 +01:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add tests.
|
|
|
|
package websvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-06-13 11:41:13 +01:00
|
|
|
"io/fs"
|
2022-04-26 18:50:09 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-08-10 11:39:28 +01:00
|
|
|
"net/netip"
|
2023-07-20 16:57:06 +01:00
|
|
|
"runtime"
|
2022-04-26 18:50:09 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/dnssvc"
|
2022-04-26 18:50:09 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-07-20 16:57:06 +01:00
|
|
|
"github.com/AdguardTeam/golibs/mathutil"
|
|
|
|
"github.com/AdguardTeam/golibs/pprofutil"
|
2022-05-12 15:41:39 +01:00
|
|
|
httptreemux "github.com/dimfeld/httptreemux/v5"
|
2022-04-26 18:50:09 +01:00
|
|
|
)
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
// ConfigManager is the configuration manager interface.
|
|
|
|
type ConfigManager interface {
|
2022-10-10 12:05:24 +01:00
|
|
|
DNS() (svc agh.ServiceWithConfig[*dnssvc.Config])
|
|
|
|
Web() (svc agh.ServiceWithConfig[*Config])
|
2022-10-04 14:02:55 +01:00
|
|
|
|
|
|
|
UpdateDNS(ctx context.Context, c *dnssvc.Config) (err error)
|
|
|
|
UpdateWeb(ctx context.Context, c *Config) (err error)
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:21:25 +01:00
|
|
|
// Service is the AdGuard Home web service. A nil *Service is a valid
|
|
|
|
// [agh.Service] that does nothing.
|
2022-04-26 18:50:09 +01:00
|
|
|
type Service struct {
|
2023-06-29 17:10:39 +01:00
|
|
|
confMgr ConfigManager
|
|
|
|
frontend fs.FS
|
|
|
|
tls *tls.Config
|
2023-07-20 16:57:06 +01:00
|
|
|
pprof *http.Server
|
2023-06-29 17:10:39 +01:00
|
|
|
start time.Time
|
|
|
|
overrideAddr netip.AddrPort
|
|
|
|
servers []*http.Server
|
|
|
|
timeout time.Duration
|
2023-07-20 16:57:06 +01:00
|
|
|
pprofPort uint16
|
2023-06-29 17:10:39 +01:00
|
|
|
forceHTTPS bool
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new properly initialized *Service. If c is nil, svc is a nil
|
2022-10-04 14:02:55 +01:00
|
|
|
// *Service that does nothing. The fields of c must not be modified after
|
|
|
|
// calling New.
|
2023-06-13 11:41:13 +01:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Get rid of this special handling of nil or explain it
|
|
|
|
// better.
|
|
|
|
func New(c *Config) (svc *Service, err error) {
|
2022-04-26 18:50:09 +01:00
|
|
|
if c == nil {
|
2023-06-13 11:41:13 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 18:50:09 +01:00
|
|
|
svc = &Service{
|
2023-06-29 17:10:39 +01:00
|
|
|
confMgr: c.ConfigManager,
|
|
|
|
frontend: c.Frontend,
|
|
|
|
tls: c.TLS,
|
|
|
|
start: c.Start,
|
|
|
|
overrideAddr: c.OverrideAddress,
|
|
|
|
timeout: c.Timeout,
|
|
|
|
forceHTTPS: c.ForceHTTPS,
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 15:41:39 +01:00
|
|
|
mux := newMux(svc)
|
2022-04-26 18:50:09 +01:00
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
if svc.overrideAddr != (netip.AddrPort{}) {
|
|
|
|
svc.servers = []*http.Server{newSrv(svc.overrideAddr, nil, mux, c.Timeout)}
|
|
|
|
} else {
|
|
|
|
for _, a := range c.Addresses {
|
|
|
|
svc.servers = append(svc.servers, newSrv(a, nil, mux, c.Timeout))
|
|
|
|
}
|
2022-04-26 18:50:09 +01:00
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
for _, a := range c.SecureAddresses {
|
|
|
|
svc.servers = append(svc.servers, newSrv(a, c.TLS, mux, c.Timeout))
|
|
|
|
}
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
|
2023-07-20 16:57:06 +01:00
|
|
|
svc.setupPprof(c.Pprof)
|
|
|
|
|
2023-06-13 11:41:13 +01:00
|
|
|
return svc, nil
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
|
2023-07-20 16:57:06 +01:00
|
|
|
// setupPprof sets the pprof properties of svc.
|
|
|
|
func (svc *Service) setupPprof(c *PprofConfig) {
|
|
|
|
if !c.Enabled {
|
|
|
|
// Set to zero explicitly in case pprof used to be enabled before a
|
|
|
|
// reconfiguration took place.
|
|
|
|
runtime.SetBlockProfileRate(0)
|
|
|
|
runtime.SetMutexProfileFraction(0)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.SetBlockProfileRate(1)
|
|
|
|
runtime.SetMutexProfileFraction(1)
|
|
|
|
|
|
|
|
pprofMux := http.NewServeMux()
|
|
|
|
pprofutil.RoutePprof(pprofMux)
|
|
|
|
|
|
|
|
svc.pprofPort = c.Port
|
|
|
|
addr := netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), c.Port)
|
|
|
|
|
|
|
|
// TODO(a.garipov): Consider making pprof timeout configurable.
|
|
|
|
svc.pprof = newSrv(addr, nil, pprofMux, 10*time.Minute)
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
// newSrv returns a new *http.Server with the given parameters.
|
|
|
|
func newSrv(
|
|
|
|
addr netip.AddrPort,
|
|
|
|
tlsConf *tls.Config,
|
|
|
|
h http.Handler,
|
|
|
|
timeout time.Duration,
|
|
|
|
) (srv *http.Server) {
|
|
|
|
addrStr := addr.String()
|
|
|
|
srv = &http.Server{
|
|
|
|
Addr: addrStr,
|
|
|
|
Handler: h,
|
|
|
|
TLSConfig: tlsConf,
|
|
|
|
ReadTimeout: timeout,
|
|
|
|
WriteTimeout: timeout,
|
|
|
|
IdleTimeout: timeout,
|
|
|
|
ReadHeaderTimeout: timeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
if tlsConf == nil {
|
|
|
|
srv.ErrorLog = log.StdLog("websvc: plain http: "+addrStr, log.ERROR)
|
|
|
|
} else {
|
|
|
|
srv.ErrorLog = log.StdLog("websvc: https: "+addrStr, log.ERROR)
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2023-06-19 10:21:32 +01:00
|
|
|
// newMux returns a new HTTP request multiplexer for the AdGuard Home web
|
2022-05-12 15:41:39 +01:00
|
|
|
// service.
|
|
|
|
func newMux(svc *Service) (mux *httptreemux.ContextMux) {
|
|
|
|
mux = httptreemux.NewContextMux()
|
|
|
|
|
|
|
|
routes := []struct {
|
|
|
|
handler http.HandlerFunc
|
|
|
|
method string
|
2023-06-13 11:41:13 +01:00
|
|
|
pattern string
|
2022-05-12 15:41:39 +01:00
|
|
|
isJSON bool
|
|
|
|
}{{
|
|
|
|
handler: svc.handleGetHealthCheck,
|
|
|
|
method: http.MethodGet,
|
2023-06-13 11:41:13 +01:00
|
|
|
pattern: PathHealthCheck,
|
|
|
|
isJSON: false,
|
|
|
|
}, {
|
|
|
|
handler: http.FileServer(http.FS(svc.frontend)).ServeHTTP,
|
|
|
|
method: http.MethodGet,
|
|
|
|
pattern: PathFrontend,
|
|
|
|
isJSON: false,
|
|
|
|
}, {
|
|
|
|
handler: http.FileServer(http.FS(svc.frontend)).ServeHTTP,
|
|
|
|
method: http.MethodGet,
|
|
|
|
pattern: PathRoot,
|
2022-05-12 15:41:39 +01:00
|
|
|
isJSON: false,
|
2022-10-04 14:02:55 +01:00
|
|
|
}, {
|
|
|
|
handler: svc.handleGetSettingsAll,
|
|
|
|
method: http.MethodGet,
|
2023-06-13 11:41:13 +01:00
|
|
|
pattern: PathV1SettingsAll,
|
2022-10-04 14:02:55 +01:00
|
|
|
isJSON: true,
|
|
|
|
}, {
|
|
|
|
handler: svc.handlePatchSettingsDNS,
|
|
|
|
method: http.MethodPatch,
|
2023-06-13 11:41:13 +01:00
|
|
|
pattern: PathV1SettingsDNS,
|
2022-10-04 14:02:55 +01:00
|
|
|
isJSON: true,
|
|
|
|
}, {
|
|
|
|
handler: svc.handlePatchSettingsHTTP,
|
|
|
|
method: http.MethodPatch,
|
2023-06-13 11:41:13 +01:00
|
|
|
pattern: PathV1SettingsHTTP,
|
2022-10-04 14:02:55 +01:00
|
|
|
isJSON: true,
|
2022-05-12 15:41:39 +01:00
|
|
|
}, {
|
|
|
|
handler: svc.handleGetV1SystemInfo,
|
|
|
|
method: http.MethodGet,
|
2023-06-13 11:41:13 +01:00
|
|
|
pattern: PathV1SystemInfo,
|
2022-05-12 15:41:39 +01:00
|
|
|
isJSON: true,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, r := range routes {
|
2023-06-13 11:41:13 +01:00
|
|
|
var hdlr http.Handler
|
2022-05-12 15:41:39 +01:00
|
|
|
if r.isJSON {
|
2023-06-13 11:41:13 +01:00
|
|
|
hdlr = jsonMw(r.handler)
|
2022-05-12 15:41:39 +01:00
|
|
|
} else {
|
2023-06-13 11:41:13 +01:00
|
|
|
hdlr = r.handler
|
2022-05-12 15:41:39 +01:00
|
|
|
}
|
2023-06-13 11:41:13 +01:00
|
|
|
|
|
|
|
mux.Handle(r.method, r.pattern, logMw(hdlr))
|
2022-05-12 15:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
// addrs returns all addresses on which this server serves the HTTP API. addrs
|
|
|
|
// must not be called simultaneously with Start. If svc was initialized with
|
|
|
|
// ":0" addresses, addrs will not return the actual bound ports until Start is
|
|
|
|
// finished.
|
|
|
|
func (svc *Service) addrs() (addrs, secureAddrs []netip.AddrPort) {
|
2023-06-29 17:10:39 +01:00
|
|
|
if svc.overrideAddr != (netip.AddrPort{}) {
|
|
|
|
return []netip.AddrPort{svc.overrideAddr}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 18:50:09 +01:00
|
|
|
for _, srv := range svc.servers {
|
2023-06-29 17:10:39 +01:00
|
|
|
// Use MustParseAddrPort, since no errors should technically happen
|
|
|
|
// here, because all servers must have a valid address.
|
|
|
|
addrPort := netip.MustParseAddrPort(srv.Addr)
|
2022-10-04 14:02:55 +01:00
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
// [srv.Serve] will set TLSConfig to an almost empty value, so, instead
|
|
|
|
// of relying only on the nilness of TLSConfig, check the length of the
|
2022-10-04 14:02:55 +01:00
|
|
|
// certificates field as well.
|
|
|
|
if srv.TLSConfig == nil || len(srv.TLSConfig.Certificates) == 0 {
|
|
|
|
addrs = append(addrs, addrPort)
|
|
|
|
} else {
|
|
|
|
secureAddrs = append(secureAddrs, addrPort)
|
|
|
|
}
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
return addrs, secureAddrs
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetHealthCheck is the handler for the GET /health-check HTTP API.
|
|
|
|
func (svc *Service) handleGetHealthCheck(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
_, _ = io.WriteString(w, "OK")
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ agh.Service = (*Service)(nil)
|
|
|
|
|
2022-08-16 11:21:25 +01:00
|
|
|
// Start implements the [agh.Service] interface for *Service. svc may be nil.
|
2022-04-26 18:50:09 +01:00
|
|
|
// After Start exits, all HTTP servers have tried to start, possibly failing and
|
|
|
|
// writing error messages to the log.
|
|
|
|
func (svc *Service) Start() (err error) {
|
|
|
|
if svc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 16:57:06 +01:00
|
|
|
pprofEnabled := svc.pprof != nil
|
|
|
|
srvNum := len(svc.servers) + mathutil.BoolToNumber[int](pprofEnabled)
|
|
|
|
|
2022-04-26 18:50:09 +01:00
|
|
|
wg := &sync.WaitGroup{}
|
2023-07-20 16:57:06 +01:00
|
|
|
wg.Add(srvNum)
|
2022-10-04 14:02:55 +01:00
|
|
|
for _, srv := range svc.servers {
|
2022-04-26 18:50:09 +01:00
|
|
|
go serve(srv, wg)
|
|
|
|
}
|
|
|
|
|
2023-07-20 16:57:06 +01:00
|
|
|
if pprofEnabled {
|
|
|
|
go serve(svc.pprof, wg)
|
|
|
|
}
|
|
|
|
|
2022-04-26 18:50:09 +01:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve starts and runs srv and writes all errors into its log.
|
|
|
|
func serve(srv *http.Server, wg *sync.WaitGroup) {
|
|
|
|
addr := srv.Addr
|
|
|
|
defer log.OnPanic(addr)
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
var proto string
|
2022-04-26 18:50:09 +01:00
|
|
|
var l net.Listener
|
|
|
|
var err error
|
|
|
|
if srv.TLSConfig == nil {
|
2022-10-04 14:02:55 +01:00
|
|
|
proto = "http"
|
2022-04-26 18:50:09 +01:00
|
|
|
l, err = net.Listen("tcp", addr)
|
|
|
|
} else {
|
2022-10-04 14:02:55 +01:00
|
|
|
proto = "https"
|
2022-04-26 18:50:09 +01:00
|
|
|
l, err = tls.Listen("tcp", addr, srv.TLSConfig)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
srv.ErrorLog.Printf("starting srv %s: binding: %s", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the server's address in case the address had the port zero, which
|
|
|
|
// would mean that a random available port was automatically chosen.
|
|
|
|
srv.Addr = l.Addr().String()
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
log.Info("websvc: starting srv %s://%s", proto, srv.Addr)
|
|
|
|
|
|
|
|
l = &waitListener{
|
|
|
|
Listener: l,
|
|
|
|
firstAcceptWG: wg,
|
|
|
|
}
|
2022-04-26 18:50:09 +01:00
|
|
|
|
|
|
|
err = srv.Serve(l)
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
srv.ErrorLog.Printf("starting srv %s: %s", addr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:21:25 +01:00
|
|
|
// Shutdown implements the [agh.Service] interface for *Service. svc may be
|
|
|
|
// nil.
|
2022-04-26 18:50:09 +01:00
|
|
|
func (svc *Service) Shutdown(ctx context.Context) (err error) {
|
|
|
|
if svc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:09:08 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "shutting down: %w") }()
|
|
|
|
|
2022-04-26 18:50:09 +01:00
|
|
|
var errs []error
|
|
|
|
for _, srv := range svc.servers {
|
2023-07-20 16:57:06 +01:00
|
|
|
shutdownErr := srv.Shutdown(ctx)
|
|
|
|
if shutdownErr != nil {
|
2023-08-15 13:09:08 +01:00
|
|
|
errs = append(errs, fmt.Errorf("srv %s: %w", srv.Addr, shutdownErr))
|
2023-07-20 16:57:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if svc.pprof != nil {
|
|
|
|
shutdownErr := svc.pprof.Shutdown(ctx)
|
|
|
|
if shutdownErr != nil {
|
2023-08-15 13:09:08 +01:00
|
|
|
errs = append(errs, fmt.Errorf("pprof srv %s: %w", svc.pprof.Addr, shutdownErr))
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:09:08 +01:00
|
|
|
return errors.Join(errs...)
|
2022-04-26 18:50:09 +01:00
|
|
|
}
|