home: persistent client ids
This commit is contained in:
parent
94d437d404
commit
8b295bfa89
|
@ -3,13 +3,19 @@ package home
|
|||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
|
||||
"github.com/AdguardTeam/dnsproxy/proxy"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/stringutil"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
// UID is the type for the unique IDs of persistent clients.
|
||||
|
@ -56,10 +62,14 @@ type persistentClient struct {
|
|||
|
||||
Name string
|
||||
|
||||
IDs []string
|
||||
Tags []string
|
||||
Upstreams []string
|
||||
|
||||
IPs []netip.Addr
|
||||
Subnets []netip.Prefix
|
||||
MACs []net.HardwareAddr
|
||||
ClientIDs []string
|
||||
|
||||
// UID is the unique identifier of the persistent client.
|
||||
UID UID
|
||||
|
||||
|
@ -75,16 +85,77 @@ type persistentClient struct {
|
|||
IgnoreStatistics bool
|
||||
}
|
||||
|
||||
// ShallowClone returns a deep copy of the client, except upstreamConfig,
|
||||
// parseIDs parses a list of strings into typed fields.
|
||||
func (c *persistentClient) parseIDs(ids []string) (err error) {
|
||||
for _, id := range ids {
|
||||
if id == "" {
|
||||
return errors.Error("clientid is empty")
|
||||
}
|
||||
|
||||
var ip netip.Addr
|
||||
if ip, err = netip.ParseAddr(id); err == nil {
|
||||
c.IPs = append(c.IPs, ip)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
var subnet netip.Prefix
|
||||
if subnet, err = netip.ParsePrefix(id); err == nil {
|
||||
c.Subnets = append(c.Subnets, subnet)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
var mac net.HardwareAddr
|
||||
if mac, err = net.ParseMAC(id); err == nil {
|
||||
c.MACs = append(c.MACs, mac)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
err = dnsforward.ValidateClientID(id)
|
||||
if err != nil {
|
||||
// Don't wrap the error, because it's informative enough as is.
|
||||
return err
|
||||
}
|
||||
|
||||
c.ClientIDs = append(c.ClientIDs, strings.ToLower(id))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ids returns a list of client ids.
|
||||
func (c *persistentClient) ids() (ids []string) {
|
||||
for _, ip := range c.IPs {
|
||||
ids = append(ids, ip.String())
|
||||
}
|
||||
|
||||
for _, subnet := range c.Subnets {
|
||||
ids = append(ids, subnet.String())
|
||||
}
|
||||
|
||||
for _, mac := range c.MACs {
|
||||
ids = append(ids, mac.String())
|
||||
}
|
||||
|
||||
return append(ids, c.ClientIDs...)
|
||||
}
|
||||
|
||||
// shallowClone returns a deep copy of the client, except upstreamConfig,
|
||||
// safeSearchConf, SafeSearch fields, because it's difficult to copy them.
|
||||
func (c *persistentClient) ShallowClone() (sh *persistentClient) {
|
||||
func (c *persistentClient) shallowClone() (sh *persistentClient) {
|
||||
clone := *c
|
||||
|
||||
clone.BlockedServices = c.BlockedServices.Clone()
|
||||
clone.IDs = stringutil.CloneSlice(c.IDs)
|
||||
clone.Tags = stringutil.CloneSlice(c.Tags)
|
||||
clone.Upstreams = stringutil.CloneSlice(c.Upstreams)
|
||||
|
||||
clone.IPs = slices.Clone(c.IPs)
|
||||
clone.Subnets = slices.Clone(c.Subnets)
|
||||
clone.MACs = slices.Clone(c.MACs)
|
||||
clone.ClientIDs = slices.Clone(c.ClientIDs)
|
||||
|
||||
return &clone
|
||||
}
|
||||
|
||||
|
|
|
@ -218,7 +218,6 @@ func (o *clientObject) toPersistent(
|
|||
cli = &persistentClient{
|
||||
Name: o.Name,
|
||||
|
||||
IDs: o.IDs,
|
||||
Upstreams: o.Upstreams,
|
||||
|
||||
UID: o.UID,
|
||||
|
@ -235,6 +234,11 @@ func (o *clientObject) toPersistent(
|
|||
UpstreamsCacheSize: o.UpstreamsCacheSize,
|
||||
}
|
||||
|
||||
err = cli.parseIDs(o.IDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing ids: %w", err)
|
||||
}
|
||||
|
||||
if (cli.UID == UID{}) {
|
||||
cli.UID, err = NewUID()
|
||||
if err != nil {
|
||||
|
@ -310,7 +314,7 @@ func (clients *clientsContainer) forConfig() (objs []*clientObject) {
|
|||
|
||||
BlockedServices: cli.BlockedServices.Clone(),
|
||||
|
||||
IDs: stringutil.CloneSlice(cli.IDs),
|
||||
IDs: stringutil.CloneSlice(cli.ids()),
|
||||
Tags: stringutil.CloneSlice(cli.Tags),
|
||||
Upstreams: stringutil.CloneSlice(cli.Upstreams),
|
||||
|
||||
|
@ -449,7 +453,7 @@ func (clients *clientsContainer) find(id string) (c *persistentClient, ok bool)
|
|||
return nil, false
|
||||
}
|
||||
|
||||
return c.ShallowClone(), true
|
||||
return c.shallowClone(), true
|
||||
}
|
||||
|
||||
// shouldCountClient is a wrapper around [clientsContainer.find] to make it a
|
||||
|
@ -534,13 +538,7 @@ func (clients *clientsContainer) findLocked(id string) (c *persistentClient, ok
|
|||
}
|
||||
|
||||
for _, c = range clients.list {
|
||||
for _, id := range c.IDs {
|
||||
var subnet netip.Prefix
|
||||
subnet, err = netip.ParsePrefix(id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, subnet := range c.Subnets {
|
||||
if subnet.Contains(ip) {
|
||||
return c, true
|
||||
}
|
||||
|
@ -560,12 +558,7 @@ func (clients *clientsContainer) findDHCP(ip netip.Addr) (c *persistentClient, o
|
|||
}
|
||||
|
||||
for _, c = range clients.list {
|
||||
for _, id := range c.IDs {
|
||||
mac, err := net.ParseMAC(id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, mac := range c.MACs {
|
||||
if bytes.Equal(mac, foundMAC) {
|
||||
return c, true
|
||||
}
|
||||
|
@ -615,22 +608,12 @@ func (clients *clientsContainer) check(c *persistentClient) (err error) {
|
|||
return errors.Error("client is nil")
|
||||
case c.Name == "":
|
||||
return errors.Error("invalid name")
|
||||
case len(c.IDs) == 0:
|
||||
case len(c.IPs)+len(c.Subnets)+len(c.MACs)+len(c.ClientIDs) == 0:
|
||||
return errors.Error("id required")
|
||||
default:
|
||||
// Go on.
|
||||
}
|
||||
|
||||
for i, id := range c.IDs {
|
||||
var norm string
|
||||
norm, err = normalizeClientIdentifier(id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("client at index %d: %w", i, err)
|
||||
}
|
||||
|
||||
c.IDs[i] = norm
|
||||
}
|
||||
|
||||
for _, t := range c.Tags {
|
||||
if !clients.allTags.Has(t) {
|
||||
return fmt.Errorf("invalid tag: %q", t)
|
||||
|
@ -647,35 +630,6 @@ func (clients *clientsContainer) check(c *persistentClient) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// normalizeClientIdentifier returns a normalized version of idStr. If idStr
|
||||
// cannot be normalized, it returns an error.
|
||||
func normalizeClientIdentifier(idStr string) (norm string, err error) {
|
||||
if idStr == "" {
|
||||
return "", errors.Error("clientid is empty")
|
||||
}
|
||||
|
||||
var ip netip.Addr
|
||||
if ip, err = netip.ParseAddr(idStr); err == nil {
|
||||
return ip.String(), nil
|
||||
}
|
||||
|
||||
var subnet netip.Prefix
|
||||
if subnet, err = netip.ParsePrefix(idStr); err == nil {
|
||||
return subnet.String(), nil
|
||||
}
|
||||
|
||||
var mac net.HardwareAddr
|
||||
if mac, err = net.ParseMAC(idStr); err == nil {
|
||||
return mac.String(), nil
|
||||
}
|
||||
|
||||
if err = dnsforward.ValidateClientID(idStr); err == nil {
|
||||
return strings.ToLower(idStr), nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("bad client identifier %q", idStr)
|
||||
}
|
||||
|
||||
// add adds a new client object. ok is false if such client already exists or
|
||||
// if an error occurred.
|
||||
func (clients *clientsContainer) add(c *persistentClient) (ok bool, err error) {
|
||||
|
@ -694,7 +648,7 @@ func (clients *clientsContainer) add(c *persistentClient) (ok bool, err error) {
|
|||
}
|
||||
|
||||
// check ID index
|
||||
for _, id := range c.IDs {
|
||||
for _, id := range c.ids() {
|
||||
var c2 *persistentClient
|
||||
c2, ok = clients.idIndex[id]
|
||||
if ok {
|
||||
|
@ -704,7 +658,7 @@ func (clients *clientsContainer) add(c *persistentClient) (ok bool, err error) {
|
|||
|
||||
clients.addLocked(c)
|
||||
|
||||
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs, len(clients.list))
|
||||
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.ids(), len(clients.list))
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
@ -715,7 +669,7 @@ func (clients *clientsContainer) addLocked(c *persistentClient) {
|
|||
clients.list[c.Name] = c
|
||||
|
||||
// update ID index
|
||||
for _, id := range c.IDs {
|
||||
for _, id := range c.ids() {
|
||||
clients.idIndex[id] = c
|
||||
}
|
||||
}
|
||||
|
@ -747,7 +701,7 @@ func (clients *clientsContainer) removeLocked(c *persistentClient) {
|
|||
delete(clients.list, c.Name)
|
||||
|
||||
// Update the ID index.
|
||||
for _, id := range c.IDs {
|
||||
for _, id := range c.ids() {
|
||||
delete(clients.idIndex, id)
|
||||
}
|
||||
}
|
||||
|
@ -772,8 +726,9 @@ func (clients *clientsContainer) update(prev, c *persistentClient) (err error) {
|
|||
}
|
||||
|
||||
// Check the ID index.
|
||||
if !slices.Equal(prev.IDs, c.IDs) {
|
||||
for _, id := range c.IDs {
|
||||
ids := c.ids()
|
||||
if !slices.Equal(prev.ids(), ids) {
|
||||
for _, id := range ids {
|
||||
existing, ok := clients.idIndex[id]
|
||||
if ok && existing != prev {
|
||||
return fmt.Errorf("id %q is used by client with name %q", id, existing.Name)
|
||||
|
|
|
@ -62,11 +62,13 @@ func TestClients(t *testing.T) {
|
|||
|
||||
cli1IP = netip.MustParseAddr(cli1)
|
||||
cli2IP = netip.MustParseAddr(cli2)
|
||||
|
||||
cliIPv6 = netip.MustParseAddr("1:2:3::4")
|
||||
)
|
||||
|
||||
c := &persistentClient{
|
||||
IDs: []string{cli1, "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
|
||||
Name: "client1",
|
||||
IPs: []netip.Addr{cli1IP, cliIPv6},
|
||||
}
|
||||
|
||||
ok, err := clients.add(c)
|
||||
|
@ -75,8 +77,8 @@ func TestClients(t *testing.T) {
|
|||
assert.True(t, ok)
|
||||
|
||||
c = &persistentClient{
|
||||
IDs: []string{cli2},
|
||||
Name: "client2",
|
||||
IPs: []netip.Addr{cli2IP},
|
||||
}
|
||||
|
||||
ok, err = clients.add(c)
|
||||
|
@ -108,8 +110,8 @@ func TestClients(t *testing.T) {
|
|||
|
||||
t.Run("add_fail_name", func(t *testing.T) {
|
||||
ok, err := clients.add(&persistentClient{
|
||||
IDs: []string{"1.2.3.5"},
|
||||
Name: "client1",
|
||||
IPs: []netip.Addr{netip.MustParseAddr("1.2.3.5")},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.False(t, ok)
|
||||
|
@ -117,7 +119,6 @@ func TestClients(t *testing.T) {
|
|||
|
||||
t.Run("add_fail_ip", func(t *testing.T) {
|
||||
ok, err := clients.add(&persistentClient{
|
||||
IDs: []string{"2.2.2.2"},
|
||||
Name: "client3",
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
@ -126,7 +127,6 @@ func TestClients(t *testing.T) {
|
|||
|
||||
t.Run("update_fail_ip", func(t *testing.T) {
|
||||
err := clients.update(&persistentClient{Name: "client1"}, &persistentClient{
|
||||
IDs: []string{"2.2.2.2"},
|
||||
Name: "client1",
|
||||
})
|
||||
assert.Error(t, err)
|
||||
|
@ -144,8 +144,8 @@ func TestClients(t *testing.T) {
|
|||
require.True(t, ok)
|
||||
|
||||
err := clients.update(prev, &persistentClient{
|
||||
IDs: []string{cliNew},
|
||||
Name: "client1",
|
||||
IPs: []netip.Addr{cliNewIP},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@ -158,8 +158,8 @@ func TestClients(t *testing.T) {
|
|||
require.True(t, ok)
|
||||
|
||||
err = clients.update(prev, &persistentClient{
|
||||
IDs: []string{cliNew},
|
||||
Name: "client1-renamed",
|
||||
IPs: []netip.Addr{cliNewIP},
|
||||
UseOwnSettings: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
@ -175,9 +175,9 @@ func TestClients(t *testing.T) {
|
|||
|
||||
assert.Nil(t, nilCli)
|
||||
|
||||
require.Len(t, c.IDs, 1)
|
||||
require.Len(t, c.ids(), 1)
|
||||
|
||||
assert.Equal(t, cliNew, c.IDs[0])
|
||||
assert.Equal(t, cliNewIP, c.IPs[0])
|
||||
})
|
||||
|
||||
t.Run("del_success", func(t *testing.T) {
|
||||
|
@ -259,8 +259,8 @@ func TestClientsWHOIS(t *testing.T) {
|
|||
ip := netip.MustParseAddr("1.1.1.2")
|
||||
|
||||
ok, err := clients.add(&persistentClient{
|
||||
IDs: []string{"1.1.1.2"},
|
||||
Name: "client1",
|
||||
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.2")},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, ok)
|
||||
|
@ -281,8 +281,10 @@ func TestClientsAddExisting(t *testing.T) {
|
|||
|
||||
// Add a client.
|
||||
ok, err := clients.add(&persistentClient{
|
||||
IDs: []string{ip.String(), "1:2:3::4", "aa:aa:aa:aa:aa:aa", "2.2.2.0/24"},
|
||||
Name: "client1",
|
||||
Name: "client1",
|
||||
IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")},
|
||||
Subnets: []netip.Prefix{netip.MustParsePrefix("2.2.2.0/24")},
|
||||
MACs: []net.HardwareAddr{{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, ok)
|
||||
|
@ -329,16 +331,16 @@ func TestClientsAddExisting(t *testing.T) {
|
|||
|
||||
// Add a new client with the same IP as for a client with MAC.
|
||||
ok, err := clients.add(&persistentClient{
|
||||
IDs: []string{ip.String()},
|
||||
Name: "client2",
|
||||
IPs: []netip.Addr{ip},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, ok)
|
||||
|
||||
// Add a new client with the IP from the first client's IP range.
|
||||
ok, err = clients.add(&persistentClient{
|
||||
IDs: []string{"2.2.2.2"},
|
||||
Name: "client3",
|
||||
IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, ok)
|
||||
|
@ -350,8 +352,8 @@ func TestClientsCustomUpstream(t *testing.T) {
|
|||
|
||||
// Add client with upstreams.
|
||||
ok, err := clients.add(&persistentClient{
|
||||
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
|
||||
Name: "client1",
|
||||
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")},
|
||||
Upstreams: []string{
|
||||
"1.1.1.1",
|
||||
"[/example.org/]8.8.8.8",
|
||||
|
|
|
@ -195,9 +195,14 @@ func (clients *clientsContainer) jsonToClient(
|
|||
return nil, err
|
||||
}
|
||||
|
||||
err = c.parseIDs(cj.IDs)
|
||||
if err != nil {
|
||||
// Don't wrap the error since it's informative enough as is.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.safeSearchConf = copySafeSearch(cj.SafeSearchConf, cj.SafeSearchEnabled)
|
||||
c.Name = cj.Name
|
||||
c.IDs = cj.IDs
|
||||
c.Tags = cj.Tags
|
||||
c.Upstreams = cj.Upstreams
|
||||
c.UseOwnSettings = !cj.UseGlobalSettings
|
||||
|
@ -286,7 +291,7 @@ func clientToJSON(c *persistentClient) (cj *clientJSON) {
|
|||
|
||||
return &clientJSON{
|
||||
Name: c.Name,
|
||||
IDs: c.IDs,
|
||||
IDs: c.ids(),
|
||||
Tags: c.Tags,
|
||||
UseGlobalSettings: !c.UseOwnSettings,
|
||||
FilteringEnabled: c.FilteringEnabled,
|
||||
|
|
Loading…
Reference in New Issue