2024-06-26 12:30:02 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-09-30 12:17:42 +01:00
|
|
|
"context"
|
2024-06-26 12:30:02 +01:00
|
|
|
"fmt"
|
2024-10-10 18:59:46 +01:00
|
|
|
"log/slog"
|
2024-06-26 12:30:02 +01:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-08-21 14:27:28 +01:00
|
|
|
"slices"
|
2024-06-26 12:30:02 +01:00
|
|
|
"sync"
|
2024-09-30 12:17:42 +01:00
|
|
|
"time"
|
2024-06-26 12:30:02 +01:00
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
2024-06-26 12:30:02 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-09-30 12:17:42 +01:00
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
2024-06-26 12:30:02 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2024-10-10 18:59:46 +01:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2024-06-26 12:30:02 +01:00
|
|
|
)
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
// allowedTags is the list of available client tags.
|
|
|
|
var allowedTags = []string{
|
|
|
|
"device_audio",
|
|
|
|
"device_camera",
|
|
|
|
"device_gameconsole",
|
|
|
|
"device_laptop",
|
|
|
|
"device_nas", // Network-attached Storage
|
|
|
|
"device_other",
|
|
|
|
"device_pc",
|
|
|
|
"device_phone",
|
|
|
|
"device_printer",
|
|
|
|
"device_securityalarm",
|
|
|
|
"device_tablet",
|
|
|
|
"device_tv",
|
|
|
|
|
|
|
|
"os_android",
|
|
|
|
"os_ios",
|
|
|
|
"os_linux",
|
|
|
|
"os_macos",
|
|
|
|
"os_other",
|
|
|
|
"os_windows",
|
|
|
|
|
|
|
|
"user_admin",
|
|
|
|
"user_child",
|
|
|
|
"user_regular",
|
|
|
|
}
|
|
|
|
|
|
|
|
// DHCP is an interface for accessing DHCP lease data the [Storage] 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. host will be empty if there is no such client, due to an
|
|
|
|
// assumption that a DHCP client must always have a hostname.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmptyDHCP is the empty [DHCP] implementation that does nothing.
|
|
|
|
type EmptyDHCP struct{}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ DHCP = EmptyDHCP{}
|
|
|
|
|
|
|
|
// Leases implements the [DHCP] interface for emptyDHCP.
|
|
|
|
func (EmptyDHCP) Leases() (leases []*dhcpsvc.Lease) { return nil }
|
|
|
|
|
|
|
|
// HostByIP implements the [DHCP] interface for emptyDHCP.
|
|
|
|
func (EmptyDHCP) HostByIP(_ netip.Addr) (host string) { return "" }
|
|
|
|
|
|
|
|
// MACByIP implements the [DHCP] interface for emptyDHCP.
|
|
|
|
func (EmptyDHCP) MACByIP(_ netip.Addr) (mac net.HardwareAddr) { return nil }
|
|
|
|
|
|
|
|
// HostsContainer is an interface for receiving updates to the system hosts
|
|
|
|
// file.
|
|
|
|
type HostsContainer interface {
|
|
|
|
Upd() (updates <-chan *hostsfile.DefaultStorage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageConfig is the client storage configuration structure.
|
|
|
|
type StorageConfig struct {
|
2024-10-14 13:03:08 +01:00
|
|
|
// Logger is used for logging the operation of the client storage. It must
|
|
|
|
// not be nil.
|
2024-10-10 18:59:46 +01:00
|
|
|
Logger *slog.Logger
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
// DHCP is used to match IPs against MACs of persistent clients and update
|
|
|
|
// [SourceDHCP] runtime client information. It must not be nil.
|
|
|
|
DHCP DHCP
|
|
|
|
|
|
|
|
// EtcHosts is used to update [SourceHostsFile] runtime client information.
|
|
|
|
EtcHosts HostsContainer
|
|
|
|
|
|
|
|
// ARPDB is used to update [SourceARP] runtime client information.
|
|
|
|
ARPDB arpdb.Interface
|
|
|
|
|
|
|
|
// InitialClients is a list of persistent clients parsed from the
|
|
|
|
// configuration file. Each client must not be nil.
|
|
|
|
InitialClients []*Persistent
|
|
|
|
|
|
|
|
// ARPClientsUpdatePeriod defines how often [SourceARP] runtime client
|
|
|
|
// information is updated.
|
|
|
|
ARPClientsUpdatePeriod time.Duration
|
|
|
|
|
|
|
|
// RuntimeSourceDHCP specifies whether to update [SourceDHCP] information
|
|
|
|
// of runtime clients.
|
|
|
|
RuntimeSourceDHCP bool
|
2024-07-01 15:34:47 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 12:30:02 +01:00
|
|
|
// Storage contains information about persistent and runtime clients.
|
|
|
|
type Storage struct {
|
2024-10-14 13:03:08 +01:00
|
|
|
// logger is used for logging the operation of the client storage. It must
|
|
|
|
// not be nil.
|
2024-10-10 18:59:46 +01:00
|
|
|
logger *slog.Logger
|
|
|
|
|
2024-06-26 12:30:02 +01:00
|
|
|
// mu protects indexes of persistent and runtime clients.
|
|
|
|
mu *sync.Mutex
|
|
|
|
|
|
|
|
// index contains information about persistent clients.
|
2024-07-01 15:34:47 +01:00
|
|
|
index *index
|
2024-06-26 12:30:02 +01:00
|
|
|
|
|
|
|
// runtimeIndex contains information about runtime clients.
|
2024-09-30 12:17:42 +01:00
|
|
|
runtimeIndex *runtimeIndex
|
|
|
|
|
|
|
|
// dhcp is used to update [SourceDHCP] runtime client information.
|
|
|
|
dhcp DHCP
|
|
|
|
|
|
|
|
// etcHosts is used to update [SourceHostsFile] runtime client information.
|
|
|
|
etcHosts HostsContainer
|
|
|
|
|
|
|
|
// arpDB is used to update [SourceARP] runtime client information.
|
|
|
|
arpDB arpdb.Interface
|
|
|
|
|
|
|
|
// done is the shutdown signaling channel.
|
|
|
|
done chan struct{}
|
|
|
|
|
|
|
|
// allowedTags is a sorted list of all allowed tags. It must not be
|
|
|
|
// modified after initialization.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use custom type.
|
|
|
|
allowedTags []string
|
|
|
|
|
|
|
|
// arpClientsUpdatePeriod defines how often [SourceARP] runtime client
|
|
|
|
// information is updated. It must be greater than zero.
|
|
|
|
arpClientsUpdatePeriod time.Duration
|
|
|
|
|
|
|
|
// runtimeSourceDHCP specifies whether to update [SourceDHCP] information
|
|
|
|
// of runtime clients.
|
|
|
|
runtimeSourceDHCP bool
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
// NewStorage returns initialized client storage. conf must not be nil.
|
2024-10-10 18:59:46 +01:00
|
|
|
func NewStorage(ctx context.Context, conf *StorageConfig) (s *Storage, err error) {
|
2024-09-30 12:17:42 +01:00
|
|
|
tags := slices.Clone(allowedTags)
|
|
|
|
slices.Sort(tags)
|
|
|
|
|
|
|
|
s = &Storage{
|
2024-10-10 18:59:46 +01:00
|
|
|
logger: conf.Logger,
|
2024-09-30 12:17:42 +01:00
|
|
|
mu: &sync.Mutex{},
|
|
|
|
index: newIndex(),
|
|
|
|
runtimeIndex: newRuntimeIndex(),
|
|
|
|
dhcp: conf.DHCP,
|
|
|
|
etcHosts: conf.EtcHosts,
|
|
|
|
arpDB: conf.ARPDB,
|
|
|
|
done: make(chan struct{}),
|
2024-10-10 18:59:46 +01:00
|
|
|
allowedTags: tags,
|
2024-09-30 12:17:42 +01:00
|
|
|
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
|
|
|
|
runtimeSourceDHCP: conf.RuntimeSourceDHCP,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range conf.InitialClients {
|
2024-10-10 18:59:46 +01:00
|
|
|
err = s.Add(ctx, p)
|
2024-09-30 12:17:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("adding client %q at index %d: %w", p.Name, i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
s.ReloadARP(ctx)
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the goroutines for updating the runtime client information.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Pass context.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) Start(ctx context.Context) (err error) {
|
|
|
|
go s.periodicARPUpdate(ctx)
|
|
|
|
go s.handleHostsUpdates(ctx)
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown gracefully stops the client storage.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Pass context.
|
|
|
|
func (s *Storage) Shutdown(_ context.Context) (err error) {
|
|
|
|
close(s.done)
|
|
|
|
|
|
|
|
return s.closeUpstreams()
|
|
|
|
}
|
|
|
|
|
|
|
|
// periodicARPUpdate periodically reloads runtime clients from ARP. It is
|
|
|
|
// intended to be used as a goroutine.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) periodicARPUpdate(ctx context.Context) {
|
2024-10-14 13:03:08 +01:00
|
|
|
defer slogutil.RecoverAndLog(ctx, s.logger)
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
t := time.NewTicker(s.arpClientsUpdatePeriod)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2024-10-10 18:59:46 +01:00
|
|
|
s.ReloadARP(ctx)
|
2024-09-30 12:17:42 +01:00
|
|
|
case <-s.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadARP reloads runtime clients from ARP, if configured.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) ReloadARP(ctx context.Context) {
|
2024-09-30 12:17:42 +01:00
|
|
|
if s.arpDB != nil {
|
2024-10-10 18:59:46 +01:00
|
|
|
s.addFromSystemARP(ctx)
|
2024-09-30 12:17:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
|
|
|
|
// command.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) addFromSystemARP(ctx context.Context) {
|
2024-09-30 12:17:42 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
if err := s.arpDB.Refresh(); err != nil {
|
|
|
|
s.arpDB = arpdb.Empty{}
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.ErrorContext(ctx, "refreshing arp container", slogutil.KeyError, err)
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := s.arpDB.Neighbors()
|
|
|
|
if len(ns) == 0 {
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(ctx, "refreshing arp container: the update is empty")
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
src := SourceARP
|
|
|
|
s.runtimeIndex.clearSource(src)
|
|
|
|
|
|
|
|
for _, n := range ns {
|
|
|
|
s.runtimeIndex.setInfo(n.IP, src, []string{n.Name})
|
|
|
|
}
|
|
|
|
|
|
|
|
removed := s.runtimeIndex.removeEmpty()
|
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"updating client aliases from arp neighborhood",
|
|
|
|
"added", len(ns),
|
|
|
|
"removed", removed,
|
|
|
|
)
|
2024-09-30 12:17:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleHostsUpdates receives the updates from the hosts container and adds
|
|
|
|
// them to the clients storage. It is intended to be used as a goroutine.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) handleHostsUpdates(ctx context.Context) {
|
2024-09-30 12:17:42 +01:00
|
|
|
if s.etcHosts == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-14 13:03:08 +01:00
|
|
|
defer slogutil.RecoverAndLog(ctx, s.logger)
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case upd, ok := <-s.etcHosts.Upd():
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
s.addFromHostsFile(ctx, upd)
|
2024-09-30 12:17:42 +01:00
|
|
|
case <-s.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addFromHostsFile fills the client-hostname pairing index from the system's
|
|
|
|
// hosts files.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) addFromHostsFile(ctx context.Context, hosts *hostsfile.DefaultStorage) {
|
2024-09-30 12:17:42 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2024-07-01 15:34:47 +01:00
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
src := SourceHostsFile
|
|
|
|
s.runtimeIndex.clearSource(src)
|
|
|
|
|
|
|
|
added := 0
|
|
|
|
hosts.RangeNames(func(addr netip.Addr, names []string) (cont bool) {
|
|
|
|
// 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.
|
|
|
|
s.runtimeIndex.setInfo(addr, src, []string{names[0]})
|
|
|
|
added++
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
removed := s.runtimeIndex.removeEmpty()
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"updating client aliases from system hosts file",
|
|
|
|
"added", added,
|
|
|
|
"removed", removed,
|
|
|
|
)
|
2024-09-30 12:17:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ AddressUpdater = (*Storage)(nil)
|
|
|
|
|
|
|
|
// UpdateAddress implements the [AddressUpdater] interface for *Storage
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) UpdateAddress(ctx context.Context, ip netip.Addr, host string, info *whois.Info) {
|
2024-09-30 12:17:42 +01:00
|
|
|
// Common fast path optimization.
|
|
|
|
if host == "" && info == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
if host != "" {
|
|
|
|
s.runtimeIndex.setInfo(ip, SourceRDNS, []string{host})
|
|
|
|
}
|
|
|
|
|
|
|
|
if info != nil {
|
2024-10-10 18:59:46 +01:00
|
|
|
s.setWHOISInfo(ctx, ip, info)
|
2024-09-30 12:17:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDHCP updates [SourceDHCP] runtime client information.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) UpdateDHCP(ctx context.Context) {
|
2024-09-30 12:17:42 +01:00
|
|
|
if s.dhcp == nil || !s.runtimeSourceDHCP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
src := SourceDHCP
|
|
|
|
s.runtimeIndex.clearSource(src)
|
|
|
|
|
|
|
|
added := 0
|
|
|
|
for _, l := range s.dhcp.Leases() {
|
|
|
|
s.runtimeIndex.setInfo(l.IP, src, []string{l.Hostname})
|
|
|
|
added++
|
|
|
|
}
|
|
|
|
|
|
|
|
removed := s.runtimeIndex.removeEmpty()
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"updating client aliases from dhcp",
|
|
|
|
"added", added,
|
|
|
|
"removed", removed,
|
|
|
|
)
|
2024-09-30 12:17:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// setWHOISInfo sets the WHOIS information for a runtime client.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) setWHOISInfo(ctx context.Context, ip netip.Addr, wi *whois.Info) {
|
2024-09-30 12:17:42 +01:00
|
|
|
_, ok := s.index.findByIP(ip)
|
|
|
|
if ok {
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"persistent client is already created, ignore whois info",
|
|
|
|
"ip", ip,
|
|
|
|
)
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rc := s.runtimeIndex.client(ip)
|
|
|
|
if rc == nil {
|
|
|
|
rc = NewRuntime(ip)
|
|
|
|
s.runtimeIndex.add(rc)
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
2024-09-30 12:17:42 +01:00
|
|
|
|
|
|
|
rc.setWHOIS(wi)
|
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(ctx, "set whois info for runtime client", "ip", ip, "whois", wi)
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add stores persistent client information or returns an error.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) Add(ctx context.Context, p *Persistent) (err error) {
|
2024-06-26 12:30:02 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "adding client: %w") }()
|
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
err = p.validate(ctx, s.logger, s.allowedTags)
|
2024-06-26 12:30:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
err = s.index.clashesUID(p)
|
2024-06-26 12:30:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
err = s.index.clashes(p)
|
2024-06-26 12:30:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
s.index.add(p)
|
2024-06-26 12:30:02 +01:00
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
s.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"client added",
|
|
|
|
"name", p.Name,
|
|
|
|
"ids", p.IDs(),
|
|
|
|
"clients_count", s.index.size(),
|
|
|
|
)
|
2024-06-26 12:30:02 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
// FindByName finds persistent client by name. And returns its shallow copy.
|
|
|
|
func (s *Storage) FindByName(name string) (p *Persistent, ok bool) {
|
2024-06-26 12:30:02 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
p, ok = s.index.findByName(name)
|
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find finds persistent client by string representation of the client ID, IP
|
2024-07-01 15:34:47 +01:00
|
|
|
// address, or MAC. And returns its shallow copy.
|
2024-09-30 12:17:42 +01:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Accept ClientIDData structure instead, which will contain
|
|
|
|
// the parsed IP address, if any.
|
2024-06-26 12:30:02 +01:00
|
|
|
func (s *Storage) Find(id string) (p *Persistent, ok bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
p, ok = s.index.find(id)
|
2024-06-26 12:30:02 +01:00
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
ip, err := netip.ParseAddr(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
foundMAC := s.dhcp.MACByIP(ip)
|
|
|
|
if foundMAC != nil {
|
|
|
|
return s.FindByMAC(foundMAC)
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:30:02 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLoose is like [Storage.Find] but it also tries to find a persistent
|
|
|
|
// client by IP address without zone. It strips the IPv6 zone index from the
|
|
|
|
// stored IP addresses before comparing, because querylog entries don't have it.
|
|
|
|
// See TODO on [querylog.logEntry.IP].
|
|
|
|
//
|
|
|
|
// Note that multiple clients can have the same IP address with different zones.
|
|
|
|
// Therefore, the result of this method is indeterminate.
|
|
|
|
func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
p, ok = s.index.find(id)
|
2024-06-26 12:30:02 +01:00
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
p = s.index.findByIPWithoutZone(ip)
|
2024-06-26 12:30:02 +01:00
|
|
|
if p != nil {
|
|
|
|
return p.ShallowClone(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
// FindByMAC finds persistent client by MAC and returns its shallow copy. s.mu
|
|
|
|
// is expected to be locked.
|
2024-07-01 15:34:47 +01:00
|
|
|
func (s *Storage) FindByMAC(mac net.HardwareAddr) (p *Persistent, ok bool) {
|
|
|
|
p, ok = s.index.findByMAC(mac)
|
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveByName removes persistent client information. ok is false if no such
|
|
|
|
// client exists by that name.
|
|
|
|
func (s *Storage) RemoveByName(name string) (ok bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
p, ok := s.index.findByName(name)
|
2024-06-26 12:30:02 +01:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.CloseUpstreams(); err != nil {
|
|
|
|
log.Error("client storage: removing client %q: %s", p.Name, err)
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
s.index.remove(p)
|
2024-06-26 12:30:02 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update finds the stored persistent client by its name and updates its
|
|
|
|
// information from p.
|
2024-10-10 18:59:46 +01:00
|
|
|
func (s *Storage) Update(ctx context.Context, name string, p *Persistent) (err error) {
|
2024-06-26 12:30:02 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "updating client: %w") }()
|
|
|
|
|
2024-10-10 18:59:46 +01:00
|
|
|
err = p.validate(ctx, s.logger, s.allowedTags)
|
2024-06-26 12:30:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
stored, ok := s.index.findByName(name)
|
2024-06-26 12:30:02 +01:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("client %q is not found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client p has a newly generated UID, so replace it with the stored one.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Remove when frontend starts handling UIDs.
|
|
|
|
p.UID = stored.UID
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
err = s.index.clashes(p)
|
2024-06-26 12:30:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
s.index.remove(stored)
|
|
|
|
s.index.add(p)
|
2024-06-26 12:30:02 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeByName calls f for each persistent client sorted by name, unless cont is
|
|
|
|
// false.
|
|
|
|
func (s *Storage) RangeByName(f func(c *Persistent) (cont bool)) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
s.index.rangeByName(f)
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the number of persistent clients.
|
|
|
|
func (s *Storage) Size() (n int) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
return s.index.size()
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
// closeUpstreams closes upstream configurations of persistent clients.
|
|
|
|
func (s *Storage) closeUpstreams() (err error) {
|
2024-06-26 12:30:02 +01:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 15:34:47 +01:00
|
|
|
return s.index.closeUpstreams()
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientRuntime returns a copy of the saved runtime client by ip. If no such
|
|
|
|
// client exists, returns nil.
|
|
|
|
func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
rc = s.runtimeIndex.client(ip)
|
|
|
|
if rc != nil {
|
|
|
|
return rc.clone()
|
2024-08-21 14:27:28 +01:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
if !s.runtimeSourceDHCP {
|
|
|
|
return nil
|
2024-08-21 14:27:28 +01:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
host := s.dhcp.HostByIP(ip)
|
|
|
|
if host == "" {
|
|
|
|
return nil
|
2024-08-21 14:27:28 +01:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
rc = s.runtimeIndex.setInfo(ip, SourceDHCP, []string{host})
|
2024-08-21 14:27:28 +01:00
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
return rc.clone()
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RangeRuntime calls f for each runtime client in an undefined order.
|
|
|
|
func (s *Storage) RangeRuntime(f func(rc *Runtime) (cont bool)) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
s.runtimeIndex.rangeClients(f)
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:17:42 +01:00
|
|
|
// AllowedTags returns the list of available client tags. tags must not be
|
|
|
|
// modified.
|
|
|
|
func (s *Storage) AllowedTags() (tags []string) {
|
|
|
|
return s.allowedTags
|
2024-06-26 12:30:02 +01:00
|
|
|
}
|