2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-03-19 18:47:22 +03:00
|
|
|
|
|
|
|
import (
|
2019-09-26 16:40:52 +03:00
|
|
|
"bytes"
|
2019-04-26 15:10:29 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-06-25 17:51:53 +03:00
|
|
|
"os/exec"
|
2019-03-19 18:47:22 +03:00
|
|
|
"runtime"
|
2020-01-28 14:06:52 +03:00
|
|
|
"sort"
|
2019-03-19 18:47:22 +03:00
|
|
|
"strings"
|
2019-04-26 15:10:29 +03:00
|
|
|
"sync"
|
2019-06-25 17:51:53 +03:00
|
|
|
"time"
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
2020-10-30 13:32:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/util"
|
2021-01-27 18:32:13 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-03-03 15:27:25 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2019-03-19 18:47:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2019-06-25 17:51:53 +03:00
|
|
|
"github.com/AdguardTeam/golibs/utils"
|
|
|
|
)
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
const clientsUpdatePeriod = 10 * time.Minute
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2020-02-18 19:27:09 +03:00
|
|
|
var webHandlersRegistered = false
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Client contains information about persistent clients.
|
2019-03-19 18:47:22 +03:00
|
|
|
type Client struct {
|
2019-09-26 16:40:52 +03:00
|
|
|
IDs []string
|
2020-01-28 14:06:52 +03:00
|
|
|
Tags []string
|
2019-04-26 15:10:29 +03:00
|
|
|
Name string
|
|
|
|
UseOwnSettings bool // false: use global settings
|
|
|
|
FilteringEnabled bool
|
|
|
|
SafeSearchEnabled bool
|
|
|
|
SafeBrowsingEnabled bool
|
|
|
|
ParentalEnabled bool
|
2019-07-18 12:27:10 +03:00
|
|
|
|
|
|
|
UseOwnBlockedServices bool // false: use global settings
|
|
|
|
BlockedServices []string
|
2019-11-06 17:24:15 +03:00
|
|
|
|
|
|
|
Upstreams []string // list of upstream servers to be used for the client's requests
|
2020-05-13 20:31:43 +03:00
|
|
|
|
|
|
|
// Custom upstream config for this client
|
2020-01-09 17:29:53 +03:00
|
|
|
// nil: not yet initialized
|
|
|
|
// not nil, but empty: initialized, no good upstreams
|
|
|
|
// not nil, not empty: Upstreams ready to be used
|
2020-05-13 20:31:43 +03:00
|
|
|
upstreamConfig *proxy.UpstreamConfig
|
2019-03-19 18:47:22 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
type clientSource uint
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Client sources. The order determines the priority.
|
2019-04-26 15:10:29 +03:00
|
|
|
const (
|
2021-01-27 18:32:13 +03:00
|
|
|
ClientSourceWHOIS clientSource = iota
|
|
|
|
ClientSourceRDNS
|
|
|
|
ClientSourceDHCP
|
|
|
|
ClientSourceARP
|
|
|
|
ClientSourceHostsFile
|
2019-04-26 15:10:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientHost information
|
|
|
|
type ClientHost struct {
|
2019-09-18 18:44:27 +03:00
|
|
|
Host string
|
|
|
|
Source clientSource
|
|
|
|
WhoisInfo [][]string // [[key,value], ...]
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientsContainer struct {
|
2021-01-27 18:32:13 +03:00
|
|
|
// 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
|
2019-12-23 19:31:27 +03:00
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
allTags map[string]bool
|
|
|
|
|
2019-12-23 19:31:27 +03:00
|
|
|
// dhcpServer is used for looking up clients IP addresses by MAC addresses
|
2019-11-22 14:21:08 +03:00
|
|
|
dhcpServer *dhcpd.Server
|
2019-11-29 17:35:26 +03:00
|
|
|
|
2020-09-08 13:56:45 +03:00
|
|
|
// dnsServer is used for checking clients IP status access list status
|
|
|
|
dnsServer *dnsforward.Server
|
2020-07-24 14:30:29 +03:00
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
autoHosts *util.AutoHosts // get entries from system hosts-files
|
|
|
|
|
2019-11-29 17:35:26 +03:00
|
|
|
testing bool // if TRUE, this object is used for internal tests
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
// Init initializes clients container
|
2020-07-03 18:20:01 +03:00
|
|
|
// dhcpServer: optional
|
2019-07-09 18:19:50 +03:00
|
|
|
// Note: this function must be called only once
|
2020-03-20 15:05:43 +03:00
|
|
|
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server, autoHosts *util.AutoHosts) {
|
2019-04-26 15:10:29 +03:00
|
|
|
if clients.list != nil {
|
|
|
|
log.Fatal("clients.list != nil")
|
|
|
|
}
|
|
|
|
clients.list = make(map[string]*Client)
|
2019-09-26 16:40:52 +03:00
|
|
|
clients.idIndex = make(map[string]*Client)
|
2019-10-11 16:58:10 +03:00
|
|
|
clients.ipHost = make(map[string]*ClientHost)
|
2020-01-28 14:06:52 +03:00
|
|
|
|
|
|
|
clients.allTags = make(map[string]bool)
|
|
|
|
for _, t := range clientTags {
|
|
|
|
clients.allTags[t] = false
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:21:08 +03:00
|
|
|
clients.dhcpServer = dhcpServer
|
2020-03-20 15:05:43 +03:00
|
|
|
clients.autoHosts = autoHosts
|
2019-09-26 16:40:52 +03:00
|
|
|
clients.addFromConfig(objects)
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2019-11-29 17:35:26 +03:00
|
|
|
if !clients.testing {
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.addFromDHCP()
|
2020-07-03 18:20:01 +03:00
|
|
|
if clients.dhcpServer != nil {
|
|
|
|
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
|
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
clients.autoHosts.SetOnChanged(clients.onHostsChanged)
|
2020-02-18 19:27:09 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2020-02-18 19:27:09 +03:00
|
|
|
// Start - start the module
|
|
|
|
func (clients *clientsContainer) Start() {
|
|
|
|
if !clients.testing {
|
|
|
|
if !webHandlersRegistered {
|
|
|
|
webHandlersRegistered = true
|
|
|
|
clients.registerWebHandlers()
|
|
|
|
}
|
|
|
|
go clients.periodicUpdate()
|
2019-11-29 17:35:26 +03:00
|
|
|
}
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
2020-02-18 14:49:50 +03:00
|
|
|
// Reload - reload auto-clients
|
|
|
|
func (clients *clientsContainer) Reload() {
|
|
|
|
clients.addFromSystemARP()
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
type clientObject struct {
|
|
|
|
Name string `yaml:"name"`
|
2020-01-28 14:06:52 +03:00
|
|
|
Tags []string `yaml:"tags"`
|
2019-09-26 16:40:52 +03:00
|
|
|
IDs []string `yaml:"ids"`
|
|
|
|
UseGlobalSettings bool `yaml:"use_global_settings"`
|
|
|
|
FilteringEnabled bool `yaml:"filtering_enabled"`
|
|
|
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
2020-01-09 15:01:54 +03:00
|
|
|
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
|
|
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
2019-09-26 16:40:52 +03:00
|
|
|
|
|
|
|
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
|
|
|
|
BlockedServices []string `yaml:"blocked_services"`
|
2019-11-06 17:24:15 +03:00
|
|
|
|
|
|
|
Upstreams []string `yaml:"upstreams"`
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
func (clients *clientsContainer) tagKnown(tag string) bool {
|
|
|
|
_, ok := clients.allTags[tag]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
func (clients *clientsContainer) addFromConfig(objects []clientObject) {
|
|
|
|
for _, cy := range objects {
|
2021-01-27 18:32:13 +03:00
|
|
|
cli := &Client{
|
2019-09-26 16:40:52 +03: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 17:24:15 +03:00
|
|
|
|
|
|
|
Upstreams: cy.Upstreams,
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2020-01-28 14:06:52 +03:00
|
|
|
|
2020-03-02 18:51:48 +03:00
|
|
|
for _, s := range cy.BlockedServices {
|
2020-02-18 20:17:35 +03:00
|
|
|
if !dnsfilter.BlockedSvcKnown(s) {
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: skipping unknown blocked-service %q", s)
|
2020-03-02 18:51:48 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cli.BlockedServices = append(cli.BlockedServices, s)
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
for _, t := range cy.Tags {
|
|
|
|
if !clients.tagKnown(t) {
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: skipping unknown tag %q", t)
|
2020-01-28 14:06:52 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cli.Tags = append(cli.Tags, t)
|
|
|
|
}
|
|
|
|
sort.Strings(cli.Tags)
|
|
|
|
|
2019-09-26 16:40:52 +03: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 12:38:58 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
for _, cli := range clients.list {
|
2019-09-26 16:40:52 +03:00
|
|
|
cy := clientObject{
|
2019-12-11 12:38:58 +03:00
|
|
|
Name: cli.Name,
|
|
|
|
UseGlobalSettings: !cli.UseOwnSettings,
|
|
|
|
FilteringEnabled: cli.FilteringEnabled,
|
|
|
|
ParentalEnabled: cli.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: cli.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: cli.SafeBrowsingEnabled,
|
2019-09-26 16:40:52 +03:00
|
|
|
UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
|
|
|
|
}
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
cy.Tags = copyStrings(cli.Tags)
|
|
|
|
cy.IDs = copyStrings(cli.IDs)
|
|
|
|
cy.BlockedServices = copyStrings(cli.BlockedServices)
|
|
|
|
cy.Upstreams = copyStrings(cli.Upstreams)
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
*objects = append(*objects, cy)
|
|
|
|
}
|
2019-12-11 12:38:58 +03:00
|
|
|
clients.lock.Unlock()
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
func (clients *clientsContainer) periodicUpdate() {
|
2019-06-25 17:51:53 +03:00
|
|
|
for {
|
2020-02-18 14:49:50 +03:00
|
|
|
clients.Reload()
|
2019-06-25 17:51:53 +03:00
|
|
|
time.Sleep(clientsUpdatePeriod)
|
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-30 10:25:02 +03:00
|
|
|
func (clients *clientsContainer) onDHCPLeaseChanged(flags int) {
|
|
|
|
switch flags {
|
|
|
|
case dhcpd.LeaseChangedAdded,
|
|
|
|
dhcpd.LeaseChangedAddedStatic,
|
|
|
|
dhcpd.LeaseChangedRemovedStatic:
|
|
|
|
clients.addFromDHCP()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
func (clients *clientsContainer) onHostsChanged() {
|
|
|
|
clients.addFromHostsFile()
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Exists checks if client with this ID already exists.
|
|
|
|
func (clients *clientsContainer) Exists(id string, source clientSource) (ok bool) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
_, ok = clients.findLocked(id)
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
var ch *ClientHost
|
|
|
|
ch, ok = clients.ipHost[id]
|
2019-09-18 18:44:27 +03:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
// Return false if the new source has higher priority.
|
|
|
|
return source <= ch.Source
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
func copyStrings(a []string) (b []string) {
|
|
|
|
return append(b, a...)
|
2020-01-28 14:06:52 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Find searches for a client by its ID.
|
|
|
|
func (clients *clientsContainer) Find(id string) (c *Client, ok bool) {
|
2019-12-23 16:27:24 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
c, ok = clients.findLocked(id)
|
2020-01-28 14:06:52 +03:00
|
|
|
if !ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2020-01-28 14:06:52 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
c.IDs = copyStrings(c.IDs)
|
|
|
|
c.Tags = copyStrings(c.Tags)
|
|
|
|
c.BlockedServices = copyStrings(c.BlockedServices)
|
|
|
|
c.Upstreams = copyStrings(c.Upstreams)
|
2020-01-28 14:06:52 +03:00
|
|
|
return c, true
|
2019-12-23 16:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:31:27 +03:00
|
|
|
// 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
|
2020-05-13 20:31:43 +03:00
|
|
|
func (clients *clientsContainer) FindUpstreams(ip string) *proxy.UpstreamConfig {
|
2019-12-23 19:31:27 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
c, ok := clients.findLocked(ip)
|
2019-12-23 19:31:27 +03:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:31:43 +03:00
|
|
|
if len(c.Upstreams) == 0 {
|
|
|
|
return nil
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:31:43 +03:00
|
|
|
if c.upstreamConfig == nil {
|
2021-03-11 12:17:54 +03:00
|
|
|
conf, err := proxy.ParseUpstreamsConfig(
|
|
|
|
c.Upstreams,
|
2021-03-03 15:27:25 +03:00
|
|
|
upstream.Options{
|
|
|
|
Bootstrap: config.DNS.BootstrapDNS,
|
|
|
|
Timeout: dnsforward.DefaultTimeout,
|
|
|
|
},
|
|
|
|
)
|
2020-05-13 20:31:43 +03:00
|
|
|
if err == nil {
|
2021-03-11 12:17:54 +03:00
|
|
|
c.upstreamConfig = &conf
|
2020-05-13 20:31:43 +03:00
|
|
|
}
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
2020-05-13 20:31:43 +03:00
|
|
|
|
|
|
|
return c.upstreamConfig
|
2019-12-23 19:31:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03: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 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
ip := net.ParseIP(id)
|
|
|
|
if ip == nil {
|
|
|
|
return nil, false
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c = range clients.list {
|
2019-09-26 16:40:52 +03:00
|
|
|
for _, id := range c.IDs {
|
|
|
|
_, ipnet, err := net.ParseCIDR(id)
|
2019-04-26 15:10:29 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
if ipnet.Contains(ip) {
|
2021-01-27 18:32:13 +03:00
|
|
|
return c, true
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:21:08 +03:00
|
|
|
if clients.dhcpServer == nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-11-22 14:21:08 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
macFound := clients.dhcpServer.FindMACbyIP(ip)
|
2019-09-26 16:40:52 +03:00
|
|
|
if macFound == nil {
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
for _, c = range clients.list {
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
hwAddr, err := net.ParseMAC(id)
|
|
|
|
if err != nil {
|
2019-04-26 15:10:29 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
if bytes.Equal(hwAddr, macFound) {
|
2021-01-27 18:32:13 +03:00
|
|
|
return c, true
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
return nil, false
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
// FindAutoClient - search for an auto-client by IP
|
2021-01-27 18:32:13 +03:00
|
|
|
func (clients *clientsContainer) FindAutoClient(ip string) (ClientHost, bool) {
|
|
|
|
ipAddr := net.ParseIP(ip)
|
|
|
|
if ipAddr == nil {
|
2019-09-26 16:40:52 +03:00
|
|
|
return ClientHost{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
ch, ok := clients.ipHost[ip]
|
2019-09-26 16:40:52 +03:00
|
|
|
if ok {
|
|
|
|
return *ch, true
|
|
|
|
}
|
|
|
|
return ClientHost{}, false
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// 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.
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
for i, id := range c.IDs {
|
2021-01-27 18:32:13 +03: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 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 17:24:15 +03:00
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
for _, t := range c.Tags {
|
|
|
|
if !clients.tagKnown(t) {
|
2021-01-27 18:32:13 +03:00
|
|
|
return fmt.Errorf("invalid tag: %q", t)
|
2020-01-28 14:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2020-01-28 14:06:52 +03:00
|
|
|
sort.Strings(c.Tags)
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
err = dnsforward.ValidateUpstreams(c.Upstreams)
|
2020-09-22 15:04:17 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return fmt.Errorf("invalid upstream servers: %w", err)
|
2019-11-06 17:24:15 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03: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 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
// check Name index
|
2021-01-27 18:32:13 +03:00
|
|
|
_, ok = clients.list[c.Name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
// check ID index
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 18:32:13 +03:00
|
|
|
var c2 *Client
|
|
|
|
c2, ok = clients.idIndex[id]
|
2019-04-26 15:10:29 +03:00
|
|
|
if ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
return false, fmt.Errorf("another client uses the same ID (%q): %q", id, c2.Name)
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
// update Name index
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.list[c.Name] = c
|
2019-09-26 16:40:52 +03:00
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.idIndex[id] = c
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs, len(clients.list))
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03: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 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
var c *Client
|
|
|
|
c, ok = clients.list[name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
// update Name index
|
2019-04-26 15:10:29 +03:00
|
|
|
delete(clients.list, name)
|
2019-09-26 16:40:52 +03:00
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-09-26 16:40:52 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// equalStringSlices returns true if the slices are equal.
|
|
|
|
func equalStringSlices(a, b []string) (ok bool) {
|
2019-09-26 16:40:52 +03:00
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
for i := range a {
|
2019-09-26 16:40:52 +03:00
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03: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 15:10:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
prev, ok := clients.list[name]
|
2019-04-26 15:10:29 +03:00
|
|
|
if !ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
return agherr.Error("client not found")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check Name index
|
2021-01-27 18:32:13 +03:00
|
|
|
if prev.Name != c.Name {
|
2019-04-26 15:10:29 +03:00
|
|
|
_, ok = clients.list[c.Name]
|
|
|
|
if ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
return agherr.Error("client already exists")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check IP index
|
2021-01-27 18:32:13 +03:00
|
|
|
if !equalStringSlices(prev.IDs, c.IDs) {
|
2019-09-26 16:40:52 +03:00
|
|
|
for _, id := range c.IDs {
|
|
|
|
c2, ok := clients.idIndex[id]
|
2021-01-27 18:32:13 +03:00
|
|
|
if ok && c2 != prev {
|
|
|
|
return fmt.Errorf("another client uses the same ID (%q): %q", id, c2.Name)
|
2019-09-26 16:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update ID index
|
2021-01-27 18:32:13 +03:00
|
|
|
for _, id := range prev.IDs {
|
2019-09-26 16:40:52 +03:00
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
|
|
|
for _, id := range c.IDs {
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.idIndex[id] = prev
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update Name index
|
2021-01-27 18:32:13 +03:00
|
|
|
if prev.Name != c.Name {
|
|
|
|
delete(clients.list, prev.Name)
|
|
|
|
clients.list[c.Name] = prev
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:31:27 +03:00
|
|
|
// update upstreams cache
|
2020-05-13 20:31:43 +03:00
|
|
|
c.upstreamConfig = nil
|
2019-12-23 19:31:27 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
*prev = *c
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// 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) {
|
2019-09-18 18:44:27 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
_, ok := clients.findLocked(ip)
|
2019-09-18 18:44:27 +03:00
|
|
|
if ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: client for %s is already created, ignore whois info", ip)
|
2019-09-23 20:41:14 +03:00
|
|
|
return
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
ch, ok := clients.ipHost[ip]
|
2019-09-18 18:44:27 +03:00
|
|
|
if ok {
|
|
|
|
ch.WhoisInfo = info
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: set whois info for auto-client %s: %q", ch.Host, info)
|
|
|
|
|
2019-09-23 20:41:14 +03:00
|
|
|
return
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 17:12:50 +03:00
|
|
|
// Create a ClientHost implicitly so that we don't do this check again
|
2019-10-11 16:58:10 +03:00
|
|
|
ch = &ClientHost{
|
2019-09-18 18:44:27 +03:00
|
|
|
Source: ClientSourceWHOIS,
|
|
|
|
}
|
|
|
|
ch.WhoisInfo = info
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.ipHost[ip] = ch
|
|
|
|
log.Debug("clients: set whois info for auto-client with IP %s: %q", ip, info)
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03: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.
|
|
|
|
func (clients *clientsContainer) AddHost(ip, host string, src clientSource) (ok bool, err error) {
|
2019-04-26 15:10:29 +03:00
|
|
|
clients.lock.Lock()
|
2021-01-27 18:32:13 +03:00
|
|
|
ok = clients.addHostLocked(ip, host, src)
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.lock.Unlock()
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
return ok, nil
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// 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]
|
2020-12-07 16:04:53 +03:00
|
|
|
if ok {
|
2021-01-27 18:32:13 +03:00
|
|
|
if ch.Source > src {
|
2020-12-07 16:04:53 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
ch.Source = src
|
2019-10-11 16:58:10 +03:00
|
|
|
} else {
|
|
|
|
ch = &ClientHost{
|
|
|
|
Host: host,
|
2021-01-27 18:32:13 +03:00
|
|
|
Source: src,
|
2019-10-11 16:58:10 +03:00
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
2019-10-11 16:58:10 +03:00
|
|
|
clients.ipHost[ip] = ch
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
|
|
|
log.Debug("clients: added %q -> %q [%d]", ip, host, len(clients.ipHost))
|
|
|
|
|
|
|
|
return true
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// rmHostsBySrc removes all entries that match the specified source.
|
|
|
|
func (clients *clientsContainer) rmHostsBySrc(src clientSource) {
|
2020-01-30 10:25:02 +03:00
|
|
|
n := 0
|
|
|
|
for k, v := range clients.ipHost {
|
2021-01-27 18:32:13 +03:00
|
|
|
if v.Source == src {
|
2020-01-30 10:25:02 +03:00
|
|
|
delete(clients.ipHost, k)
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 16:04:53 +03:00
|
|
|
|
|
|
|
log.Debug("clients: removed %d client aliases", n)
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// addFromHostsFile fills the client-hostname pairing index from the system's
|
|
|
|
// hosts files.
|
2019-07-09 18:19:50 +03:00
|
|
|
func (clients *clientsContainer) addFromHostsFile() {
|
2020-03-20 15:05:43 +03:00
|
|
|
hosts := clients.autoHosts.List()
|
2019-03-19 18:47:22 +03:00
|
|
|
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2020-11-06 17:34:40 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.rmHostsBySrc(ClientSourceHostsFile)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
n := 0
|
2020-11-06 17:34:40 +03:00
|
|
|
for ip, names := range hosts {
|
|
|
|
for _, name := range names {
|
2021-01-27 18:32:13 +03:00
|
|
|
ok := clients.addHostLocked(ip, name, ClientSourceHostsFile)
|
2020-11-06 17:34:40 +03:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2019-03-19 18:47:22 +03:00
|
|
|
}
|
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
log.Debug("Clients: added %d client aliases from system hosts-file", n)
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
|
|
|
|
// command.
|
2019-07-09 18:19:50 +03:00
|
|
|
func (clients *clientsContainer) addFromSystemARP() {
|
2019-06-25 17:51:53 +03:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("arp", "-a")
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Tracef("executing %q %q", cmd.Path, cmd.Args)
|
2019-06-25 17:51:53 +03:00
|
|
|
data, err := cmd.Output()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("command %q has failed: %q code:%d",
|
2019-06-25 17:51:53 +03:00
|
|
|
cmd.Path, err, cmd.ProcessState.ExitCode())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:25:02 +03:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
clients.rmHostsBySrc(ClientSourceARP)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
n := 0
|
2021-01-27 18:32:13 +03:00
|
|
|
// TODO(a.garipov): Rewrite to use bufio.Scanner.
|
2019-06-25 17:51:53 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
ok := clients.addHostLocked(ip, host, ClientSourceARP)
|
2019-06-25 17:51:53 +03:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
log.Debug("clients: added %d client aliases from 'arp -a' command output", n)
|
2019-06-25 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// addFromDHCP adds the clients that have a non-empty hostname from the DHCP
|
|
|
|
// server.
|
2019-09-12 16:15:10 +03:00
|
|
|
func (clients *clientsContainer) addFromDHCP() {
|
2019-11-22 14:21:08 +03:00
|
|
|
if clients.dhcpServer == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-30 10:25:02 +03:00
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
clients.rmHostsBySrc(ClientSourceDHCP)
|
2020-01-30 10:25:02 +03:00
|
|
|
|
|
|
|
leases := clients.dhcpServer.Leases(dhcpd.LeasesAll)
|
2019-10-16 12:03:51 +03:00
|
|
|
n := 0
|
2019-09-12 16:15:10 +03:00
|
|
|
for _, l := range leases {
|
2021-01-27 18:32:13 +03:00
|
|
|
if l.Hostname == "" {
|
2019-09-12 16:15:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
ok := clients.addHostLocked(l.IP.String(), l.Hostname, ClientSourceDHCP)
|
2019-10-16 12:03:51 +03:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-09-12 16:15:10 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
log.Debug("clients: added %d client aliases from dhcp", n)
|
2019-09-12 16:15:10 +03:00
|
|
|
}
|