* dhcp,clients: DHCP server module is passed to Clients module during initialization.

This commit is contained in:
Simon Zolin 2019-11-22 14:21:08 +03:00
parent 127a68a39f
commit 149fcc0f2d
5 changed files with 38 additions and 8 deletions

View File

@ -88,6 +88,13 @@ func (s *Server) CheckConfig(config ServerConfig) error {
return tmpServer.setConfig(config) 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 // Init checks the configuration and initializes the server
func (s *Server) Init(config ServerConfig) error { func (s *Server) Init(config ServerConfig) error {
err := s.setConfig(config) err := s.setConfig(config)
@ -98,6 +105,11 @@ func (s *Server) Init(config ServerConfig) error {
return nil return nil
} }
// WriteDiskConfig - write configuration
func (s *Server) WriteDiskConfig(c *ServerConfig) {
*c = s.conf
}
func (s *Server) setConfig(config ServerConfig) error { func (s *Server) setConfig(config ServerConfig) error {
s.conf = config s.conf = config
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename) s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)

View File

@ -12,6 +12,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/utils" "github.com/AdguardTeam/golibs/utils"
) )
@ -59,17 +60,20 @@ type clientsContainer struct {
idIndex map[string]*Client // IP -> client idIndex map[string]*Client // IP -> client
ipHost map[string]*ClientHost // IP -> Hostname ipHost map[string]*ClientHost // IP -> Hostname
lock sync.Mutex lock sync.Mutex
dhcpServer *dhcpd.Server
} }
// Init initializes clients container // Init initializes clients container
// Note: this function must be called only once // 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 { if clients.list != nil {
log.Fatal("clients.list != nil") log.Fatal("clients.list != nil")
} }
clients.list = make(map[string]*Client) clients.list = make(map[string]*Client)
clients.idIndex = make(map[string]*Client) clients.idIndex = make(map[string]*Client)
clients.ipHost = make(map[string]*ClientHost) clients.ipHost = make(map[string]*ClientHost)
clients.dhcpServer = dhcpServer
clients.addFromConfig(objects) clients.addFromConfig(objects)
go clients.periodicUpdate() 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 { if macFound == nil {
return Client{}, false return Client{}, false
} }
@ -533,13 +540,16 @@ func (clients *clientsContainer) addFromSystemARP() {
// add clients from DHCP that have non-empty Hostname property // add clients from DHCP that have non-empty Hostname property
func (clients *clientsContainer) addFromDHCP() { func (clients *clientsContainer) addFromDHCP() {
leases := config.dhcpServer.Leases() if clients.dhcpServer == nil {
return
}
leases := clients.dhcpServer.Leases()
n := 0 n := 0
for _, l := range leases { for _, l := range leases {
if len(l.Hostname) == 0 { if len(l.Hostname) == 0 {
continue continue
} }
ok, _ := config.clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP) ok, _ := clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
if ok { if ok {
n++ n++
} }

View File

@ -12,7 +12,7 @@ func TestClients(t *testing.T) {
var b bool var b bool
clients := clientsContainer{} clients := clientsContainer{}
clients.Init(nil) clients.Init(nil, nil)
// add // add
c = Client{ c = Client{
@ -149,7 +149,7 @@ func TestClients(t *testing.T) {
func TestClientsWhois(t *testing.T) { func TestClientsWhois(t *testing.T) {
var c Client var c Client
clients := clientsContainer{} clients := clientsContainer{}
clients.Init(nil) clients.Init(nil, nil)
whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}} whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}}
// set whois info on new client // set whois info on new client

View File

@ -67,7 +67,7 @@ type configuration struct {
dnsctx dnsContext dnsctx dnsContext
dnsFilter *dnsfilter.Dnsfilter dnsFilter *dnsfilter.Dnsfilter
dnsServer *dnsforward.Server dnsServer *dnsforward.Server
dhcpServer dhcpd.Server dhcpServer *dhcpd.Server
httpServer *http.Server httpServer *http.Server
httpsServer HTTPSServer httpsServer HTTPSServer
@ -325,6 +325,12 @@ func (c *configuration) write() error {
config.DNS.DnsfilterConf = c config.DNS.DnsfilterConf = c
} }
if config.dhcpServer != nil {
c := dhcpd.ServerConfig{}
config.dhcpServer.WriteDiskConfig(&c)
config.DHCP = c
}
configFile := config.getConfigFilename() configFile := config.getConfigFilename()
log.Debug("Writing YAML file: %s", configFile) log.Debug("Writing YAML file: %s", configFile)
yamlText, err := yaml.Marshal(&config) yamlText, err := yaml.Marshal(&config)

View File

@ -20,6 +20,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/NYTimes/gziphandler" "github.com/NYTimes/gziphandler"
"github.com/gobuffalo/packr" "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 config.Clients = nil
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&