2022-10-04 16:02:55 +03: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 20:50:09 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add tests.
|
|
|
|
package websvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-06-13 13:41:13 +03:00
|
|
|
"io/fs"
|
2022-04-26 20:50:09 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-08-10 13:39:28 +03:00
|
|
|
"net/netip"
|
2022-04-26 20:50:09 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/dnssvc"
|
2022-04-26 20:50:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2022-05-12 17:41:39 +03:00
|
|
|
httptreemux "github.com/dimfeld/httptreemux/v5"
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
// ConfigManager is the configuration manager interface.
|
|
|
|
type ConfigManager interface {
|
2022-10-10 14:05:24 +03:00
|
|
|
DNS() (svc agh.ServiceWithConfig[*dnssvc.Config])
|
|
|
|
Web() (svc agh.ServiceWithConfig[*Config])
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
UpdateDNS(ctx context.Context, c *dnssvc.Config) (err error)
|
|
|
|
UpdateWeb(ctx context.Context, c *Config) (err error)
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
// Config is the AdGuard Home web service configuration structure.
|
|
|
|
type Config struct {
|
2022-10-04 16:02:55 +03:00
|
|
|
// ConfigManager is used to show information about services as well as
|
|
|
|
// dynamically reconfigure them.
|
|
|
|
ConfigManager ConfigManager
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
// Frontend is the filesystem with the frontend and other statically
|
|
|
|
// compiled files.
|
|
|
|
Frontend fs.FS
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
// TLS is the optional TLS configuration. If TLS is not nil,
|
|
|
|
// SecureAddresses must not be empty.
|
|
|
|
TLS *tls.Config
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
// Start is the time of start of AdGuard Home.
|
|
|
|
Start time.Time
|
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
// OverrideAddress is the initial or override address for the HTTP API. If
|
|
|
|
// set, it is used instead of [Addresses] and [SecureAddresses].
|
|
|
|
OverrideAddress netip.AddrPort
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
// Addresses are the addresses on which to serve the plain HTTP API.
|
2022-08-10 13:39:28 +03:00
|
|
|
Addresses []netip.AddrPort
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
// SecureAddresses are the addresses on which to serve the HTTPS API. If
|
|
|
|
// SecureAddresses is not empty, TLS must not be nil.
|
2022-08-10 13:39:28 +03:00
|
|
|
SecureAddresses []netip.AddrPort
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
// Timeout is the timeout for all server operations.
|
|
|
|
Timeout time.Duration
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
// ForceHTTPS tells if all requests to Addresses should be redirected to a
|
|
|
|
// secure address instead.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use; define rules, which address to redirect to.
|
|
|
|
ForceHTTPS bool
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
// Service is the AdGuard Home web service. A nil *Service is a valid
|
|
|
|
// [agh.Service] that does nothing.
|
2022-04-26 20:50:09 +03:00
|
|
|
type Service struct {
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgr ConfigManager
|
|
|
|
frontend fs.FS
|
|
|
|
tls *tls.Config
|
|
|
|
start time.Time
|
|
|
|
overrideAddr netip.AddrPort
|
|
|
|
servers []*http.Server
|
|
|
|
timeout time.Duration
|
|
|
|
forceHTTPS bool
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new properly initialized *Service. If c is nil, svc is a nil
|
2022-10-04 16:02:55 +03:00
|
|
|
// *Service that does nothing. The fields of c must not be modified after
|
|
|
|
// calling New.
|
2023-06-13 13:41:13 +03: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 20:50:09 +03:00
|
|
|
if c == nil {
|
2023-06-13 13:41:13 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
svc = &Service{
|
2023-06-29 19:10:39 +03: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 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2022-05-12 17:41:39 +03:00
|
|
|
mux := newMux(svc)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2023-06-29 19:10:39 +03: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 20:50:09 +03:00
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
for _, a := range c.SecureAddresses {
|
|
|
|
svc.servers = append(svc.servers, newSrv(a, c.TLS, mux, c.Timeout))
|
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
return svc, nil
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 19:10:39 +03: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 12:21:32 +03:00
|
|
|
// newMux returns a new HTTP request multiplexer for the AdGuard Home web
|
2022-05-12 17:41:39 +03:00
|
|
|
// service.
|
|
|
|
func newMux(svc *Service) (mux *httptreemux.ContextMux) {
|
|
|
|
mux = httptreemux.NewContextMux()
|
|
|
|
|
|
|
|
routes := []struct {
|
|
|
|
handler http.HandlerFunc
|
|
|
|
method string
|
2023-06-13 13:41:13 +03:00
|
|
|
pattern string
|
2022-05-12 17:41:39 +03:00
|
|
|
isJSON bool
|
|
|
|
}{{
|
|
|
|
handler: svc.handleGetHealthCheck,
|
|
|
|
method: http.MethodGet,
|
2023-06-13 13:41:13 +03: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 17:41:39 +03:00
|
|
|
isJSON: false,
|
2022-10-04 16:02:55 +03:00
|
|
|
}, {
|
|
|
|
handler: svc.handleGetSettingsAll,
|
|
|
|
method: http.MethodGet,
|
2023-06-13 13:41:13 +03:00
|
|
|
pattern: PathV1SettingsAll,
|
2022-10-04 16:02:55 +03:00
|
|
|
isJSON: true,
|
|
|
|
}, {
|
|
|
|
handler: svc.handlePatchSettingsDNS,
|
|
|
|
method: http.MethodPatch,
|
2023-06-13 13:41:13 +03:00
|
|
|
pattern: PathV1SettingsDNS,
|
2022-10-04 16:02:55 +03:00
|
|
|
isJSON: true,
|
|
|
|
}, {
|
|
|
|
handler: svc.handlePatchSettingsHTTP,
|
|
|
|
method: http.MethodPatch,
|
2023-06-13 13:41:13 +03:00
|
|
|
pattern: PathV1SettingsHTTP,
|
2022-10-04 16:02:55 +03:00
|
|
|
isJSON: true,
|
2022-05-12 17:41:39 +03:00
|
|
|
}, {
|
|
|
|
handler: svc.handleGetV1SystemInfo,
|
|
|
|
method: http.MethodGet,
|
2023-06-13 13:41:13 +03:00
|
|
|
pattern: PathV1SystemInfo,
|
2022-05-12 17:41:39 +03:00
|
|
|
isJSON: true,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, r := range routes {
|
2023-06-13 13:41:13 +03:00
|
|
|
var hdlr http.Handler
|
2022-05-12 17:41:39 +03:00
|
|
|
if r.isJSON {
|
2023-06-13 13:41:13 +03:00
|
|
|
hdlr = jsonMw(r.handler)
|
2022-05-12 17:41:39 +03:00
|
|
|
} else {
|
2023-06-13 13:41:13 +03:00
|
|
|
hdlr = r.handler
|
2022-05-12 17:41:39 +03:00
|
|
|
}
|
2023-06-13 13:41:13 +03:00
|
|
|
|
|
|
|
mux.Handle(r.method, r.pattern, logMw(hdlr))
|
2022-05-12 17:41:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:02:55 +03: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 19:10:39 +03:00
|
|
|
if svc.overrideAddr != (netip.AddrPort{}) {
|
|
|
|
return []netip.AddrPort{svc.overrideAddr}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
for _, srv := range svc.servers {
|
2023-06-29 19:10:39 +03: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 16:02:55 +03:00
|
|
|
|
2023-06-29 19:10:39 +03: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 16:02:55 +03: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 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
return addrs, secureAddrs
|
2022-04-26 20:50:09 +03: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 13:21:25 +03:00
|
|
|
// Start implements the [agh.Service] interface for *Service. svc may be nil.
|
2022-04-26 20:50:09 +03: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
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
2022-10-04 16:02:55 +03:00
|
|
|
wg.Add(len(svc.servers))
|
|
|
|
for _, srv := range svc.servers {
|
2022-04-26 20:50:09 +03:00
|
|
|
go serve(srv, wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:02:55 +03:00
|
|
|
var proto string
|
2022-04-26 20:50:09 +03:00
|
|
|
var l net.Listener
|
|
|
|
var err error
|
|
|
|
if srv.TLSConfig == nil {
|
2022-10-04 16:02:55 +03:00
|
|
|
proto = "http"
|
2022-04-26 20:50:09 +03:00
|
|
|
l, err = net.Listen("tcp", addr)
|
|
|
|
} else {
|
2022-10-04 16:02:55 +03:00
|
|
|
proto = "https"
|
2022-04-26 20:50:09 +03: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 16:02:55 +03:00
|
|
|
log.Info("websvc: starting srv %s://%s", proto, srv.Addr)
|
|
|
|
|
|
|
|
l = &waitListener{
|
|
|
|
Listener: l,
|
|
|
|
firstAcceptWG: wg,
|
|
|
|
}
|
2022-04-26 20:50:09 +03: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 13:21:25 +03:00
|
|
|
// Shutdown implements the [agh.Service] interface for *Service. svc may be
|
|
|
|
// nil.
|
2022-04-26 20:50:09 +03:00
|
|
|
func (svc *Service) Shutdown(ctx context.Context) (err error) {
|
|
|
|
if svc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
for _, srv := range svc.servers {
|
|
|
|
serr := srv.Shutdown(ctx)
|
|
|
|
if serr != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("shutting down srv %s: %w", srv.Addr, serr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2022-10-04 16:02:55 +03:00
|
|
|
return errors.List("shutting down", errs...)
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
// Config returns the current configuration of the web service. Config 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) Config() (c *Config) {
|
|
|
|
c = &Config{
|
|
|
|
ConfigManager: svc.confMgr,
|
|
|
|
TLS: svc.tls,
|
|
|
|
// Leave Addresses and SecureAddresses empty and get the actual
|
|
|
|
// addresses that include the :0 ones later.
|
|
|
|
Start: svc.start,
|
|
|
|
Timeout: svc.timeout,
|
|
|
|
ForceHTTPS: svc.forceHTTPS,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Addresses, c.SecureAddresses = svc.addrs()
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|