mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-08-16 22:20:30 +03:00
Pull request 2244: AG-27492-client-storage-usage
Squashed commit of the following: commit 46956d0f5a36fbcd2324125bcc146bdb57cb3f7e Merge:85ccad786
3993f4c47
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 1 16:11:59 2024 +0300 Merge branch 'master' into AG-27492-client-storage-usage commit85ccad7862
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 28 18:05:37 2024 +0300 all: imp docs commite7043efcda
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jun 27 20:10:26 2024 +0300 all: client storage usage
This commit is contained in:
parent
3993f4c476
commit
9d1c45fd94
@ -30,8 +30,8 @@ func macToKey(mac net.HardwareAddr) (key macKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index stores all information about persistent clients.
|
// index stores all information about persistent clients.
|
||||||
type Index struct {
|
type index struct {
|
||||||
// nameToUID maps client name to UID.
|
// nameToUID maps client name to UID.
|
||||||
nameToUID map[string]UID
|
nameToUID map[string]UID
|
||||||
|
|
||||||
@ -51,9 +51,9 @@ type Index struct {
|
|||||||
subnetToUID aghalg.SortedMap[netip.Prefix, UID]
|
subnetToUID aghalg.SortedMap[netip.Prefix, UID]
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewIndex initializes the new instance of client index.
|
// newIndex initializes the new instance of client index.
|
||||||
func NewIndex() (ci *Index) {
|
func newIndex() (ci *index) {
|
||||||
return &Index{
|
return &index{
|
||||||
nameToUID: map[string]UID{},
|
nameToUID: map[string]UID{},
|
||||||
clientIDToUID: map[string]UID{},
|
clientIDToUID: map[string]UID{},
|
||||||
ipToUID: map[netip.Addr]UID{},
|
ipToUID: map[netip.Addr]UID{},
|
||||||
@ -63,9 +63,9 @@ func NewIndex() (ci *Index) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add stores information about a persistent client in the index. c must be
|
// add stores information about a persistent client in the index. c must be
|
||||||
// non-nil, have a UID, and contain at least one identifier.
|
// non-nil, have a UID, and contain at least one identifier.
|
||||||
func (ci *Index) Add(c *Persistent) {
|
func (ci *index) add(c *Persistent) {
|
||||||
if (c.UID == UID{}) {
|
if (c.UID == UID{}) {
|
||||||
panic("client must contain uid")
|
panic("client must contain uid")
|
||||||
}
|
}
|
||||||
@ -92,9 +92,9 @@ func (ci *Index) Add(c *Persistent) {
|
|||||||
ci.uidToClient[c.UID] = c
|
ci.uidToClient[c.UID] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClashesUID returns existing persistent client with the same UID as c. Note
|
// clashesUID returns existing persistent client with the same UID as c. Note
|
||||||
// that this is only possible when configuration contains duplicate fields.
|
// that this is only possible when configuration contains duplicate fields.
|
||||||
func (ci *Index) ClashesUID(c *Persistent) (err error) {
|
func (ci *index) clashesUID(c *Persistent) (err error) {
|
||||||
p, ok := ci.uidToClient[c.UID]
|
p, ok := ci.uidToClient[c.UID]
|
||||||
if ok {
|
if ok {
|
||||||
return fmt.Errorf("another client %q uses the same uid", p.Name)
|
return fmt.Errorf("another client %q uses the same uid", p.Name)
|
||||||
@ -103,9 +103,9 @@ func (ci *Index) ClashesUID(c *Persistent) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clashes returns an error if the index contains a different persistent client
|
// clashes returns an error if the index contains a different persistent client
|
||||||
// with at least a single identifier contained by c. c must be non-nil.
|
// with at least a single identifier contained by c. c must be non-nil.
|
||||||
func (ci *Index) Clashes(c *Persistent) (err error) {
|
func (ci *index) clashes(c *Persistent) (err error) {
|
||||||
if p := ci.clashesName(c); p != nil {
|
if p := ci.clashesName(c); p != nil {
|
||||||
return fmt.Errorf("another client uses the same name %q", p.Name)
|
return fmt.Errorf("another client uses the same name %q", p.Name)
|
||||||
}
|
}
|
||||||
@ -139,8 +139,8 @@ func (ci *Index) Clashes(c *Persistent) (err error) {
|
|||||||
|
|
||||||
// clashesName returns existing persistent client with the same name as c or
|
// clashesName returns existing persistent client with the same name as c or
|
||||||
// nil. c must be non-nil.
|
// nil. c must be non-nil.
|
||||||
func (ci *Index) clashesName(c *Persistent) (existing *Persistent) {
|
func (ci *index) clashesName(c *Persistent) (existing *Persistent) {
|
||||||
existing, ok := ci.FindByName(c.Name)
|
existing, ok := ci.findByName(c.Name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -154,7 +154,7 @@ func (ci *Index) clashesName(c *Persistent) (existing *Persistent) {
|
|||||||
|
|
||||||
// clashesIP returns a previous client with the same IP address as c. c must be
|
// clashesIP returns a previous client with the same IP address as c. c must be
|
||||||
// non-nil.
|
// non-nil.
|
||||||
func (ci *Index) clashesIP(c *Persistent) (p *Persistent, ip netip.Addr) {
|
func (ci *index) clashesIP(c *Persistent) (p *Persistent, ip netip.Addr) {
|
||||||
for _, ip := range c.IPs {
|
for _, ip := range c.IPs {
|
||||||
existing, ok := ci.ipToUID[ip]
|
existing, ok := ci.ipToUID[ip]
|
||||||
if ok && existing != c.UID {
|
if ok && existing != c.UID {
|
||||||
@ -167,7 +167,7 @@ func (ci *Index) clashesIP(c *Persistent) (p *Persistent, ip netip.Addr) {
|
|||||||
|
|
||||||
// clashesSubnet returns a previous client with the same subnet as c. c must be
|
// clashesSubnet returns a previous client with the same subnet as c. c must be
|
||||||
// non-nil.
|
// non-nil.
|
||||||
func (ci *Index) clashesSubnet(c *Persistent) (p *Persistent, s netip.Prefix) {
|
func (ci *index) clashesSubnet(c *Persistent) (p *Persistent, s netip.Prefix) {
|
||||||
for _, s = range c.Subnets {
|
for _, s = range c.Subnets {
|
||||||
var existing UID
|
var existing UID
|
||||||
var ok bool
|
var ok bool
|
||||||
@ -193,7 +193,7 @@ func (ci *Index) clashesSubnet(c *Persistent) (p *Persistent, s netip.Prefix) {
|
|||||||
|
|
||||||
// clashesMAC returns a previous client with the same MAC address as c. c must
|
// clashesMAC returns a previous client with the same MAC address as c. c must
|
||||||
// be non-nil.
|
// be non-nil.
|
||||||
func (ci *Index) clashesMAC(c *Persistent) (p *Persistent, mac net.HardwareAddr) {
|
func (ci *index) clashesMAC(c *Persistent) (p *Persistent, mac net.HardwareAddr) {
|
||||||
for _, mac = range c.MACs {
|
for _, mac = range c.MACs {
|
||||||
k := macToKey(mac)
|
k := macToKey(mac)
|
||||||
existing, ok := ci.macToUID[k]
|
existing, ok := ci.macToUID[k]
|
||||||
@ -205,9 +205,9 @@ func (ci *Index) clashesMAC(c *Persistent) (p *Persistent, mac net.HardwareAddr)
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find finds persistent client by string representation of the client ID, IP
|
// find finds persistent client by string representation of the client ID, IP
|
||||||
// address, or MAC.
|
// address, or MAC.
|
||||||
func (ci *Index) Find(id string) (c *Persistent, ok bool) {
|
func (ci *index) find(id string) (c *Persistent, ok bool) {
|
||||||
uid, found := ci.clientIDToUID[id]
|
uid, found := ci.clientIDToUID[id]
|
||||||
if found {
|
if found {
|
||||||
return ci.uidToClient[uid], true
|
return ci.uidToClient[uid], true
|
||||||
@ -224,14 +224,14 @@ func (ci *Index) Find(id string) (c *Persistent, ok bool) {
|
|||||||
|
|
||||||
mac, err := net.ParseMAC(id)
|
mac, err := net.ParseMAC(id)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return ci.FindByMAC(mac)
|
return ci.findByMAC(mac)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByName finds persistent client by name.
|
// findByName finds persistent client by name.
|
||||||
func (ci *Index) FindByName(name string) (c *Persistent, found bool) {
|
func (ci *index) findByName(name string) (c *Persistent, found bool) {
|
||||||
uid, found := ci.nameToUID[name]
|
uid, found := ci.nameToUID[name]
|
||||||
if found {
|
if found {
|
||||||
return ci.uidToClient[uid], true
|
return ci.uidToClient[uid], true
|
||||||
@ -241,7 +241,7 @@ func (ci *Index) FindByName(name string) (c *Persistent, found bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// findByIP finds persistent client by IP address.
|
// findByIP finds persistent client by IP address.
|
||||||
func (ci *Index) findByIP(ip netip.Addr) (c *Persistent, found bool) {
|
func (ci *index) findByIP(ip netip.Addr) (c *Persistent, found bool) {
|
||||||
uid, found := ci.ipToUID[ip]
|
uid, found := ci.ipToUID[ip]
|
||||||
if found {
|
if found {
|
||||||
return ci.uidToClient[uid], true
|
return ci.uidToClient[uid], true
|
||||||
@ -266,8 +266,8 @@ func (ci *Index) findByIP(ip netip.Addr) (c *Persistent, found bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByMAC finds persistent client by MAC.
|
// findByMAC finds persistent client by MAC.
|
||||||
func (ci *Index) FindByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
|
func (ci *index) findByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
|
||||||
k := macToKey(mac)
|
k := macToKey(mac)
|
||||||
uid, found := ci.macToUID[k]
|
uid, found := ci.macToUID[k]
|
||||||
if found {
|
if found {
|
||||||
@ -277,13 +277,13 @@ func (ci *Index) FindByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByIPWithoutZone finds a persistent client by IP address without zone. It
|
// findByIPWithoutZone finds a persistent client by IP address without zone. It
|
||||||
// strips the IPv6 zone index from the stored IP addresses before comparing,
|
// 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].
|
// 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.
|
// Note that multiple clients can have the same IP address with different zones.
|
||||||
// Therefore, the result of this method is indeterminate.
|
// Therefore, the result of this method is indeterminate.
|
||||||
func (ci *Index) FindByIPWithoutZone(ip netip.Addr) (c *Persistent) {
|
func (ci *index) findByIPWithoutZone(ip netip.Addr) (c *Persistent) {
|
||||||
if (ip == netip.Addr{}) {
|
if (ip == netip.Addr{}) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -297,9 +297,9 @@ func (ci *Index) FindByIPWithoutZone(ip netip.Addr) (c *Persistent) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes information about persistent client from the index. c must be
|
// remove removes information about persistent client from the index. c must be
|
||||||
// non-nil.
|
// non-nil.
|
||||||
func (ci *Index) Delete(c *Persistent) {
|
func (ci *index) remove(c *Persistent) {
|
||||||
delete(ci.nameToUID, c.Name)
|
delete(ci.nameToUID, c.Name)
|
||||||
|
|
||||||
for _, id := range c.ClientIDs {
|
for _, id := range c.ClientIDs {
|
||||||
@ -322,24 +322,14 @@ func (ci *Index) Delete(c *Persistent) {
|
|||||||
delete(ci.uidToClient, c.UID)
|
delete(ci.uidToClient, c.UID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the number of persistent clients.
|
// size returns the number of persistent clients.
|
||||||
func (ci *Index) Size() (n int) {
|
func (ci *index) size() (n int) {
|
||||||
return len(ci.uidToClient)
|
return len(ci.uidToClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Range calls f for each persistent client, unless cont is false. The order is
|
// rangeByName is like [Index.Range] but sorts the persistent clients by name
|
||||||
// undefined.
|
|
||||||
func (ci *Index) Range(f func(c *Persistent) (cont bool)) {
|
|
||||||
for _, c := range ci.uidToClient {
|
|
||||||
if !f(c) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RangeByName is like [Index.Range] but sorts the persistent clients by name
|
|
||||||
// before iterating ensuring a predictable order.
|
// before iterating ensuring a predictable order.
|
||||||
func (ci *Index) RangeByName(f func(c *Persistent) (cont bool)) {
|
func (ci *index) rangeByName(f func(c *Persistent) (cont bool)) {
|
||||||
cs := maps.Values(ci.uidToClient)
|
cs := maps.Values(ci.uidToClient)
|
||||||
slices.SortFunc(cs, func(a, b *Persistent) (n int) {
|
slices.SortFunc(cs, func(a, b *Persistent) (n int) {
|
||||||
return strings.Compare(a.Name, b.Name)
|
return strings.Compare(a.Name, b.Name)
|
||||||
@ -352,10 +342,10 @@ func (ci *Index) RangeByName(f func(c *Persistent) (cont bool)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseUpstreams closes upstream configurations of persistent clients.
|
// closeUpstreams closes upstream configurations of persistent clients.
|
||||||
func (ci *Index) CloseUpstreams() (err error) {
|
func (ci *index) closeUpstreams() (err error) {
|
||||||
var errs []error
|
var errs []error
|
||||||
ci.RangeByName(func(c *Persistent) (cont bool) {
|
ci.rangeByName(func(c *Persistent) (cont bool) {
|
||||||
err = c.CloseUpstreams()
|
err = c.CloseUpstreams()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
|
@ -11,12 +11,12 @@ import (
|
|||||||
|
|
||||||
// newIDIndex is a helper function that returns a client index filled with
|
// newIDIndex is a helper function that returns a client index filled with
|
||||||
// persistent clients from the m. It also generates a UID for each client.
|
// persistent clients from the m. It also generates a UID for each client.
|
||||||
func newIDIndex(m []*Persistent) (ci *Index) {
|
func newIDIndex(m []*Persistent) (ci *index) {
|
||||||
ci = NewIndex()
|
ci = newIndex()
|
||||||
|
|
||||||
for _, c := range m {
|
for _, c := range m {
|
||||||
c.UID = MustNewUID()
|
c.UID = MustNewUID()
|
||||||
ci.Add(c)
|
ci.add(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ci
|
return ci
|
||||||
@ -110,7 +110,7 @@ func TestClientIndex_Find(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
for _, id := range tc.ids {
|
for _, id := range tc.ids {
|
||||||
c, ok := ci.Find(id)
|
c, ok := ci.find(id)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
assert.Equal(t, tc.want, c)
|
assert.Equal(t, tc.want, c)
|
||||||
@ -119,7 +119,7 @@ func TestClientIndex_Find(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("not_found", func(t *testing.T) {
|
t.Run("not_found", func(t *testing.T) {
|
||||||
_, ok := ci.Find(cliIPNone)
|
_, ok := ci.find(cliIPNone)
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -171,11 +171,11 @@ func TestClientIndex_Clashes(t *testing.T) {
|
|||||||
clone := tc.client.ShallowClone()
|
clone := tc.client.ShallowClone()
|
||||||
clone.UID = MustNewUID()
|
clone.UID = MustNewUID()
|
||||||
|
|
||||||
err := ci.Clashes(clone)
|
err := ci.clashes(clone)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
ci.Delete(tc.client)
|
ci.remove(tc.client)
|
||||||
err = ci.Clashes(clone)
|
err = ci.clashes(clone)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -293,7 +293,7 @@ func TestIndex_FindByIPWithoutZone(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
c := ci.FindByIPWithoutZone(tc.ip.WithZone(""))
|
c := ci.findByIPWithoutZone(tc.ip.WithZone(""))
|
||||||
require.Equal(t, tc.want, c)
|
require.Equal(t, tc.want, c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -339,7 +339,7 @@ func TestClientIndex_RangeByName(t *testing.T) {
|
|||||||
ci := newIDIndex(tc.want)
|
ci := newIDIndex(tc.want)
|
||||||
|
|
||||||
var got []*Persistent
|
var got []*Persistent
|
||||||
ci.RangeByName(func(c *Persistent) (cont bool) {
|
ci.rangeByName(func(c *Persistent) (cont bool) {
|
||||||
got = append(got, c)
|
got = append(got, c)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -65,6 +65,7 @@ type Persistent struct {
|
|||||||
// upstream must be used.
|
// upstream must be used.
|
||||||
UpstreamConfig *proxy.CustomUpstreamConfig
|
UpstreamConfig *proxy.CustomUpstreamConfig
|
||||||
|
|
||||||
|
// SafeSearch handles search engine hosts rewrites.
|
||||||
SafeSearch filtering.SafeSearch
|
SafeSearch filtering.SafeSearch
|
||||||
|
|
||||||
// BlockedServices is the configuration of blocked services of a client. It
|
// BlockedServices is the configuration of blocked services of a client. It
|
||||||
@ -74,29 +75,62 @@ type Persistent struct {
|
|||||||
// Name of the persistent client. Must not be empty.
|
// Name of the persistent client. Must not be empty.
|
||||||
Name string
|
Name string
|
||||||
|
|
||||||
Tags []string
|
// Tags is a list of client tags that categorize the client.
|
||||||
|
Tags []string
|
||||||
|
|
||||||
|
// Upstreams is a list of custom upstream DNS servers for the client.
|
||||||
Upstreams []string
|
Upstreams []string
|
||||||
|
|
||||||
|
// IPs is a list of IP addresses that identify the client. The client must
|
||||||
|
// have at least one ID (IP, subnet, MAC, or ClientID).
|
||||||
IPs []netip.Addr
|
IPs []netip.Addr
|
||||||
|
|
||||||
|
// Subnets identifying the client. The client must have at least one ID
|
||||||
|
// (IP, subnet, MAC, or ClientID).
|
||||||
|
//
|
||||||
// TODO(s.chzhen): Use netutil.Prefix.
|
// TODO(s.chzhen): Use netutil.Prefix.
|
||||||
Subnets []netip.Prefix
|
Subnets []netip.Prefix
|
||||||
MACs []net.HardwareAddr
|
|
||||||
|
// MACs identifying the client. The client must have at least one ID (IP,
|
||||||
|
// subnet, MAC, or ClientID).
|
||||||
|
MACs []net.HardwareAddr
|
||||||
|
|
||||||
|
// ClientIDs identifying the client. The client must have at least one ID
|
||||||
|
// (IP, subnet, MAC, or ClientID).
|
||||||
ClientIDs []string
|
ClientIDs []string
|
||||||
|
|
||||||
// UID is the unique identifier of the persistent client.
|
// UID is the unique identifier of the persistent client.
|
||||||
UID UID
|
UID UID
|
||||||
|
|
||||||
UpstreamsCacheSize uint32
|
// UpstreamsCacheSize is the cache size for custom upstreams.
|
||||||
|
UpstreamsCacheSize uint32
|
||||||
|
|
||||||
|
// UpstreamsCacheEnabled specifies whether custom upstreams are used.
|
||||||
UpstreamsCacheEnabled bool
|
UpstreamsCacheEnabled bool
|
||||||
|
|
||||||
UseOwnSettings bool
|
// UseOwnSettings specifies whether custom filtering settings are used.
|
||||||
FilteringEnabled bool
|
UseOwnSettings bool
|
||||||
SafeBrowsingEnabled bool
|
|
||||||
ParentalEnabled bool
|
|
||||||
UseOwnBlockedServices bool
|
|
||||||
IgnoreQueryLog bool
|
|
||||||
IgnoreStatistics bool
|
|
||||||
|
|
||||||
|
// FilteringEnabled specifies whether filtering is enabled.
|
||||||
|
FilteringEnabled bool
|
||||||
|
|
||||||
|
// SafeBrowsingEnabled specifies whether safe browsing is enabled.
|
||||||
|
SafeBrowsingEnabled bool
|
||||||
|
|
||||||
|
// ParentalEnabled specifies whether parental control is enabled.
|
||||||
|
ParentalEnabled bool
|
||||||
|
|
||||||
|
// UseOwnBlockedServices specifies whether custom services are blocked.
|
||||||
|
UseOwnBlockedServices bool
|
||||||
|
|
||||||
|
// IgnoreQueryLog specifies whether the client requests are logged.
|
||||||
|
IgnoreQueryLog bool
|
||||||
|
|
||||||
|
// IgnoreStatistics specifies whether the client requests are counted.
|
||||||
|
IgnoreStatistics bool
|
||||||
|
|
||||||
|
// SafeSearchConf is the safe search filtering configuration.
|
||||||
|
//
|
||||||
// TODO(d.kolyshev): Make SafeSearchConf a pointer.
|
// TODO(d.kolyshev): Make SafeSearchConf a pointer.
|
||||||
SafeSearchConf filtering.SafeSearchConfig
|
SafeSearchConf filtering.SafeSearchConfig
|
||||||
}
|
}
|
||||||
@ -134,21 +168,6 @@ func (c *Persistent) validate(allTags *container.MapSet[string]) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTags sets the tags if they are known, otherwise logs an unknown tag.
|
|
||||||
func (c *Persistent) SetTags(tags []string, known *container.MapSet[string]) {
|
|
||||||
for _, t := range tags {
|
|
||||||
if !known.Has(t) {
|
|
||||||
log.Info("skipping unknown tag %q", t)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Tags = append(c.Tags, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
slices.Sort(c.Tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetIDs parses a list of strings into typed fields and returns an error if
|
// SetIDs parses a list of strings into typed fields and returns an error if
|
||||||
// there is one.
|
// there is one.
|
||||||
func (c *Persistent) SetIDs(ids []string) (err error) {
|
func (c *Persistent) SetIDs(ids []string) (err error) {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/golibs/container"
|
||||||
"github.com/AdguardTeam/golibs/testutil"
|
"github.com/AdguardTeam/golibs/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -126,13 +127,19 @@ func TestPersistent_EqualIDs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPersistent_Validate(t *testing.T) {
|
func TestPersistent_Validate(t *testing.T) {
|
||||||
// TODO(s.chzhen): Add test cases.
|
const (
|
||||||
|
allowedTag = "allowed_tag"
|
||||||
|
notAllowedTag = "not_allowed_tag"
|
||||||
|
)
|
||||||
|
|
||||||
|
allowedTags := container.NewMapSet(allowedTag)
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
cli *Persistent
|
cli *Persistent
|
||||||
wantErrMsg string
|
wantErrMsg string
|
||||||
}{{
|
}{{
|
||||||
name: "basic",
|
name: "success",
|
||||||
cli: &Persistent{
|
cli: &Persistent{
|
||||||
Name: "basic",
|
Name: "basic",
|
||||||
IPs: []netip.Addr{
|
IPs: []netip.Addr{
|
||||||
@ -162,11 +169,24 @@ func TestPersistent_Validate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantErrMsg: "uid required",
|
wantErrMsg: "uid required",
|
||||||
|
}, {
|
||||||
|
name: "not_allowed_tag",
|
||||||
|
cli: &Persistent{
|
||||||
|
Name: "basic",
|
||||||
|
IPs: []netip.Addr{
|
||||||
|
netip.MustParseAddr("1.2.3.4"),
|
||||||
|
},
|
||||||
|
UID: MustNewUID(),
|
||||||
|
Tags: []string{
|
||||||
|
notAllowedTag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErrMsg: `invalid tag: "` + notAllowedTag + `"`,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
err := tc.cli.validate(nil)
|
err := tc.cli.validate(allowedTags)
|
||||||
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,14 @@ import (
|
|||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config is the client storage configuration structure.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Expand.
|
||||||
|
type Config struct {
|
||||||
|
// AllowedTags is a list of all allowed client tags.
|
||||||
|
AllowedTags []string
|
||||||
|
}
|
||||||
|
|
||||||
// Storage contains information about persistent and runtime clients.
|
// Storage contains information about persistent and runtime clients.
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
// allowedTags is a set of all allowed tags.
|
// allowedTags is a set of all allowed tags.
|
||||||
@ -20,18 +28,22 @@ type Storage struct {
|
|||||||
mu *sync.Mutex
|
mu *sync.Mutex
|
||||||
|
|
||||||
// index contains information about persistent clients.
|
// index contains information about persistent clients.
|
||||||
index *Index
|
index *index
|
||||||
|
|
||||||
// runtimeIndex contains information about runtime clients.
|
// runtimeIndex contains information about runtime clients.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
runtimeIndex *RuntimeIndex
|
runtimeIndex *RuntimeIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStorage returns initialized client storage.
|
// NewStorage returns initialized client storage. conf must not be nil.
|
||||||
func NewStorage(allowedTags *container.MapSet[string]) (s *Storage) {
|
func NewStorage(conf *Config) (s *Storage) {
|
||||||
|
allowedTags := container.NewMapSet(conf.AllowedTags...)
|
||||||
|
|
||||||
return &Storage{
|
return &Storage{
|
||||||
allowedTags: allowedTags,
|
allowedTags: allowedTags,
|
||||||
mu: &sync.Mutex{},
|
mu: &sync.Mutex{},
|
||||||
index: NewIndex(),
|
index: newIndex(),
|
||||||
runtimeIndex: NewRuntimeIndex(),
|
runtimeIndex: NewRuntimeIndex(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,40 +61,45 @@ func (s *Storage) Add(p *Persistent) (err error) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
err = s.index.ClashesUID(p)
|
err = s.index.clashesUID(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Don't wrap the error since there is already an annotation deferred.
|
// Don't wrap the error since there is already an annotation deferred.
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.index.Clashes(p)
|
err = s.index.clashes(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Don't wrap the error since there is already an annotation deferred.
|
// Don't wrap the error since there is already an annotation deferred.
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.index.Add(p)
|
s.index.add(p)
|
||||||
|
|
||||||
log.Debug("client storage: added %q: IDs: %q [%d]", p.Name, p.IDs(), s.index.Size())
|
log.Debug("client storage: added %q: IDs: %q [%d]", p.Name, p.IDs(), s.index.size())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByName finds persistent client by name.
|
// FindByName finds persistent client by name. And returns its shallow copy.
|
||||||
func (s *Storage) FindByName(name string) (c *Persistent, found bool) {
|
func (s *Storage) FindByName(name string) (p *Persistent, ok bool) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
return s.index.FindByName(name)
|
p, ok = s.index.findByName(name)
|
||||||
|
if ok {
|
||||||
|
return p.ShallowClone(), ok
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find finds persistent client by string representation of the client ID, IP
|
// Find finds persistent client by string representation of the client ID, IP
|
||||||
// address, or MAC. And returns it shallow copy.
|
// address, or MAC. And returns its shallow copy.
|
||||||
func (s *Storage) Find(id string) (p *Persistent, ok bool) {
|
func (s *Storage) Find(id string) (p *Persistent, ok bool) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
p, ok = s.index.Find(id)
|
p, ok = s.index.find(id)
|
||||||
if ok {
|
if ok {
|
||||||
return p.ShallowClone(), ok
|
return p.ShallowClone(), ok
|
||||||
}
|
}
|
||||||
@ -101,12 +118,12 @@ func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
p, ok = s.index.Find(id)
|
p, ok = s.index.find(id)
|
||||||
if ok {
|
if ok {
|
||||||
return p.ShallowClone(), ok
|
return p.ShallowClone(), ok
|
||||||
}
|
}
|
||||||
|
|
||||||
p = s.index.FindByIPWithoutZone(ip)
|
p = s.index.findByIPWithoutZone(ip)
|
||||||
if p != nil {
|
if p != nil {
|
||||||
return p.ShallowClone(), true
|
return p.ShallowClone(), true
|
||||||
}
|
}
|
||||||
@ -114,12 +131,17 @@ func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByMAC finds persistent client by MAC.
|
// FindByMAC finds persistent client by MAC and returns its shallow copy.
|
||||||
func (s *Storage) FindByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
|
func (s *Storage) FindByMAC(mac net.HardwareAddr) (p *Persistent, ok bool) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
return s.index.FindByMAC(mac)
|
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
|
// RemoveByName removes persistent client information. ok is false if no such
|
||||||
@ -128,7 +150,7 @@ func (s *Storage) RemoveByName(name string) (ok bool) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
p, ok := s.index.FindByName(name)
|
p, ok := s.index.findByName(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -137,7 +159,7 @@ func (s *Storage) RemoveByName(name string) (ok bool) {
|
|||||||
log.Error("client storage: removing client %q: %s", p.Name, err)
|
log.Error("client storage: removing client %q: %s", p.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.index.Delete(p)
|
s.index.remove(p)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -156,7 +178,7 @@ func (s *Storage) Update(name string, p *Persistent) (err error) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
stored, ok := s.index.FindByName(name)
|
stored, ok := s.index.findByName(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("client %q is not found", name)
|
return fmt.Errorf("client %q is not found", name)
|
||||||
}
|
}
|
||||||
@ -166,14 +188,14 @@ func (s *Storage) Update(name string, p *Persistent) (err error) {
|
|||||||
// TODO(s.chzhen): Remove when frontend starts handling UIDs.
|
// TODO(s.chzhen): Remove when frontend starts handling UIDs.
|
||||||
p.UID = stored.UID
|
p.UID = stored.UID
|
||||||
|
|
||||||
err = s.index.Clashes(p)
|
err = s.index.clashes(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Don't wrap the error since there is already an annotation deferred.
|
// Don't wrap the error since there is already an annotation deferred.
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.index.Delete(stored)
|
s.index.remove(stored)
|
||||||
s.index.Add(p)
|
s.index.add(p)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -184,7 +206,7 @@ func (s *Storage) RangeByName(f func(c *Persistent) (cont bool)) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
s.index.RangeByName(f)
|
s.index.rangeByName(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the number of persistent clients.
|
// Size returns the number of persistent clients.
|
||||||
@ -192,7 +214,7 @@ func (s *Storage) Size() (n int) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
return s.index.Size()
|
return s.index.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseUpstreams closes upstream configurations of persistent clients.
|
// CloseUpstreams closes upstream configurations of persistent clients.
|
||||||
@ -200,11 +222,13 @@ func (s *Storage) CloseUpstreams() (err error) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
return s.index.CloseUpstreams()
|
return s.index.closeUpstreams()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientRuntime returns a copy of the saved runtime client by ip. If no such
|
// ClientRuntime returns a copy of the saved runtime client by ip. If no such
|
||||||
// client exists, returns nil.
|
// client exists, returns nil.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
|
func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
@ -214,6 +238,8 @@ func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
|
|||||||
|
|
||||||
// AddRuntime saves the runtime client information in the storage. IP address
|
// AddRuntime saves the runtime client information in the storage. IP address
|
||||||
// of a client must be unique. rc must not be nil.
|
// of a client must be unique. rc must not be nil.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
func (s *Storage) AddRuntime(rc *Runtime) {
|
func (s *Storage) AddRuntime(rc *Runtime) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
@ -222,6 +248,8 @@ func (s *Storage) AddRuntime(rc *Runtime) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SizeRuntime returns the number of the runtime clients.
|
// SizeRuntime returns the number of the runtime clients.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
func (s *Storage) SizeRuntime() (n int) {
|
func (s *Storage) SizeRuntime() (n int) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
@ -230,6 +258,8 @@ func (s *Storage) SizeRuntime() (n int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RangeRuntime calls f for each runtime client in an undefined order.
|
// RangeRuntime calls f for each runtime client in an undefined order.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
func (s *Storage) RangeRuntime(f func(rc *Runtime) (cont bool)) {
|
func (s *Storage) RangeRuntime(f func(rc *Runtime) (cont bool)) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
@ -238,6 +268,8 @@ func (s *Storage) RangeRuntime(f func(rc *Runtime) (cont bool)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteRuntime removes the runtime client by ip.
|
// DeleteRuntime removes the runtime client by ip.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
func (s *Storage) DeleteRuntime(ip netip.Addr) {
|
func (s *Storage) DeleteRuntime(ip netip.Addr) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
@ -247,6 +279,8 @@ func (s *Storage) DeleteRuntime(ip netip.Addr) {
|
|||||||
|
|
||||||
// DeleteBySource removes all runtime clients that have information only from
|
// DeleteBySource removes all runtime clients that have information only from
|
||||||
// the specified source and returns the number of removed clients.
|
// the specified source and returns the number of removed clients.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Use it.
|
||||||
func (s *Storage) DeleteBySource(src Source) (n int) {
|
func (s *Storage) DeleteBySource(src Source) (n int) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
@ -16,7 +16,9 @@ import (
|
|||||||
func newStorage(tb testing.TB, m []*client.Persistent) (s *client.Storage) {
|
func newStorage(tb testing.TB, m []*client.Persistent) (s *client.Storage) {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
|
||||||
s = client.NewStorage(nil)
|
s = client.NewStorage(&client.Config{
|
||||||
|
AllowedTags: nil,
|
||||||
|
})
|
||||||
|
|
||||||
for _, c := range m {
|
for _, c := range m {
|
||||||
c.UID = client.MustNewUID()
|
c.UID = client.MustNewUID()
|
||||||
@ -57,7 +59,9 @@ func TestStorage_Add(t *testing.T) {
|
|||||||
UID: existingClientUID,
|
UID: existingClientUID,
|
||||||
}
|
}
|
||||||
|
|
||||||
s := client.NewStorage(nil)
|
s := client.NewStorage(&client.Config{
|
||||||
|
AllowedTags: nil,
|
||||||
|
})
|
||||||
err := s.Add(existingClient)
|
err := s.Add(existingClient)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@ -137,7 +141,9 @@ func TestStorage_RemoveByName(t *testing.T) {
|
|||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
}
|
}
|
||||||
|
|
||||||
s := client.NewStorage(nil)
|
s := client.NewStorage(&client.Config{
|
||||||
|
AllowedTags: nil,
|
||||||
|
})
|
||||||
err := s.Add(existingClient)
|
err := s.Add(existingClient)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@ -162,7 +168,9 @@ func TestStorage_RemoveByName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("duplicate_remove", func(t *testing.T) {
|
t.Run("duplicate_remove", func(t *testing.T) {
|
||||||
s = client.NewStorage(nil)
|
s = client.NewStorage(&client.Config{
|
||||||
|
AllowedTags: nil,
|
||||||
|
})
|
||||||
err = s.Add(existingClient)
|
err = s.Add(existingClient)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
||||||
"github.com/AdguardTeam/dnsproxy/proxy"
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
||||||
"github.com/AdguardTeam/dnsproxy/upstream"
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
||||||
"github.com/AdguardTeam/golibs/container"
|
|
||||||
"github.com/AdguardTeam/golibs/errors"
|
"github.com/AdguardTeam/golibs/errors"
|
||||||
"github.com/AdguardTeam/golibs/hostsfile"
|
"github.com/AdguardTeam/golibs/hostsfile"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
@ -45,14 +44,12 @@ type DHCP interface {
|
|||||||
|
|
||||||
// clientsContainer is the storage of all runtime and persistent clients.
|
// clientsContainer is the storage of all runtime and persistent clients.
|
||||||
type clientsContainer struct {
|
type clientsContainer struct {
|
||||||
// clientIndex stores information about persistent clients.
|
// storage stores information about persistent clients.
|
||||||
clientIndex *client.Index
|
storage *client.Storage
|
||||||
|
|
||||||
// runtimeIndex stores information about runtime clients.
|
// runtimeIndex stores information about runtime clients.
|
||||||
runtimeIndex *client.RuntimeIndex
|
runtimeIndex *client.RuntimeIndex
|
||||||
|
|
||||||
allTags *container.MapSet[string]
|
|
||||||
|
|
||||||
// dhcp is the DHCP service implementation.
|
// dhcp is the DHCP service implementation.
|
||||||
dhcp DHCP
|
dhcp DHCP
|
||||||
|
|
||||||
@ -104,15 +101,15 @@ func (clients *clientsContainer) Init(
|
|||||||
filteringConf *filtering.Config,
|
filteringConf *filtering.Config,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
// TODO(s.chzhen): Refactor it.
|
// TODO(s.chzhen): Refactor it.
|
||||||
if clients.clientIndex != nil {
|
if clients.storage != nil {
|
||||||
return errors.Error("clients container already initialized")
|
return errors.Error("clients container already initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
clients.runtimeIndex = client.NewRuntimeIndex()
|
clients.runtimeIndex = client.NewRuntimeIndex()
|
||||||
|
|
||||||
clients.clientIndex = client.NewIndex()
|
clients.storage = client.NewStorage(&client.Config{
|
||||||
|
AllowedTags: clientTags,
|
||||||
clients.allTags = container.NewMapSet(clientTags...)
|
})
|
||||||
|
|
||||||
// TODO(e.burkov): Use [dhcpsvc] implementation when it's ready.
|
// TODO(e.burkov): Use [dhcpsvc] implementation when it's ready.
|
||||||
clients.dhcp = dhcpServer
|
clients.dhcp = dhcpServer
|
||||||
@ -217,7 +214,6 @@ type clientObject struct {
|
|||||||
// toPersistent returns an initialized persistent client if there are no errors.
|
// toPersistent returns an initialized persistent client if there are no errors.
|
||||||
func (o *clientObject) toPersistent(
|
func (o *clientObject) toPersistent(
|
||||||
filteringConf *filtering.Config,
|
filteringConf *filtering.Config,
|
||||||
allTags *container.MapSet[string],
|
|
||||||
) (cli *client.Persistent, err error) {
|
) (cli *client.Persistent, err error) {
|
||||||
cli = &client.Persistent{
|
cli = &client.Persistent{
|
||||||
Name: o.Name,
|
Name: o.Name,
|
||||||
@ -274,7 +270,7 @@ func (o *clientObject) toPersistent(
|
|||||||
|
|
||||||
cli.BlockedServices = o.BlockedServices.Clone()
|
cli.BlockedServices = o.BlockedServices.Clone()
|
||||||
|
|
||||||
cli.SetTags(o.Tags, allTags)
|
cli.Tags = slices.Clone(o.Tags)
|
||||||
|
|
||||||
return cli, nil
|
return cli, nil
|
||||||
}
|
}
|
||||||
@ -287,22 +283,14 @@ func (clients *clientsContainer) addFromConfig(
|
|||||||
) (err error) {
|
) (err error) {
|
||||||
for i, o := range objects {
|
for i, o := range objects {
|
||||||
var cli *client.Persistent
|
var cli *client.Persistent
|
||||||
cli, err = o.toPersistent(filteringConf, clients.allTags)
|
cli, err = o.toPersistent(filteringConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("clients: init persistent client at index %d: %w", i, err)
|
return fmt.Errorf("clients: init persistent client at index %d: %w", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(s.chzhen): Consider moving to the client index constructor.
|
err = clients.storage.Add(cli)
|
||||||
err = clients.clientIndex.ClashesUID(cli)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("adding client %s at index %d: %w", cli.Name, i, err)
|
return fmt.Errorf("adding client %q at index %d: %w", cli.Name, i, err)
|
||||||
}
|
|
||||||
|
|
||||||
err = clients.add(cli)
|
|
||||||
if err != nil {
|
|
||||||
// TODO(s.chzhen): Return an error instead of logging if more
|
|
||||||
// stringent requirements are implemented.
|
|
||||||
log.Error("clients: adding client %s at index %d: %s", cli.Name, i, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,8 +303,8 @@ func (clients *clientsContainer) forConfig() (objs []*clientObject) {
|
|||||||
clients.lock.Lock()
|
clients.lock.Lock()
|
||||||
defer clients.lock.Unlock()
|
defer clients.lock.Unlock()
|
||||||
|
|
||||||
objs = make([]*clientObject, 0, clients.clientIndex.Size())
|
objs = make([]*clientObject, 0, clients.storage.Size())
|
||||||
clients.clientIndex.RangeByName(func(cli *client.Persistent) (cont bool) {
|
clients.storage.RangeByName(func(cli *client.Persistent) (cont bool) {
|
||||||
objs = append(objs, &clientObject{
|
objs = append(objs, &clientObject{
|
||||||
Name: cli.Name,
|
Name: cli.Name,
|
||||||
|
|
||||||
@ -360,7 +348,7 @@ func (clients *clientsContainer) periodicUpdate() {
|
|||||||
|
|
||||||
// clientSource checks if client with this IP address already exists and returns
|
// clientSource checks if client with this IP address already exists and returns
|
||||||
// the source which updated it last. It returns [client.SourceNone] if the
|
// the source which updated it last. It returns [client.SourceNone] if the
|
||||||
// client doesn't exist.
|
// client doesn't exist. Note that it is only used in tests.
|
||||||
func (clients *clientsContainer) clientSource(ip netip.Addr) (src client.Source) {
|
func (clients *clientsContainer) clientSource(ip netip.Addr) (src client.Source) {
|
||||||
clients.lock.Lock()
|
clients.lock.Lock()
|
||||||
defer clients.lock.Unlock()
|
defer clients.lock.Unlock()
|
||||||
@ -419,12 +407,8 @@ func (clients *clientsContainer) clientOrArtificial(
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
cli, ok := clients.find(id)
|
cli, ok := clients.storage.FindLoose(ip, id)
|
||||||
if !ok {
|
if ok {
|
||||||
cli = clients.clientIndex.FindByIPWithoutZone(ip)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cli != nil {
|
|
||||||
return &querylog.Client{
|
return &querylog.Client{
|
||||||
Name: cli.Name,
|
Name: cli.Name,
|
||||||
IgnoreQueryLog: cli.IgnoreQueryLog,
|
IgnoreQueryLog: cli.IgnoreQueryLog,
|
||||||
@ -456,7 +440,7 @@ func (clients *clientsContainer) find(id string) (c *client.Persistent, ok bool)
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.ShallowClone(), true
|
return c, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// shouldCountClient is a wrapper around [clientsContainer.find] to make it a
|
// shouldCountClient is a wrapper around [clientsContainer.find] to make it a
|
||||||
@ -530,7 +514,7 @@ func (clients *clientsContainer) UpstreamConfigByID(
|
|||||||
// findLocked searches for a client by its ID. clients.lock is expected to be
|
// findLocked searches for a client by its ID. clients.lock is expected to be
|
||||||
// locked.
|
// locked.
|
||||||
func (clients *clientsContainer) findLocked(id string) (c *client.Persistent, ok bool) {
|
func (clients *clientsContainer) findLocked(id string) (c *client.Persistent, ok bool) {
|
||||||
c, ok = clients.clientIndex.Find(id)
|
c, ok = clients.storage.Find(id)
|
||||||
if ok {
|
if ok {
|
||||||
return c, true
|
return c, true
|
||||||
}
|
}
|
||||||
@ -552,7 +536,7 @@ func (clients *clientsContainer) findDHCP(ip netip.Addr) (c *client.Persistent,
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return clients.clientIndex.FindByMAC(foundMAC)
|
return clients.storage.FindByMAC(foundMAC)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtimeClient returns a runtime client from internal index. Note that it
|
// runtimeClient returns a runtime client from internal index. Note that it
|
||||||
@ -586,114 +570,6 @@ func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *client.Ru
|
|||||||
return rc
|
return rc
|
||||||
}
|
}
|
||||||
|
|
||||||
// check validates the client. It also sorts the client tags.
|
|
||||||
func (clients *clientsContainer) check(c *client.Persistent) (err error) {
|
|
||||||
switch {
|
|
||||||
case c == nil:
|
|
||||||
return errors.Error("client is nil")
|
|
||||||
case c.Name == "":
|
|
||||||
return errors.Error("invalid name")
|
|
||||||
case c.IDsLen() == 0:
|
|
||||||
return errors.Error("id required")
|
|
||||||
default:
|
|
||||||
// Go on.
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, t := range c.Tags {
|
|
||||||
if !clients.allTags.Has(t) {
|
|
||||||
return fmt.Errorf("invalid tag: %q", t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(s.chzhen): Move to the constructor.
|
|
||||||
slices.Sort(c.Tags)
|
|
||||||
|
|
||||||
_, err = proxy.ParseUpstreamsConfig(c.Upstreams, &upstream.Options{})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid upstream servers: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// add adds a persistent client or returns an error.
|
|
||||||
func (clients *clientsContainer) add(c *client.Persistent) (err error) {
|
|
||||||
err = clients.check(c)
|
|
||||||
if err != nil {
|
|
||||||
// Don't wrap the error since it's informative enough as is.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
clients.lock.Lock()
|
|
||||||
defer clients.lock.Unlock()
|
|
||||||
|
|
||||||
err = clients.clientIndex.Clashes(c)
|
|
||||||
if err != nil {
|
|
||||||
// Don't wrap the error since it's informative enough as is.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
clients.addLocked(c)
|
|
||||||
|
|
||||||
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs(), clients.clientIndex.Size())
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// addLocked c to the indexes. clients.lock is expected to be locked.
|
|
||||||
func (clients *clientsContainer) addLocked(c *client.Persistent) {
|
|
||||||
clients.clientIndex.Add(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove removes a client. ok is false if there is no such client.
|
|
||||||
func (clients *clientsContainer) remove(name string) (ok bool) {
|
|
||||||
clients.lock.Lock()
|
|
||||||
defer clients.lock.Unlock()
|
|
||||||
|
|
||||||
c, ok := clients.clientIndex.FindByName(name)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
clients.removeLocked(c)
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeLocked removes c from the indexes. clients.lock is expected to be
|
|
||||||
// locked.
|
|
||||||
func (clients *clientsContainer) removeLocked(c *client.Persistent) {
|
|
||||||
if err := c.CloseUpstreams(); err != nil {
|
|
||||||
log.Error("client container: removing client %s: %s", c.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the ID index.
|
|
||||||
clients.clientIndex.Delete(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// update updates a client by its name.
|
|
||||||
func (clients *clientsContainer) update(prev, c *client.Persistent) (err error) {
|
|
||||||
err = clients.check(c)
|
|
||||||
if err != nil {
|
|
||||||
// Don't wrap the error since it's informative enough as is.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
clients.lock.Lock()
|
|
||||||
defer clients.lock.Unlock()
|
|
||||||
|
|
||||||
err = clients.clientIndex.Clashes(c)
|
|
||||||
if err != nil {
|
|
||||||
// Don't wrap the error since it's informative enough as is.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
clients.removeLocked(prev)
|
|
||||||
clients.addLocked(c)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// setWHOISInfo sets the WHOIS information for a client. clients.lock is
|
// setWHOISInfo sets the WHOIS information for a client. clients.lock is
|
||||||
// expected to be locked.
|
// expected to be locked.
|
||||||
func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
|
func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
|
||||||
@ -855,5 +731,5 @@ func (clients *clientsContainer) addFromSystemARP() {
|
|||||||
// close gracefully closes all the client-specific upstream configurations of
|
// close gracefully closes all the client-specific upstream configurations of
|
||||||
// the persistent clients.
|
// the persistent clients.
|
||||||
func (clients *clientsContainer) close() (err error) {
|
func (clients *clientsContainer) close() (err error) {
|
||||||
return clients.clientIndex.CloseUpstreams()
|
return clients.storage.CloseUpstreams()
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ func TestClients(t *testing.T) {
|
|||||||
IPs: []netip.Addr{cli1IP, cliIPv6},
|
IPs: []netip.Addr{cli1IP, cliIPv6},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := clients.add(c)
|
err := clients.storage.Add(c)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
c = &client.Persistent{
|
c = &client.Persistent{
|
||||||
@ -81,7 +81,7 @@ func TestClients(t *testing.T) {
|
|||||||
IPs: []netip.Addr{cli2IP},
|
IPs: []netip.Addr{cli2IP},
|
||||||
}
|
}
|
||||||
|
|
||||||
err = clients.add(c)
|
err = clients.storage.Add(c)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
c, ok := clients.find(cli1)
|
c, ok := clients.find(cli1)
|
||||||
@ -107,7 +107,7 @@ func TestClients(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("add_fail_name", func(t *testing.T) {
|
t.Run("add_fail_name", func(t *testing.T) {
|
||||||
err := clients.add(&client.Persistent{
|
err := clients.storage.Add(&client.Persistent{
|
||||||
Name: "client1",
|
Name: "client1",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
IPs: []netip.Addr{netip.MustParseAddr("1.2.3.5")},
|
IPs: []netip.Addr{netip.MustParseAddr("1.2.3.5")},
|
||||||
@ -116,7 +116,7 @@ func TestClients(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("add_fail_ip", func(t *testing.T) {
|
t.Run("add_fail_ip", func(t *testing.T) {
|
||||||
err := clients.add(&client.Persistent{
|
err := clients.storage.Add(&client.Persistent{
|
||||||
Name: "client3",
|
Name: "client3",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
})
|
})
|
||||||
@ -124,7 +124,7 @@ func TestClients(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("update_fail_ip", func(t *testing.T) {
|
t.Run("update_fail_ip", func(t *testing.T) {
|
||||||
err := clients.update(&client.Persistent{Name: "client1"}, &client.Persistent{
|
err := clients.storage.Update("client1", &client.Persistent{
|
||||||
Name: "client1",
|
Name: "client1",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
})
|
})
|
||||||
@ -139,11 +139,11 @@ func TestClients(t *testing.T) {
|
|||||||
cliNewIP = netip.MustParseAddr(cliNew)
|
cliNewIP = netip.MustParseAddr(cliNew)
|
||||||
)
|
)
|
||||||
|
|
||||||
prev, ok := clients.clientIndex.FindByName("client1")
|
prev, ok := clients.storage.FindByName("client1")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.NotNil(t, prev)
|
require.NotNil(t, prev)
|
||||||
|
|
||||||
err := clients.update(prev, &client.Persistent{
|
err := clients.storage.Update("client1", &client.Persistent{
|
||||||
Name: "client1",
|
Name: "client1",
|
||||||
UID: prev.UID,
|
UID: prev.UID,
|
||||||
IPs: []netip.Addr{cliNewIP},
|
IPs: []netip.Addr{cliNewIP},
|
||||||
@ -155,11 +155,11 @@ func TestClients(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, clients.clientSource(cliNewIP), client.SourcePersistent)
|
assert.Equal(t, clients.clientSource(cliNewIP), client.SourcePersistent)
|
||||||
|
|
||||||
prev, ok = clients.clientIndex.FindByName("client1")
|
prev, ok = clients.storage.FindByName("client1")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.NotNil(t, prev)
|
require.NotNil(t, prev)
|
||||||
|
|
||||||
err = clients.update(prev, &client.Persistent{
|
err = clients.storage.Update("client1", &client.Persistent{
|
||||||
Name: "client1-renamed",
|
Name: "client1-renamed",
|
||||||
UID: prev.UID,
|
UID: prev.UID,
|
||||||
IPs: []netip.Addr{cliNewIP},
|
IPs: []netip.Addr{cliNewIP},
|
||||||
@ -173,7 +173,7 @@ func TestClients(t *testing.T) {
|
|||||||
assert.Equal(t, "client1-renamed", c.Name)
|
assert.Equal(t, "client1-renamed", c.Name)
|
||||||
assert.True(t, c.UseOwnSettings)
|
assert.True(t, c.UseOwnSettings)
|
||||||
|
|
||||||
nilCli, ok := clients.clientIndex.FindByName("client1")
|
nilCli, ok := clients.storage.FindByName("client1")
|
||||||
require.False(t, ok)
|
require.False(t, ok)
|
||||||
|
|
||||||
assert.Nil(t, nilCli)
|
assert.Nil(t, nilCli)
|
||||||
@ -184,7 +184,7 @@ func TestClients(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("del_success", func(t *testing.T) {
|
t.Run("del_success", func(t *testing.T) {
|
||||||
ok := clients.remove("client1-renamed")
|
ok := clients.storage.RemoveByName("client1-renamed")
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
_, ok = clients.find("1.1.1.2")
|
_, ok = clients.find("1.1.1.2")
|
||||||
@ -192,7 +192,7 @@ func TestClients(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("del_fail", func(t *testing.T) {
|
t.Run("del_fail", func(t *testing.T) {
|
||||||
ok := clients.remove("client3")
|
ok := clients.storage.RemoveByName("client3")
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ func TestClientsWHOIS(t *testing.T) {
|
|||||||
t.Run("can't_set_manually-added", func(t *testing.T) {
|
t.Run("can't_set_manually-added", func(t *testing.T) {
|
||||||
ip := netip.MustParseAddr("1.1.1.2")
|
ip := netip.MustParseAddr("1.1.1.2")
|
||||||
|
|
||||||
err := clients.add(&client.Persistent{
|
err := clients.storage.Add(&client.Persistent{
|
||||||
Name: "client1",
|
Name: "client1",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.2")},
|
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.2")},
|
||||||
@ -272,7 +272,7 @@ func TestClientsWHOIS(t *testing.T) {
|
|||||||
rc := clients.runtimeIndex.Client(ip)
|
rc := clients.runtimeIndex.Client(ip)
|
||||||
require.Nil(t, rc)
|
require.Nil(t, rc)
|
||||||
|
|
||||||
assert.True(t, clients.remove("client1"))
|
assert.True(t, clients.storage.RemoveByName("client1"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,7 +283,7 @@ func TestClientsAddExisting(t *testing.T) {
|
|||||||
ip := netip.MustParseAddr("1.1.1.1")
|
ip := netip.MustParseAddr("1.1.1.1")
|
||||||
|
|
||||||
// Add a client.
|
// Add a client.
|
||||||
err := clients.add(&client.Persistent{
|
err := clients.storage.Add(&client.Persistent{
|
||||||
Name: "client1",
|
Name: "client1",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")},
|
IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")},
|
||||||
@ -333,7 +333,7 @@ func TestClientsAddExisting(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Add a new client with the same IP as for a client with MAC.
|
// Add a new client with the same IP as for a client with MAC.
|
||||||
err = clients.add(&client.Persistent{
|
err = clients.storage.Add(&client.Persistent{
|
||||||
Name: "client2",
|
Name: "client2",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
IPs: []netip.Addr{ip},
|
IPs: []netip.Addr{ip},
|
||||||
@ -341,7 +341,7 @@ func TestClientsAddExisting(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Add a new client with the IP from the first client's IP range.
|
// Add a new client with the IP from the first client's IP range.
|
||||||
err = clients.add(&client.Persistent{
|
err = clients.storage.Add(&client.Persistent{
|
||||||
Name: "client3",
|
Name: "client3",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")},
|
IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")},
|
||||||
@ -354,7 +354,7 @@ func TestClientsCustomUpstream(t *testing.T) {
|
|||||||
clients := newClientsContainer(t)
|
clients := newClientsContainer(t)
|
||||||
|
|
||||||
// Add client with upstreams.
|
// Add client with upstreams.
|
||||||
err := clients.add(&client.Persistent{
|
err := clients.storage.Add(&client.Persistent{
|
||||||
Name: "client1",
|
Name: "client1",
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")},
|
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")},
|
||||||
|
@ -96,7 +96,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
|
|||||||
clients.lock.Lock()
|
clients.lock.Lock()
|
||||||
defer clients.lock.Unlock()
|
defer clients.lock.Unlock()
|
||||||
|
|
||||||
clients.clientIndex.Range(func(c *client.Persistent) (cont bool) {
|
clients.storage.RangeByName(func(c *client.Persistent) (cont bool) {
|
||||||
cj := clientToJSON(c)
|
cj := clientToJSON(c)
|
||||||
data.Clients = append(data.Clients, cj)
|
data.Clients = append(data.Clients, cj)
|
||||||
|
|
||||||
@ -336,7 +336,7 @@ func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = clients.add(c)
|
err = clients.storage.Add(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
|
||||||
@ -364,7 +364,7 @@ func (clients *clientsContainer) handleDelClient(w http.ResponseWriter, r *http.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !clients.remove(cj.Name) {
|
if !clients.storage.RemoveByName(cj.Name) {
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "Client not found")
|
aghhttp.Error(r, w, http.StatusBadRequest, "Client not found")
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -399,30 +399,14 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var prev *client.Persistent
|
c, err := clients.jsonToClient(dj.Data, nil)
|
||||||
var ok bool
|
|
||||||
|
|
||||||
func() {
|
|
||||||
clients.lock.Lock()
|
|
||||||
defer clients.lock.Unlock()
|
|
||||||
|
|
||||||
prev, ok = clients.clientIndex.FindByName(dj.Name)
|
|
||||||
}()
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "client not found")
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := clients.jsonToClient(dj.Data, prev)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = clients.update(prev, c)
|
err = clients.storage.Update(dj.Name, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
|
||||||
|
@ -198,11 +198,11 @@ func TestClientsContainer_HandleDelClient(t *testing.T) {
|
|||||||
clients := newClientsContainer(t)
|
clients := newClientsContainer(t)
|
||||||
|
|
||||||
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
|
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
|
||||||
err := clients.add(clientOne)
|
err := clients.storage.Add(clientOne)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
|
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
|
||||||
err = clients.add(clientTwo)
|
err = clients.storage.Add(clientTwo)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})
|
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})
|
||||||
@ -260,7 +260,7 @@ func TestClientsContainer_HandleUpdateClient(t *testing.T) {
|
|||||||
clients := newClientsContainer(t)
|
clients := newClientsContainer(t)
|
||||||
|
|
||||||
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
|
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
|
||||||
err := clients.add(clientOne)
|
err := clients.storage.Add(clientOne)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assertPersistentClients(t, clients, []*client.Persistent{clientOne})
|
assertPersistentClients(t, clients, []*client.Persistent{clientOne})
|
||||||
@ -342,11 +342,11 @@ func TestClientsContainer_HandleFindClient(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
|
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
|
||||||
err := clients.add(clientOne)
|
err := clients.storage.Add(clientOne)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
|
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
|
||||||
err = clients.add(clientTwo)
|
err = clients.storage.Add(clientTwo)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})
|
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})
|
||||||
|
@ -13,17 +13,21 @@ import (
|
|||||||
|
|
||||||
var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
|
var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
|
||||||
|
|
||||||
// newIDIndex is a helper function that returns a client index filled with
|
// newStorage is a helper function that returns a client storage filled with
|
||||||
// persistent clients from the m. It also generates a UID for each client.
|
// persistent clients. It also generates a UID for each client.
|
||||||
func newIDIndex(m []*client.Persistent) (ci *client.Index) {
|
func newStorage(tb testing.TB, clients []*client.Persistent) (s *client.Storage) {
|
||||||
ci = client.NewIndex()
|
tb.Helper()
|
||||||
|
|
||||||
for _, c := range m {
|
s = client.NewStorage(&client.Config{
|
||||||
c.UID = client.MustNewUID()
|
AllowedTags: nil,
|
||||||
ci.Add(c)
|
})
|
||||||
|
|
||||||
|
for _, p := range clients {
|
||||||
|
p.UID = client.MustNewUID()
|
||||||
|
require.NoError(tb, s.Add(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
return ci
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApplyAdditionalFiltering(t *testing.T) {
|
func TestApplyAdditionalFiltering(t *testing.T) {
|
||||||
@ -36,7 +40,8 @@ func TestApplyAdditionalFiltering(t *testing.T) {
|
|||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Context.clients.clientIndex = newIDIndex([]*client.Persistent{{
|
Context.clients.storage = newStorage(t, []*client.Persistent{{
|
||||||
|
Name: "default",
|
||||||
ClientIDs: []string{"default"},
|
ClientIDs: []string{"default"},
|
||||||
UseOwnSettings: false,
|
UseOwnSettings: false,
|
||||||
SafeSearchConf: filtering.SafeSearchConfig{Enabled: false},
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: false},
|
||||||
@ -44,6 +49,7 @@ func TestApplyAdditionalFiltering(t *testing.T) {
|
|||||||
SafeBrowsingEnabled: false,
|
SafeBrowsingEnabled: false,
|
||||||
ParentalEnabled: false,
|
ParentalEnabled: false,
|
||||||
}, {
|
}, {
|
||||||
|
Name: "custom_filtering",
|
||||||
ClientIDs: []string{"custom_filtering"},
|
ClientIDs: []string{"custom_filtering"},
|
||||||
UseOwnSettings: true,
|
UseOwnSettings: true,
|
||||||
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
||||||
@ -51,6 +57,7 @@ func TestApplyAdditionalFiltering(t *testing.T) {
|
|||||||
SafeBrowsingEnabled: true,
|
SafeBrowsingEnabled: true,
|
||||||
ParentalEnabled: true,
|
ParentalEnabled: true,
|
||||||
}, {
|
}, {
|
||||||
|
Name: "partial_custom_filtering",
|
||||||
ClientIDs: []string{"partial_custom_filtering"},
|
ClientIDs: []string{"partial_custom_filtering"},
|
||||||
UseOwnSettings: true,
|
UseOwnSettings: true,
|
||||||
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
||||||
@ -121,16 +128,19 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
|
|||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Context.clients.clientIndex = newIDIndex([]*client.Persistent{{
|
Context.clients.storage = newStorage(t, []*client.Persistent{{
|
||||||
|
Name: "default",
|
||||||
ClientIDs: []string{"default"},
|
ClientIDs: []string{"default"},
|
||||||
UseOwnBlockedServices: false,
|
UseOwnBlockedServices: false,
|
||||||
}, {
|
}, {
|
||||||
|
Name: "no_services",
|
||||||
ClientIDs: []string{"no_services"},
|
ClientIDs: []string{"no_services"},
|
||||||
BlockedServices: &filtering.BlockedServices{
|
BlockedServices: &filtering.BlockedServices{
|
||||||
Schedule: schedule.EmptyWeekly(),
|
Schedule: schedule.EmptyWeekly(),
|
||||||
},
|
},
|
||||||
UseOwnBlockedServices: true,
|
UseOwnBlockedServices: true,
|
||||||
}, {
|
}, {
|
||||||
|
Name: "services",
|
||||||
ClientIDs: []string{"services"},
|
ClientIDs: []string{"services"},
|
||||||
BlockedServices: &filtering.BlockedServices{
|
BlockedServices: &filtering.BlockedServices{
|
||||||
Schedule: schedule.EmptyWeekly(),
|
Schedule: schedule.EmptyWeekly(),
|
||||||
@ -138,6 +148,7 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
|
|||||||
},
|
},
|
||||||
UseOwnBlockedServices: true,
|
UseOwnBlockedServices: true,
|
||||||
}, {
|
}, {
|
||||||
|
Name: "invalid_services",
|
||||||
ClientIDs: []string{"invalid_services"},
|
ClientIDs: []string{"invalid_services"},
|
||||||
BlockedServices: &filtering.BlockedServices{
|
BlockedServices: &filtering.BlockedServices{
|
||||||
Schedule: schedule.EmptyWeekly(),
|
Schedule: schedule.EmptyWeekly(),
|
||||||
@ -145,6 +156,7 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
|
|||||||
},
|
},
|
||||||
UseOwnBlockedServices: true,
|
UseOwnBlockedServices: true,
|
||||||
}, {
|
}, {
|
||||||
|
Name: "allow_all",
|
||||||
ClientIDs: []string{"allow_all"},
|
ClientIDs: []string{"allow_all"},
|
||||||
BlockedServices: &filtering.BlockedServices{
|
BlockedServices: &filtering.BlockedServices{
|
||||||
Schedule: schedule.FullWeekly(),
|
Schedule: schedule.FullWeekly(),
|
||||||
|
Loading…
Reference in New Issue
Block a user