2022-08-16 13:21:25 +03:00
|
|
|
// Package dnssvc contains the AdGuard Home DNS service.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Define, if all methods of a *Service should work with a nil
|
|
|
|
// receiver.
|
|
|
|
package dnssvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-11-13 15:44:21 +03:00
|
|
|
"log/slog"
|
2022-08-16 13:21:25 +03:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2022-10-04 16:02:55 +03:00
|
|
|
"sync/atomic"
|
2022-08-16 13:21:25 +03:00
|
|
|
"time"
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2022-10-04 16:02:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
2023-11-16 14:05:10 +03:00
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
// TODO(a.garipov): Add a “dnsproxy proxy” package to shield us from changes
|
|
|
|
// and replacement of module dnsproxy.
|
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2023-11-16 14:05:10 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2022-08-16 13:21:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service is the AdGuard Home DNS service. A nil *Service is a valid
|
|
|
|
// [agh.Service] that does nothing.
|
2023-06-28 12:48:53 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Consider saving a [*proxy.Config] instance for those
|
|
|
|
// fields that are only used in [New] and [Service.Config].
|
2022-08-16 13:21:25 +03:00
|
|
|
type Service struct {
|
2024-11-13 15:44:21 +03:00
|
|
|
logger *slog.Logger
|
2023-06-28 12:48:53 +03:00
|
|
|
proxy *proxy.Proxy
|
|
|
|
bootstraps []string
|
2023-11-16 14:05:10 +03:00
|
|
|
bootstrapResolvers []*upstream.UpstreamResolver
|
2023-06-28 12:48:53 +03:00
|
|
|
upstreams []string
|
|
|
|
dns64Prefixes []netip.Prefix
|
|
|
|
upsTimeout time.Duration
|
|
|
|
running atomic.Bool
|
|
|
|
bootstrapPreferIPv6 bool
|
|
|
|
useDNS64 bool
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new properly initialized *Service. If c is nil, svc is a nil
|
|
|
|
// *Service that does nothing. The fields of c must not be modified after
|
|
|
|
// calling New.
|
|
|
|
func New(c *Config) (svc *Service, err error) {
|
|
|
|
if c == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
svc = &Service{
|
2024-11-13 15:44:21 +03:00
|
|
|
logger: c.Logger,
|
2023-06-28 12:48:53 +03:00
|
|
|
bootstraps: c.BootstrapServers,
|
|
|
|
upstreams: c.UpstreamServers,
|
|
|
|
dns64Prefixes: c.DNS64Prefixes,
|
|
|
|
upsTimeout: c.UpstreamTimeout,
|
|
|
|
bootstrapPreferIPv6: c.BootstrapPreferIPv6,
|
|
|
|
useDNS64: c.UseDNS64,
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
upstreams, resolvers, err := addressesToUpstreams(
|
2023-06-28 12:48:53 +03:00
|
|
|
c.UpstreamServers,
|
|
|
|
c.BootstrapServers,
|
|
|
|
c.UpstreamTimeout,
|
|
|
|
c.BootstrapPreferIPv6,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("converting upstreams: %w", err)
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
svc.bootstrapResolvers = resolvers
|
2024-03-11 18:17:04 +03:00
|
|
|
svc.proxy, err = proxy.New(&proxy.Config{
|
2024-11-13 15:44:21 +03:00
|
|
|
Logger: svc.logger,
|
2024-03-11 18:17:04 +03:00
|
|
|
UDPListenAddr: udpAddrs(c.Addresses),
|
|
|
|
TCPListenAddr: tcpAddrs(c.Addresses),
|
|
|
|
UpstreamConfig: &proxy.UpstreamConfig{
|
|
|
|
Upstreams: upstreams,
|
2022-08-16 13:21:25 +03:00
|
|
|
},
|
2024-03-11 18:17:04 +03:00
|
|
|
UseDNS64: c.UseDNS64,
|
|
|
|
DNS64Prefs: c.DNS64Prefixes,
|
|
|
|
})
|
2022-08-16 13:21:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("proxy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addressesToUpstreams is a wrapper around [upstream.AddressToUpstream]. It
|
|
|
|
// accepts a slice of addresses and other upstream parameters, and returns a
|
|
|
|
// slice of upstreams.
|
|
|
|
func addressesToUpstreams(
|
|
|
|
upsStrs []string,
|
|
|
|
bootstraps []string,
|
|
|
|
timeout time.Duration,
|
2023-06-28 12:48:53 +03:00
|
|
|
preferIPv6 bool,
|
2023-11-16 14:05:10 +03:00
|
|
|
) (upstreams []upstream.Upstream, boots []*upstream.UpstreamResolver, err error) {
|
|
|
|
opts := &upstream.Options{
|
|
|
|
Timeout: timeout,
|
|
|
|
PreferIPv6: preferIPv6,
|
|
|
|
}
|
|
|
|
|
|
|
|
boots, err = aghnet.ParseBootstraps(bootstraps, opts)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, since it's informative enough as is.
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(e.burkov): Add system hosts resolver here.
|
|
|
|
var bootstrap upstream.ParallelResolver
|
|
|
|
for _, r := range boots {
|
2024-01-16 13:18:22 +03:00
|
|
|
bootstrap = append(bootstrap, upstream.NewCachingResolver(r))
|
2023-11-16 14:05:10 +03:00
|
|
|
}
|
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
upstreams = make([]upstream.Upstream, len(upsStrs))
|
|
|
|
for i, upsStr := range upsStrs {
|
|
|
|
upstreams[i], err = upstream.AddressToUpstream(upsStr, &upstream.Options{
|
2023-11-16 14:05:10 +03:00
|
|
|
Bootstrap: bootstrap,
|
2023-06-28 12:48:53 +03:00
|
|
|
Timeout: timeout,
|
|
|
|
PreferIPv6: preferIPv6,
|
2022-08-16 13:21:25 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-11-16 14:05:10 +03:00
|
|
|
return nil, boots, fmt.Errorf("upstream at index %d: %w", i, err)
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
return upstreams, boots, nil
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// tcpAddrs converts []netip.AddrPort into []*net.TCPAddr.
|
|
|
|
func tcpAddrs(addrPorts []netip.AddrPort) (tcpAddrs []*net.TCPAddr) {
|
|
|
|
if addrPorts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpAddrs = make([]*net.TCPAddr, len(addrPorts))
|
|
|
|
for i, a := range addrPorts {
|
|
|
|
tcpAddrs[i] = net.TCPAddrFromAddrPort(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tcpAddrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// udpAddrs converts []netip.AddrPort into []*net.UDPAddr.
|
|
|
|
func udpAddrs(addrPorts []netip.AddrPort) (udpAddrs []*net.UDPAddr) {
|
|
|
|
if addrPorts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
udpAddrs = make([]*net.UDPAddr, len(addrPorts))
|
|
|
|
for i, a := range addrPorts {
|
|
|
|
udpAddrs[i] = net.UDPAddrFromAddrPort(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return udpAddrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
2024-11-13 15:44:21 +03:00
|
|
|
var _ agh.ServiceWithConfig[*Config] = (*Service)(nil)
|
2022-08-16 13:21:25 +03:00
|
|
|
|
|
|
|
// Start implements the [agh.Service] interface for *Service. svc may be nil.
|
|
|
|
// After Start exits, all DNS servers have tried to start, but there is no
|
|
|
|
// guarantee that they did. Errors from the servers are written to the log.
|
2024-11-13 15:44:21 +03:00
|
|
|
func (svc *Service) Start(ctx context.Context) (err error) {
|
2022-08-16 13:21:25 +03:00
|
|
|
if svc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
defer func() {
|
|
|
|
// TODO(a.garipov): [proxy.Proxy.Start] doesn't actually have any way to
|
|
|
|
// tell when all servers are actually up, so at best this is merely an
|
|
|
|
// assumption.
|
2023-02-08 13:39:04 +03:00
|
|
|
svc.running.Store(err == nil)
|
2022-10-04 16:02:55 +03:00
|
|
|
}()
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
return svc.proxy.Start(ctx)
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown implements the [agh.Service] interface for *Service. svc may be
|
|
|
|
// nil.
|
|
|
|
func (svc *Service) Shutdown(ctx context.Context) (err error) {
|
|
|
|
if svc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
errs := []error{
|
2024-03-11 18:17:04 +03:00
|
|
|
svc.proxy.Shutdown(ctx),
|
2023-11-16 14:05:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range svc.bootstrapResolvers {
|
|
|
|
errs = append(errs, errors.Annotate(b.Close(), "closing bootstrap %s: %w", b.Address()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Join(errs...)
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2022-08-16 13:21:25 +03:00
|
|
|
func (svc *Service) Config() (c *Config) {
|
|
|
|
// TODO(a.garipov): Do we need to get the TCP addresses separately?
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
var addrs []netip.AddrPort
|
2023-02-08 13:39:04 +03:00
|
|
|
if svc.running.Load() {
|
2022-10-04 16:02:55 +03:00
|
|
|
udpAddrs := svc.proxy.Addrs(proxy.ProtoUDP)
|
|
|
|
addrs = make([]netip.AddrPort, len(udpAddrs))
|
|
|
|
for i, a := range udpAddrs {
|
|
|
|
addrs[i] = a.(*net.UDPAddr).AddrPort()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conf := svc.proxy.Config
|
|
|
|
udpAddrs := conf.UDPListenAddr
|
|
|
|
addrs = make([]netip.AddrPort, len(udpAddrs))
|
|
|
|
for i, a := range udpAddrs {
|
|
|
|
addrs[i] = a.AddrPort()
|
|
|
|
}
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c = &Config{
|
2024-11-13 15:44:21 +03:00
|
|
|
Logger: svc.logger,
|
2023-06-28 12:48:53 +03:00
|
|
|
Addresses: addrs,
|
|
|
|
BootstrapServers: svc.bootstraps,
|
|
|
|
UpstreamServers: svc.upstreams,
|
|
|
|
DNS64Prefixes: svc.dns64Prefixes,
|
|
|
|
UpstreamTimeout: svc.upsTimeout,
|
|
|
|
BootstrapPreferIPv6: svc.bootstrapPreferIPv6,
|
|
|
|
UseDNS64: svc.useDNS64,
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|