AdGuardHome/internal/client/index.go

359 lines
8.1 KiB
Go
Raw Normal View History

2024-03-12 17:45:11 +03:00
package client
import (
"fmt"
"net"
"net/netip"
2024-05-15 13:34:12 +03:00
"slices"
"strings"
2024-03-12 17:45:11 +03:00
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
2024-05-15 13:34:12 +03:00
"github.com/AdguardTeam/golibs/errors"
"golang.org/x/exp/maps"
2024-03-12 17:45:11 +03:00
)
// macKey contains MAC as byte array of 6, 8, or 20 bytes.
type macKey any
// macToKey converts mac into key of type macKey, which is used as the key of
// the [clientIndex.macToUID]. mac must be valid MAC address.
func macToKey(mac net.HardwareAddr) (key macKey) {
switch len(mac) {
case 6:
return [6]byte(mac)
case 8:
return [8]byte(mac)
case 20:
return [20]byte(mac)
default:
panic(fmt.Errorf("invalid mac address %#v", mac))
}
}
2024-07-03 15:38:37 +03:00
// index stores all information about persistent clients.
type index struct {
2024-05-15 13:34:12 +03:00
// nameToUID maps client name to UID.
nameToUID map[string]UID
2024-03-12 17:45:11 +03:00
// clientIDToUID maps client ID to UID.
clientIDToUID map[string]UID
// ipToUID maps IP address to UID.
ipToUID map[netip.Addr]UID
// macToUID maps MAC address to UID.
macToUID map[macKey]UID
// uidToClient maps UID to the persistent client.
uidToClient map[UID]*Persistent
// subnetToUID maps subnet to UID.
subnetToUID aghalg.SortedMap[netip.Prefix, UID]
}
2024-07-03 15:38:37 +03:00
// newIndex initializes the new instance of client index.
func newIndex() (ci *index) {
return &index{
2024-05-15 13:34:12 +03:00
nameToUID: map[string]UID{},
2024-03-12 17:45:11 +03:00
clientIDToUID: map[string]UID{},
ipToUID: map[netip.Addr]UID{},
subnetToUID: aghalg.NewSortedMap[netip.Prefix, UID](subnetCompare),
macToUID: map[macKey]UID{},
uidToClient: map[UID]*Persistent{},
}
}
2024-07-03 15:38:37 +03:00
// add stores information about a persistent client in the index. c must be
// non-nil, have a UID, and contain at least one identifier.
func (ci *index) add(c *Persistent) {
2024-03-12 17:45:11 +03:00
if (c.UID == UID{}) {
panic("client must contain uid")
}
2024-05-15 13:34:12 +03:00
ci.nameToUID[c.Name] = c.UID
2024-03-12 17:45:11 +03:00
for _, id := range c.ClientIDs {
ci.clientIDToUID[id] = c.UID
}
for _, ip := range c.IPs {
ci.ipToUID[ip] = c.UID
}
for _, pref := range c.Subnets {
ci.subnetToUID.Set(pref, c.UID)
}
for _, mac := range c.MACs {
k := macToKey(mac)
ci.macToUID[k] = c.UID
}
ci.uidToClient[c.UID] = c
}
2024-07-03 15:38:37 +03:00
// clashesUID returns existing persistent client with the same UID as c. Note
2024-05-15 13:34:12 +03:00
// that this is only possible when configuration contains duplicate fields.
2024-07-03 15:38:37 +03:00
func (ci *index) clashesUID(c *Persistent) (err error) {
2024-05-15 13:34:12 +03:00
p, ok := ci.uidToClient[c.UID]
if ok {
return fmt.Errorf("another client %q uses the same uid", p.Name)
}
return nil
}
2024-07-03 15:38:37 +03:00
// clashes returns an error if the index contains a different persistent client
2024-03-12 17:45:11 +03:00
// with at least a single identifier contained by c. c must be non-nil.
2024-07-03 15:38:37 +03:00
func (ci *index) clashes(c *Persistent) (err error) {
2024-05-15 13:34:12 +03:00
if p := ci.clashesName(c); p != nil {
return fmt.Errorf("another client uses the same name %q", p.Name)
}
2024-03-12 17:45:11 +03:00
for _, id := range c.ClientIDs {
existing, ok := ci.clientIDToUID[id]
if ok && existing != c.UID {
p := ci.uidToClient[existing]
2024-05-15 13:34:12 +03:00
return fmt.Errorf("another client %q uses the same ClientID %q", p.Name, id)
2024-03-12 17:45:11 +03:00
}
}
p, ip := ci.clashesIP(c)
if p != nil {
return fmt.Errorf("another client %q uses the same IP %q", p.Name, ip)
}
p, s := ci.clashesSubnet(c)
if p != nil {
return fmt.Errorf("another client %q uses the same subnet %q", p.Name, s)
}
p, mac := ci.clashesMAC(c)
if p != nil {
return fmt.Errorf("another client %q uses the same MAC %q", p.Name, mac)
}
return nil
}
2024-05-15 13:34:12 +03:00
// clashesName returns existing persistent client with the same name as c or
// nil. c must be non-nil.
2024-07-03 15:38:37 +03:00
func (ci *index) clashesName(c *Persistent) (existing *Persistent) {
existing, ok := ci.findByName(c.Name)
2024-05-15 13:34:12 +03:00
if !ok {
return nil
}
if existing.UID != c.UID {
return existing
}
return nil
}
2024-03-12 17:45:11 +03:00
// clashesIP returns a previous client with the same IP address as c. c must be
// non-nil.
2024-07-03 15:38:37 +03:00
func (ci *index) clashesIP(c *Persistent) (p *Persistent, ip netip.Addr) {
2024-03-12 17:45:11 +03:00
for _, ip := range c.IPs {
existing, ok := ci.ipToUID[ip]
if ok && existing != c.UID {
return ci.uidToClient[existing], ip
}
}
return nil, netip.Addr{}
}
// clashesSubnet returns a previous client with the same subnet as c. c must be
// non-nil.
2024-07-03 15:38:37 +03:00
func (ci *index) clashesSubnet(c *Persistent) (p *Persistent, s netip.Prefix) {
2024-03-12 17:45:11 +03:00
for _, s = range c.Subnets {
var existing UID
var ok bool
ci.subnetToUID.Range(func(p netip.Prefix, uid UID) (cont bool) {
if s == p {
existing = uid
ok = true
return false
}
return true
})
if ok && existing != c.UID {
return ci.uidToClient[existing], s
}
}
return nil, netip.Prefix{}
}
// clashesMAC returns a previous client with the same MAC address as c. c must
// be non-nil.
2024-07-03 15:38:37 +03:00
func (ci *index) clashesMAC(c *Persistent) (p *Persistent, mac net.HardwareAddr) {
2024-03-12 17:45:11 +03:00
for _, mac = range c.MACs {
k := macToKey(mac)
existing, ok := ci.macToUID[k]
if ok && existing != c.UID {
return ci.uidToClient[existing], mac
}
}
return nil, nil
}
2024-07-03 15:38:37 +03:00
// find finds persistent client by string representation of the client ID, IP
2024-03-12 17:45:11 +03:00
// address, or MAC.
2024-07-03 15:38:37 +03:00
func (ci *index) find(id string) (c *Persistent, ok bool) {
2024-03-12 17:45:11 +03:00
uid, found := ci.clientIDToUID[id]
if found {
return ci.uidToClient[uid], true
}
ip, err := netip.ParseAddr(id)
if err == nil {
// MAC addresses can be successfully parsed as IP addresses.
c, found = ci.findByIP(ip)
if found {
return c, true
}
}
mac, err := net.ParseMAC(id)
if err == nil {
2024-07-03 15:38:37 +03:00
return ci.findByMAC(mac)
2024-03-12 17:45:11 +03:00
}
return nil, false
}
2024-07-03 15:38:37 +03:00
// findByName finds persistent client by name.
func (ci *index) findByName(name string) (c *Persistent, found bool) {
2024-05-15 13:34:12 +03:00
uid, found := ci.nameToUID[name]
if found {
return ci.uidToClient[uid], true
}
return nil, false
}
// findByIP finds persistent client by IP address.
2024-07-03 15:38:37 +03:00
func (ci *index) findByIP(ip netip.Addr) (c *Persistent, found bool) {
2024-03-12 17:45:11 +03:00
uid, found := ci.ipToUID[ip]
if found {
return ci.uidToClient[uid], true
}
2024-05-15 13:34:12 +03:00
ipWithoutZone := ip.WithZone("")
2024-03-12 17:45:11 +03:00
ci.subnetToUID.Range(func(pref netip.Prefix, id UID) (cont bool) {
2024-05-15 13:34:12 +03:00
// Remove zone before checking because prefixes strip zones.
if pref.Contains(ipWithoutZone) {
2024-03-12 17:45:11 +03:00
uid, found = id, true
return false
}
return true
})
if found {
return ci.uidToClient[uid], true
}
return nil, false
}
2024-07-03 15:38:37 +03:00
// findByMAC finds persistent client by MAC.
func (ci *index) findByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
2024-03-12 17:45:11 +03:00
k := macToKey(mac)
uid, found := ci.macToUID[k]
if found {
return ci.uidToClient[uid], true
}
return nil, false
}
2024-07-03 15:38:37 +03:00
// findByIPWithoutZone finds a persistent client by IP address without zone. It
2024-05-15 13:34:12 +03:00
// 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.
2024-07-03 15:38:37 +03:00
func (ci *index) findByIPWithoutZone(ip netip.Addr) (c *Persistent) {
2024-05-15 13:34:12 +03:00
if (ip == netip.Addr{}) {
return nil
}
for addr, uid := range ci.ipToUID {
if addr.WithZone("") == ip {
return ci.uidToClient[uid]
}
}
return nil
}
2024-07-03 15:38:37 +03:00
// remove removes information about persistent client from the index. c must be
2024-03-12 17:45:11 +03:00
// non-nil.
2024-07-03 15:38:37 +03:00
func (ci *index) remove(c *Persistent) {
2024-05-15 13:34:12 +03:00
delete(ci.nameToUID, c.Name)
2024-03-12 17:45:11 +03:00
for _, id := range c.ClientIDs {
delete(ci.clientIDToUID, id)
}
for _, ip := range c.IPs {
delete(ci.ipToUID, ip)
}
for _, pref := range c.Subnets {
ci.subnetToUID.Del(pref)
}
for _, mac := range c.MACs {
k := macToKey(mac)
delete(ci.macToUID, k)
}
delete(ci.uidToClient, c.UID)
}
2024-05-15 13:34:12 +03:00
2024-07-03 15:38:37 +03:00
// size returns the number of persistent clients.
func (ci *index) size() (n int) {
2024-05-15 13:34:12 +03:00
return len(ci.uidToClient)
}
2024-07-03 15:38:37 +03:00
// rangeByName is like [Index.Range] but sorts the persistent clients by name
2024-05-15 13:34:12 +03:00
// before iterating ensuring a predictable order.
2024-07-03 15:38:37 +03:00
func (ci *index) rangeByName(f func(c *Persistent) (cont bool)) {
2024-05-15 13:34:12 +03:00
cs := maps.Values(ci.uidToClient)
slices.SortFunc(cs, func(a, b *Persistent) (n int) {
return strings.Compare(a.Name, b.Name)
})
for _, c := range cs {
if !f(c) {
break
}
}
}
2024-07-03 15:38:37 +03:00
// closeUpstreams closes upstream configurations of persistent clients.
func (ci *index) closeUpstreams() (err error) {
2024-05-15 13:34:12 +03:00
var errs []error
2024-07-03 15:38:37 +03:00
ci.rangeByName(func(c *Persistent) (cont bool) {
2024-05-15 13:34:12 +03:00
err = c.CloseUpstreams()
if err != nil {
errs = append(errs, err)
}
return true
})
return errors.Join(errs...)
}