Add hosts file as source for hostname resolver

This commit is contained in:
Cuong Manh Le
2023-08-22 02:08:44 +00:00
committed by Cuong Manh Le
parent e355fd70ab
commit 82e44b01af
6 changed files with 116 additions and 0 deletions
+19
View File
@@ -73,6 +73,7 @@ type Table struct {
arp *arpDiscover
ptr *ptrDiscover
mdns *mdns
hf *hostsFile
cfg *ctrld.Config
quitCh chan struct{}
selfIP string
@@ -134,6 +135,17 @@ func (t *Table) init() {
t.refreshers = append(t.refreshers, t.merlin)
}
}
if t.discoverHosts() {
t.hf = &hostsFile{}
ctrld.ProxyLogger.Load().Debug().Msg("start hosts file discovery")
if err := t.hf.init(); err != nil {
ctrld.ProxyLogger.Load().Error().Err(err).Msg("could not init hosts file discover")
} else {
t.hostnameResolvers = append(t.hostnameResolvers, t.hf)
t.refreshers = append(t.refreshers, t.hf)
}
go t.hf.watchChanges()
}
if t.discoverDHCP() {
t.dhcp = &dhcp{selfIP: t.selfIP}
ctrld.ProxyLogger.Load().Debug().Msg("start dhcp discovery")
@@ -328,6 +340,13 @@ func (t *Table) discoverPTR() bool {
return *t.cfg.Service.DiscoverPtr
}
func (t *Table) discoverHosts() bool {
if t.cfg.Service.DiscoverHosts == nil {
return true
}
return *t.cfg.Service.DiscoverHosts
}
// normalizeIP normalizes the ip parsed from dnsmasq/dhcpd lease file.
func normalizeIP(in string) string {
// dnsmasq may put ip with interface index in lease file, strip it here.