2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-03-19 18:47:22 +03:00
|
|
|
|
|
|
|
import (
|
2019-09-26 16:40:52 +03:00
|
|
|
"bytes"
|
2022-08-04 19:05:28 +03:00
|
|
|
"encoding"
|
2019-04-26 15:10:29 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-11-02 16:18:02 +03:00
|
|
|
"net/netip"
|
2020-01-28 14:06:52 +03:00
|
|
|
"sort"
|
2019-03-19 18:47:22 +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"
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
|
|
|
"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"
|
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"
|
2019-03-19 18:47:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 16:03:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2022-11-02 16:18:02 +03:00
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
"golang.org/x/exp/slices"
|
2019-06-25 17:51:53 +03:00
|
|
|
)
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
const clientsUpdatePeriod = 10 * time.Minute
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2020-02-18 19:27:09 +03:00
|
|
|
var webHandlersRegistered = false
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Client contains information about persistent clients.
|
2019-03-19 18:47:22 +03:00
|
|
|
type Client struct {
|
2021-05-06 16:41:33 +03:00
|
|
|
// upstreamConfig is the custom upstream config for this client. If
|
|
|
|
// it's nil, it has not been initialized yet. If it's non-nil and
|
|
|
|
// empty, there are no valid upstreams. If it's non-nil and non-empty,
|
|
|
|
// these upstream must be used.
|
|
|
|
upstreamConfig *proxy.UpstreamConfig
|
2019-07-18 12:27:10 +03:00
|
|
|
|
2021-05-06 16:41:33 +03:00
|
|
|
Name string
|
2019-11-06 17:24:15 +03:00
|
|
|
|
2021-05-06 16:41:33 +03:00
|
|
|
IDs []string
|
|
|
|
Tags []string
|
|
|
|
BlockedServices []string
|
|
|
|
Upstreams []string
|
2020-05-13 20:31:43 +03:00
|
|
|
|
2021-05-06 16:41:33 +03:00
|
|
|
UseOwnSettings bool
|
|
|
|
FilteringEnabled bool
|
|
|
|
SafeSearchEnabled bool
|
|
|
|
SafeBrowsingEnabled bool
|
|
|
|
ParentalEnabled bool
|
|
|
|
UseOwnBlockedServices bool
|
2019-03-19 18:47:22 +03:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// closeUpstreams closes the client-specific upstream config of c if any.
|
|
|
|
func (c *Client) closeUpstreams() (err error) {
|
|
|
|
if c.upstreamConfig != nil {
|
|
|
|
err = c.upstreamConfig.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("closing upstreams of client %q: %w", c.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
type clientSource uint
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
// Clients information sources. The order determines the priority.
|
2019-04-26 15:10:29 +03:00
|
|
|
const (
|
2023-02-15 16:53:29 +03:00
|
|
|
ClientSourceNone clientSource = iota
|
|
|
|
ClientSourceWHOIS
|
2021-01-27 18:32:13 +03:00
|
|
|
ClientSourceARP
|
2022-03-22 15:21:03 +03:00
|
|
|
ClientSourceRDNS
|
2021-06-10 14:54:47 +03:00
|
|
|
ClientSourceDHCP
|
2021-01-27 18:32:13 +03:00
|
|
|
ClientSourceHostsFile
|
2023-02-15 16:53:29 +03:00
|
|
|
ClientSourcePersistent
|
2019-04-26 15:10:29 +03:00
|
|
|
)
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
// type check
|
2022-08-04 19:05:28 +03:00
|
|
|
var _ fmt.Stringer = clientSource(0)
|
|
|
|
|
|
|
|
// String returns a human-readable name of cs.
|
|
|
|
func (cs clientSource) String() (s string) {
|
|
|
|
switch cs {
|
|
|
|
case ClientSourceWHOIS:
|
|
|
|
return "WHOIS"
|
|
|
|
case ClientSourceARP:
|
|
|
|
return "ARP"
|
|
|
|
case ClientSourceRDNS:
|
|
|
|
return "rDNS"
|
|
|
|
case ClientSourceDHCP:
|
|
|
|
return "DHCP"
|
|
|
|
case ClientSourceHostsFile:
|
|
|
|
return "etc/hosts"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
// type check
|
2022-08-04 19:05:28 +03:00
|
|
|
var _ encoding.TextMarshaler = clientSource(0)
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler for the clientSource.
|
|
|
|
func (cs clientSource) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(cs.String()), nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
// clientSourceConf is used to configure where the runtime clients will be
|
|
|
|
// obtained from.
|
|
|
|
type clientSourcesConf struct {
|
|
|
|
WHOIS bool `yaml:"whois"`
|
|
|
|
ARP bool `yaml:"arp"`
|
|
|
|
RDNS bool `yaml:"rdns"`
|
|
|
|
DHCP bool `yaml:"dhcp"`
|
|
|
|
HostsFile bool `yaml:"hosts"`
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
// RuntimeClient information
|
|
|
|
type RuntimeClient struct {
|
2021-06-18 18:13:36 +03:00
|
|
|
WHOISInfo *RuntimeClientWHOISInfo
|
2019-09-18 18:44:27 +03:00
|
|
|
Host string
|
|
|
|
Source clientSource
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
// RuntimeClientWHOISInfo is the filtered WHOIS data for a runtime client.
|
|
|
|
type RuntimeClientWHOISInfo struct {
|
2021-04-02 17:30:39 +03:00
|
|
|
City string `json:"city,omitempty"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
|
|
Orgname string `json:"orgname,omitempty"`
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientsContainer struct {
|
2021-01-27 18:32:13 +03:00
|
|
|
// TODO(a.garipov): Perhaps use a number of separate indices for
|
2022-11-23 16:52:05 +03:00
|
|
|
// different types (string, netip.Addr, and so on).
|
2021-06-29 15:53:28 +03:00
|
|
|
list map[string]*Client // name -> client
|
|
|
|
idIndex map[string]*Client // ID -> client
|
|
|
|
|
|
|
|
// ipToRC is the IP address to *RuntimeClient map.
|
2022-11-02 16:18:02 +03:00
|
|
|
ipToRC map[netip.Addr]*RuntimeClient
|
2021-06-29 15:53:28 +03:00
|
|
|
|
|
|
|
lock sync.Mutex
|
2019-12-23 19:31:27 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
allTags *stringutil.Set
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2019-12-23 19:31:27 +03:00
|
|
|
// dhcpServer is used for looking up clients IP addresses by MAC addresses
|
2022-09-14 16:36:29 +03:00
|
|
|
dhcpServer dhcpd.Interface
|
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
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
// arpdb stores the neighbors retrieved from ARP.
|
|
|
|
arpdb aghnet.ARPDB
|
|
|
|
|
2019-11-29 17:35:26 +03:00
|
|
|
testing bool // if TRUE, this object is used for internal tests
|
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,
|
2022-09-14 16:36:29 +03:00
|
|
|
dhcpServer dhcpd.Interface,
|
2021-10-14 19:39:21 +03:00
|
|
|
etcHosts *aghnet.HostsContainer,
|
2022-06-02 17:55:48 +03:00
|
|
|
arpdb aghnet.ARPDB,
|
2021-04-12 18:31:45 +03:00
|
|
|
) {
|
2019-04-26 15:10:29 +03:00
|
|
|
if clients.list != nil {
|
|
|
|
log.Fatal("clients.list != nil")
|
|
|
|
}
|
|
|
|
clients.list = make(map[string]*Client)
|
2019-09-26 16:40:52 +03:00
|
|
|
clients.idIndex = make(map[string]*Client)
|
2022-11-02 16:18:02 +03:00
|
|
|
clients.ipToRC = map[netip.Addr]*RuntimeClient{}
|
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
|
|
|
|
2019-11-22 14:21:08 +03:00
|
|
|
clients.dhcpServer = dhcpServer
|
2021-04-14 19:18:48 +03:00
|
|
|
clients.etcHosts = etcHosts
|
2022-06-02 17:55:48 +03:00
|
|
|
clients.arpdb = arpdb
|
2019-09-26 16:40:52 +03:00
|
|
|
clients.addFromConfig(objects)
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
if clients.testing {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.updateFromDHCP(true)
|
|
|
|
if clients.dhcpServer != nil {
|
|
|
|
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
if clients.etcHosts != nil {
|
|
|
|
go clients.handleHostsUpdates()
|
|
|
|
}
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clients *clientsContainer) handleHostsUpdates() {
|
2022-06-02 17:55:48 +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
|
|
|
|
2020-02-18 19:27:09 +03:00
|
|
|
// Start - start the module
|
|
|
|
func (clients *clientsContainer) Start() {
|
|
|
|
if !clients.testing {
|
|
|
|
if !webHandlersRegistered {
|
|
|
|
webHandlersRegistered = true
|
|
|
|
clients.registerWebHandlers()
|
|
|
|
}
|
|
|
|
go clients.periodicUpdate()
|
2019-11-29 17:35:26 +03:00
|
|
|
}
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
// Reload reloads runtime clients.
|
2020-02-18 14:49:50 +03:00
|
|
|
func (clients *clientsContainer) Reload() {
|
2022-06-02 17:55:48 +03:00
|
|
|
if clients.arpdb != nil {
|
|
|
|
clients.addFromSystemARP()
|
|
|
|
}
|
2020-02-18 14:49:50 +03:00
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
type clientObject struct {
|
2021-09-13 20:16:06 +03:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
|
|
|
Tags []string `yaml:"tags"`
|
|
|
|
IDs []string `yaml:"ids"`
|
|
|
|
BlockedServices []string `yaml:"blocked_services"`
|
|
|
|
Upstreams []string `yaml:"upstreams"`
|
|
|
|
|
|
|
|
UseGlobalSettings bool `yaml:"use_global_settings"`
|
|
|
|
FilteringEnabled bool `yaml:"filtering_enabled"`
|
|
|
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
|
|
|
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
|
|
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
|
|
|
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2021-12-27 19:40:39 +03:00
|
|
|
// addFromConfig initializes the clients container with objects from the
|
2021-12-13 15:18:21 +03:00
|
|
|
// configuration file.
|
|
|
|
func (clients *clientsContainer) addFromConfig(objects []*clientObject) {
|
|
|
|
for _, o := range objects {
|
2021-01-27 18:32:13 +03:00
|
|
|
cli := &Client{
|
2021-12-13 15:18:21 +03:00
|
|
|
Name: o.Name,
|
2019-09-26 16:40:52 +03:00
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
IDs: o.IDs,
|
|
|
|
Upstreams: o.Upstreams,
|
2019-11-06 17:24:15 +03:00
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
UseOwnSettings: !o.UseGlobalSettings,
|
|
|
|
FilteringEnabled: o.FilteringEnabled,
|
|
|
|
ParentalEnabled: o.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: o.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: o.SafeBrowsingEnabled,
|
|
|
|
UseOwnBlockedServices: !o.UseGlobalBlockedServices,
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
for _, s := range o.BlockedServices {
|
|
|
|
if filtering.BlockedSvcKnown(s) {
|
|
|
|
cli.BlockedServices = append(cli.BlockedServices, s)
|
|
|
|
} else {
|
|
|
|
log.Info("clients: skipping unknown blocked service %q", s)
|
2020-03-02 18:51:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:18:21 +03:00
|
|
|
for _, t := range o.Tags {
|
|
|
|
if clients.allTags.Has(t) {
|
|
|
|
cli.Tags = append(cli.Tags, t)
|
|
|
|
} else {
|
|
|
|
log.Info("clients: skipping unknown tag %q", t)
|
2020-01-28 14:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-13 15:18:21 +03:00
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
sort.Strings(cli.Tags)
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
_, err := clients.Add(cli)
|
|
|
|
if err != nil {
|
2021-12-13 15:18:21 +03:00
|
|
|
log.Error("clients: adding clients %s: %s", cli.Name, err)
|
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,
|
|
|
|
|
|
|
|
Tags: stringutil.CloneSlice(cli.Tags),
|
|
|
|
IDs: stringutil.CloneSlice(cli.IDs),
|
|
|
|
BlockedServices: stringutil.CloneSlice(cli.BlockedServices),
|
|
|
|
Upstreams: stringutil.CloneSlice(cli.Upstreams),
|
|
|
|
|
2019-12-11 12:38:58 +03:00
|
|
|
UseGlobalSettings: !cli.UseOwnSettings,
|
|
|
|
FilteringEnabled: cli.FilteringEnabled,
|
|
|
|
ParentalEnabled: cli.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: cli.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: cli.SafeBrowsingEnabled,
|
2019-09-26 16:40:52 +03:00
|
|
|
UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
|
|
|
|
}
|
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.
|
2021-12-13 15:18:21 +03:00
|
|
|
sort.Slice(objs, func(i, j int) bool { return objs[i].Name < objs[j].Name })
|
|
|
|
|
|
|
|
return objs
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
func (clients *clientsContainer) periodicUpdate() {
|
2022-06-02 17:55:48 +03:00
|
|
|
defer log.OnPanic("clients container")
|
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
for {
|
2020-02-18 14:49:50 +03:00
|
|
|
clients.Reload()
|
2019-06-25 17:51:53 +03:00
|
|
|
time.Sleep(clientsUpdatePeriod)
|
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-30 10:25:02 +03:00
|
|
|
func (clients *clientsContainer) onDHCPLeaseChanged(flags int) {
|
|
|
|
switch flags {
|
|
|
|
case dhcpd.LeaseChangedAdded,
|
|
|
|
dhcpd.LeaseChangedAddedStatic,
|
|
|
|
dhcpd.LeaseChangedRemovedStatic:
|
2021-04-15 17:52:53 +03:00
|
|
|
clients.updateFromDHCP(true)
|
|
|
|
case dhcpd.LeaseChangedRemovedAll:
|
|
|
|
clients.updateFromDHCP(false)
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
// clientSource checks if client with this IP address already exists and returns
|
|
|
|
// the source which updated it last. It returns [ClientSourceNone] if the
|
|
|
|
// client doesn't exist.
|
|
|
|
func (clients *clientsContainer) clientSource(ip netip.Addr) (src clientSource) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
_, ok := clients.findLocked(ip.String())
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
2023-02-15 16:53:29 +03:00
|
|
|
return ClientSourcePersistent
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
rc, ok := clients.ipToRC[ip]
|
2019-09-18 18:44:27 +03:00
|
|
|
if !ok {
|
2023-02-15 16:53:29 +03:00
|
|
|
return ClientSourceNone
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2023-02-15 16:53:29 +03:00
|
|
|
return rc.Source
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
func toQueryLogWHOIS(wi *RuntimeClientWHOISInfo) (cw *querylog.ClientWHOIS) {
|
2021-04-02 17:30:39 +03:00
|
|
|
if wi == nil {
|
2021-06-18 18:13:36 +03:00
|
|
|
return &querylog.ClientWHOIS{}
|
2021-04-02 17:30:39 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
return &querylog.ClientWHOIS{
|
2021-04-02 17:30:39 +03:00
|
|
|
City: wi.City,
|
|
|
|
Country: wi.Country,
|
|
|
|
Orgname: wi.Orgname,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:16:06 +03:00
|
|
|
// findMultiple is a wrapper around 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-23 16:52:05 +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-23 16:52:05 +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 {
|
|
|
|
c.WHOIS = &querylog.ClientWHOIS{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-10 17:57:09 +03:00
|
|
|
client, ok := clients.Find(id)
|
|
|
|
if ok {
|
2021-04-02 17:30:39 +03:00
|
|
|
return &querylog.Client{
|
2021-09-13 20:16:06 +03:00
|
|
|
Name: client.Name,
|
|
|
|
}, false
|
2021-09-10 17:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var rc *RuntimeClient
|
2022-11-02 16:18:02 +03:00
|
|
|
rc, ok = clients.findRuntimeClient(ip)
|
2021-09-10 17:57:09 +03:00
|
|
|
if ok {
|
|
|
|
return &querylog.Client{
|
2021-09-13 20:16:06 +03:00
|
|
|
Name: rc.Host,
|
|
|
|
WHOIS: toQueryLogWHOIS(rc.WHOISInfo),
|
|
|
|
}, 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
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
func (clients *clientsContainer) Find(id string) (c *Client, 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
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
c.IDs = stringutil.CloneSlice(c.IDs)
|
|
|
|
c.Tags = stringutil.CloneSlice(c.Tags)
|
|
|
|
c.BlockedServices = stringutil.CloneSlice(c.BlockedServices)
|
|
|
|
c.Upstreams = stringutil.CloneSlice(c.Upstreams)
|
2022-08-04 19:05:28 +03:00
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
return c, true
|
2019-12-23 16:27:24 +03:00
|
|
|
}
|
|
|
|
|
2021-05-28 13:02:59 +03:00
|
|
|
// findUpstreams returns upstreams configured for the client, identified either
|
|
|
|
// by its IP address or its ClientID. upsConf is nil if the client isn't found
|
|
|
|
// or if the client has no custom upstreams.
|
|
|
|
func (clients *clientsContainer) findUpstreams(
|
|
|
|
id string,
|
|
|
|
) (upsConf *proxy.UpstreamConfig, 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
|
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
|
|
|
}
|
|
|
|
|
2021-05-28 13:02:59 +03:00
|
|
|
if c.upstreamConfig != nil {
|
|
|
|
return c.upstreamConfig, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
var conf *proxy.UpstreamConfig
|
2021-05-28 13:02:59 +03:00
|
|
|
conf, err = proxy.ParseUpstreamsConfig(
|
|
|
|
upstreams,
|
2021-06-29 15:53:28 +03:00
|
|
|
&upstream.Options{
|
2022-10-03 18:52:20 +03:00
|
|
|
Bootstrap: config.DNS.BootstrapDNS,
|
|
|
|
Timeout: config.DNS.UpstreamTimeout.Duration,
|
|
|
|
HTTPVersions: dnsforward.UpstreamHTTPVersions(config.DNS.UseHTTP3Upstreams),
|
2021-05-28 13:02:59 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
2020-05-13 20:31:43 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// findLocked searches for a client by its ID. For internal use only.
|
|
|
|
func (clients *clientsContainer) findLocked(id string) (c *Client, ok bool) {
|
|
|
|
c, ok = clients.idIndex[id]
|
|
|
|
if ok {
|
|
|
|
return c, true
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 16:52:05 +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 {
|
2019-09-26 16:40:52 +03:00
|
|
|
for _, id := range c.IDs {
|
2022-11-23 16:52:05 +03:00
|
|
|
var n netip.Prefix
|
|
|
|
n, err = netip.ParsePrefix(id)
|
2019-04-26 15:10:29 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
if n.Contains(ip) {
|
2021-01-27 18:32:13 +03:00
|
|
|
return c, true
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:21:08 +03:00
|
|
|
if clients.dhcpServer == nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-11-22 14:21:08 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
macFound := clients.dhcpServer.FindMACbyIP(ip.AsSlice())
|
2019-09-26 16:40:52 +03:00
|
|
|
if macFound == 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 {
|
|
|
|
for _, id := range c.IDs {
|
2022-11-23 16:52:05 +03:00
|
|
|
var mac net.HardwareAddr
|
|
|
|
mac, err = net.ParseMAC(id)
|
2019-09-26 16:40:52 +03:00
|
|
|
if err != nil {
|
2019-04-26 15:10:29 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
if bytes.Equal(mac, macFound) {
|
2021-01-27 18:32:13 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// findRuntimeClient finds a runtime client by their IP.
|
2022-11-23 16:52:05 +03:00
|
|
|
func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *RuntimeClient, ok bool) {
|
|
|
|
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-23 16:52:05 +03:00
|
|
|
rc, ok = clients.ipToRC[ip]
|
|
|
|
|
|
|
|
return rc, ok
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// check validates the client.
|
|
|
|
func (clients *clientsContainer) check(c *Client) (err error) {
|
|
|
|
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")
|
2021-01-27 18:32:13 +03:00
|
|
|
case len(c.IDs) == 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
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
for i, id := range c.IDs {
|
2021-01-27 18:32:13 +03:00
|
|
|
// Normalize structured data.
|
2022-11-23 16:52:05 +03:00
|
|
|
var (
|
|
|
|
ip netip.Addr
|
|
|
|
n netip.Prefix
|
|
|
|
mac net.HardwareAddr
|
|
|
|
)
|
|
|
|
|
|
|
|
if ip, err = netip.ParseAddr(id); err == nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
c.IDs[i] = ip.String()
|
2022-11-23 16:52:05 +03:00
|
|
|
} else if n, err = netip.ParsePrefix(id); err == nil {
|
|
|
|
c.IDs[i] = n.String()
|
2021-01-27 18:32:13 +03:00
|
|
|
} else if mac, err = net.ParseMAC(id); err == nil {
|
|
|
|
c.IDs[i] = mac.String()
|
|
|
|
} else if err = dnsforward.ValidateClientID(id); err == nil {
|
2022-05-04 21:01:41 +03:00
|
|
|
c.IDs[i] = strings.ToLower(id)
|
2021-01-27 18:32:13 +03:00
|
|
|
} else {
|
2022-02-10 15:42:59 +03:00
|
|
|
return fmt.Errorf("invalid clientid at index %d: %q", i, id)
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 17:24:15 +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
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
sort.Strings(c.Tags)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Add adds a new client object. ok is false if such client already exists or
|
|
|
|
// if an error occurred.
|
|
|
|
func (clients *clientsContainer) Add(c *Client) (ok bool, err error) {
|
|
|
|
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
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 18:32:13 +03:00
|
|
|
var c2 *Client
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs, len(clients.list))
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Del removes a client. ok is false if there is no such client.
|
|
|
|
func (clients *clientsContainer) Del(name string) (ok bool) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
var c *Client
|
|
|
|
c, ok = clients.list[name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
if err := c.closeUpstreams(); err != nil {
|
|
|
|
log.Error("client container: removing client %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
// update Name index
|
2019-04-26 15:10:29 +03:00
|
|
|
delete(clients.list, name)
|
2019-09-26 16:40:52 +03:00
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// equalStringSlices returns true if the slices are equal.
|
|
|
|
func equalStringSlices(a, b []string) (ok bool) {
|
2019-09-26 16:40:52 +03:00
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
for i := range a {
|
2019-09-26 16:40:52 +03:00
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Update updates a client by its name.
|
|
|
|
func (clients *clientsContainer) Update(name string, c *Client) (err error) {
|
|
|
|
err = clients.check(c)
|
2019-04-26 15:10:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
prev, ok := clients.list[name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if !ok {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("client not found")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:32:08 +03:00
|
|
|
// First, check the name index.
|
2021-01-27 18:32:13 +03:00
|
|
|
if prev.Name != c.Name {
|
2019-04-26 15:10:29 +03:00
|
|
|
_, ok = clients.list[c.Name]
|
|
|
|
if ok {
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error("client already exists")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:32:08 +03:00
|
|
|
// Second, check the IP index.
|
2021-01-27 18:32:13 +03:00
|
|
|
if !equalStringSlices(prev.IDs, c.IDs) {
|
2019-09-26 16:40:52 +03:00
|
|
|
for _, id := range c.IDs {
|
2021-03-12 14:32:08 +03:00
|
|
|
c2, ok2 := clients.idIndex[id]
|
|
|
|
if ok2 && c2 != prev {
|
|
|
|
return fmt.Errorf("another client uses the same id (%q): %q", id, c2.Name)
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// Update ID index.
|
2021-01-27 18:32:13 +03:00
|
|
|
for _, id := range prev.IDs {
|
2019-09-26 16:40:52 +03:00
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.idIndex[id] = prev
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// Update name index.
|
2021-01-27 18:32:13 +03:00
|
|
|
if prev.Name != c.Name {
|
|
|
|
delete(clients.list, prev.Name)
|
|
|
|
clients.list[c.Name] = prev
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// Update upstreams cache.
|
|
|
|
err = c.closeUpstreams()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-23 19:31:27 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
*prev = *c
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// setWHOISInfo sets the WHOIS information for a client.
|
2022-11-23 16:52:05 +03:00
|
|
|
func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *RuntimeClientWHOISInfo) {
|
2019-09-18 18:44:27 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
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)
|
2019-09-23 20:41:14 +03:00
|
|
|
return
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
rc, ok := clients.ipToRC[ip]
|
2019-09-18 18:44:27 +03:00
|
|
|
if ok {
|
2021-06-18 18:13:36 +03:00
|
|
|
rc.WHOISInfo = wi
|
2021-04-02 17:30:39 +03:00
|
|
|
log.Debug("clients: set whois info for runtime client %s: %+v", rc.Host, wi)
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-09-23 20:41:14 +03:00
|
|
|
return
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
// Create a RuntimeClient implicitly so that we don't do this check
|
|
|
|
// again.
|
|
|
|
rc = &RuntimeClient{
|
2019-09-18 18:44:27 +03:00
|
|
|
Source: ClientSourceWHOIS,
|
|
|
|
}
|
2021-04-02 17:30:39 +03:00
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
rc.WHOISInfo = wi
|
2021-06-29 15:53:28 +03:00
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
clients.ipToRC[ip] = rc
|
2021-04-02 17:30:39 +03:00
|
|
|
|
|
|
|
log.Debug("clients: set whois info for runtime client with ip %s: %+v", ip, wi)
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-10-14 19:39:21 +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.
|
2022-11-23 16:52:05 +03:00
|
|
|
func (clients *clientsContainer) AddHost(
|
|
|
|
ip netip.Addr,
|
|
|
|
host string,
|
|
|
|
src clientSource,
|
|
|
|
) (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-23 16:52:05 +03:00
|
|
|
return clients.addHostLocked(ip, host, src)
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2022-11-02 16:18:02 +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,
|
|
|
|
src clientSource,
|
|
|
|
) (ok bool) {
|
|
|
|
rc, ok := clients.ipToRC[ip]
|
2020-12-07 16:04:53 +03:00
|
|
|
if ok {
|
2021-04-02 17:30:39 +03:00
|
|
|
if rc.Source > src {
|
2020-12-07 16:04:53 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
rc.Host = host
|
2021-04-02 17:30:39 +03:00
|
|
|
rc.Source = src
|
2019-10-11 16:58:10 +03:00
|
|
|
} else {
|
2021-04-02 17:30:39 +03:00
|
|
|
rc = &RuntimeClient{
|
|
|
|
Host: host,
|
|
|
|
Source: src,
|
2021-06-18 18:13:36 +03:00
|
|
|
WHOISInfo: &RuntimeClientWHOISInfo{},
|
2019-10-11 16:58:10 +03:00
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
clients.ipToRC[ip] = rc
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
log.Debug("clients: added %s -> %q [%d]", ip, 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.
|
|
|
|
func (clients *clientsContainer) rmHostsBySrc(src clientSource) {
|
2020-01-30 10:25:02 +03:00
|
|
|
n := 0
|
2022-11-02 16:18:02 +03:00
|
|
|
for ip, rc := range clients.ipToRC {
|
2021-06-29 15:53:28 +03:00
|
|
|
if rc.Source == src {
|
2022-11-02 16:18:02 +03:00
|
|
|
delete(clients.ipToRC, ip)
|
2020-01-30 10:25:02 +03:00
|
|
|
n++
|
|
|
|
}
|
2022-11-02 16:18:02 +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.
|
2022-11-02 16:18:02 +03:00
|
|
|
func (clients *clientsContainer) addFromHostsFile(hosts aghnet.HostsRecords) {
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2020-11-06 17:34:40 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.rmHostsBySrc(ClientSourceHostsFile)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
n := 0
|
2022-11-02 16:18:02 +03:00
|
|
|
for ip, rec := range hosts {
|
2022-07-07 19:33:32 +03:00
|
|
|
clients.addHostLocked(ip, rec.Canonical, ClientSourceHostsFile)
|
|
|
|
n++
|
2022-11-02 16:18:02 +03:00
|
|
|
}
|
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() {
|
2022-06-02 17:55:48 +03:00
|
|
|
if err := clients.arpdb.Refresh(); err != nil {
|
|
|
|
log.Error("refreshing arp container: %s", err)
|
|
|
|
|
|
|
|
clients.arpdb = aghnet.EmptyARPDB{}
|
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
ns := clients.arpdb.Neighbors()
|
|
|
|
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
|
|
|
|
|
|
|
clients.rmHostsBySrc(ClientSourceARP)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
added := 0
|
|
|
|
for _, n := range ns {
|
|
|
|
if clients.addHostLocked(n.IP, n.Name, ClientSourceARP) {
|
|
|
|
added++
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:55:48 +03:00
|
|
|
log.Debug("clients: added %d client aliases from arp neighborhood", added)
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
2021-04-15 17:52:53 +03:00
|
|
|
// updateFromDHCP adds the clients that have a non-empty hostname from the DHCP
|
2021-01-27 18:32:13 +03:00
|
|
|
// server.
|
2021-04-15 17:52:53 +03:00
|
|
|
func (clients *clientsContainer) updateFromDHCP(add bool) {
|
2022-06-02 17:55:48 +03:00
|
|
|
if clients.dhcpServer == nil || !config.Clients.Sources.DHCP {
|
2019-11-22 14:21:08 +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
|
|
|
clients.rmHostsBySrc(ClientSourceDHCP)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2021-04-15 17:52:53 +03:00
|
|
|
if !add {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:25:02 +03:00
|
|
|
leases := clients.dhcpServer.Leases(dhcpd.LeasesAll)
|
2019-10-16 12:03:51 +03:00
|
|
|
n := 0
|
2019-09-12 16:15:10 +03:00
|
|
|
for _, l := range leases {
|
2021-01-27 18:32:13 +03:00
|
|
|
if l.Hostname == "" {
|
2019-09-12 16:15:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
// TODO(a.garipov): Remove once we switch to netip.Addr more fully.
|
|
|
|
ipAddr, err := netutil.IPToAddrNoMapped(l.IP)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("clients: bad client ip %v from dhcp: %s", l.IP, err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := clients.addHostLocked(ipAddr, l.Hostname, ClientSourceDHCP)
|
2019-10-16 12:03:51 +03:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-09-12 16:15:10 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
log.Debug("clients: added %d client aliases from dhcp", n)
|
2019-09-12 16:15:10 +03:00
|
|
|
}
|
2022-11-02 16:18:02 +03:00
|
|
|
|
|
|
|
// close gracefully closes all the client-specific upstream configurations of
|
|
|
|
// the persistent clients.
|
|
|
|
func (clients *clientsContainer) close() (err error) {
|
|
|
|
persistent := maps.Values(clients.list)
|
|
|
|
slices.SortFunc(persistent, func(a, b *Client) (less bool) { return a.Name < b.Name })
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
for _, cli := range persistent {
|
|
|
|
if err = cli.closeUpstreams(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.List("closing client specific upstreams", errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|