2019-06-10 01:33:19 -07:00
|
|
|
package home
|
2019-03-19 08:47:22 -07:00
|
|
|
|
|
|
|
import (
|
2019-09-26 06:40:52 -07:00
|
|
|
"bytes"
|
2019-04-26 05:10:29 -07:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-06-25 07:51:53 -07:00
|
|
|
"os/exec"
|
2019-03-19 08:47:22 -07:00
|
|
|
"runtime"
|
2020-01-28 04:06:52 -07:00
|
|
|
"sort"
|
2019-03-19 08:47:22 -07:00
|
|
|
"strings"
|
2019-04-26 05:10:29 -07:00
|
|
|
"sync"
|
2019-06-25 07:51:53 -07:00
|
|
|
"time"
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2021-04-07 06:36:38 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2020-10-30 03:32:02 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-05-21 06:15:47 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-04-02 07:30:39 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
2021-01-27 08:32:13 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-03-03 05:27:25 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-05-24 07:28:11 -07:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-03-19 08:47:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 06:03:37 -07:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-29 07:40:31 -07:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2019-06-25 07:51:53 -07:00
|
|
|
)
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
const clientsUpdatePeriod = 10 * time.Minute
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2020-02-18 09:27:09 -07:00
|
|
|
var webHandlersRegistered = false
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// Client contains information about persistent clients.
|
2019-03-19 08:47:22 -07:00
|
|
|
type Client struct {
|
2021-05-06 06:41:33 -07:00
|
|
|
// upstreamConfig is the custom upstream config for this client. If
|
|
|
|
// it's nil, it has not been initialized yet. If it's non-nil and
|
|
|
|
// empty, there are no valid upstreams. If it's non-nil and non-empty,
|
|
|
|
// these upstream must be used.
|
|
|
|
upstreamConfig *proxy.UpstreamConfig
|
2019-07-18 02:27:10 -07:00
|
|
|
|
2021-05-06 06:41:33 -07:00
|
|
|
Name string
|
2019-11-06 07:24:15 -07:00
|
|
|
|
2021-05-06 06:41:33 -07:00
|
|
|
IDs []string
|
|
|
|
Tags []string
|
|
|
|
BlockedServices []string
|
|
|
|
Upstreams []string
|
2020-05-13 10:31:43 -07:00
|
|
|
|
2021-05-06 06:41:33 -07:00
|
|
|
UseOwnSettings bool
|
|
|
|
FilteringEnabled bool
|
|
|
|
SafeSearchEnabled bool
|
|
|
|
SafeBrowsingEnabled bool
|
|
|
|
ParentalEnabled bool
|
|
|
|
UseOwnBlockedServices bool
|
2019-03-19 08:47:22 -07:00
|
|
|
}
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
type clientSource uint
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// Client sources. The order determines the priority.
|
2019-04-26 05:10:29 -07:00
|
|
|
const (
|
2021-01-27 08:32:13 -07:00
|
|
|
ClientSourceWHOIS clientSource = iota
|
|
|
|
ClientSourceRDNS
|
|
|
|
ClientSourceARP
|
2021-06-10 04:54:47 -07:00
|
|
|
ClientSourceDHCP
|
2021-01-27 08:32:13 -07:00
|
|
|
ClientSourceHostsFile
|
2019-04-26 05:10:29 -07:00
|
|
|
)
|
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
// RuntimeClient information
|
|
|
|
type RuntimeClient struct {
|
2021-06-18 08:13:36 -07:00
|
|
|
WHOISInfo *RuntimeClientWHOISInfo
|
2019-09-18 08:44:27 -07:00
|
|
|
Host string
|
|
|
|
Source clientSource
|
2021-04-02 07:30:39 -07:00
|
|
|
}
|
|
|
|
|
2021-06-18 08:13:36 -07:00
|
|
|
// RuntimeClientWHOISInfo is the filtered WHOIS data for a runtime client.
|
|
|
|
type RuntimeClientWHOISInfo struct {
|
2021-04-02 07:30:39 -07:00
|
|
|
City string `json:"city,omitempty"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
|
|
Orgname string `json:"orgname,omitempty"`
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientsContainer struct {
|
2021-01-27 08:32:13 -07:00
|
|
|
// TODO(a.garipov): Perhaps use a number of separate indices for
|
|
|
|
// different types (string, net.IP, and so on).
|
2021-06-29 05:53:28 -07:00
|
|
|
list map[string]*Client // name -> client
|
|
|
|
idIndex map[string]*Client // ID -> client
|
|
|
|
|
|
|
|
// ipToRC is the IP address to *RuntimeClient map.
|
2021-08-09 06:03:37 -07:00
|
|
|
ipToRC *netutil.IPMap
|
2021-06-29 05:53:28 -07:00
|
|
|
|
|
|
|
lock sync.Mutex
|
2019-12-23 09:31:27 -07:00
|
|
|
|
2021-07-29 07:40:31 -07:00
|
|
|
allTags *stringutil.Set
|
2020-01-28 04:06:52 -07:00
|
|
|
|
2019-12-23 09:31:27 -07:00
|
|
|
// dhcpServer is used for looking up clients IP addresses by MAC addresses
|
2019-11-22 04:21:08 -07:00
|
|
|
dhcpServer *dhcpd.Server
|
2019-11-29 07:35:26 -07:00
|
|
|
|
2020-09-08 03:56:45 -07:00
|
|
|
// dnsServer is used for checking clients IP status access list status
|
|
|
|
dnsServer *dnsforward.Server
|
2020-07-24 04:30:29 -07:00
|
|
|
|
2021-04-12 08:31:45 -07:00
|
|
|
etcHosts *aghnet.EtcHostsContainer // get entries from system hosts-files
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2019-11-29 07:35:26 -07:00
|
|
|
testing bool // if TRUE, this object is used for internal tests
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
// Init initializes clients container
|
2020-07-03 08:20:01 -07:00
|
|
|
// dhcpServer: optional
|
2019-07-09 08:19:50 -07:00
|
|
|
// Note: this function must be called only once
|
2021-04-12 08:31:45 -07:00
|
|
|
func (clients *clientsContainer) Init(
|
|
|
|
objects []clientObject,
|
|
|
|
dhcpServer *dhcpd.Server,
|
|
|
|
etcHosts *aghnet.EtcHostsContainer,
|
|
|
|
) {
|
2019-04-26 05:10:29 -07:00
|
|
|
if clients.list != nil {
|
|
|
|
log.Fatal("clients.list != nil")
|
|
|
|
}
|
|
|
|
clients.list = make(map[string]*Client)
|
2019-09-26 06:40:52 -07:00
|
|
|
clients.idIndex = make(map[string]*Client)
|
2021-08-09 06:03:37 -07:00
|
|
|
clients.ipToRC = netutil.NewIPMap(0)
|
2020-01-28 04:06:52 -07:00
|
|
|
|
2021-07-29 07:40:31 -07:00
|
|
|
clients.allTags = stringutil.NewSet(clientTags...)
|
2020-01-28 04:06:52 -07:00
|
|
|
|
2019-11-22 04:21:08 -07:00
|
|
|
clients.dhcpServer = dhcpServer
|
2021-04-14 09:18:48 -07:00
|
|
|
clients.etcHosts = etcHosts
|
2019-09-26 06:40:52 -07:00
|
|
|
clients.addFromConfig(objects)
|
2019-04-26 05:10:29 -07:00
|
|
|
|
2019-11-29 07:35:26 -07:00
|
|
|
if !clients.testing {
|
2021-04-15 07:52:53 -07:00
|
|
|
clients.updateFromDHCP(true)
|
2020-07-03 08:20:01 -07:00
|
|
|
if clients.dhcpServer != nil {
|
|
|
|
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
|
|
|
|
}
|
2021-04-12 08:31:45 -07:00
|
|
|
if clients.etcHosts != nil {
|
|
|
|
clients.etcHosts.SetOnChanged(clients.onHostsChanged)
|
|
|
|
}
|
2020-02-18 09:27:09 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 00:25:02 -07:00
|
|
|
|
2020-02-18 09:27:09 -07:00
|
|
|
// Start - start the module
|
|
|
|
func (clients *clientsContainer) Start() {
|
|
|
|
if !clients.testing {
|
|
|
|
if !webHandlersRegistered {
|
|
|
|
webHandlersRegistered = true
|
|
|
|
clients.registerWebHandlers()
|
|
|
|
}
|
|
|
|
go clients.periodicUpdate()
|
2019-11-29 07:35:26 -07:00
|
|
|
}
|
2019-06-25 07:51:53 -07:00
|
|
|
}
|
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
// Reload reloads runtime clients.
|
2020-02-18 04:49:50 -07:00
|
|
|
func (clients *clientsContainer) Reload() {
|
|
|
|
clients.addFromSystemARP()
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
type clientObject struct {
|
|
|
|
Name string `yaml:"name"`
|
2020-01-28 04:06:52 -07:00
|
|
|
Tags []string `yaml:"tags"`
|
2019-09-26 06:40:52 -07:00
|
|
|
IDs []string `yaml:"ids"`
|
|
|
|
UseGlobalSettings bool `yaml:"use_global_settings"`
|
|
|
|
FilteringEnabled bool `yaml:"filtering_enabled"`
|
|
|
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
2020-01-09 05:01:54 -07:00
|
|
|
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
|
|
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
2019-09-26 06:40:52 -07:00
|
|
|
|
|
|
|
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
|
|
|
|
BlockedServices []string `yaml:"blocked_services"`
|
2019-11-06 07:24:15 -07:00
|
|
|
|
|
|
|
Upstreams []string `yaml:"upstreams"`
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
|
2021-04-20 06:26:19 -07:00
|
|
|
func (clients *clientsContainer) tagKnown(tag string) (ok bool) {
|
|
|
|
return clients.allTags.Has(tag)
|
2020-01-28 04:06:52 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
func (clients *clientsContainer) addFromConfig(objects []clientObject) {
|
|
|
|
for _, cy := range objects {
|
2021-01-27 08:32:13 -07:00
|
|
|
cli := &Client{
|
2019-09-26 06:40:52 -07:00
|
|
|
Name: cy.Name,
|
|
|
|
IDs: cy.IDs,
|
|
|
|
UseOwnSettings: !cy.UseGlobalSettings,
|
|
|
|
FilteringEnabled: cy.FilteringEnabled,
|
|
|
|
ParentalEnabled: cy.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: cy.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: cy.SafeBrowsingEnabled,
|
|
|
|
|
|
|
|
UseOwnBlockedServices: !cy.UseGlobalBlockedServices,
|
2019-11-06 07:24:15 -07:00
|
|
|
|
|
|
|
Upstreams: cy.Upstreams,
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
2020-01-28 04:06:52 -07:00
|
|
|
|
2020-03-02 08:51:48 -07:00
|
|
|
for _, s := range cy.BlockedServices {
|
2021-05-21 06:15:47 -07:00
|
|
|
if !filtering.BlockedSvcKnown(s) {
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Debug("clients: skipping unknown blocked-service %q", s)
|
2020-03-02 08:51:48 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cli.BlockedServices = append(cli.BlockedServices, s)
|
|
|
|
}
|
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
for _, t := range cy.Tags {
|
|
|
|
if !clients.tagKnown(t) {
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Debug("clients: skipping unknown tag %q", t)
|
2020-01-28 04:06:52 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cli.Tags = append(cli.Tags, t)
|
|
|
|
}
|
|
|
|
sort.Strings(cli.Tags)
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
_, err := clients.Add(cli)
|
|
|
|
if err != nil {
|
|
|
|
log.Tracef("clientAdd: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig - write configuration
|
|
|
|
func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) {
|
2019-12-11 02:38:58 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
for _, cli := range clients.list {
|
2019-09-26 06:40:52 -07:00
|
|
|
cy := clientObject{
|
2019-12-11 02:38:58 -07:00
|
|
|
Name: cli.Name,
|
|
|
|
UseGlobalSettings: !cli.UseOwnSettings,
|
|
|
|
FilteringEnabled: cli.FilteringEnabled,
|
|
|
|
ParentalEnabled: cli.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: cli.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: cli.SafeBrowsingEnabled,
|
2019-09-26 06:40:52 -07:00
|
|
|
UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
|
|
|
|
}
|
2019-12-11 02:38:58 -07:00
|
|
|
|
2021-07-29 07:40:31 -07:00
|
|
|
cy.Tags = stringutil.CloneSlice(cli.Tags)
|
|
|
|
cy.IDs = stringutil.CloneSlice(cli.IDs)
|
|
|
|
cy.BlockedServices = stringutil.CloneSlice(cli.BlockedServices)
|
|
|
|
cy.Upstreams = stringutil.CloneSlice(cli.Upstreams)
|
2019-12-11 02:38:58 -07:00
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
*objects = append(*objects, cy)
|
|
|
|
}
|
2019-12-11 02:38:58 -07:00
|
|
|
clients.lock.Unlock()
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) periodicUpdate() {
|
2019-06-25 07:51:53 -07:00
|
|
|
for {
|
2020-02-18 04:49:50 -07:00
|
|
|
clients.Reload()
|
2019-06-25 07:51:53 -07:00
|
|
|
time.Sleep(clientsUpdatePeriod)
|
|
|
|
}
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
func (clients *clientsContainer) onDHCPLeaseChanged(flags int) {
|
|
|
|
switch flags {
|
|
|
|
case dhcpd.LeaseChangedAdded,
|
|
|
|
dhcpd.LeaseChangedAddedStatic,
|
|
|
|
dhcpd.LeaseChangedRemovedStatic:
|
2021-04-15 07:52:53 -07:00
|
|
|
clients.updateFromDHCP(true)
|
|
|
|
case dhcpd.LeaseChangedRemovedAll:
|
|
|
|
clients.updateFromDHCP(false)
|
2020-01-30 00:25:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
func (clients *clientsContainer) onHostsChanged() {
|
|
|
|
clients.addFromHostsFile()
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
// Exists checks if client with this IP address already exists.
|
|
|
|
func (clients *clientsContainer) Exists(ip net.IP, source clientSource) (ok bool) {
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
_, ok = clients.findLocked(ip.String())
|
2019-04-26 05:10:29 -07:00
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
rc, ok := clients.findRuntimeClientLocked(ip)
|
2019-09-18 08:44:27 -07:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
|
|
|
// Return false if the new source has higher priority.
|
2021-04-02 07:30:39 -07:00
|
|
|
return source <= rc.Source
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2021-06-18 08:13:36 -07:00
|
|
|
func toQueryLogWHOIS(wi *RuntimeClientWHOISInfo) (cw *querylog.ClientWHOIS) {
|
2021-04-02 07:30:39 -07:00
|
|
|
if wi == nil {
|
2021-06-18 08:13:36 -07:00
|
|
|
return &querylog.ClientWHOIS{}
|
2021-04-02 07:30:39 -07:00
|
|
|
}
|
|
|
|
|
2021-06-18 08:13:36 -07:00
|
|
|
return &querylog.ClientWHOIS{
|
2021-04-02 07:30:39 -07:00
|
|
|
City: wi.City,
|
|
|
|
Country: wi.Country,
|
|
|
|
Orgname: wi.Orgname,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 07:57:09 -07:00
|
|
|
// findMultiple returns info about client. If no information about the client
|
|
|
|
// is found, it sends the client by default only with the "Disallowed" field
|
|
|
|
// filled in. err is always nil.
|
2021-04-02 07:30:39 -07:00
|
|
|
func (clients *clientsContainer) findMultiple(ids []string) (c *querylog.Client, err error) {
|
2021-09-10 07:57:09 -07:00
|
|
|
var emptyClient *querylog.Client
|
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
for _, id := range ids {
|
2021-06-29 05:53:28 -07:00
|
|
|
ip := net.ParseIP(id)
|
2021-09-10 07:57:09 -07:00
|
|
|
disallowed, disallowedRule := clients.dnsServer.IsBlockedClient(ip, id)
|
2021-04-02 07:30:39 -07:00
|
|
|
|
2021-09-10 07:57:09 -07:00
|
|
|
client := clients.clientInfo(ip, id, disallowed, disallowedRule)
|
|
|
|
|
|
|
|
if client.Name == "" && client.DisallowedRule == "" {
|
|
|
|
emptyClient = client
|
2021-04-02 07:30:39 -07:00
|
|
|
|
2021-09-10 07:57:09 -07:00
|
|
|
continue
|
2021-04-02 07:30:39 -07:00
|
|
|
}
|
|
|
|
|
2021-09-10 07:57:09 -07:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return emptyClient, nil
|
|
|
|
}
|
2021-04-02 07:30:39 -07:00
|
|
|
|
2021-09-10 07:57:09 -07:00
|
|
|
// clientInfo is a wrapper around Find to make it a valid client finder for
|
|
|
|
// the query log.
|
|
|
|
func (clients *clientsContainer) clientInfo(
|
|
|
|
ip net.IP,
|
|
|
|
id string,
|
|
|
|
disallowed bool,
|
|
|
|
rule string,
|
|
|
|
) (c *querylog.Client) {
|
|
|
|
whois := &querylog.ClientWHOIS{}
|
|
|
|
client, ok := clients.Find(id)
|
|
|
|
if ok {
|
2021-04-02 07:30:39 -07:00
|
|
|
return &querylog.Client{
|
2021-09-10 07:57:09 -07:00
|
|
|
Name: client.Name,
|
|
|
|
DisallowedRule: rule,
|
2021-06-18 08:13:36 -07:00
|
|
|
WHOIS: whois,
|
2021-04-02 07:30:39 -07:00
|
|
|
Disallowed: disallowed,
|
2021-09-10 07:57:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rc *RuntimeClient
|
|
|
|
rc, ok = clients.FindRuntimeClient(ip)
|
|
|
|
if ok {
|
|
|
|
return &querylog.Client{
|
|
|
|
Name: rc.Host,
|
|
|
|
DisallowedRule: rule,
|
|
|
|
WHOIS: toQueryLogWHOIS(rc.WHOISInfo),
|
|
|
|
Disallowed: disallowed,
|
|
|
|
}
|
2021-04-02 07:30:39 -07:00
|
|
|
}
|
|
|
|
|
2021-09-10 07:57:09 -07:00
|
|
|
return &querylog.Client{
|
|
|
|
Name: "",
|
|
|
|
DisallowedRule: rule,
|
|
|
|
WHOIS: &querylog.ClientWHOIS{},
|
|
|
|
Disallowed: disallowed,
|
|
|
|
}
|
2021-04-02 07:30:39 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
func (clients *clientsContainer) Find(id string) (c *Client, ok bool) {
|
2019-12-23 06:27:24 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
c, ok = clients.findLocked(id)
|
2020-01-28 04:06:52 -07:00
|
|
|
if !ok {
|
2021-01-27 08:32:13 -07:00
|
|
|
return nil, false
|
2020-01-28 04:06:52 -07:00
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2021-07-29 07:40:31 -07:00
|
|
|
c.IDs = stringutil.CloneSlice(c.IDs)
|
|
|
|
c.Tags = stringutil.CloneSlice(c.Tags)
|
|
|
|
c.BlockedServices = stringutil.CloneSlice(c.BlockedServices)
|
|
|
|
c.Upstreams = stringutil.CloneSlice(c.Upstreams)
|
2020-01-28 04:06:52 -07:00
|
|
|
return c, true
|
2019-12-23 06:27:24 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 03:02:59 -07:00
|
|
|
// findUpstreams returns upstreams configured for the client, identified either
|
|
|
|
// by its IP address or its ClientID. upsConf is nil if the client isn't found
|
|
|
|
// or if the client has no custom upstreams.
|
|
|
|
func (clients *clientsContainer) findUpstreams(
|
|
|
|
id string,
|
|
|
|
) (upsConf *proxy.UpstreamConfig, err error) {
|
2019-12-23 09:31:27 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-05-28 03:02:59 -07:00
|
|
|
c, ok := clients.findLocked(id)
|
2019-12-23 09:31:27 -07:00
|
|
|
if !ok {
|
2021-05-28 03:02:59 -07:00
|
|
|
return nil, nil
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 07:40:31 -07:00
|
|
|
upstreams := stringutil.FilterOut(c.Upstreams, dnsforward.IsCommentOrEmpty)
|
2021-04-13 03:44:29 -07:00
|
|
|
if len(upstreams) == 0 {
|
2021-05-28 03:02:59 -07:00
|
|
|
return nil, nil
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 03:02:59 -07:00
|
|
|
if c.upstreamConfig != nil {
|
|
|
|
return c.upstreamConfig, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
var conf *proxy.UpstreamConfig
|
2021-05-28 03:02:59 -07:00
|
|
|
conf, err = proxy.ParseUpstreamsConfig(
|
|
|
|
upstreams,
|
2021-06-29 05:53:28 -07:00
|
|
|
&upstream.Options{
|
2021-05-28 03:02:59 -07:00
|
|
|
Bootstrap: config.DNS.BootstrapDNS,
|
2021-06-15 07:36:49 -07:00
|
|
|
Timeout: config.DNS.UpstreamTimeout.Duration,
|
2021-05-28 03:02:59 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
2020-05-13 10:31:43 -07:00
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
c.upstreamConfig = conf
|
2021-05-28 03:02:59 -07:00
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
return conf, nil
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// 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
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
ip := net.ParseIP(id)
|
|
|
|
if ip == nil {
|
|
|
|
return nil, false
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c = range clients.list {
|
2019-09-26 06:40:52 -07:00
|
|
|
for _, id := range c.IDs {
|
|
|
|
_, ipnet, err := net.ParseCIDR(id)
|
2019-04-26 05:10:29 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2021-01-20 07:27:53 -07:00
|
|
|
if ipnet.Contains(ip) {
|
2021-01-27 08:32:13 -07:00
|
|
|
return c, true
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 04:21:08 -07:00
|
|
|
if clients.dhcpServer == nil {
|
2021-01-27 08:32:13 -07:00
|
|
|
return nil, false
|
2019-11-22 04:21:08 -07:00
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2021-01-20 07:27:53 -07:00
|
|
|
macFound := clients.dhcpServer.FindMACbyIP(ip)
|
2019-09-26 06:40:52 -07:00
|
|
|
if macFound == nil {
|
2021-01-27 08:32:13 -07:00
|
|
|
return nil, false
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
for _, c = range clients.list {
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
hwAddr, err := net.ParseMAC(id)
|
|
|
|
if err != nil {
|
2019-04-26 05:10:29 -07:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
if bytes.Equal(hwAddr, macFound) {
|
2021-01-27 08:32:13 -07:00
|
|
|
return c, true
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
return nil, false
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
// findRuntimeClientLocked finds a runtime client by their IP address. For
|
|
|
|
// internal use only.
|
|
|
|
func (clients *clientsContainer) findRuntimeClientLocked(ip net.IP) (rc *RuntimeClient, ok bool) {
|
|
|
|
var v interface{}
|
|
|
|
v, ok = clients.ipToRC.Get(ip)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, ok = v.(*RuntimeClient)
|
|
|
|
if !ok {
|
|
|
|
log.Error("clients: bad type %T in ipToRC for %s", v, ip)
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc, true
|
|
|
|
}
|
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
// FindRuntimeClient finds a runtime client by their IP.
|
2021-06-29 05:53:28 -07:00
|
|
|
func (clients *clientsContainer) FindRuntimeClient(ip net.IP) (rc *RuntimeClient, ok bool) {
|
|
|
|
if ip == nil {
|
|
|
|
return nil, false
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
return clients.findRuntimeClientLocked(ip)
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// check validates the client.
|
|
|
|
func (clients *clientsContainer) check(c *Client) (err error) {
|
|
|
|
switch {
|
|
|
|
case c == nil:
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("client is nil")
|
2021-01-27 08:32:13 -07:00
|
|
|
case c.Name == "":
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("invalid name")
|
2021-01-27 08:32:13 -07:00
|
|
|
case len(c.IDs) == 0:
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("id required")
|
2021-01-27 08:32:13 -07:00
|
|
|
default:
|
|
|
|
// Go on.
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
for i, id := range c.IDs {
|
2021-01-27 08:32:13 -07:00
|
|
|
// 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)
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 07:24:15 -07:00
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
for _, t := range c.Tags {
|
|
|
|
if !clients.tagKnown(t) {
|
2021-01-27 08:32:13 -07:00
|
|
|
return fmt.Errorf("invalid tag: %q", t)
|
2020-01-28 04:06:52 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
sort.Strings(c.Tags)
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
err = dnsforward.ValidateUpstreams(c.Upstreams)
|
2020-09-22 05:04:17 -07:00
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("invalid upstream servers: %w", err)
|
2019-11-06 07:24:15 -07:00
|
|
|
}
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// 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
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
// check Name index
|
2021-01-27 08:32:13 -07:00
|
|
|
_, ok = clients.list[c.Name]
|
2019-04-26 05:10:29 -07:00
|
|
|
if ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
// check ID index
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 08:32:13 -07:00
|
|
|
var c2 *Client
|
|
|
|
c2, ok = clients.idIndex[id]
|
2019-04-26 05:10:29 -07:00
|
|
|
if ok {
|
2021-01-27 08:32:13 -07:00
|
|
|
return false, fmt.Errorf("another client uses the same ID (%q): %q", id, c2.Name)
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
// update Name index
|
2021-01-27 08:32:13 -07:00
|
|
|
clients.list[c.Name] = c
|
2019-09-26 06:40:52 -07:00
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 08:32:13 -07:00
|
|
|
clients.idIndex[id] = c
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs, len(clients.list))
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// Del removes a client. ok is false if there is no such client.
|
|
|
|
func (clients *clientsContainer) Del(name string) (ok bool) {
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
var c *Client
|
|
|
|
c, ok = clients.list[name]
|
2019-04-26 05:10:29 -07:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
// update Name index
|
2019-04-26 05:10:29 -07:00
|
|
|
delete(clients.list, name)
|
2019-09-26 06:40:52 -07:00
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// equalStringSlices returns true if the slices are equal.
|
|
|
|
func equalStringSlices(a, b []string) (ok bool) {
|
2019-09-26 06:40:52 -07:00
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
|
|
|
for i := range a {
|
2019-09-26 06:40:52 -07:00
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// Update updates a client by its name.
|
|
|
|
func (clients *clientsContainer) Update(name string, c *Client) (err error) {
|
|
|
|
err = clients.check(c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
prev, ok := clients.list[name]
|
2019-04-26 05:10:29 -07:00
|
|
|
if !ok {
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("client not found")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2021-03-12 04:32:08 -07:00
|
|
|
// First, check the name index.
|
2021-01-27 08:32:13 -07:00
|
|
|
if prev.Name != c.Name {
|
2019-04-26 05:10:29 -07:00
|
|
|
_, ok = clients.list[c.Name]
|
|
|
|
if ok {
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("client already exists")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:32:08 -07:00
|
|
|
// Second, check the IP index.
|
2021-01-27 08:32:13 -07:00
|
|
|
if !equalStringSlices(prev.IDs, c.IDs) {
|
2019-09-26 06:40:52 -07:00
|
|
|
for _, id := range c.IDs {
|
2021-03-12 04:32:08 -07:00
|
|
|
c2, ok2 := clients.idIndex[id]
|
|
|
|
if ok2 && c2 != prev {
|
|
|
|
return fmt.Errorf("another client uses the same id (%q): %q", id, c2.Name)
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update ID index
|
2021-01-27 08:32:13 -07:00
|
|
|
for _, id := range prev.IDs {
|
2019-09-26 06:40:52 -07:00
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 08:32:13 -07:00
|
|
|
clients.idIndex[id] = prev
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update Name index
|
2021-01-27 08:32:13 -07:00
|
|
|
if prev.Name != c.Name {
|
|
|
|
delete(clients.list, prev.Name)
|
|
|
|
clients.list[c.Name] = prev
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:31:27 -07:00
|
|
|
// update upstreams cache
|
2020-05-13 10:31:43 -07:00
|
|
|
c.upstreamConfig = nil
|
2019-12-23 09:31:27 -07:00
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
*prev = *c
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 08:13:36 -07:00
|
|
|
// SetWHOISInfo sets the WHOIS information for a client.
|
2021-06-29 05:53:28 -07:00
|
|
|
func (clients *clientsContainer) SetWHOISInfo(ip net.IP, wi *RuntimeClientWHOISInfo) {
|
2019-09-18 08:44:27 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
_, ok := clients.findLocked(ip.String())
|
2019-09-18 08:44:27 -07:00
|
|
|
if ok {
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Debug("clients: client for %s is already created, ignore whois info", ip)
|
2019-09-23 10:41:14 -07:00
|
|
|
return
|
2019-09-18 08:44:27 -07:00
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
rc, ok := clients.findRuntimeClientLocked(ip)
|
2019-09-18 08:44:27 -07:00
|
|
|
if ok {
|
2021-06-18 08:13:36 -07:00
|
|
|
rc.WHOISInfo = wi
|
2021-04-02 07:30:39 -07:00
|
|
|
log.Debug("clients: set whois info for runtime client %s: %+v", rc.Host, wi)
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2019-09-23 10:41:14 -07:00
|
|
|
return
|
2019-09-18 08:44:27 -07:00
|
|
|
}
|
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
// Create a RuntimeClient implicitly so that we don't do this check
|
|
|
|
// again.
|
|
|
|
rc = &RuntimeClient{
|
2019-09-18 08:44:27 -07:00
|
|
|
Source: ClientSourceWHOIS,
|
|
|
|
}
|
2021-04-02 07:30:39 -07:00
|
|
|
|
2021-06-18 08:13:36 -07:00
|
|
|
rc.WHOISInfo = wi
|
2021-06-29 05:53:28 -07:00
|
|
|
|
|
|
|
clients.ipToRC.Set(ip, rc)
|
2021-04-02 07:30:39 -07:00
|
|
|
|
|
|
|
log.Debug("clients: set whois info for runtime client with ip %s: %+v", ip, wi)
|
2019-09-18 08:44:27 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// AddHost adds a new IP-hostname pairing. The priorities of the sources is
|
|
|
|
// taken into account. ok is true if the pairing was added.
|
2021-06-29 05:53:28 -07:00
|
|
|
func (clients *clientsContainer) AddHost(ip net.IP, host string, src clientSource) (ok bool, err error) {
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.lock.Lock()
|
2021-03-31 05:00:47 -07:00
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
ok = clients.addHostLocked(ip, host, src)
|
|
|
|
|
|
|
|
return ok, nil
|
2020-01-30 00:25:02 -07:00
|
|
|
}
|
2019-04-26 05:10:29 -07:00
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// addHostLocked adds a new IP-hostname pairing. For internal use only.
|
2021-06-29 05:53:28 -07:00
|
|
|
func (clients *clientsContainer) addHostLocked(ip net.IP, host string, src clientSource) (ok bool) {
|
2021-04-02 07:30:39 -07:00
|
|
|
var rc *RuntimeClient
|
2021-06-29 05:53:28 -07:00
|
|
|
rc, ok = clients.findRuntimeClientLocked(ip)
|
2020-12-07 06:04:53 -07:00
|
|
|
if ok {
|
2021-04-02 07:30:39 -07:00
|
|
|
if rc.Source > src {
|
2020-12-07 06:04:53 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
rc.Source = src
|
2019-10-11 06:58:10 -07:00
|
|
|
} else {
|
2021-04-02 07:30:39 -07:00
|
|
|
rc = &RuntimeClient{
|
|
|
|
Host: host,
|
|
|
|
Source: src,
|
2021-06-18 08:13:36 -07:00
|
|
|
WHOISInfo: &RuntimeClientWHOISInfo{},
|
2019-10-11 06:58:10 -07:00
|
|
|
}
|
2020-12-07 06:04:53 -07:00
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
clients.ipToRC.Set(ip, rc)
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2020-12-07 06:04:53 -07:00
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
log.Debug("clients: added %s -> %q [%d]", ip, host, clients.ipToRC.Len())
|
2020-12-07 06:04:53 -07:00
|
|
|
|
|
|
|
return true
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// rmHostsBySrc removes all entries that match the specified source.
|
|
|
|
func (clients *clientsContainer) rmHostsBySrc(src clientSource) {
|
2020-01-30 00:25:02 -07:00
|
|
|
n := 0
|
2021-06-29 05:53:28 -07:00
|
|
|
clients.ipToRC.Range(func(ip net.IP, v interface{}) (cont bool) {
|
|
|
|
rc, ok := v.(*RuntimeClient)
|
|
|
|
if !ok {
|
|
|
|
log.Error("clients: bad type %T in ipToRC for %s", v, ip)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.Source == src {
|
|
|
|
clients.ipToRC.Del(ip)
|
2020-01-30 00:25:02 -07:00
|
|
|
n++
|
|
|
|
}
|
2021-06-29 05:53:28 -07:00
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2020-12-07 06:04:53 -07:00
|
|
|
|
|
|
|
log.Debug("clients: removed %d client aliases", n)
|
2020-01-30 00:25:02 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// addFromHostsFile fills the client-hostname pairing index from the system's
|
|
|
|
// hosts files.
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) addFromHostsFile() {
|
2021-04-12 08:31:45 -07:00
|
|
|
if clients.etcHosts == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts := clients.etcHosts.List()
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2020-11-06 07:34:40 -07:00
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
clients.rmHostsBySrc(ClientSourceHostsFile)
|
2020-01-30 00:25:02 -07:00
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
n := 0
|
2021-06-29 05:53:28 -07:00
|
|
|
hosts.Range(func(ip net.IP, v interface{}) (cont bool) {
|
|
|
|
names, ok := v.([]string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("dns: bad type %T in ipToRC for %s", v, ip)
|
|
|
|
}
|
|
|
|
|
2020-11-06 07:34:40 -07:00
|
|
|
for _, name := range names {
|
2021-06-29 05:53:28 -07:00
|
|
|
ok = clients.addHostLocked(ip, name, ClientSourceHostsFile)
|
2020-11-06 07:34:40 -07:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Debug("clients: added %d client aliases from system hosts-file", n)
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
|
|
|
|
// command.
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) addFromSystemARP() {
|
2019-06-25 07:51:53 -07:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("arp", "-a")
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Tracef("executing %q %q", cmd.Path, cmd.Args)
|
2019-06-25 07:51:53 -07:00
|
|
|
data, err := cmd.Output()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Debug("command %q has failed: %q code:%d",
|
2019-06-25 07:51:53 -07:00
|
|
|
cmd.Path, err, cmd.ProcessState.ExitCode())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2021-01-27 08:32:13 -07:00
|
|
|
|
|
|
|
clients.rmHostsBySrc(ClientSourceARP)
|
2020-01-30 00:25:02 -07:00
|
|
|
|
2019-06-25 07:51:53 -07:00
|
|
|
n := 0
|
2021-01-27 08:32:13 -07:00
|
|
|
// TODO(a.garipov): Rewrite to use bufio.Scanner.
|
2019-06-25 07:51:53 -07:00
|
|
|
lines := strings.Split(string(data), "\n")
|
|
|
|
for _, ln := range lines {
|
2021-06-29 05:53:28 -07:00
|
|
|
lparen := strings.Index(ln, " (")
|
|
|
|
rparen := strings.Index(ln, ") ")
|
|
|
|
if lparen == -1 || rparen == -1 || lparen >= rparen {
|
2019-06-25 07:51:53 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
host := ln[:lparen]
|
|
|
|
ipStr := ln[lparen+2 : rparen]
|
|
|
|
ip := net.ParseIP(ipStr)
|
2021-08-09 06:03:37 -07:00
|
|
|
if netutil.ValidateDomainName(host) != nil || ip == nil {
|
2019-06-25 07:51:53 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
ok := clients.addHostLocked(ip, host, ClientSourceARP)
|
2019-06-25 07:51:53 -07:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
log.Debug("clients: added %d client aliases from 'arp -a' command output", n)
|
2019-06-25 07:51:53 -07:00
|
|
|
}
|
|
|
|
|
2021-04-15 07:52:53 -07:00
|
|
|
// updateFromDHCP adds the clients that have a non-empty hostname from the DHCP
|
2021-01-27 08:32:13 -07:00
|
|
|
// server.
|
2021-04-15 07:52:53 -07:00
|
|
|
func (clients *clientsContainer) updateFromDHCP(add bool) {
|
2019-11-22 04:21:08 -07:00
|
|
|
if clients.dhcpServer == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-30 00:25:02 -07:00
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
clients.rmHostsBySrc(ClientSourceDHCP)
|
2020-01-30 00:25:02 -07:00
|
|
|
|
2021-04-15 07:52:53 -07:00
|
|
|
if !add {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
leases := clients.dhcpServer.Leases(dhcpd.LeasesAll)
|
2019-10-16 02:03:51 -07:00
|
|
|
n := 0
|
2019-09-12 06:15:10 -07:00
|
|
|
for _, l := range leases {
|
2021-01-27 08:32:13 -07:00
|
|
|
if l.Hostname == "" {
|
2019-09-12 06:15:10 -07:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
2021-06-29 05:53:28 -07:00
|
|
|
ok := clients.addHostLocked(l.IP, l.Hostname, ClientSourceDHCP)
|
2019-10-16 02:03:51 -07:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-09-12 06:15:10 -07:00
|
|
|
}
|
2021-01-27 08:32:13 -07:00
|
|
|
|
|
|
|
log.Debug("clients: added %d client aliases from dhcp", n)
|
2019-09-12 06:15:10 -07:00
|
|
|
}
|