Pull request 2378: AGDNS-2750-find-client
Merge in DNS/adguard-home from AGDNS-2750-find-client to master Squashed commit of the following: commit 98f1a8ca4622b6f502a5092273b9724203fe0bd8 Merge: 9270222d84ccc2a213Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 23 17:53:20 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 9270222d8e9e03038e9434b54496cbb6164463cd Merge: 6468ceec8c7c62ad3bAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:58 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6468ceec82d30084771a53ff6720a8c11c68bf2f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:52 2025 +0300 home: imp docs commit 3fd4735a0d6db4fdf2d46f3da9794a687fdcaa8b Merge: 1311a5869a8fdf1c55Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:43:36 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 1311a58695de00f20c9704378ee6e964a44d1c59 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:42:41 2025 +0300 home: imp code commit b1f2c4c883c9476c5135140abac31f8ae6609b4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 16 16:47:59 2025 +0300 home: imp code commit d0a5abd66587c1ad602c2ccf6c8a45a3dfe39a5c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 15 14:58:31 2025 +0300 client: imp naming commit 5accdca325551237f003f1c416891b488fe5290b Merge: 6a00232f74d258972dAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:40:40 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6a00232f76a0fe5ce781aa01637b6e04ace7250d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:30:32 2025 +0300 home: imp code commit 8633886457c6aab75f5676494b1f49d9811e9ab9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 11 15:29:25 2025 +0300 all: imp code commit d6f16879e7b054a5ffac59131d2a6eff1da659c0 Merge: 58236fdec6d282ae71Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:35:23 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 58236fdec5b64e83a44680ff8a89badc18ec81f1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:23:01 2025 +0300 all: upd ci commit 3c4d946d7970987677d4ac984394e18987a29f9a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:16:03 2025 +0300 all: upd go commit cc1c97734506a9ffbe70fd3c676284e58a21ba46 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 20:58:56 2025 +0300 all: imp code commit 8f061c933152481a4c80eef2af575efd4919d82b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 9 16:49:11 2025 +0300 all: imp docs commit 8d19355f1c519211a56cec3f23d527922d4f2ee0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 7 21:35:06 2025 +0300 all: imp code commit f1e853f57e5d54d13bedcdab4f8e21e112f3a356 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 2 14:57:40 2025 +0300 all: imp code commit 6a6ac7f899f29ddc90a583c80562233e646ba1d6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:51:56 2025 +0300 client: imp tests commit 52040ee7393d0483c682f2f37d7b70f12f9cf621 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:28:18 2025 +0300 all: imp code commit 1e09208dbd2d35c3f6b2ade169324e23d1a643a5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 26 15:33:02 2025 +0300 all: imp code ... and 2 more commits
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/hostsfile"
|
||||
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
||||
"github.com/AdguardTeam/golibs/netutil"
|
||||
"github.com/AdguardTeam/golibs/timeutil"
|
||||
)
|
||||
|
||||
@@ -433,48 +435,186 @@ func (s *Storage) Add(ctx context.Context, p *Persistent) (err error) {
|
||||
ctx,
|
||||
"client added",
|
||||
"name", p.Name,
|
||||
"ids", p.IDs(),
|
||||
"ids", p.Identifiers(),
|
||||
"clients_count", s.index.size(),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindByName finds persistent client by name. And returns its shallow copy.
|
||||
func (s *Storage) FindByName(name string) (p *Persistent, ok bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
// FindParams represents the parameters for searching a client. At least one
|
||||
// field must be non-empty.
|
||||
type FindParams struct {
|
||||
// ClientID is a unique identifier for the client used in DoH, DoT, and DoQ
|
||||
// DNS queries.
|
||||
ClientID ClientID
|
||||
|
||||
p, ok = s.index.findByName(name)
|
||||
if ok {
|
||||
return p.ShallowClone(), ok
|
||||
}
|
||||
// RemoteIP is the IP address used as a client search parameter.
|
||||
RemoteIP netip.Addr
|
||||
|
||||
return nil, false
|
||||
// Subnet is the CIDR used as a client search parameter.
|
||||
Subnet netip.Prefix
|
||||
|
||||
// MAC is the physical hardware address used as a client search parameter.
|
||||
MAC net.HardwareAddr
|
||||
|
||||
// UID is the unique ID of persistent client used as a search parameter.
|
||||
//
|
||||
// TODO(s.chzhen): Use this.
|
||||
UID UID
|
||||
}
|
||||
|
||||
// Find finds persistent client by string representation of the ClientID, IP
|
||||
// address, or MAC. And returns its shallow copy.
|
||||
// ErrBadIdentifier is returned by [FindParams.Set] when it cannot parse the
|
||||
// provided client identifier.
|
||||
const ErrBadIdentifier errors.Error = "bad client identifier"
|
||||
|
||||
// Set clears the stored search parameters and parses the string representation
|
||||
// of the search parameter into typed parameter, storing it. In some cases, it
|
||||
// may result in storing both an IP address and a MAC address because they might
|
||||
// have identical string representations. It returns [ErrBadIdentifier] if id
|
||||
// cannot be parsed.
|
||||
//
|
||||
// TODO(s.chzhen): Accept ClientIDData structure instead, which will contain
|
||||
// the parsed IP address, if any.
|
||||
func (s *Storage) Find(id string) (p *Persistent, ok bool) {
|
||||
// TODO(s.chzhen): Add support for UID.
|
||||
func (p *FindParams) Set(id string) (err error) {
|
||||
*p = FindParams{}
|
||||
|
||||
isClientID := true
|
||||
|
||||
if netutil.IsValidIPString(id) {
|
||||
// It is safe to use [netip.MustParseAddr] because it has already been
|
||||
// validated that id contains the string representation of the IP
|
||||
// address.
|
||||
p.RemoteIP = netip.MustParseAddr(id)
|
||||
|
||||
// Even if id can be parsed as an IP address, it may be a MAC address.
|
||||
// So do not return prematurely, continue parsing.
|
||||
isClientID = false
|
||||
}
|
||||
|
||||
if canBeValidIPPrefixString(id) {
|
||||
p.Subnet, err = netip.ParsePrefix(id)
|
||||
if err == nil {
|
||||
isClientID = false
|
||||
}
|
||||
}
|
||||
|
||||
if canBeMACString(id) {
|
||||
p.MAC, err = net.ParseMAC(id)
|
||||
if err == nil {
|
||||
isClientID = false
|
||||
}
|
||||
}
|
||||
|
||||
if !isClientID {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !isValidClientID(id) {
|
||||
return ErrBadIdentifier
|
||||
}
|
||||
|
||||
p.ClientID = ClientID(id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// canBeValidIPPrefixString is a best-effort check to determine if s is a valid
|
||||
// CIDR before using [netip.ParsePrefix], aimed at reducing allocations.
|
||||
//
|
||||
// TODO(s.chzhen): Replace this implementation with the more robust version
|
||||
// from golibs.
|
||||
func canBeValidIPPrefixString(s string) (ok bool) {
|
||||
ipStr, bitStr, ok := strings.Cut(s, "/")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if bitStr == "" || len(bitStr) > 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
bits := 0
|
||||
for _, c := range bitStr {
|
||||
if c < '0' || c > '9' {
|
||||
return false
|
||||
}
|
||||
|
||||
bits = bits*10 + int(c-'0')
|
||||
}
|
||||
|
||||
if bits > 128 {
|
||||
return false
|
||||
}
|
||||
|
||||
return netutil.IsValidIPString(ipStr)
|
||||
}
|
||||
|
||||
// canBeMACString is a best-effort check to determine if s is a valid MAC
|
||||
// address before using [net.ParseMAC], aimed at reducing allocations.
|
||||
//
|
||||
// TODO(s.chzhen): Replace this implementation with the more robust version
|
||||
// from golibs.
|
||||
func canBeMACString(s string) (ok bool) {
|
||||
switch len(s) {
|
||||
case
|
||||
len("0000.0000.0000"),
|
||||
len("00:00:00:00:00:00"),
|
||||
len("0000.0000.0000.0000"),
|
||||
len("00:00:00:00:00:00:00:00"),
|
||||
len("0000.0000.0000.0000.0000.0000.0000.0000.0000.0000"),
|
||||
len("00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Find represents the parameters for searching a client. params must not be
|
||||
// nil and must have at least one non-empty field.
|
||||
func (s *Storage) Find(params *FindParams) (p *Persistent, ok bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
p, ok = s.index.find(id)
|
||||
isClientID := params.ClientID != ""
|
||||
isRemoteIP := params.RemoteIP != (netip.Addr{})
|
||||
isSubnet := params.Subnet != (netip.Prefix{})
|
||||
isMAC := params.MAC != nil
|
||||
|
||||
for {
|
||||
switch {
|
||||
case isClientID:
|
||||
isClientID = false
|
||||
p, ok = s.index.findByClientID(params.ClientID)
|
||||
case isRemoteIP:
|
||||
isRemoteIP = false
|
||||
p, ok = s.findByIP(params.RemoteIP)
|
||||
case isSubnet:
|
||||
isSubnet = false
|
||||
p, ok = s.index.findByCIDR(params.Subnet)
|
||||
case isMAC:
|
||||
isMAC = false
|
||||
p, ok = s.index.findByMAC(params.MAC)
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if ok {
|
||||
return p.ShallowClone(), true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// findByIP finds persistent client by IP address. s.mu is expected to be
|
||||
// locked.
|
||||
func (s *Storage) findByIP(addr netip.Addr) (p *Persistent, ok bool) {
|
||||
p, ok = s.index.findByIP(addr)
|
||||
if ok {
|
||||
return p.ShallowClone(), ok
|
||||
return p, true
|
||||
}
|
||||
|
||||
ip, err := netip.ParseAddr(id)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
foundMAC := s.dhcp.MACByIP(ip)
|
||||
foundMAC := s.dhcp.MACByIP(addr)
|
||||
if foundMAC != nil {
|
||||
return s.FindByMAC(foundMAC)
|
||||
return s.index.findByMAC(foundMAC)
|
||||
}
|
||||
|
||||
return nil, false
|
||||
@@ -487,6 +627,8 @@ func (s *Storage) Find(id string) (p *Persistent, ok bool) {
|
||||
//
|
||||
// Note that multiple clients can have the same IP address with different zones.
|
||||
// Therefore, the result of this method is indeterminate.
|
||||
//
|
||||
// TODO(s.chzhen): Consider accepting [FindParams].
|
||||
func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -498,7 +640,7 @@ func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
||||
|
||||
foundMAC := s.dhcp.MACByIP(ip)
|
||||
if foundMAC != nil {
|
||||
return s.FindByMAC(foundMAC)
|
||||
return s.index.findByMAC(foundMAC)
|
||||
}
|
||||
|
||||
p = s.index.findByIPWithoutZone(ip)
|
||||
@@ -509,17 +651,6 @@ func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// FindByMAC finds persistent client by MAC and returns its shallow copy. s.mu
|
||||
// is expected to be locked.
|
||||
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
|
||||
}
|
||||
|
||||
// RemoveByName removes persistent client information. ok is false if no such
|
||||
// client exists by that name.
|
||||
func (s *Storage) RemoveByName(ctx context.Context, name string) (ok bool) {
|
||||
@@ -648,7 +779,7 @@ func (s *Storage) CustomUpstreamConfig(
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
c, ok := s.index.findByClientID(id)
|
||||
c, ok := s.index.findByClientID(ClientID(id))
|
||||
if !ok {
|
||||
c, ok = s.index.findByIP(addr)
|
||||
}
|
||||
@@ -682,7 +813,7 @@ func (s *Storage) ClearUpstreamCache() {
|
||||
// ClientID or client IP address, and applies it to the filtering settings.
|
||||
// setts must not be nil.
|
||||
func (s *Storage) ApplyClientFiltering(id string, addr netip.Addr, setts *filtering.Settings) {
|
||||
c, ok := s.index.findByClientID(id)
|
||||
c, ok := s.index.findByClientID(ClientID(id))
|
||||
if !ok {
|
||||
c, ok = s.index.findByIP(addr)
|
||||
}
|
||||
@@ -690,7 +821,7 @@ func (s *Storage) ApplyClientFiltering(id string, addr netip.Addr, setts *filter
|
||||
if !ok {
|
||||
foundMAC := s.dhcp.MACByIP(addr)
|
||||
if foundMAC != nil {
|
||||
c, ok = s.FindByMAC(foundMAC)
|
||||
c, ok = s.index.findByMAC(foundMAC)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user