mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 21:15:35 +03:00
86444eacc2
Merge in DNS/adguard-home from 2704-local-addresses-vol.2 to master Updates #2704. Updates #2829. Squashed commit of the following: commit 507d038c2709de59246fc0b65c3c4ab8e38d1990 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 31 14:33:05 2021 +0300 aghtest: fix file name commit 8e19f99337bee1d88ad6595adb96f9bb23fa3c41 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 31 14:06:43 2021 +0300 aghnet: rm redundant mutexes commit 361fa418b33ed160ca20862be1c455ab9378c03f Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 31 13:45:30 2021 +0300 all: fix names, docs commit 14034f4f0230d7aaa3645054946ae5c278089a99 Merge: 35e265cca72ce1cf
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 31 13:38:15 2021 +0300 Merge branch 'master' into 2704-local-addresses-vol.2 commit 35e265cc8cd308ef1fda414b58c0217cb5f258e4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 31 13:33:35 2021 +0300 aghnet: imp naming commit 7a7edac7208a40697d7bc50682b923a144e28e2b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Mar 30 20:59:54 2021 +0300 changelog: oops, nope yet commit d26a5d2513daf662ac92053b5e235189a64cc022 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Mar 30 20:55:53 2021 +0300 all: some renaming for the glory of semantics commit 9937fa619452b0742616217b975e3ff048d58acb Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Mar 29 15:34:42 2021 +0300 all: log changes commit d8d9e6dfeea8474466ee25f27021efdd3ddb1592 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 26 18:32:23 2021 +0300 all: imp localresolver, imp cutting off own addresses commit 344140df449b85925f19b460fd7dc7c08e29c35a Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Mar 26 14:53:33 2021 +0300 all: imp code quality commit 1c5c0babec73b125044e23dd3aa75d8eefc19b28 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Mar 25 20:44:08 2021 +0300 all: fix go.mod commit 0b9fb3c2369a752e893af8ddc45a86bb9fb27ce5 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Mar 25 20:38:51 2021 +0300 all: add error handling commit a7a2e51f57fc6f8f74b95a264ad345cd2a9e026e Merge: c13be63427f4f052
Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Mar 25 19:48:36 2021 +0300 Merge branch 'master' into 2704-local-addresses-vol.2 commit c13be634f47bcaed9320a732a51c0e4752d0dad0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Mar 25 18:52:28 2021 +0300 all: cover rdns with tests, imp aghnet functionality commit 48bed9025944530c613ee53e7961d6d5fbabf8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 24 20:18:07 2021 +0300 home: make rdns great again commit 1dbacfc8d5b6895807797998317fe3cc814617c1 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Mar 24 16:07:52 2021 +0300 all: imp external client restriction commit 1208a319a7f4ffe7b7fa8956f245d7a19437c0a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Mar 22 15:26:45 2021 +0300 all: finish local ptr processor commit c8827fc3db289e1a5d7a11d057743bab39957b02 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Mar 2 13:41:22 2021 +0300 all: imp ipdetector, add local ptr processor
733 lines
17 KiB
Go
733 lines
17 KiB
Go
package home
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net"
|
|
"os/exec"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/util"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/utils"
|
|
)
|
|
|
|
const clientsUpdatePeriod = 10 * time.Minute
|
|
|
|
var webHandlersRegistered = false
|
|
|
|
// Client contains information about persistent clients.
|
|
type Client struct {
|
|
IDs []string
|
|
Tags []string
|
|
Name string
|
|
UseOwnSettings bool // false: use global settings
|
|
FilteringEnabled bool
|
|
SafeSearchEnabled bool
|
|
SafeBrowsingEnabled bool
|
|
ParentalEnabled bool
|
|
|
|
UseOwnBlockedServices bool // false: use global settings
|
|
BlockedServices []string
|
|
|
|
Upstreams []string // list of upstream servers to be used for the client's requests
|
|
|
|
// Custom upstream config for this client
|
|
// nil: not yet initialized
|
|
// not nil, but empty: initialized, no good upstreams
|
|
// not nil, not empty: Upstreams ready to be used
|
|
upstreamConfig *proxy.UpstreamConfig
|
|
}
|
|
|
|
type clientSource uint
|
|
|
|
// Client sources. The order determines the priority.
|
|
const (
|
|
ClientSourceWHOIS clientSource = iota
|
|
ClientSourceRDNS
|
|
ClientSourceDHCP
|
|
ClientSourceARP
|
|
ClientSourceHostsFile
|
|
)
|
|
|
|
// ClientHost information
|
|
type ClientHost struct {
|
|
Host string
|
|
Source clientSource
|
|
WhoisInfo [][]string // [[key,value], ...]
|
|
}
|
|
|
|
type clientsContainer struct {
|
|
// TODO(a.garipov): Perhaps use a number of separate indices for
|
|
// different types (string, net.IP, and so on).
|
|
list map[string]*Client // name -> client
|
|
idIndex map[string]*Client // ID -> client
|
|
ipHost map[string]*ClientHost // IP -> Hostname
|
|
lock sync.Mutex
|
|
|
|
allTags map[string]bool
|
|
|
|
// dhcpServer is used for looking up clients IP addresses by MAC addresses
|
|
dhcpServer *dhcpd.Server
|
|
|
|
// dnsServer is used for checking clients IP status access list status
|
|
dnsServer *dnsforward.Server
|
|
|
|
autoHosts *util.AutoHosts // get entries from system hosts-files
|
|
|
|
testing bool // if TRUE, this object is used for internal tests
|
|
}
|
|
|
|
// Init initializes clients container
|
|
// dhcpServer: optional
|
|
// Note: this function must be called only once
|
|
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server, autoHosts *util.AutoHosts) {
|
|
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.allTags = make(map[string]bool)
|
|
for _, t := range clientTags {
|
|
clients.allTags[t] = false
|
|
}
|
|
|
|
clients.dhcpServer = dhcpServer
|
|
clients.autoHosts = autoHosts
|
|
clients.addFromConfig(objects)
|
|
|
|
if !clients.testing {
|
|
clients.addFromDHCP()
|
|
if clients.dhcpServer != nil {
|
|
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
|
|
}
|
|
clients.autoHosts.SetOnChanged(clients.onHostsChanged)
|
|
}
|
|
}
|
|
|
|
// Start - start the module
|
|
func (clients *clientsContainer) Start() {
|
|
if !clients.testing {
|
|
if !webHandlersRegistered {
|
|
webHandlersRegistered = true
|
|
clients.registerWebHandlers()
|
|
}
|
|
go clients.periodicUpdate()
|
|
}
|
|
}
|
|
|
|
// Reload - reload auto-clients
|
|
func (clients *clientsContainer) Reload() {
|
|
clients.addFromSystemARP()
|
|
}
|
|
|
|
type clientObject struct {
|
|
Name string `yaml:"name"`
|
|
Tags []string `yaml:"tags"`
|
|
IDs []string `yaml:"ids"`
|
|
UseGlobalSettings bool `yaml:"use_global_settings"`
|
|
FilteringEnabled bool `yaml:"filtering_enabled"`
|
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
|
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
|
|
|
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
|
|
BlockedServices []string `yaml:"blocked_services"`
|
|
|
|
Upstreams []string `yaml:"upstreams"`
|
|
}
|
|
|
|
func (clients *clientsContainer) tagKnown(tag string) bool {
|
|
_, ok := clients.allTags[tag]
|
|
return ok
|
|
}
|
|
|
|
func (clients *clientsContainer) addFromConfig(objects []clientObject) {
|
|
for _, cy := range objects {
|
|
cli := &Client{
|
|
Name: cy.Name,
|
|
IDs: cy.IDs,
|
|
UseOwnSettings: !cy.UseGlobalSettings,
|
|
FilteringEnabled: cy.FilteringEnabled,
|
|
ParentalEnabled: cy.ParentalEnabled,
|
|
SafeSearchEnabled: cy.SafeSearchEnabled,
|
|
SafeBrowsingEnabled: cy.SafeBrowsingEnabled,
|
|
|
|
UseOwnBlockedServices: !cy.UseGlobalBlockedServices,
|
|
|
|
Upstreams: cy.Upstreams,
|
|
}
|
|
|
|
for _, s := range cy.BlockedServices {
|
|
if !dnsfilter.BlockedSvcKnown(s) {
|
|
log.Debug("clients: skipping unknown blocked-service %q", s)
|
|
continue
|
|
}
|
|
cli.BlockedServices = append(cli.BlockedServices, s)
|
|
}
|
|
|
|
for _, t := range cy.Tags {
|
|
if !clients.tagKnown(t) {
|
|
log.Debug("clients: skipping unknown tag %q", t)
|
|
continue
|
|
}
|
|
cli.Tags = append(cli.Tags, t)
|
|
}
|
|
sort.Strings(cli.Tags)
|
|
|
|
_, err := clients.Add(cli)
|
|
if err != nil {
|
|
log.Tracef("clientAdd: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// WriteDiskConfig - write configuration
|
|
func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) {
|
|
clients.lock.Lock()
|
|
for _, cli := range clients.list {
|
|
cy := clientObject{
|
|
Name: cli.Name,
|
|
UseGlobalSettings: !cli.UseOwnSettings,
|
|
FilteringEnabled: cli.FilteringEnabled,
|
|
ParentalEnabled: cli.ParentalEnabled,
|
|
SafeSearchEnabled: cli.SafeSearchEnabled,
|
|
SafeBrowsingEnabled: cli.SafeBrowsingEnabled,
|
|
UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
|
|
}
|
|
|
|
cy.Tags = copyStrings(cli.Tags)
|
|
cy.IDs = copyStrings(cli.IDs)
|
|
cy.BlockedServices = copyStrings(cli.BlockedServices)
|
|
cy.Upstreams = copyStrings(cli.Upstreams)
|
|
|
|
*objects = append(*objects, cy)
|
|
}
|
|
clients.lock.Unlock()
|
|
}
|
|
|
|
func (clients *clientsContainer) periodicUpdate() {
|
|
for {
|
|
clients.Reload()
|
|
time.Sleep(clientsUpdatePeriod)
|
|
}
|
|
}
|
|
|
|
func (clients *clientsContainer) onDHCPLeaseChanged(flags int) {
|
|
switch flags {
|
|
case dhcpd.LeaseChangedAdded,
|
|
dhcpd.LeaseChangedAddedStatic,
|
|
dhcpd.LeaseChangedRemovedStatic:
|
|
clients.addFromDHCP()
|
|
}
|
|
}
|
|
|
|
func (clients *clientsContainer) onHostsChanged() {
|
|
clients.addFromHostsFile()
|
|
}
|
|
|
|
// Exists checks if client with this ID already exists.
|
|
func (clients *clientsContainer) Exists(id string, source clientSource) (ok bool) {
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
_, ok = clients.findLocked(id)
|
|
if ok {
|
|
return true
|
|
}
|
|
|
|
var ch *ClientHost
|
|
ch, ok = clients.ipHost[id]
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
// Return false if the new source has higher priority.
|
|
return source <= ch.Source
|
|
}
|
|
|
|
func copyStrings(a []string) (b []string) {
|
|
return append(b, a...)
|
|
}
|
|
|
|
// Find searches for a client by its ID.
|
|
func (clients *clientsContainer) Find(id string) (c *Client, ok bool) {
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
c, ok = clients.findLocked(id)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
|
|
c.IDs = copyStrings(c.IDs)
|
|
c.Tags = copyStrings(c.Tags)
|
|
c.BlockedServices = copyStrings(c.BlockedServices)
|
|
c.Upstreams = copyStrings(c.Upstreams)
|
|
return c, true
|
|
}
|
|
|
|
// FindUpstreams looks for upstreams configured for the client
|
|
// If no client found for this IP, or if no custom upstreams are configured,
|
|
// this method returns nil
|
|
func (clients *clientsContainer) FindUpstreams(ip string) *proxy.UpstreamConfig {
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
c, ok := clients.findLocked(ip)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
if len(c.Upstreams) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if c.upstreamConfig == nil {
|
|
conf, err := proxy.ParseUpstreamsConfig(
|
|
c.Upstreams,
|
|
upstream.Options{
|
|
Bootstrap: config.DNS.BootstrapDNS,
|
|
Timeout: dnsforward.DefaultTimeout,
|
|
},
|
|
)
|
|
if err == nil {
|
|
c.upstreamConfig = &conf
|
|
}
|
|
}
|
|
|
|
return c.upstreamConfig
|
|
}
|
|
|
|
// findLocked searches for a client by its ID. For internal use only.
|
|
func (clients *clientsContainer) findLocked(id string) (c *Client, ok bool) {
|
|
c, ok = clients.idIndex[id]
|
|
if ok {
|
|
return c, true
|
|
}
|
|
|
|
ip := net.ParseIP(id)
|
|
if ip == nil {
|
|
return nil, false
|
|
}
|
|
|
|
for _, c = range clients.list {
|
|
for _, id := range c.IDs {
|
|
_, ipnet, err := net.ParseCIDR(id)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if ipnet.Contains(ip) {
|
|
return c, true
|
|
}
|
|
}
|
|
}
|
|
|
|
if clients.dhcpServer == nil {
|
|
return nil, false
|
|
}
|
|
|
|
macFound := clients.dhcpServer.FindMACbyIP(ip)
|
|
if macFound == nil {
|
|
return nil, false
|
|
}
|
|
|
|
for _, c = range clients.list {
|
|
for _, id := range c.IDs {
|
|
hwAddr, err := net.ParseMAC(id)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if bytes.Equal(hwAddr, macFound) {
|
|
return c, true
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, false
|
|
}
|
|
|
|
// FindAutoClient - search for an auto-client by IP
|
|
func (clients *clientsContainer) FindAutoClient(ip string) (ClientHost, bool) {
|
|
ipAddr := net.ParseIP(ip)
|
|
if ipAddr == nil {
|
|
return ClientHost{}, false
|
|
}
|
|
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
ch, ok := clients.ipHost[ip]
|
|
if ok {
|
|
return *ch, true
|
|
}
|
|
return ClientHost{}, false
|
|
}
|
|
|
|
// check validates the client.
|
|
func (clients *clientsContainer) check(c *Client) (err error) {
|
|
switch {
|
|
case c == nil:
|
|
return agherr.Error("client is nil")
|
|
case c.Name == "":
|
|
return agherr.Error("invalid name")
|
|
case len(c.IDs) == 0:
|
|
return agherr.Error("id required")
|
|
default:
|
|
// Go on.
|
|
}
|
|
|
|
for i, id := range c.IDs {
|
|
// Normalize structured data.
|
|
var ip net.IP
|
|
var ipnet *net.IPNet
|
|
var mac net.HardwareAddr
|
|
if ip = net.ParseIP(id); ip != nil {
|
|
c.IDs[i] = ip.String()
|
|
} else if ip, ipnet, err = net.ParseCIDR(id); err == nil {
|
|
ipnet.IP = ip
|
|
c.IDs[i] = ipnet.String()
|
|
} else if mac, err = net.ParseMAC(id); err == nil {
|
|
c.IDs[i] = mac.String()
|
|
} else if err = dnsforward.ValidateClientID(id); err == nil {
|
|
c.IDs[i] = id
|
|
} else {
|
|
return fmt.Errorf("invalid client id at index %d: %q", i, id)
|
|
}
|
|
}
|
|
|
|
for _, t := range c.Tags {
|
|
if !clients.tagKnown(t) {
|
|
return fmt.Errorf("invalid tag: %q", t)
|
|
}
|
|
}
|
|
|
|
sort.Strings(c.Tags)
|
|
|
|
err = dnsforward.ValidateUpstreams(c.Upstreams)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid upstream servers: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Add adds a new client object. ok is false if such client already exists or
|
|
// if an error occurred.
|
|
func (clients *clientsContainer) Add(c *Client) (ok bool, err error) {
|
|
err = clients.check(c)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
// check Name index
|
|
_, ok = clients.list[c.Name]
|
|
if ok {
|
|
return false, nil
|
|
}
|
|
|
|
// check ID index
|
|
for _, id := range c.IDs {
|
|
var c2 *Client
|
|
c2, ok = clients.idIndex[id]
|
|
if ok {
|
|
return false, fmt.Errorf("another client uses the same ID (%q): %q", id, c2.Name)
|
|
}
|
|
}
|
|
|
|
// update Name index
|
|
clients.list[c.Name] = c
|
|
|
|
// update ID index
|
|
for _, id := range c.IDs {
|
|
clients.idIndex[id] = c
|
|
}
|
|
|
|
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs, len(clients.list))
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// Del removes a client. ok is false if there is no such client.
|
|
func (clients *clientsContainer) Del(name string) (ok bool) {
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
var c *Client
|
|
c, ok = clients.list[name]
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
// update Name index
|
|
delete(clients.list, name)
|
|
|
|
// update ID index
|
|
for _, id := range c.IDs {
|
|
delete(clients.idIndex, id)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// equalStringSlices returns true if the slices are equal.
|
|
func equalStringSlices(a, b []string) (ok bool) {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// Update updates a client by its name.
|
|
func (clients *clientsContainer) Update(name string, c *Client) (err error) {
|
|
err = clients.check(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
prev, ok := clients.list[name]
|
|
if !ok {
|
|
return agherr.Error("client not found")
|
|
}
|
|
|
|
// First, check the name index.
|
|
if prev.Name != c.Name {
|
|
_, ok = clients.list[c.Name]
|
|
if ok {
|
|
return agherr.Error("client already exists")
|
|
}
|
|
}
|
|
|
|
// Second, check the IP index.
|
|
if !equalStringSlices(prev.IDs, c.IDs) {
|
|
for _, id := range c.IDs {
|
|
c2, ok2 := clients.idIndex[id]
|
|
if ok2 && c2 != prev {
|
|
return fmt.Errorf("another client uses the same id (%q): %q", id, c2.Name)
|
|
}
|
|
}
|
|
|
|
// update ID index
|
|
for _, id := range prev.IDs {
|
|
delete(clients.idIndex, id)
|
|
}
|
|
for _, id := range c.IDs {
|
|
clients.idIndex[id] = prev
|
|
}
|
|
}
|
|
|
|
// update Name index
|
|
if prev.Name != c.Name {
|
|
delete(clients.list, prev.Name)
|
|
clients.list[c.Name] = prev
|
|
}
|
|
|
|
// update upstreams cache
|
|
c.upstreamConfig = nil
|
|
|
|
*prev = *c
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetWhoisInfo sets the WHOIS information for a client.
|
|
//
|
|
// TODO(a.garipov): Perhaps replace [][]string with map[string]string.
|
|
func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
_, ok := clients.findLocked(ip)
|
|
if ok {
|
|
log.Debug("clients: client for %s is already created, ignore whois info", ip)
|
|
return
|
|
}
|
|
|
|
ch, ok := clients.ipHost[ip]
|
|
if ok {
|
|
ch.WhoisInfo = info
|
|
log.Debug("clients: set whois info for auto-client %s: %q", ch.Host, info)
|
|
|
|
return
|
|
}
|
|
|
|
// Create a ClientHost implicitly so that we don't do this check again
|
|
ch = &ClientHost{
|
|
Source: ClientSourceWHOIS,
|
|
}
|
|
ch.WhoisInfo = info
|
|
clients.ipHost[ip] = ch
|
|
log.Debug("clients: set whois info for auto-client with IP %s: %q", ip, info)
|
|
}
|
|
|
|
// AddHost adds a new IP-hostname pairing. The priorities of the sources is
|
|
// taken into account. ok is true if the pairing was added.
|
|
func (clients *clientsContainer) AddHost(ip, host string, src clientSource) (ok bool, err error) {
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
ok = clients.addHostLocked(ip, host, src)
|
|
|
|
return ok, nil
|
|
}
|
|
|
|
// addHostLocked adds a new IP-hostname pairing. For internal use only.
|
|
func (clients *clientsContainer) addHostLocked(ip, host string, src clientSource) (ok bool) {
|
|
var ch *ClientHost
|
|
ch, ok = clients.ipHost[ip]
|
|
if ok {
|
|
if ch.Source > src {
|
|
return false
|
|
}
|
|
|
|
ch.Source = src
|
|
} else {
|
|
ch = &ClientHost{
|
|
Host: host,
|
|
Source: src,
|
|
}
|
|
|
|
clients.ipHost[ip] = ch
|
|
}
|
|
|
|
log.Debug("clients: added %q -> %q [%d]", ip, host, len(clients.ipHost))
|
|
|
|
return true
|
|
}
|
|
|
|
// rmHostsBySrc removes all entries that match the specified source.
|
|
func (clients *clientsContainer) rmHostsBySrc(src clientSource) {
|
|
n := 0
|
|
for k, v := range clients.ipHost {
|
|
if v.Source == src {
|
|
delete(clients.ipHost, k)
|
|
n++
|
|
}
|
|
}
|
|
|
|
log.Debug("clients: removed %d client aliases", n)
|
|
}
|
|
|
|
// addFromHostsFile fills the client-hostname pairing index from the system's
|
|
// hosts files.
|
|
func (clients *clientsContainer) addFromHostsFile() {
|
|
hosts := clients.autoHosts.List()
|
|
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
clients.rmHostsBySrc(ClientSourceHostsFile)
|
|
|
|
n := 0
|
|
for ip, names := range hosts {
|
|
for _, name := range names {
|
|
ok := clients.addHostLocked(ip, name, ClientSourceHostsFile)
|
|
if ok {
|
|
n++
|
|
}
|
|
}
|
|
}
|
|
|
|
log.Debug("Clients: added %d client aliases from system hosts-file", n)
|
|
}
|
|
|
|
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
|
|
// command.
|
|
func (clients *clientsContainer) addFromSystemARP() {
|
|
if runtime.GOOS == "windows" {
|
|
return
|
|
}
|
|
|
|
cmd := exec.Command("arp", "-a")
|
|
log.Tracef("executing %q %q", cmd.Path, cmd.Args)
|
|
data, err := cmd.Output()
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
log.Debug("command %q has failed: %q code:%d",
|
|
cmd.Path, err, cmd.ProcessState.ExitCode())
|
|
return
|
|
}
|
|
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
clients.rmHostsBySrc(ClientSourceARP)
|
|
|
|
n := 0
|
|
// TODO(a.garipov): Rewrite to use bufio.Scanner.
|
|
lines := strings.Split(string(data), "\n")
|
|
for _, ln := range lines {
|
|
open := strings.Index(ln, " (")
|
|
close := strings.Index(ln, ") ")
|
|
if open == -1 || close == -1 || open >= close {
|
|
continue
|
|
}
|
|
|
|
host := ln[:open]
|
|
ip := ln[open+2 : close]
|
|
if utils.IsValidHostname(host) != nil || net.ParseIP(ip) == nil {
|
|
continue
|
|
}
|
|
|
|
ok := clients.addHostLocked(ip, host, ClientSourceARP)
|
|
if ok {
|
|
n++
|
|
}
|
|
}
|
|
|
|
log.Debug("clients: added %d client aliases from 'arp -a' command output", n)
|
|
}
|
|
|
|
// addFromDHCP adds the clients that have a non-empty hostname from the DHCP
|
|
// server.
|
|
func (clients *clientsContainer) addFromDHCP() {
|
|
if clients.dhcpServer == nil {
|
|
return
|
|
}
|
|
|
|
clients.lock.Lock()
|
|
defer clients.lock.Unlock()
|
|
|
|
clients.rmHostsBySrc(ClientSourceDHCP)
|
|
|
|
leases := clients.dhcpServer.Leases(dhcpd.LeasesAll)
|
|
n := 0
|
|
for _, l := range leases {
|
|
if l.Hostname == "" {
|
|
continue
|
|
}
|
|
|
|
ok := clients.addHostLocked(l.IP.String(), l.Hostname, ClientSourceDHCP)
|
|
if ok {
|
|
n++
|
|
}
|
|
}
|
|
|
|
log.Debug("clients: added %d client aliases from dhcp", n)
|
|
}
|