AdGuardHome/internal/client/index.go

250 lines
5.4 KiB
Go
Raw Normal View History

package client
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
import (
"fmt"
"net"
"net/netip"
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
)
// 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))
}
}
// Index stores all information about persistent clients.
type Index struct {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
// subnetToUID maps subnet to UID.
subnetToUID aghalg.SortedMap[netip.Prefix, UID]
}
// NewIndex initializes the new instance of client index.
func NewIndex() (ci *Index) {
return &Index{
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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{},
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
}
}
// Add stores information about a persistent client in the index. c must be
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
// non-nil and contain UID.
func (ci *Index) Add(c *Persistent) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
if (c.UID == UID{}) {
panic("client must contain uid")
}
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
}
// Clashes returns an error if the index contains a different persistent client
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
// with at least a single identifier contained by c. c must be non-nil.
func (ci *Index) Clashes(c *Persistent) (err error) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
for _, id := range c.ClientIDs {
existing, ok := ci.clientIDToUID[id]
if ok && existing != c.UID {
p := ci.uidToClient[existing]
return fmt.Errorf("another client %q uses the same ID %q", p.Name, id)
}
}
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
}
// clashesIP returns a previous client with the same IP address as c. c must be
// non-nil.
func (ci *Index) clashesIP(c *Persistent) (p *Persistent, ip netip.Addr) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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.
func (ci *Index) clashesSubnet(c *Persistent) (p *Persistent, s netip.Prefix) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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.
func (ci *Index) clashesMAC(c *Persistent) (p *Persistent, mac net.HardwareAddr) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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
}
// Find finds persistent client by string representation of the client ID, IP
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
// address, or MAC.
func (ci *Index) Find(id string) (c *Persistent, ok bool) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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 {
return ci.findByMAC(mac)
}
return nil, false
}
// find finds persistent client by IP address.
func (ci *Index) findByIP(ip netip.Addr) (c *Persistent, found bool) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
uid, found := ci.ipToUID[ip]
if found {
return ci.uidToClient[uid], true
}
ci.subnetToUID.Range(func(pref netip.Prefix, id UID) (cont bool) {
if pref.Contains(ip) {
uid, found = id, true
return false
}
return true
})
if found {
return ci.uidToClient[uid], true
}
return nil, false
}
// find finds persistent client by MAC.
func (ci *Index) findByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
k := macToKey(mac)
uid, found := ci.macToUID[k]
if found {
return ci.uidToClient[uid], true
}
return nil, false
}
// Delete removes information about persistent client from the index. c must be
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +03:00
// non-nil.
func (ci *Index) Delete(c *Persistent) {
Pull request 2138: AG-27492-client-persistent-storage Squashed commit of the following: commit 37e33ec761cfa30164125af2c5bb40789412355e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:25:25 2024 +0300 aghalg: imp code commit 6b2f09a44298b474ec1bdf3d027fb4941d2f7bea Merge: b8ea924aa 37736289e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 14 15:04:59 2024 +0300 Merge branch 'master' into AG-27492-client-persistent-storage commit b8ea924aa7ed4c052760a6068f945d83d184e7e3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 19:07:52 2024 +0300 home: imp tests commit aa6fec03b1a1ead96bc76919b7ad51ae19626633 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 13 14:54:28 2024 +0300 home: imp docs commit 10637fdec47d0b035cf5c7949ddcd9ec564851a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 8 20:16:11 2024 +0300 all: imp code commit b45c7d868ddb1be73e119b3260e2a866d57baa91 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Feb 7 19:15:11 2024 +0300 aghalg: add tests commit 7abe33dbaa7221ddbc8b7d802dbfa7f951d90cf8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 6 20:50:22 2024 +0300 all: imp code, tests commit 4a44e993c9bd393d2cb9853108eae1ad91e64402 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Feb 1 14:59:11 2024 +0300 all: persistent client index commit 66b16e216e03e9f3d5e69496a89b18a9d732b564 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jan 31 15:06:05 2024 +0300 aghalg: ordered map
2024-02-15 14:08:05 +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)
}