From 149fcc0f2dccbe0018122f1efa5b8977cf6ff273 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Fri, 22 Nov 2019 14:21:08 +0300 Subject: [PATCH] * dhcp,clients: DHCP server module is passed to Clients module during initialization. --- dhcpd/dhcpd.go | 12 ++++++++++++ home/clients.go | 18 ++++++++++++++---- home/clients_test.go | 4 ++-- home/config.go | 8 +++++++- home/home.go | 4 +++- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index 1231063f..74bc45d4 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -88,6 +88,13 @@ func (s *Server) CheckConfig(config ServerConfig) error { return tmpServer.setConfig(config) } +// Create - create object +func Create(config ServerConfig) *Server { + s := Server{} + s.conf = config + return &s +} + // Init checks the configuration and initializes the server func (s *Server) Init(config ServerConfig) error { err := s.setConfig(config) @@ -98,6 +105,11 @@ func (s *Server) Init(config ServerConfig) error { return nil } +// WriteDiskConfig - write configuration +func (s *Server) WriteDiskConfig(c *ServerConfig) { + *c = s.conf +} + func (s *Server) setConfig(config ServerConfig) error { s.conf = config s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename) diff --git a/home/clients.go b/home/clients.go index 2b92c4d2..2a5abb50 100644 --- a/home/clients.go +++ b/home/clients.go @@ -12,6 +12,7 @@ import ( "sync" "time" + "github.com/AdguardTeam/AdGuardHome/dhcpd" "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/utils" ) @@ -59,17 +60,20 @@ type clientsContainer struct { idIndex map[string]*Client // IP -> client ipHost map[string]*ClientHost // IP -> Hostname lock sync.Mutex + + dhcpServer *dhcpd.Server } // Init initializes clients container // Note: this function must be called only once -func (clients *clientsContainer) Init(objects []clientObject) { +func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server) { if clients.list != nil { log.Fatal("clients.list != nil") } clients.list = make(map[string]*Client) clients.idIndex = make(map[string]*Client) clients.ipHost = make(map[string]*ClientHost) + clients.dhcpServer = dhcpServer clients.addFromConfig(objects) go clients.periodicUpdate() @@ -190,7 +194,10 @@ func (clients *clientsContainer) Find(ip string) (Client, bool) { } } - macFound := config.dhcpServer.FindMACbyIP(ipAddr) + if clients.dhcpServer == nil { + return Client{}, false + } + macFound := clients.dhcpServer.FindMACbyIP(ipAddr) if macFound == nil { return Client{}, false } @@ -533,13 +540,16 @@ func (clients *clientsContainer) addFromSystemARP() { // add clients from DHCP that have non-empty Hostname property func (clients *clientsContainer) addFromDHCP() { - leases := config.dhcpServer.Leases() + if clients.dhcpServer == nil { + return + } + leases := clients.dhcpServer.Leases() n := 0 for _, l := range leases { if len(l.Hostname) == 0 { continue } - ok, _ := config.clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP) + ok, _ := clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP) if ok { n++ } diff --git a/home/clients_test.go b/home/clients_test.go index 70493a19..c830ee34 100644 --- a/home/clients_test.go +++ b/home/clients_test.go @@ -12,7 +12,7 @@ func TestClients(t *testing.T) { var b bool clients := clientsContainer{} - clients.Init(nil) + clients.Init(nil, nil) // add c = Client{ @@ -149,7 +149,7 @@ func TestClients(t *testing.T) { func TestClientsWhois(t *testing.T) { var c Client clients := clientsContainer{} - clients.Init(nil) + clients.Init(nil, nil) whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}} // set whois info on new client diff --git a/home/config.go b/home/config.go index c7bbb269..61053ebe 100644 --- a/home/config.go +++ b/home/config.go @@ -67,7 +67,7 @@ type configuration struct { dnsctx dnsContext dnsFilter *dnsfilter.Dnsfilter dnsServer *dnsforward.Server - dhcpServer dhcpd.Server + dhcpServer *dhcpd.Server httpServer *http.Server httpsServer HTTPSServer @@ -325,6 +325,12 @@ func (c *configuration) write() error { config.DNS.DnsfilterConf = c } + if config.dhcpServer != nil { + c := dhcpd.ServerConfig{} + config.dhcpServer.WriteDiskConfig(&c) + config.DHCP = c + } + configFile := config.getConfigFilename() log.Debug("Writing YAML file: %s", configFile) yamlText, err := yaml.Marshal(&config) diff --git a/home/home.go b/home/home.go index cf407a00..9a059cd5 100644 --- a/home/home.go +++ b/home/home.go @@ -20,6 +20,7 @@ import ( "syscall" "time" + "github.com/AdguardTeam/AdGuardHome/dhcpd" "github.com/AdguardTeam/golibs/log" "github.com/NYTimes/gziphandler" "github.com/gobuffalo/packr" @@ -118,7 +119,8 @@ func run(args options) { } } - config.clients.Init(config.Clients) + config.dhcpServer = dhcpd.Create(config.DHCP) + config.clients.Init(config.Clients, config.dhcpServer) config.Clients = nil if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&