all: imp code
This commit is contained in:
parent
f7315be742
commit
ba0ba2478c
|
@ -1,7 +1,6 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
@ -9,7 +8,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
||||||
|
@ -19,6 +17,33 @@ import (
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Tags is the list of available client tags.
|
||||||
|
var Tags = []string{
|
||||||
|
"device_audio",
|
||||||
|
"device_camera",
|
||||||
|
"device_gameconsole",
|
||||||
|
"device_laptop",
|
||||||
|
"device_nas", // Network-attached Storage
|
||||||
|
"device_other",
|
||||||
|
"device_pc",
|
||||||
|
"device_phone",
|
||||||
|
"device_printer",
|
||||||
|
"device_securityalarm",
|
||||||
|
"device_tablet",
|
||||||
|
"device_tv",
|
||||||
|
|
||||||
|
"os_android",
|
||||||
|
"os_ios",
|
||||||
|
"os_linux",
|
||||||
|
"os_macos",
|
||||||
|
"os_other",
|
||||||
|
"os_windows",
|
||||||
|
|
||||||
|
"user_admin",
|
||||||
|
"user_child",
|
||||||
|
"user_regular",
|
||||||
|
}
|
||||||
|
|
||||||
// DHCP is an interface for accessing DHCP lease data the [Storage] needs.
|
// DHCP is an interface for accessing DHCP lease data the [Storage] needs.
|
||||||
type DHCP interface {
|
type DHCP interface {
|
||||||
// Leases returns all the DHCP leases.
|
// Leases returns all the DHCP leases.
|
||||||
|
@ -35,20 +60,20 @@ type DHCP interface {
|
||||||
MACByIP(ip netip.Addr) (mac net.HardwareAddr)
|
MACByIP(ip netip.Addr) (mac net.HardwareAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// emptyDHCP is the empty [DHCP] implementation that does nothing.
|
// EmptyDHCP is the empty [DHCP] implementation that does nothing.
|
||||||
type emptyDHCP struct{}
|
type EmptyDHCP struct{}
|
||||||
|
|
||||||
// type check
|
// type check
|
||||||
var _ DHCP = emptyDHCP{}
|
var _ DHCP = EmptyDHCP{}
|
||||||
|
|
||||||
// Leases implements the [DHCP] interface for emptyDHCP.
|
// Leases implements the [DHCP] interface for emptyDHCP.
|
||||||
func (emptyDHCP) Leases() (leases []*dhcpsvc.Lease) { return nil }
|
func (EmptyDHCP) Leases() (leases []*dhcpsvc.Lease) { return nil }
|
||||||
|
|
||||||
// HostByIP implements the [DHCP] interface for emptyDHCP.
|
// HostByIP implements the [DHCP] interface for emptyDHCP.
|
||||||
func (emptyDHCP) HostByIP(_ netip.Addr) (host string) { return "" }
|
func (EmptyDHCP) HostByIP(_ netip.Addr) (host string) { return "" }
|
||||||
|
|
||||||
// MACByIP implements the [DHCP] interface for emptyDHCP.
|
// MACByIP implements the [DHCP] interface for emptyDHCP.
|
||||||
func (emptyDHCP) MACByIP(_ netip.Addr) (mac net.HardwareAddr) { return nil }
|
func (EmptyDHCP) MACByIP(_ netip.Addr) (mac net.HardwareAddr) { return nil }
|
||||||
|
|
||||||
// HostsContainer is an interface for receiving updates to the system hosts
|
// HostsContainer is an interface for receiving updates to the system hosts
|
||||||
// file.
|
// file.
|
||||||
|
@ -56,9 +81,10 @@ type HostsContainer interface {
|
||||||
Upd() (updates <-chan *hostsfile.DefaultStorage)
|
Upd() (updates <-chan *hostsfile.DefaultStorage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config is the client storage configuration structure.
|
// StorageConfig is the client storage configuration structure.
|
||||||
type Config struct {
|
type StorageConfig struct {
|
||||||
// DHCP is used to update [SourceDHCP] runtime client information.
|
// DHCP is used to update [SourceDHCP] runtime client information. It must
|
||||||
|
// not be nil.
|
||||||
DHCP DHCP
|
DHCP DHCP
|
||||||
|
|
||||||
// EtcHosts is used to update [SourceHostsFile] runtime client information.
|
// EtcHosts is used to update [SourceHostsFile] runtime client information.
|
||||||
|
@ -67,9 +93,6 @@ type Config struct {
|
||||||
// ARPDB is used to update [SourceARP] runtime client information.
|
// ARPDB is used to update [SourceARP] runtime client information.
|
||||||
ARPDB arpdb.Interface
|
ARPDB arpdb.Interface
|
||||||
|
|
||||||
// AllowedTags is a list of all allowed client tags.
|
|
||||||
AllowedTags []string
|
|
||||||
|
|
||||||
// InitialClients is a list of persistent clients parsed from the
|
// InitialClients is a list of persistent clients parsed from the
|
||||||
// configuration file. Each client must not be nil.
|
// configuration file. Each client must not be nil.
|
||||||
InitialClients []*Persistent
|
InitialClients []*Persistent
|
||||||
|
@ -111,14 +134,13 @@ type Storage struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStorage returns initialized client storage. conf must not be nil.
|
// NewStorage returns initialized client storage. conf must not be nil.
|
||||||
func NewStorage(conf *Config) (s *Storage, err error) {
|
func NewStorage(conf *StorageConfig) (s *Storage, err error) {
|
||||||
allowedTags := container.NewMapSet(conf.AllowedTags...)
|
|
||||||
s = &Storage{
|
s = &Storage{
|
||||||
allowedTags: allowedTags,
|
allowedTags: container.NewMapSet(Tags...),
|
||||||
mu: &sync.Mutex{},
|
mu: &sync.Mutex{},
|
||||||
index: newIndex(),
|
index: newIndex(),
|
||||||
runtimeIndex: newRuntimeIndex(),
|
runtimeIndex: newRuntimeIndex(),
|
||||||
dhcp: cmp.Or(conf.DHCP, DHCP(emptyDHCP{})),
|
dhcp: conf.DHCP,
|
||||||
etcHosts: conf.EtcHosts,
|
etcHosts: conf.EtcHosts,
|
||||||
arpDB: conf.ARPDB,
|
arpDB: conf.ARPDB,
|
||||||
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
|
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
|
||||||
|
@ -132,16 +154,14 @@ func NewStorage(conf *Config) (s *Storage, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if hc, ok := s.etcHosts.(*aghnet.HostsContainer); ok && hc == nil {
|
|
||||||
s.etcHosts = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ReloadARP()
|
s.ReloadARP()
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start starts the goroutines for updating the runtime client information.
|
// Start starts the goroutines for updating the runtime client information.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Pass context.
|
||||||
func (s *Storage) Start(_ context.Context) (err error) {
|
func (s *Storage) Start(_ context.Context) (err error) {
|
||||||
go s.periodicARPUpdate()
|
go s.periodicARPUpdate()
|
||||||
go s.handleHostsUpdates()
|
go s.handleHostsUpdates()
|
||||||
|
@ -150,6 +170,8 @@ func (s *Storage) Start(_ context.Context) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown gracefully stops the client storage.
|
// Shutdown gracefully stops the client storage.
|
||||||
|
//
|
||||||
|
// TODO(s.chzhen): Pass context.
|
||||||
func (s *Storage) Shutdown(_ context.Context) (err error) {
|
func (s *Storage) Shutdown(_ context.Context) (err error) {
|
||||||
close(s.done)
|
close(s.done)
|
||||||
|
|
||||||
|
|
|
@ -109,7 +109,8 @@ func TestStorage_Add_hostsfile(t *testing.T) {
|
||||||
onUpd: func() (updates <-chan *hostsfile.DefaultStorage) { return hostCh },
|
onUpd: func() (updates <-chan *hostsfile.DefaultStorage) { return hostCh },
|
||||||
}
|
}
|
||||||
|
|
||||||
storage, err := client.NewStorage(&client.Config{
|
storage, err := client.NewStorage(&client.StorageConfig{
|
||||||
|
DHCP: client.EmptyDHCP{},
|
||||||
EtcHosts: h,
|
EtcHosts: h,
|
||||||
ARPClientsUpdatePeriod: testTimeout / 10,
|
ARPClientsUpdatePeriod: testTimeout / 10,
|
||||||
})
|
})
|
||||||
|
@ -196,7 +197,8 @@ func TestStorage_Add_arp(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
storage, err := client.NewStorage(&client.Config{
|
storage, err := client.NewStorage(&client.StorageConfig{
|
||||||
|
DHCP: client.EmptyDHCP{},
|
||||||
ARPDB: a,
|
ARPDB: a,
|
||||||
ARPClientsUpdatePeriod: testTimeout / 10,
|
ARPClientsUpdatePeriod: testTimeout / 10,
|
||||||
})
|
})
|
||||||
|
@ -270,7 +272,9 @@ func TestStorage_Add_whois(t *testing.T) {
|
||||||
cliName3 = "client_three"
|
cliName3 = "client_three"
|
||||||
)
|
)
|
||||||
|
|
||||||
storage, err := client.NewStorage(&client.Config{})
|
storage, err := client.NewStorage(&client.StorageConfig{
|
||||||
|
DHCP: client.EmptyDHCP{},
|
||||||
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
whois := &whois.Info{
|
whois := &whois.Info{
|
||||||
|
@ -359,7 +363,7 @@ func TestClientsDHCP(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
storage, err := client.NewStorage(&client.Config{
|
storage, err := client.NewStorage(&client.StorageConfig{
|
||||||
DHCP: d,
|
DHCP: d,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -431,7 +435,7 @@ func TestClientsAddExisting(t *testing.T) {
|
||||||
dhcpServer, err := dhcpd.Create(config)
|
dhcpServer, err := dhcpd.Create(config)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
storage, err := client.NewStorage(&client.Config{
|
storage, err := client.NewStorage(&client.StorageConfig{
|
||||||
DHCP: dhcpServer,
|
DHCP: dhcpServer,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -495,8 +499,8 @@ func TestClientsAddExisting(t *testing.T) {
|
||||||
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, err := client.NewStorage(&client.Config{
|
s, err := client.NewStorage(&client.StorageConfig{
|
||||||
AllowedTags: nil,
|
DHCP: client.EmptyDHCP{},
|
||||||
})
|
})
|
||||||
require.NoError(tb, err)
|
require.NoError(tb, err)
|
||||||
|
|
||||||
|
@ -544,9 +548,7 @@ func TestStorage_Add(t *testing.T) {
|
||||||
UID: existingClientUID,
|
UID: existingClientUID,
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := client.NewStorage(&client.Config{
|
s, err := client.NewStorage(&client.StorageConfig{})
|
||||||
AllowedTags: []string{allowedTag},
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = s.Add(existingClient)
|
err = s.Add(existingClient)
|
||||||
|
@ -637,9 +639,7 @@ func TestStorage_RemoveByName(t *testing.T) {
|
||||||
UID: client.MustNewUID(),
|
UID: client.MustNewUID(),
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := client.NewStorage(&client.Config{
|
s, err := client.NewStorage(&client.StorageConfig{})
|
||||||
AllowedTags: nil,
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = s.Add(existingClient)
|
err = s.Add(existingClient)
|
||||||
|
@ -666,9 +666,7 @@ func TestStorage_RemoveByName(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("duplicate_remove", func(t *testing.T) {
|
t.Run("duplicate_remove", func(t *testing.T) {
|
||||||
s, err = client.NewStorage(&client.Config{
|
s, err = client.NewStorage(&client.StorageConfig{})
|
||||||
AllowedTags: nil,
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = s.Add(existingClient)
|
err = s.Add(existingClient)
|
||||||
|
|
|
@ -95,15 +95,15 @@ func (clients *clientsContainer) Init(
|
||||||
// TODO(e.burkov): The option should probably be returned, since hosts file
|
// TODO(e.burkov): The option should probably be returned, since hosts file
|
||||||
// currently used not only for clients' information enrichment, but also in
|
// currently used not only for clients' information enrichment, but also in
|
||||||
// the filtering module and upstream addresses resolution.
|
// the filtering module and upstream addresses resolution.
|
||||||
|
var EtcHosts client.HostsContainer = etcHosts
|
||||||
if !config.Clients.Sources.HostsFile {
|
if !config.Clients.Sources.HostsFile {
|
||||||
etcHosts = nil
|
EtcHosts = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
clients.storage, err = client.NewStorage(&client.Config{
|
clients.storage, err = client.NewStorage(&client.StorageConfig{
|
||||||
AllowedTags: clientTags,
|
|
||||||
InitialClients: confClients,
|
InitialClients: confClients,
|
||||||
DHCP: dhcpServer,
|
DHCP: dhcpServer,
|
||||||
EtcHosts: etcHosts,
|
EtcHosts: EtcHosts,
|
||||||
ARPDB: arpDB,
|
ARPDB: arpDB,
|
||||||
ARPClientsUpdatePeriod: arpClientsUpdatePeriod,
|
ARPClientsUpdatePeriod: arpClientsUpdatePeriod,
|
||||||
})
|
})
|
||||||
|
|
|
@ -20,7 +20,7 @@ func newClientsContainer(t *testing.T) (c *clientsContainer) {
|
||||||
testing: true,
|
testing: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, c.Init(nil, nil, nil, nil, &filtering.Config{}))
|
require.NoError(t, c.Init(nil, client.EmptyDHCP{}, nil, nil, &filtering.Config{}))
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
data.Tags = clientTags
|
data.Tags = client.Tags
|
||||||
|
|
||||||
aghhttp.WriteJSONResponseOK(w, r, data)
|
aghhttp.WriteJSONResponseOK(w, r, data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
package home
|
|
||||||
|
|
||||||
var clientTags = []string{
|
|
||||||
"device_audio",
|
|
||||||
"device_camera",
|
|
||||||
"device_gameconsole",
|
|
||||||
"device_laptop",
|
|
||||||
"device_nas", // Network-attached Storage
|
|
||||||
"device_other",
|
|
||||||
"device_pc",
|
|
||||||
"device_phone",
|
|
||||||
"device_printer",
|
|
||||||
"device_securityalarm",
|
|
||||||
"device_tablet",
|
|
||||||
"device_tv",
|
|
||||||
|
|
||||||
"os_android",
|
|
||||||
"os_ios",
|
|
||||||
"os_linux",
|
|
||||||
"os_macos",
|
|
||||||
"os_other",
|
|
||||||
"os_windows",
|
|
||||||
|
|
||||||
"user_admin",
|
|
||||||
"user_child",
|
|
||||||
"user_regular",
|
|
||||||
}
|
|
|
@ -18,9 +18,7 @@ var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
|
||||||
func newStorage(tb testing.TB, clients []*client.Persistent) (s *client.Storage) {
|
func newStorage(tb testing.TB, clients []*client.Persistent) (s *client.Storage) {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
|
||||||
s, err := client.NewStorage(&client.Config{
|
s, err := client.NewStorage(&client.StorageConfig{})
|
||||||
AllowedTags: nil,
|
|
||||||
})
|
|
||||||
require.NoError(tb, err)
|
require.NoError(tb, err)
|
||||||
|
|
||||||
for _, p := range clients {
|
for _, p := range clients {
|
||||||
|
|
Loading…
Reference in New Issue