2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-03-19 18:47:22 +03:00
|
|
|
|
|
|
|
import (
|
2019-04-26 15:10:29 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-10-24 17:49:52 +03:00
|
|
|
"net/netip"
|
2022-05-04 21:01:41 +03:00
|
|
|
"strings"
|
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"
|
2023-06-22 14:18:43 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
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"
|
2021-04-02 17:30:39 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
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"
|
2023-12-07 16:48:55 +03:00
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
2019-03-19 18:47:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2022-10-19 16:13:05 +03:00
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
"golang.org/x/exp/slices"
|
2019-06-25 17:51:53 +03:00
|
|
|
)
|
|
|
|
|
2023-06-28 13:46:04 +03:00
|
|
|
// DHCP is an interface for accessing DHCP lease data the [clientsContainer]
|
2023-06-22 14:18:43 +03:00
|
|
|
// needs.
|
|
|
|
type DHCP interface {
|
|
|
|
// Leases returns all the DHCP leases.
|
|
|
|
Leases() (leases []*dhcpsvc.Lease)
|
|
|
|
|
|
|
|
// HostByIP returns the hostname of the DHCP client with the given IP
|
|
|
|
// address. The address will be netip.Addr{} if there is no such client,
|
2023-08-30 14:02:12 +03:00
|
|
|
// due to an assumption that a DHCP client must always have a hostname.
|
2023-06-22 14:18:43 +03:00
|
|
|
HostByIP(ip netip.Addr) (host string)
|
|
|
|
|
|
|
|
// MACByIP returns the MAC address for the given IP address leased. It
|
|
|
|
// returns nil if there is no such client, due to an assumption that a DHCP
|
|
|
|
// client must always have a MAC address.
|
|
|
|
MACByIP(ip netip.Addr) (mac net.HardwareAddr)
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-03-01 13:27:24 +03:00
|
|
|
// TODO(a.garipov): Perhaps use a number of separate indices for different
|
|
|
|
// types (string, netip.Addr, and so on).
|
2023-12-28 17:26:17 +03:00
|
|
|
list map[string]*persistentClient // name -> client
|
|
|
|
idIndex map[string]*persistentClient // ID -> client
|
2021-06-29 15:53:28 +03:00
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
// ipToRC maps IP addresses to runtime client information.
|
|
|
|
ipToRC map[netip.Addr]*client.Runtime
|
2021-06-29 15:53:28 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
allTags *stringutil.Set
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// dhcp is the DHCP service implementation.
|
|
|
|
dhcp DHCP
|
2019-11-29 17:35:26 +03:00
|
|
|
|
2020-09-08 13:56:45 +03:00
|
|
|
// dnsServer is used for checking clients IP status access list status
|
|
|
|
dnsServer *dnsforward.Server
|
2020-07-24 14:30:29 +03:00
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
// etcHosts contains list of rewrite rules taken from the operating system's
|
2021-12-27 19:40:39 +03:00
|
|
|
// hosts database.
|
2021-10-14 19:39:21 +03:00
|
|
|
etcHosts *aghnet.HostsContainer
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// arpDB stores the neighbors retrieved from ARP.
|
|
|
|
arpDB arpdb.Interface
|
2022-03-15 20:57:46 +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
|
|
|
}
|
|
|
|
|
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(
|
2021-12-13 15:18:21 +03:00
|
|
|
objects []*clientObject,
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcpServer 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) {
|
2019-04-26 15:10:29 +03:00
|
|
|
if clients.list != nil {
|
|
|
|
log.Fatal("clients.list != nil")
|
|
|
|
}
|
2023-04-06 14:12:50 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
clients.list = map[string]*persistentClient{}
|
|
|
|
clients.idIndex = map[string]*persistentClient{}
|
2023-12-13 13:39:52 +03:00
|
|
|
clients.ipToRC = map[netip.Addr]*client.Runtime{}
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
clients.allTags = stringutil.NewSet(clientTags...)
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// TODO(e.burkov): Use [dhcpsvc] implementation when it's ready.
|
|
|
|
clients.dhcp = dhcpServer
|
|
|
|
|
2021-04-14 19:18:48 +03:00
|
|
|
clients.etcHosts = etcHosts
|
2023-08-24 13:42:17 +03:00
|
|
|
clients.arpDB = arpDB
|
2023-06-27 18:03:07 +03:00
|
|
|
err = clients.addFromConfig(objects, filteringConf)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2023-04-06 14:12:50 +03:00
|
|
|
clients.safeSearchCacheSize = filteringConf.SafeSearchCacheSize
|
|
|
|
clients.safeSearchCacheTTL = time.Minute * time.Duration(filteringConf.CacheTime)
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
if clients.testing {
|
2023-06-27 18:03:07 +03:00
|
|
|
return nil
|
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.
|
|
|
|
if config.Clients.Sources.HostsFile && clients.etcHosts != nil {
|
2022-04-26 13:04:16 +03:00
|
|
|
go clients.handleHostsUpdates()
|
|
|
|
}
|
2023-06-27 18:03:07 +03:00
|
|
|
|
|
|
|
return nil
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
// handleHostsUpdates receives the updates from the hosts container and adds
|
|
|
|
// them to the clients container. It's used to be called in a separate
|
|
|
|
// goroutine.
|
2021-10-14 19:39:21 +03:00
|
|
|
func (clients *clientsContainer) handleHostsUpdates() {
|
2022-04-26 13:04:16 +03:00
|
|
|
for upd := range clients.etcHosts.Upd() {
|
|
|
|
clients.addFromHostsFile(upd)
|
2020-02-18 19:27:09 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 10:25:02 +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.
|
2020-02-18 19:27:09 +03:00
|
|
|
func (clients *clientsContainer) Start() {
|
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
|
|
|
|
|
|
|
go clients.periodicUpdate()
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// reloadARP reloads runtime clients from ARP, if configured.
|
|
|
|
func (clients *clientsContainer) reloadARP() {
|
2023-08-24 13:42:17 +03:00
|
|
|
if clients.arpDB != nil {
|
2022-04-26 13:04:16 +03:00
|
|
|
clients.addFromSystemARP()
|
|
|
|
}
|
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.
|
|
|
|
UID UID `yaml:"uid"`
|
|
|
|
|
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(
|
2023-06-27 18:03:07 +03:00
|
|
|
filteringConf *filtering.Config,
|
2023-12-28 17:26:17 +03:00
|
|
|
allTags *stringutil.Set,
|
|
|
|
) (cli *persistentClient, err error) {
|
|
|
|
cli = &persistentClient{
|
|
|
|
Name: o.Name,
|
|
|
|
|
|
|
|
Upstreams: o.Upstreams,
|
|
|
|
|
|
|
|
UID: o.UID,
|
|
|
|
|
|
|
|
UseOwnSettings: !o.UseGlobalSettings,
|
|
|
|
FilteringEnabled: o.FilteringEnabled,
|
|
|
|
ParentalEnabled: o.ParentalEnabled,
|
|
|
|
safeSearchConf: o.SafeSearchConf,
|
|
|
|
SafeBrowsingEnabled: o.SafeBrowsingEnabled,
|
|
|
|
UseOwnBlockedServices: !o.UseGlobalBlockedServices,
|
|
|
|
IgnoreQueryLog: o.IgnoreQueryLog,
|
|
|
|
IgnoreStatistics: o.IgnoreStatistics,
|
|
|
|
UpstreamsCacheEnabled: o.UpstreamsCacheEnabled,
|
|
|
|
UpstreamsCacheSize: o.UpstreamsCacheSize,
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
err = cli.setIDs(o.IDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing ids: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
if (cli.UID == UID{}) {
|
|
|
|
cli.UID, err = NewUID()
|
|
|
|
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 {
|
|
|
|
o.SafeSearchConf.CustomResolver = safeSearchResolver{}
|
2023-03-15 14:31:07 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
err = cli.setSafeSearch(
|
|
|
|
o.SafeSearchConf,
|
|
|
|
filteringConf.SafeSearchCacheSize,
|
|
|
|
time.Minute*time.Duration(filteringConf.CacheTime),
|
|
|
|
)
|
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
|
|
|
}
|
2023-12-28 17:26:17 +03:00
|
|
|
}
|
2020-03-02 18:51:48 +03:00
|
|
|
|
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-01-17 13:24:21 +03:00
|
|
|
cli.setTags(o.Tags, allTags)
|
2021-12-13 15:18:21 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addFromConfig initializes the clients container with objects from the
|
|
|
|
// configuration file.
|
|
|
|
func (clients *clientsContainer) addFromConfig(
|
|
|
|
objects []*clientObject,
|
|
|
|
filteringConf *filtering.Config,
|
|
|
|
) (err error) {
|
|
|
|
for i, o := range objects {
|
|
|
|
var cli *persistentClient
|
|
|
|
cli, err = o.toPersistent(filteringConf, clients.allTags)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("clients: init persistent client at index %d: %w", i, err)
|
|
|
|
}
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
_, err = clients.add(cli)
|
2019-09-26 16:40:52 +03:00
|
|
|
if err != nil {
|
2023-12-28 17:26:17 +03:00
|
|
|
log.Error("clients: adding client at index %d %s: %s", i, cli.Name, err)
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-27 18:03:07 +03:00
|
|
|
|
|
|
|
return nil
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2021-12-13 15:28:12 +03:00
|
|
|
objs = make([]*clientObject, 0, len(clients.list))
|
2019-12-11 12:38:58 +03:00
|
|
|
for _, cli := range clients.list {
|
2021-12-13 15:18:21 +03:00
|
|
|
o := &clientObject{
|
|
|
|
Name: cli.Name,
|
|
|
|
|
2023-06-27 18:03:07 +03:00
|
|
|
BlockedServices: cli.BlockedServices.Clone(),
|
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
IDs: cli.ids(),
|
2023-06-27 18:03:07 +03:00
|
|
|
Tags: stringutil.CloneSlice(cli.Tags),
|
|
|
|
Upstreams: stringutil.CloneSlice(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,
|
2023-03-15 14:31:07 +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,
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
objs = append(objs, o)
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2021-12-12 13:46:13 +03:00
|
|
|
|
|
|
|
// Maps aren't guaranteed to iterate in the same order each time, so the
|
2021-12-13 14:56:48 +03:00
|
|
|
// above loop can generate different orderings when writing to the config
|
|
|
|
// file: this produces lots of diffs in config files, so sort objects by
|
|
|
|
// name before writing.
|
2023-08-10 20:00:17 +03:00
|
|
|
slices.SortStableFunc(objs, func(a, b *clientObject) (res int) {
|
|
|
|
return strings.Compare(a.Name, b.Name)
|
2023-02-21 16:38:22 +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
|
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
func (clients *clientsContainer) periodicUpdate() {
|
2022-03-23 20:47:45 +03:00
|
|
|
defer log.OnPanic("clients container")
|
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
for {
|
2023-03-01 13:27:24 +03:00
|
|
|
clients.reloadARP()
|
|
|
|
time.Sleep(arpClientsUpdatePeriod)
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2023-02-10 16:40:36 +03:00
|
|
|
// clientSource checks if client with this IP address already exists and returns
|
2023-09-01 15:24:44 +03:00
|
|
|
// the source which updated it last. It returns [client.SourceNone] if the
|
2023-02-10 16:40:36 +03:00
|
|
|
// client doesn't exist.
|
2023-09-01 15:24:44 +03:00
|
|
|
func (clients *clientsContainer) clientSource(ip netip.Addr) (src client.Source) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2023-02-10 16:40:36 +03:00
|
|
|
_, ok := clients.findLocked(ip.String())
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
2023-09-01 15:24:44 +03:00
|
|
|
return client.SourcePersistent
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
rc, ok := clients.ipToRC[ip]
|
2023-06-22 14:18:43 +03:00
|
|
|
if ok {
|
2023-12-13 13:39:52 +03:00
|
|
|
src, _ = rc.Info()
|
2023-08-30 14:02:12 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 15:24:44 +03:00
|
|
|
if src < client.SourceDHCP && clients.dhcp.HostByIP(ip) != "" {
|
|
|
|
src = client.SourceDHCP
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
return src
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
c.Disallowed, c.DisallowedRule = clients.dnsServer.IsBlockedClient(ip, id)
|
|
|
|
if c.WHOIS == nil {
|
2023-06-21 12:53:53 +03:00
|
|
|
c.WHOIS = &whois.Info{}
|
2021-09-13 20:16:06 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
cli, ok := clients.find(id)
|
2021-09-10 17:57:09 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
var rc *client.Runtime
|
2022-10-25 15:08:12 +03:00
|
|
|
rc, ok = clients.findRuntimeClient(ip)
|
2021-09-10 17:57:09 +03:00
|
|
|
if ok {
|
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
|
|
|
// find returns a shallow copy of the client if there is one found.
|
|
|
|
func (clients *clientsContainer) find(id string) (c *persistentClient, ok bool) {
|
2019-12-23 16:27:24 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
c, ok = clients.findLocked(id)
|
2020-01-28 14:06:52 +03:00
|
|
|
if !ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2020-01-28 14:06:52 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
return c.shallowClone(), true
|
2019-12-23 16:27:24 +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 {
|
2023-05-25 17:45:55 +03:00
|
|
|
client, ok := clients.findLocked(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()
|
|
|
|
|
2021-05-28 13:02:59 +03:00
|
|
|
c, ok := clients.findLocked(id)
|
2019-12-23 19:31:27 +03:00
|
|
|
if !ok {
|
2021-05-28 13:02:59 +03:00
|
|
|
return nil, nil
|
2023-11-16 14:05:10 +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,
|
|
|
|
)
|
2021-06-29 15:53:28 +03:00
|
|
|
c.upstreamConfig = conf
|
2021-05-28 13:02:59 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
return conf, nil
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
|
|
|
|
2023-03-01 13:27:24 +03:00
|
|
|
// findLocked searches for a client by its ID. clients.lock is expected to be
|
|
|
|
// locked.
|
2023-12-28 17:26:17 +03:00
|
|
|
func (clients *clientsContainer) findLocked(id string) (c *persistentClient, ok bool) {
|
2021-01-27 18:32:13 +03:00
|
|
|
c, ok = clients.idIndex[id]
|
|
|
|
if ok {
|
|
|
|
return c, true
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
ip, err := netip.ParseAddr(id)
|
|
|
|
if err != nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c = range clients.list {
|
2024-01-17 13:24:21 +03:00
|
|
|
for _, subnet := range c.Subnets {
|
2023-03-01 13:27:24 +03:00
|
|
|
if subnet.Contains(ip) {
|
2021-01-27 18:32:13 +03:00
|
|
|
return c, true
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// TODO(e.burkov): Iterate through clients.list only once.
|
|
|
|
return clients.findDHCP(ip)
|
2023-03-01 13:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// findDHCP searches for a client by its MAC, if the DHCP server is active and
|
|
|
|
// there is such client. clients.lock is expected to be locked.
|
2023-12-28 17:26:17 +03:00
|
|
|
func (clients *clientsContainer) findDHCP(ip netip.Addr) (c *persistentClient, ok bool) {
|
2023-08-30 14:02:12 +03:00
|
|
|
foundMAC := clients.dhcp.MACByIP(ip)
|
2023-03-01 13:27:24 +03:00
|
|
|
if foundMAC == nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
for _, c = range clients.list {
|
2024-01-17 13:24:21 +03:00
|
|
|
_, found := slices.BinarySearchFunc(c.MACs, foundMAC, slices.Compare[net.HardwareAddr])
|
|
|
|
if found {
|
|
|
|
return c, true
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// runtimeClient returns a runtime client from internal index. Note that it
|
|
|
|
// doesn't include DHCP clients.
|
2023-12-13 13:39:52 +03:00
|
|
|
func (clients *clientsContainer) runtimeClient(ip netip.Addr) (rc *client.Runtime, ok bool) {
|
2022-11-09 14:37:07 +03:00
|
|
|
if ip == (netip.Addr{}) {
|
2021-06-29 15:53:28 +03:00
|
|
|
return nil, false
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
rc, ok = clients.ipToRC[ip]
|
|
|
|
|
|
|
|
return rc, ok
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// findRuntimeClient finds a runtime client by their IP.
|
2023-12-13 13:39:52 +03:00
|
|
|
func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *client.Runtime, ok bool) {
|
|
|
|
rc, ok = clients.runtimeClient(ip)
|
2023-08-30 14:02:12 +03:00
|
|
|
host := clients.dhcp.HostByIP(ip)
|
2023-12-13 13:39:52 +03:00
|
|
|
|
|
|
|
if host != "" {
|
|
|
|
if !ok {
|
|
|
|
rc = &client.Runtime{}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.SetInfo(client.SourceDHCP, []string{host})
|
|
|
|
|
|
|
|
return rc, true
|
2023-08-30 14:02:12 +03:00
|
|
|
}
|
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
return rc, ok
|
2023-08-30 14:02:12 +03:00
|
|
|
}
|
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
// check validates the client. It also sorts the client tags.
|
2023-12-28 17:26:17 +03:00
|
|
|
func (clients *clientsContainer) check(c *persistentClient) (err error) {
|
2021-01-27 18:32:13 +03:00
|
|
|
switch {
|
|
|
|
case c == nil:
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("client is nil")
|
2021-01-27 18:32:13 +03:00
|
|
|
case c.Name == "":
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("invalid name")
|
2024-01-17 13:24:21 +03:00
|
|
|
case c.idsLen() == 0:
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("id required")
|
2021-01-27 18:32:13 +03:00
|
|
|
default:
|
|
|
|
// Go on.
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
for _, t := range c.Tags {
|
2021-12-13 15:18:21 +03:00
|
|
|
if !clients.allTags.Has(t) {
|
2021-01-27 18:32:13 +03:00
|
|
|
return fmt.Errorf("invalid tag: %q", t)
|
2020-01-28 14:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
// TODO(s.chzhen): Move to the constructor.
|
2023-02-21 16:38:22 +03:00
|
|
|
slices.Sort(c.Tags)
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
err = dnsforward.ValidateUpstreams(c.Upstreams)
|
2020-09-22 15:04:17 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("invalid upstream servers: %w", err)
|
2019-11-06 17:24:15 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// add adds a new client object. ok is false if such client already exists or
|
2021-01-27 18:32:13 +03:00
|
|
|
// if an error occurred.
|
2023-12-28 17:26:17 +03:00
|
|
|
func (clients *clientsContainer) add(c *persistentClient) (ok bool, err error) {
|
2021-01-27 18:32:13 +03:00
|
|
|
err = clients.check(c)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
// check Name index
|
2021-01-27 18:32:13 +03:00
|
|
|
_, ok = clients.list[c.Name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
// check ID index
|
2024-01-17 13:24:21 +03:00
|
|
|
ids := c.ids()
|
|
|
|
for _, id := range ids {
|
2023-12-28 17:26:17 +03:00
|
|
|
var c2 *persistentClient
|
2021-01-27 18:32:13 +03:00
|
|
|
c2, ok = clients.idIndex[id]
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
return false, fmt.Errorf("another client uses the same ID (%q): %q", id, c2.Name)
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
clients.addLocked(c)
|
2023-05-25 17:45:55 +03:00
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
log.Debug("clients: added %q: ID:%q [%d]", c.Name, ids, len(clients.list))
|
2023-05-25 17:45:55 +03:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// addLocked c to the indexes. clients.lock is expected to be locked.
|
|
|
|
func (clients *clientsContainer) addLocked(c *persistentClient) {
|
2019-09-26 16:40:52 +03:00
|
|
|
// update Name index
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.list[c.Name] = c
|
2019-09-26 16:40:52 +03:00
|
|
|
|
|
|
|
// update ID index
|
2024-01-17 13:24:21 +03:00
|
|
|
for _, id := range c.ids() {
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.idIndex[id] = c
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// remove removes a client. ok is false if there is no such client.
|
|
|
|
func (clients *clientsContainer) remove(name string) (ok bool) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
var c *persistentClient
|
2021-01-27 18:32:13 +03:00
|
|
|
c, ok = clients.list[name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
clients.removeLocked(c)
|
2023-05-25 17:45:55 +03:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// removeLocked removes c from the indexes. clients.lock is expected to be
|
|
|
|
// locked.
|
|
|
|
func (clients *clientsContainer) removeLocked(c *persistentClient) {
|
2023-11-16 14:05:10 +03:00
|
|
|
if err := c.closeUpstreams(); err != nil {
|
|
|
|
log.Error("client container: removing client %s: %s", c.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the name index.
|
2023-05-25 17:45:55 +03:00
|
|
|
delete(clients.list, c.Name)
|
2019-09-26 16:40:52 +03:00
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
// Update the ID index.
|
2024-01-17 13:24:21 +03:00
|
|
|
for _, id := range c.ids() {
|
2019-09-26 16:40:52 +03:00
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
// update updates a client by its name.
|
|
|
|
func (clients *clientsContainer) update(prev, c *persistentClient) (err error) {
|
2021-01-27 18:32:13 +03:00
|
|
|
err = clients.check(c)
|
2019-04-26 15:10:29 +03:00
|
|
|
if err != nil {
|
2023-05-25 17:45:55 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
2019-04-26 15:10:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2023-05-25 17:45:55 +03:00
|
|
|
// Check the name index.
|
2021-01-27 18:32:13 +03:00
|
|
|
if prev.Name != c.Name {
|
2023-05-25 17:45:55 +03:00
|
|
|
_, ok := clients.list[c.Name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("client already exists")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:24:21 +03:00
|
|
|
if c.equalIDs(prev) {
|
|
|
|
clients.removeLocked(prev)
|
|
|
|
clients.addLocked(c)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 17:45:55 +03:00
|
|
|
// Check the ID index.
|
2024-01-17 13:24:21 +03:00
|
|
|
for _, id := range c.ids() {
|
|
|
|
existing, ok := clients.idIndex[id]
|
|
|
|
if ok && existing != prev {
|
|
|
|
return fmt.Errorf("id %q is used by client with name %q", id, existing.Name)
|
2023-03-01 13:27:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:26:17 +03:00
|
|
|
clients.removeLocked(prev)
|
|
|
|
clients.addLocked(c)
|
2023-03-01 13:27:24 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// setWHOISInfo sets the WHOIS information for a client. clients.lock is
|
|
|
|
// expected to be locked.
|
2023-06-21 12:53:53 +03:00
|
|
|
func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
|
2021-06-29 15:53:28 +03:00
|
|
|
_, ok := clients.findLocked(ip.String())
|
2019-09-18 18:44:27 +03:00
|
|
|
if ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: client for %s is already created, ignore whois info", ip)
|
2023-06-22 14:18:43 +03:00
|
|
|
|
2019-09-23 20:41:14 +03:00
|
|
|
return
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
rc, ok := clients.ipToRC[ip]
|
2023-06-22 14:18:43 +03:00
|
|
|
if !ok {
|
|
|
|
// Create a RuntimeClient implicitly so that we don't do this check
|
|
|
|
// again.
|
2023-12-13 13:39:52 +03:00
|
|
|
rc = &client.Runtime{}
|
2023-06-22 14:18:43 +03:00
|
|
|
clients.ipToRC[ip] = rc
|
2019-09-18 18:44:27 +03:00
|
|
|
|
2023-06-22 14:18:43 +03:00
|
|
|
log.Debug("clients: set whois info for runtime client with ip %s: %+v", ip, wi)
|
|
|
|
} else {
|
2023-12-13 13:39:52 +03:00
|
|
|
host, _ := rc.Info()
|
|
|
|
log.Debug("clients: set whois info for runtime client %s: %+v", host, wi)
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
2021-04-02 17:30:39 +03:00
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
rc.SetWHOIS(wi)
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// addHost adds a new IP-hostname pairing. The priorities of the sources are
|
2021-01-27 18:32:13 +03:00
|
|
|
// taken into account. ok is true if the pairing was added.
|
2023-07-18 17:02:07 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Only used in internal tests. Consider removing.
|
|
|
|
func (clients *clientsContainer) addHost(
|
2022-11-09 14:37:07 +03:00
|
|
|
ip netip.Addr,
|
|
|
|
host string,
|
2023-09-01 15:24:44 +03:00
|
|
|
src client.Source,
|
2022-11-09 14:37:07 +03:00
|
|
|
) (ok bool) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
2021-03-31 15:00:47 +03:00
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
return clients.addHostLocked(ip, host, src)
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
2019-04-26 15:10:29 +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
|
|
|
|
func (clients *clientsContainer) UpdateAddress(ip netip.Addr, host string, info *whois.Info) {
|
|
|
|
// Common fast path optimization.
|
|
|
|
if host == "" && info == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
if host != "" {
|
2023-09-01 15:24:44 +03:00
|
|
|
ok := clients.addHostLocked(ip, host, client.SourceRDNS)
|
2023-07-18 17:02:07 +03:00
|
|
|
if !ok {
|
|
|
|
log.Debug("clients: host for client %q already set with higher priority source", ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info != nil {
|
|
|
|
clients.setWHOISInfo(ip, info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
// addHostLocked adds a new IP-hostname pairing. clients.lock is expected to be
|
|
|
|
// locked.
|
|
|
|
func (clients *clientsContainer) addHostLocked(
|
|
|
|
ip netip.Addr,
|
|
|
|
host string,
|
2023-09-01 15:24:44 +03:00
|
|
|
src client.Source,
|
2022-10-25 15:08:12 +03:00
|
|
|
) (ok bool) {
|
|
|
|
rc, ok := clients.ipToRC[ip]
|
2023-06-22 14:18:43 +03:00
|
|
|
if !ok {
|
2023-09-01 15:24:44 +03:00
|
|
|
if src < client.SourceDHCP {
|
2023-08-30 14:02:12 +03:00
|
|
|
if clients.dhcp.HostByIP(ip) != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
rc = &client.Runtime{}
|
2022-10-25 15:08:12 +03:00
|
|
|
clients.ipToRC[ip] = rc
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
rc.SetInfo(src, []string{host})
|
2023-06-22 14:18:43 +03:00
|
|
|
|
2023-12-13 13:39:52 +03:00
|
|
|
log.Debug("clients: adding client info %s -> %q %q [%d]", ip, src, host, len(clients.ipToRC))
|
2020-12-07 16:04:53 +03:00
|
|
|
|
|
|
|
return true
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// rmHostsBySrc removes all entries that match the specified source.
|
2023-09-01 15:24:44 +03:00
|
|
|
func (clients *clientsContainer) rmHostsBySrc(src client.Source) {
|
2020-01-30 10:25:02 +03:00
|
|
|
n := 0
|
2022-10-24 17:49:52 +03:00
|
|
|
for ip, rc := range clients.ipToRC {
|
2023-12-13 13:39:52 +03:00
|
|
|
rc.Unset(src)
|
|
|
|
if rc.IsEmpty() {
|
2022-10-24 17:49:52 +03:00
|
|
|
delete(clients.ipToRC, ip)
|
2020-01-30 10:25:02 +03:00
|
|
|
n++
|
|
|
|
}
|
2022-10-24 17:49:52 +03:00
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
|
|
|
log.Debug("clients: removed %d client aliases", n)
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// addFromHostsFile fills the client-hostname pairing index from the system's
|
|
|
|
// hosts files.
|
2023-12-07 16:48:55 +03:00
|
|
|
func (clients *clientsContainer) addFromHostsFile(hosts *hostsfile.DefaultStorage) {
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2020-11-06 17:34:40 +03:00
|
|
|
|
2023-09-01 15:24:44 +03:00
|
|
|
clients.rmHostsBySrc(client.SourceHostsFile)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
n := 0
|
2023-12-07 16:48:55 +03:00
|
|
|
hosts.RangeNames(func(addr netip.Addr, names []string) (cont bool) {
|
2023-08-22 16:45:11 +03:00
|
|
|
// Only the first name of the first record is considered a canonical
|
|
|
|
// hostname for the IP address.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Consider using all the names from all the records.
|
2023-12-07 16:48:55 +03:00
|
|
|
if clients.addHostLocked(addr, names[0], client.SourceHostsFile) {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2021-06-29 15:53:28 +03:00
|
|
|
|
2022-07-07 19:33:32 +03:00
|
|
|
log.Debug("clients: added %d client aliases from system hosts file", n)
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
|
|
|
|
// command.
|
2019-07-09 18:19:50 +03:00
|
|
|
func (clients *clientsContainer) addFromSystemARP() {
|
2023-08-24 13:42:17 +03:00
|
|
|
if err := clients.arpDB.Refresh(); err != nil {
|
2022-03-15 20:57:46 +03:00
|
|
|
log.Error("refreshing arp container: %s", err)
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
clients.arpDB = arpdb.Empty{}
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
ns := clients.arpDB.Neighbors()
|
2022-03-15 20:57:46 +03:00
|
|
|
if len(ns) == 0 {
|
|
|
|
log.Debug("refreshing arp container: the update is empty")
|
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2023-09-01 15:24:44 +03:00
|
|
|
clients.rmHostsBySrc(client.SourceARP)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
added := 0
|
|
|
|
for _, n := range ns {
|
2023-09-01 15:24:44 +03:00
|
|
|
if clients.addHostLocked(n.IP, n.Name, client.SourceARP) {
|
2022-03-15 20:57:46 +03:00
|
|
|
added++
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
log.Debug("clients: added %d client aliases from arp neighborhood", added)
|
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.
|
2022-10-21 20:14:43 +03:00
|
|
|
func (clients *clientsContainer) close() (err error) {
|
2022-10-19 16:13:05 +03:00
|
|
|
persistent := maps.Values(clients.list)
|
2023-12-28 17:26:17 +03:00
|
|
|
slices.SortFunc(persistent, func(a, b *persistentClient) (res int) {
|
2023-08-10 20:00:17 +03:00
|
|
|
return strings.Compare(a.Name, b.Name)
|
|
|
|
})
|
2022-10-19 16:13:05 +03:00
|
|
|
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
for _, cli := range persistent {
|
|
|
|
if err = cli.closeUpstreams(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:09:08 +03:00
|
|
|
return errors.Join(errs...)
|
2022-10-19 16:13:05 +03:00
|
|
|
}
|