2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-03-19 18:47:22 +03:00
|
|
|
|
|
|
|
import (
|
2024-09-30 14:17:42 +03:00
|
|
|
"context"
|
2019-04-26 15:10:29 +03:00
|
|
|
"fmt"
|
2024-10-09 16:31:03 +03:00
|
|
|
"log/slog"
|
2022-10-24 17:49:52 +03:00
|
|
|
"net/netip"
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2019-04-26 15:10:29 +03:00
|
|
|
"sync"
|
2019-06-25 17:51:53 +03:00
|
|
|
"time"
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2021-04-07 16:36:38 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2023-08-24 13:42:17 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
2023-07-18 17:02:07 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2024-10-09 16:31:03 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
|
2021-04-02 17:30:39 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
2024-06-18 14:27:25 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
|
2023-06-21 12:53:53 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
2021-01-27 18:32:13 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-03-03 15:27:25 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-10-09 16:31:03 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2019-06-25 17:51:53 +03:00
|
|
|
)
|
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// clientsContainer is the storage of all runtime and persistent clients.
|
2019-04-26 15:10:29 +03:00
|
|
|
type clientsContainer struct {
|
2024-10-09 16:31:03 +03:00
|
|
|
// baseLogger is used to create loggers with custom prefixes for safe search
|
|
|
|
// filter. It must not be nil.
|
|
|
|
baseLogger *slog.Logger
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
// storage stores information about persistent clients.
|
|
|
|
storage *client.Storage
|
2021-06-29 15:53:28 +03:00
|
|
|
|
2024-05-13 20:09:18 +03:00
|
|
|
// clientChecker checks if a client is blocked by the current access
|
|
|
|
// settings.
|
|
|
|
clientChecker BlockedClientChecker
|
2020-07-24 14:30:29 +03:00
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// lock protects all fields.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use a pointer and describe which fields are protected in
|
2023-04-07 13:17:40 +03:00
|
|
|
// more detail. Use sync.RWMutex.
|
2023-03-01 13:27:24 +03:00
|
|
|
lock sync.Mutex
|
|
|
|
|
2023-04-06 14:12:50 +03:00
|
|
|
// safeSearchCacheSize is the size of the safe search cache to use for
|
|
|
|
// persistent clients.
|
|
|
|
safeSearchCacheSize uint
|
|
|
|
|
|
|
|
// safeSearchCacheTTL is the TTL of the safe search cache to use for
|
|
|
|
// persistent clients.
|
|
|
|
safeSearchCacheTTL time.Duration
|
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// testing is a flag that disables some features for internal tests.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Awful. Remove.
|
|
|
|
testing bool
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:09:18 +03:00
|
|
|
// BlockedClientChecker checks if a client is blocked by the current access
|
|
|
|
// settings.
|
|
|
|
type BlockedClientChecker interface {
|
|
|
|
IsBlockedClient(ip netip.Addr, clientID string) (blocked bool, rule string)
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
// Init initializes clients container
|
2020-07-03 18:20:01 +03:00
|
|
|
// dhcpServer: optional
|
2019-07-09 18:19:50 +03:00
|
|
|
// Note: this function must be called only once
|
2021-04-12 18:31:45 +03:00
|
|
|
func (clients *clientsContainer) Init(
|
2024-10-09 16:31:03 +03:00
|
|
|
ctx context.Context,
|
|
|
|
baseLogger *slog.Logger,
|
2021-12-13 15:18:21 +03:00
|
|
|
objects []*clientObject,
|
2024-09-30 14:17:42 +03:00
|
|
|
dhcpServer client.DHCP,
|
2021-10-14 19:39:21 +03:00
|
|
|
etcHosts *aghnet.HostsContainer,
|
2023-08-24 13:42:17 +03:00
|
|
|
arpDB arpdb.Interface,
|
2023-03-15 14:31:07 +03:00
|
|
|
filteringConf *filtering.Config,
|
2023-06-27 18:03:07 +03:00
|
|
|
) (err error) {
|
2024-05-13 20:09:18 +03:00
|
|
|
// TODO(s.chzhen): Refactor it.
|
2024-07-01 17:34:47 +03:00
|
|
|
if clients.storage != nil {
|
2024-05-13 20:09:18 +03:00
|
|
|
return errors.Error("clients container already initialized")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2023-04-06 14:12:50 +03:00
|
|
|
|
2024-10-09 16:31:03 +03:00
|
|
|
clients.baseLogger = baseLogger
|
2024-10-02 20:48:58 +03:00
|
|
|
clients.safeSearchCacheSize = filteringConf.SafeSearchCacheSize
|
|
|
|
clients.safeSearchCacheTTL = time.Minute * time.Duration(filteringConf.CacheTime)
|
|
|
|
|
2024-09-30 14:17:42 +03:00
|
|
|
confClients := make([]*client.Persistent, 0, len(objects))
|
|
|
|
for i, o := range objects {
|
|
|
|
var p *client.Persistent
|
2024-10-09 16:31:03 +03:00
|
|
|
p, err = o.toPersistent(ctx, baseLogger, clients.safeSearchCacheSize, clients.safeSearchCacheTTL)
|
2024-09-30 14:17:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init persistent client at index %d: %w", i, err)
|
|
|
|
}
|
2023-04-06 14:12:50 +03:00
|
|
|
|
2024-09-30 14:17:42 +03:00
|
|
|
confClients = append(confClients, p)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
// The clients.etcHosts may be nil even if config.Clients.Sources.HostsFile
|
|
|
|
// is true, because of the deprecated option --no-etc-hosts.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): The option should probably be returned, since hosts file
|
|
|
|
// currently used not only for clients' information enrichment, but also in
|
|
|
|
// the filtering module and upstream addresses resolution.
|
2024-10-04 18:10:56 +03:00
|
|
|
var hosts client.HostsContainer
|
|
|
|
if config.Clients.Sources.HostsFile && etcHosts != nil {
|
|
|
|
hosts = etcHosts
|
2024-09-30 14:17:42 +03:00
|
|
|
}
|
|
|
|
|
2024-10-22 13:57:54 +03:00
|
|
|
clients.storage, err = client.NewStorage(ctx, &client.StorageConfig{
|
|
|
|
Logger: baseLogger.With(slogutil.KeyPrefix, "client_storage"),
|
2024-09-30 14:17:42 +03:00
|
|
|
InitialClients: confClients,
|
|
|
|
DHCP: dhcpServer,
|
|
|
|
EtcHosts: hosts,
|
|
|
|
ARPDB: arpDB,
|
|
|
|
ARPClientsUpdatePeriod: arpClientsUpdatePeriod,
|
|
|
|
RuntimeSourceDHCP: config.Clients.Sources.DHCP,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init client storage: %w", err)
|
2022-04-26 13:04:16 +03:00
|
|
|
}
|
2023-06-27 18:03:07 +03:00
|
|
|
|
|
|
|
return nil
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// webHandlersRegistered prevents a [clientsContainer] from registering its web
|
2023-03-01 13:27:24 +03:00
|
|
|
// handlers more than once.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Refactor HTTP handler registration logic.
|
|
|
|
var webHandlersRegistered = false
|
|
|
|
|
|
|
|
// Start starts the clients container.
|
2024-09-30 14:17:42 +03:00
|
|
|
func (clients *clientsContainer) Start(ctx context.Context) (err error) {
|
2023-03-01 13:27:24 +03:00
|
|
|
if clients.testing {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !webHandlersRegistered {
|
|
|
|
webHandlersRegistered = true
|
|
|
|
clients.registerWebHandlers()
|
2019-11-29 17:35:26 +03:00
|
|
|
}
|
2023-03-01 13:27:24 +03:00
|
|
|
|
2024-09-30 14:17:42 +03:00
|
|
|
return clients.storage.Start(ctx)
|
2020-02-18 14:49:50 +03:00
|
|
|
}
|
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// clientObject is the YAML representation of a persistent client.
|
2019-09-26 16:40:52 +03:00
|
|
|
type clientObject struct {
|
2023-03-15 14:31:07 +03:00
|
|
|
SafeSearchConf filtering.SafeSearchConfig `yaml:"safe_search"`
|
|
|
|
|
2023-06-27 18:03:07 +03:00
|
|
|
// BlockedServices is the configuration of blocked services of a client.
|
|
|
|
BlockedServices *filtering.BlockedServices `yaml:"blocked_services"`
|
|
|
|
|
2021-09-13 20:16:06 +03:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
2023-06-27 18:03:07 +03:00
|
|
|
IDs []string `yaml:"ids"`
|
|
|
|
Tags []string `yaml:"tags"`
|
|
|
|
Upstreams []string `yaml:"upstreams"`
|
2021-09-13 20:16:06 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// UID is the unique identifier of the persistent client.
|
2024-02-27 13:48:11 +03:00
|
|
|
UID client.UID `yaml:"uid"`
|
2023-12-28 17:26:17 +03:00
|
|
|
|
2023-11-17 15:51:51 +03:00
|
|
|
// UpstreamsCacheSize is the DNS cache size (in bytes).
|
|
|
|
//
|
|
|
|
// TODO(d.kolyshev): Use [datasize.Bytesize].
|
|
|
|
UpstreamsCacheSize uint32 `yaml:"upstreams_cache_size"`
|
|
|
|
|
|
|
|
// UpstreamsCacheEnabled indicates if the DNS cache is enabled.
|
|
|
|
UpstreamsCacheEnabled bool `yaml:"upstreams_cache_enabled"`
|
|
|
|
|
2021-09-13 20:16:06 +03:00
|
|
|
UseGlobalSettings bool `yaml:"use_global_settings"`
|
|
|
|
FilteringEnabled bool `yaml:"filtering_enabled"`
|
|
|
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
|
|
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
|
|
|
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
|
2023-04-07 13:17:40 +03:00
|
|
|
|
|
|
|
IgnoreQueryLog bool `yaml:"ignore_querylog"`
|
|
|
|
IgnoreStatistics bool `yaml:"ignore_statistics"`
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// toPersistent returns an initialized persistent client if there are no errors.
|
|
|
|
func (o *clientObject) toPersistent(
|
2024-10-09 16:31:03 +03:00
|
|
|
ctx context.Context,
|
|
|
|
baseLogger *slog.Logger,
|
2024-10-02 20:48:58 +03:00
|
|
|
safeSearchCacheSize uint,
|
|
|
|
safeSearchCacheTTL time.Duration,
|
2024-02-27 13:48:11 +03:00
|
|
|
) (cli *client.Persistent, err error) {
|
|
|
|
cli = &client.Persistent{
|
2023-12-28 17:26:17 +03:00
|
|
|
Name: o.Name,
|
|
|
|
|
|
|
|
Upstreams: o.Upstreams,
|
|
|
|
|
|
|
|
UID: o.UID,
|
|
|
|
|
|
|
|
UseOwnSettings: !o.UseGlobalSettings,
|
|
|
|
FilteringEnabled: o.FilteringEnabled,
|
|
|
|
ParentalEnabled: o.ParentalEnabled,
|
2024-02-27 13:48:11 +03:00
|
|
|
SafeSearchConf: o.SafeSearchConf,
|
2023-12-28 17:26:17 +03:00
|
|
|
SafeBrowsingEnabled: o.SafeBrowsingEnabled,
|
|
|
|
UseOwnBlockedServices: !o.UseGlobalBlockedServices,
|
|
|
|
IgnoreQueryLog: o.IgnoreQueryLog,
|
|
|
|
IgnoreStatistics: o.IgnoreStatistics,
|
|
|
|
UpstreamsCacheEnabled: o.UpstreamsCacheEnabled,
|
|
|
|
UpstreamsCacheSize: o.UpstreamsCacheSize,
|
|
|
|
}
|
|
|
|
|
2024-02-27 13:48:11 +03:00
|
|
|
err = cli.SetIDs(o.IDs)
|
2024-01-17 13:24:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing ids: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-27 13:48:11 +03:00
|
|
|
if (cli.UID == client.UID{}) {
|
|
|
|
cli.UID, err = client.NewUID()
|
2023-12-28 17:26:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("generating uid: %w", err)
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2023-12-28 17:26:17 +03:00
|
|
|
}
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
if o.SafeSearchConf.Enabled {
|
2024-10-09 16:31:03 +03:00
|
|
|
logger := baseLogger.With(
|
|
|
|
slogutil.KeyPrefix, safesearch.LogPrefix,
|
|
|
|
safesearch.LogKeyClient, cli.Name,
|
2023-12-28 17:26:17 +03:00
|
|
|
)
|
2024-10-09 16:31:03 +03:00
|
|
|
var ss *safesearch.Default
|
|
|
|
ss, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
|
|
|
|
Logger: logger,
|
|
|
|
ServicesConfig: o.SafeSearchConf,
|
|
|
|
ClientName: cli.Name,
|
|
|
|
CacheSize: safeSearchCacheSize,
|
|
|
|
CacheTTL: safeSearchCacheTTL,
|
|
|
|
})
|
2023-06-27 18:03:07 +03:00
|
|
|
if err != nil {
|
2023-12-28 17:26:17 +03:00
|
|
|
return nil, fmt.Errorf("init safesearch %q: %w", cli.Name, err)
|
2020-03-02 18:51:48 +03:00
|
|
|
}
|
2024-10-09 16:31:03 +03:00
|
|
|
|
|
|
|
cli.SafeSearch = ss
|
2023-12-28 17:26:17 +03:00
|
|
|
}
|
2020-03-02 18:51:48 +03:00
|
|
|
|
2024-06-18 14:27:25 +03:00
|
|
|
if o.BlockedServices == nil {
|
|
|
|
o.BlockedServices = &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
err = o.BlockedServices.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init blocked services %q: %w", cli.Name, err)
|
|
|
|
}
|
2023-06-27 18:03:07 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
cli.BlockedServices = o.BlockedServices.Clone()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
cli.Tags = slices.Clone(o.Tags)
|
2021-12-13 15:18:21 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
// forConfig returns all currently known persistent clients as objects for the
|
|
|
|
// configuration file.
|
|
|
|
func (clients *clientsContainer) forConfig() (objs []*clientObject) {
|
2019-12-11 12:38:58 +03:00
|
|
|
clients.lock.Lock()
|
2021-12-12 13:46:13 +03:00
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
objs = make([]*clientObject, 0, clients.storage.Size())
|
|
|
|
clients.storage.RangeByName(func(cli *client.Persistent) (cont bool) {
|
2024-05-13 20:09:18 +03:00
|
|
|
objs = append(objs, &clientObject{
|
2021-12-13 15:18:21 +03:00
|
|
|
Name: cli.Name,
|
|
|
|
|
2023-06-27 18:03:07 +03:00
|
|
|
BlockedServices: cli.BlockedServices.Clone(),
|
|
|
|
|
2024-02-27 13:48:11 +03:00
|
|
|
IDs: cli.IDs(),
|
2024-04-03 13:44:51 +03:00
|
|
|
Tags: slices.Clone(cli.Tags),
|
|
|
|
Upstreams: slices.Clone(cli.Upstreams),
|
2021-12-13 15:18:21 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
UID: cli.UID,
|
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
UseGlobalSettings: !cli.UseOwnSettings,
|
|
|
|
FilteringEnabled: cli.FilteringEnabled,
|
|
|
|
ParentalEnabled: cli.ParentalEnabled,
|
2024-02-27 13:48:11 +03:00
|
|
|
SafeSearchConf: cli.SafeSearchConf,
|
2019-12-11 12:38:58 +03:00
|
|
|
SafeBrowsingEnabled: cli.SafeBrowsingEnabled,
|
2019-09-26 16:40:52 +03:00
|
|
|
UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
|
2023-04-07 13:17:40 +03:00
|
|
|
IgnoreQueryLog: cli.IgnoreQueryLog,
|
|
|
|
IgnoreStatistics: cli.IgnoreStatistics,
|
2023-11-20 15:28:08 +03:00
|
|
|
UpstreamsCacheEnabled: cli.UpstreamsCacheEnabled,
|
|
|
|
UpstreamsCacheSize: cli.UpstreamsCacheSize,
|
2024-05-13 20:09:18 +03:00
|
|
|
})
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2024-05-13 20:09:18 +03:00
|
|
|
return true
|
|
|
|
})
|
2021-12-12 13:46:13 +03:00
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
return objs
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// arpClientsUpdatePeriod defines how often ARP clients are updated.
|
|
|
|
const arpClientsUpdatePeriod = 10 * time.Minute
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// findMultiple is a wrapper around [clientsContainer.find] to make it a valid
|
|
|
|
// client finder for the query log. c is never nil; if no information about the
|
|
|
|
// client is found, it returns an artificial client record by only setting the
|
|
|
|
// blocking-related fields. err is always nil.
|
2021-04-02 17:30:39 +03:00
|
|
|
func (clients *clientsContainer) findMultiple(ids []string) (c *querylog.Client, err error) {
|
2021-09-13 20:16:06 +03:00
|
|
|
var artClient *querylog.Client
|
|
|
|
var art bool
|
2021-04-02 17:30:39 +03:00
|
|
|
for _, id := range ids {
|
2022-11-09 14:37:07 +03:00
|
|
|
ip, _ := netip.ParseAddr(id)
|
|
|
|
c, art = clients.clientOrArtificial(ip, id)
|
2021-09-13 20:16:06 +03:00
|
|
|
if art {
|
|
|
|
artClient = c
|
2021-04-02 17:30:39 +03:00
|
|
|
|
2021-09-10 17:57:09 +03:00
|
|
|
continue
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:16:06 +03:00
|
|
|
return c, nil
|
2021-09-10 17:57:09 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:16:06 +03:00
|
|
|
return artClient, nil
|
2021-09-10 17:57:09 +03:00
|
|
|
}
|
2021-04-02 17:30:39 +03:00
|
|
|
|
2021-09-13 20:16:06 +03:00
|
|
|
// clientOrArtificial returns information about one client. If art is true,
|
|
|
|
// this is an artificial client record, meaning that we currently don't have any
|
|
|
|
// records about this client besides maybe whether or not it is blocked. c is
|
|
|
|
// never nil.
|
|
|
|
func (clients *clientsContainer) clientOrArtificial(
|
2022-11-09 14:37:07 +03:00
|
|
|
ip netip.Addr,
|
2021-09-10 17:57:09 +03:00
|
|
|
id string,
|
2021-09-13 20:16:06 +03:00
|
|
|
) (c *querylog.Client, art bool) {
|
|
|
|
defer func() {
|
2024-05-13 20:09:18 +03:00
|
|
|
c.Disallowed, c.DisallowedRule = clients.clientChecker.IsBlockedClient(ip, id)
|
2021-09-13 20:16:06 +03:00
|
|
|
if c.WHOIS == nil {
|
2023-06-21 12:53:53 +03:00
|
|
|
c.WHOIS = &whois.Info{}
|
2021-09-13 20:16:06 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
cli, ok := clients.storage.FindLoose(ip, id)
|
|
|
|
if ok {
|
2021-04-02 17:30:39 +03:00
|
|
|
return &querylog.Client{
|
2023-12-13 13:39:52 +03:00
|
|
|
Name: cli.Name,
|
|
|
|
IgnoreQueryLog: cli.IgnoreQueryLog,
|
2021-09-13 20:16:06 +03:00
|
|
|
}, false
|
2021-09-10 17:57:09 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 14:17:42 +03:00
|
|
|
rc := clients.storage.ClientRuntime(ip)
|
2024-04-04 19:17:23 +03:00
|
|
|
if rc != nil {
|
2023-12-13 13:39:52 +03:00
|
|
|
_, host := rc.Info()
|
|
|
|
|
2021-09-10 17:57:09 +03:00
|
|
|
return &querylog.Client{
|
2023-12-13 13:39:52 +03:00
|
|
|
Name: host,
|
|
|
|
WHOIS: rc.WHOIS(),
|
2021-09-13 20:16:06 +03:00
|
|
|
}, false
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 17:57:09 +03:00
|
|
|
return &querylog.Client{
|
2021-09-13 20:16:06 +03:00
|
|
|
Name: "",
|
|
|
|
}, true
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// shouldCountClient is a wrapper around [clientsContainer.find] to make it a
|
|
|
|
// valid client information finder for the statistics. If no information about
|
|
|
|
// the client is found, it returns true.
|
2023-04-07 13:17:40 +03:00
|
|
|
func (clients *clientsContainer) shouldCountClient(ids []string) (y bool) {
|
2023-05-25 17:45:55 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2023-04-07 13:17:40 +03:00
|
|
|
for _, id := range ids {
|
2024-09-30 14:17:42 +03:00
|
|
|
client, ok := clients.storage.Find(id)
|
2023-04-07 13:17:40 +03:00
|
|
|
if ok {
|
|
|
|
return !client.IgnoreStatistics
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
// type check
|
|
|
|
var _ dnsforward.ClientsContainer = (*clientsContainer)(nil)
|
|
|
|
|
|
|
|
// UpstreamConfigByID implements the [dnsforward.ClientsContainer] interface for
|
2023-11-17 15:51:51 +03:00
|
|
|
// *clientsContainer. upsConf is nil if the client isn't found or if the client
|
|
|
|
// has no custom upstreams.
|
2023-11-16 14:05:10 +03:00
|
|
|
func (clients *clientsContainer) UpstreamConfigByID(
|
2021-05-28 13:02:59 +03:00
|
|
|
id string,
|
2023-11-16 14:05:10 +03:00
|
|
|
bootstrap upstream.Resolver,
|
2023-11-17 15:51:51 +03:00
|
|
|
) (conf *proxy.CustomUpstreamConfig, err error) {
|
2019-12-23 19:31:27 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2024-09-30 14:17:42 +03:00
|
|
|
c, ok := clients.storage.Find(id)
|
2019-12-23 19:31:27 +03:00
|
|
|
if !ok {
|
2021-05-28 13:02:59 +03:00
|
|
|
return nil, nil
|
2024-02-27 13:48:11 +03:00
|
|
|
} else if c.UpstreamConfig != nil {
|
|
|
|
return c.UpstreamConfig, nil
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
upstreams := stringutil.FilterOut(c.Upstreams, dnsforward.IsCommentOrEmpty)
|
2021-04-13 13:44:29 +03:00
|
|
|
if len(upstreams) == 0 {
|
2021-05-28 13:02:59 +03:00
|
|
|
return nil, nil
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
|
|
|
|
2023-11-17 15:51:51 +03:00
|
|
|
var upsConf *proxy.UpstreamConfig
|
|
|
|
upsConf, err = proxy.ParseUpstreamsConfig(
|
2021-05-28 13:02:59 +03:00
|
|
|
upstreams,
|
2021-06-29 15:53:28 +03:00
|
|
|
&upstream.Options{
|
2023-11-16 14:05:10 +03:00
|
|
|
Bootstrap: bootstrap,
|
2022-10-03 18:08:05 +03:00
|
|
|
Timeout: config.DNS.UpstreamTimeout.Duration,
|
|
|
|
HTTPVersions: dnsforward.UpstreamHTTPVersions(config.DNS.UseHTTP3Upstreams),
|
2023-04-04 13:28:21 +03:00
|
|
|
PreferIPv6: config.DNS.BootstrapPreferIPv6,
|
2021-05-28 13:02:59 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-11-16 14:05:10 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
2021-05-28 13:02:59 +03:00
|
|
|
return nil, err
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
2020-05-13 20:31:43 +03:00
|
|
|
|
2023-11-17 15:51:51 +03:00
|
|
|
conf = proxy.NewCustomUpstreamConfig(
|
|
|
|
upsConf,
|
|
|
|
c.UpstreamsCacheEnabled,
|
|
|
|
int(c.UpstreamsCacheSize),
|
|
|
|
config.DNS.EDNSClientSubnet.Enabled,
|
|
|
|
)
|
2024-02-27 13:48:11 +03:00
|
|
|
c.UpstreamConfig = conf
|
2021-05-28 13:02:59 +03:00
|
|
|
|
2024-10-22 13:57:54 +03:00
|
|
|
// TODO(s.chzhen): Pass context.
|
|
|
|
err = clients.storage.Update(context.TODO(), c.Name, c)
|
2024-10-10 16:33:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting upstream config: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
return conf, nil
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// type check
|
|
|
|
var _ client.AddressUpdater = (*clientsContainer)(nil)
|
|
|
|
|
|
|
|
// UpdateAddress implements the [client.AddressUpdater] interface for
|
|
|
|
// *clientsContainer
|
2024-10-22 13:57:54 +03:00
|
|
|
func (clients *clientsContainer) UpdateAddress(
|
|
|
|
ctx context.Context,
|
|
|
|
ip netip.Addr,
|
|
|
|
host string,
|
|
|
|
info *whois.Info,
|
|
|
|
) {
|
|
|
|
clients.storage.UpdateAddress(ctx, ip, host, info)
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 20:14:43 +03:00
|
|
|
// close gracefully closes all the client-specific upstream configurations of
|
2022-10-19 16:13:05 +03:00
|
|
|
// the persistent clients.
|
2024-09-30 14:17:42 +03:00
|
|
|
func (clients *clientsContainer) close(ctx context.Context) (err error) {
|
|
|
|
return clients.storage.Shutdown(ctx)
|
2022-10-19 16:13:05 +03:00
|
|
|
}
|