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
|
|
|
|
2019-11-22 04:21:08 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
2020-02-18 10:17:35 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
2019-11-06 07:24:15 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsforward"
|
2020-03-20 05:05:43 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/util"
|
2019-12-23 09:31:27 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2019-03-19 08:47:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2019-06-25 07:51:53 -07:00
|
|
|
"github.com/AdguardTeam/golibs/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
clientsUpdatePeriod = 1 * time.Hour
|
2019-03-19 08:47:22 -07:00
|
|
|
)
|
|
|
|
|
2020-02-18 09:27:09 -07:00
|
|
|
var webHandlersRegistered = false
|
|
|
|
|
2019-03-19 08:47:22 -07:00
|
|
|
// Client information
|
|
|
|
type Client struct {
|
2019-09-26 06:40:52 -07:00
|
|
|
IDs []string
|
2020-01-28 04:06:52 -07:00
|
|
|
Tags []string
|
2019-04-26 05:10:29 -07:00
|
|
|
Name string
|
|
|
|
UseOwnSettings bool // false: use global settings
|
|
|
|
FilteringEnabled bool
|
|
|
|
SafeSearchEnabled bool
|
|
|
|
SafeBrowsingEnabled bool
|
|
|
|
ParentalEnabled bool
|
2019-07-18 02:27:10 -07:00
|
|
|
|
|
|
|
UseOwnBlockedServices bool // false: use global settings
|
|
|
|
BlockedServices []string
|
2019-11-06 07:24:15 -07:00
|
|
|
|
|
|
|
Upstreams []string // list of upstream servers to be used for the client's requests
|
2020-01-09 07:29:53 -07:00
|
|
|
// Upstream objects:
|
|
|
|
// nil: not yet initialized
|
|
|
|
// not nil, but empty: initialized, no good upstreams
|
|
|
|
// not nil, not empty: Upstreams ready to be used
|
|
|
|
upstreamObjects []upstream.Upstream
|
2019-03-19 08:47:22 -07:00
|
|
|
}
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
type clientSource uint
|
|
|
|
|
2019-09-18 08:44:27 -07:00
|
|
|
// Client sources
|
2019-04-26 05:10:29 -07:00
|
|
|
const (
|
2019-09-18 08:44:27 -07:00
|
|
|
// Priority: etc/hosts > DHCP > ARP > rDNS > WHOIS
|
|
|
|
ClientSourceWHOIS clientSource = iota // from WHOIS
|
|
|
|
ClientSourceRDNS // from rDNS
|
|
|
|
ClientSourceDHCP // from DHCP
|
|
|
|
ClientSourceARP // from 'arp -a'
|
|
|
|
ClientSourceHostsFile // from /etc/hosts
|
2019-04-26 05:10:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientHost information
|
|
|
|
type ClientHost struct {
|
2019-09-18 08:44:27 -07:00
|
|
|
Host string
|
|
|
|
Source clientSource
|
|
|
|
WhoisInfo [][]string // [[key,value], ...]
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientsContainer struct {
|
2019-10-11 06:58:10 -07:00
|
|
|
list map[string]*Client // name -> client
|
2019-09-26 06:40:52 -07:00
|
|
|
idIndex map[string]*Client // IP -> client
|
2019-10-11 06:58:10 -07:00
|
|
|
ipHost map[string]*ClientHost // IP -> Hostname
|
2020-01-09 07:29:53 -07:00
|
|
|
lock sync.Mutex
|
2019-12-23 09:31:27 -07:00
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
allTags map[string]bool
|
|
|
|
|
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-03-20 05:05:43 -07:00
|
|
|
autoHosts *util.AutoHosts // get entries from system hosts-files
|
|
|
|
|
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
|
|
|
|
// Note: this function must be called only once
|
2020-03-20 05:05:43 -07:00
|
|
|
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server, autoHosts *util.AutoHosts) {
|
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)
|
2019-10-11 06:58:10 -07:00
|
|
|
clients.ipHost = make(map[string]*ClientHost)
|
2020-01-28 04:06:52 -07:00
|
|
|
|
|
|
|
clients.allTags = make(map[string]bool)
|
|
|
|
for _, t := range clientTags {
|
|
|
|
clients.allTags[t] = false
|
|
|
|
}
|
|
|
|
|
2019-11-22 04:21:08 -07:00
|
|
|
clients.dhcpServer = dhcpServer
|
2020-03-20 05:05:43 -07:00
|
|
|
clients.autoHosts = autoHosts
|
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 {
|
2020-01-30 00:25:02 -07:00
|
|
|
clients.addFromDHCP()
|
|
|
|
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
|
2020-03-20 05:05:43 -07:00
|
|
|
clients.autoHosts.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
|
|
|
}
|
2020-02-18 09:27:09 -07:00
|
|
|
|
2019-06-25 07:51:53 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 04:49:50 -07:00
|
|
|
// Reload - reload auto-clients
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
func (clients *clientsContainer) tagKnown(tag string) bool {
|
|
|
|
_, ok := clients.allTags[tag]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
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,
|
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 {
|
2020-02-18 10:17:35 -07:00
|
|
|
if !dnsfilter.BlockedSvcKnown(s) {
|
2020-03-02 08:51:48 -07:00
|
|
|
log.Debug("Clients: skipping unknown blocked-service '%s'", s)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cli.BlockedServices = append(cli.BlockedServices, s)
|
|
|
|
}
|
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
for _, t := range cy.Tags {
|
|
|
|
if !clients.tagKnown(t) {
|
|
|
|
log.Debug("Clients: skipping unknown tag '%s'", t)
|
|
|
|
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
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
cy.Tags = stringArrayDup(cli.Tags)
|
|
|
|
cy.IDs = stringArrayDup(cli.IDs)
|
|
|
|
cy.BlockedServices = stringArrayDup(cli.BlockedServices)
|
|
|
|
cy.Upstreams = stringArrayDup(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:
|
|
|
|
clients.addFromDHCP()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
func (clients *clientsContainer) onHostsChanged() {
|
|
|
|
clients.addFromHostsFile()
|
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
// Exists checks if client with this IP already exists
|
2019-09-18 08:44:27 -07:00
|
|
|
func (clients *clientsContainer) Exists(ip string, source clientSource) bool {
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2019-12-23 10:02:06 -07:00
|
|
|
_, ok := clients.findByIP(ip)
|
2019-04-26 05:10:29 -07:00
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:44:27 -07:00
|
|
|
ch, ok := clients.ipHost[ip]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if source > ch.Source {
|
|
|
|
return false // we're going to overwrite this client's info with a stronger source
|
|
|
|
}
|
|
|
|
return true
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
func stringArrayDup(a []string) []string {
|
|
|
|
a2 := make([]string, len(a))
|
|
|
|
copy(a2, a)
|
|
|
|
return a2
|
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
// Find searches for a client by IP
|
|
|
|
func (clients *clientsContainer) Find(ip string) (Client, bool) {
|
2019-12-23 06:27:24 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2020-01-28 04:06:52 -07:00
|
|
|
c, ok := clients.findByIP(ip)
|
|
|
|
if !ok {
|
|
|
|
return Client{}, false
|
|
|
|
}
|
|
|
|
c.IDs = stringArrayDup(c.IDs)
|
|
|
|
c.Tags = stringArrayDup(c.Tags)
|
|
|
|
c.BlockedServices = stringArrayDup(c.BlockedServices)
|
|
|
|
c.Upstreams = stringArrayDup(c.Upstreams)
|
|
|
|
return c, true
|
2019-12-23 06:27:24 -07:00
|
|
|
}
|
|
|
|
|
2020-01-09 07:29:53 -07:00
|
|
|
func upstreamArrayCopy(a []upstream.Upstream) []upstream.Upstream {
|
|
|
|
a2 := make([]upstream.Upstream, len(a))
|
|
|
|
copy(a2, a)
|
|
|
|
return a2
|
|
|
|
}
|
|
|
|
|
2019-12-23 09:31:27 -07: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
|
|
|
|
func (clients *clientsContainer) FindUpstreams(ip string) []upstream.Upstream {
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
c, ok := clients.findByIP(ip)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 07:29:53 -07:00
|
|
|
if c.upstreamObjects == nil {
|
|
|
|
c.upstreamObjects = make([]upstream.Upstream, 0)
|
|
|
|
for _, us := range c.Upstreams {
|
|
|
|
u, err := upstream.AddressToUpstream(us, upstream.Options{Timeout: dnsforward.DefaultTimeout})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("upstream.AddressToUpstream: %s: %s", us, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.upstreamObjects = append(c.upstreamObjects, u)
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 07:29:53 -07:00
|
|
|
if len(c.upstreamObjects) == 0 {
|
|
|
|
return nil
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
2020-01-09 07:29:53 -07:00
|
|
|
return upstreamArrayCopy(c.upstreamObjects)
|
2019-12-23 09:31:27 -07:00
|
|
|
}
|
|
|
|
|
2019-12-23 06:27:24 -07:00
|
|
|
// Find searches for a client by IP (and does not lock anything)
|
|
|
|
func (clients *clientsContainer) findByIP(ip string) (Client, bool) {
|
2019-09-26 06:40:52 -07:00
|
|
|
ipAddr := net.ParseIP(ip)
|
|
|
|
if ipAddr == nil {
|
|
|
|
return Client{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
c, ok := clients.idIndex[ip]
|
2019-04-26 05:10:29 -07:00
|
|
|
if ok {
|
2019-06-07 01:37:55 -07:00
|
|
|
return *c, true
|
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
|
|
|
|
}
|
2019-09-26 06:40:52 -07:00
|
|
|
if ipnet.Contains(ipAddr) {
|
|
|
|
return *c, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 04:21:08 -07:00
|
|
|
if clients.dhcpServer == nil {
|
|
|
|
return Client{}, false
|
|
|
|
}
|
|
|
|
macFound := clients.dhcpServer.FindMACbyIP(ipAddr)
|
2019-09-26 06:40:52 -07:00
|
|
|
if macFound == nil {
|
|
|
|
return Client{}, false
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2019-09-26 06:40:52 -07:00
|
|
|
if bytes.Equal(hwAddr, macFound) {
|
2019-06-07 01:37:55 -07:00
|
|
|
return *c, true
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:37:55 -07:00
|
|
|
return Client{}, false
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
// Check if Client object's fields are correct
|
2020-01-28 04:06:52 -07:00
|
|
|
func (clients *clientsContainer) check(c *Client) error {
|
2019-04-26 05:10:29 -07:00
|
|
|
if len(c.Name) == 0 {
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("invalid Name")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
if len(c.IDs) == 0 {
|
|
|
|
return fmt.Errorf("ID required")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
for i, id := range c.IDs {
|
|
|
|
ip := net.ParseIP(id)
|
|
|
|
if ip != nil {
|
|
|
|
c.IDs[i] = ip.String() // normalize IP address
|
|
|
|
continue
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2019-09-26 06:40:52 -07:00
|
|
|
|
|
|
|
_, _, err := net.ParseCIDR(id)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = net.ParseMAC(id)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2019-09-26 06:40:52 -07:00
|
|
|
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("invalid ID: %s", 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) {
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("invalid tag: %s", t)
|
2020-01-28 04:06:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(c.Tags)
|
|
|
|
|
2019-11-06 07:24:15 -07:00
|
|
|
if len(c.Upstreams) != 0 {
|
|
|
|
err := dnsforward.ValidateUpstreams(c.Upstreams)
|
|
|
|
if err != nil {
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("invalid upstream servers: %s", err)
|
2019-11-06 07:24:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new client object
|
|
|
|
// Return true: success; false: client exists.
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) Add(c Client) (bool, error) {
|
2020-01-28 04:06:52 -07:00
|
|
|
e := clients.check(&c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if e != nil {
|
|
|
|
return false, e
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
// check Name index
|
|
|
|
_, ok := clients.list[c.Name]
|
|
|
|
if ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
// check ID index
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
c2, ok := clients.idIndex[id]
|
2019-04-26 05:10:29 -07:00
|
|
|
if ok {
|
2020-04-05 08:34:43 -07:00
|
|
|
return false, fmt.Errorf("another client uses the same ID (%s): %s", id, c2.Name)
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
// update Name index
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.list[c.Name] = &c
|
2019-09-26 06:40:52 -07:00
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
clients.idIndex[id] = &c
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
log.Debug("Clients: added '%s': ID:%v [%d]", c.Name, c.IDs, len(clients.list))
|
2019-04-26 05:10:29 -07:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
// Del removes a client
|
|
|
|
func (clients *clientsContainer) Del(name string) bool {
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
c, ok := clients.list[name]
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return TRUE if arrays are equal
|
|
|
|
func arraysEqual(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i != len(a); i++ {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 05:10:29 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update a client
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) Update(name string, c Client) error {
|
2020-01-28 04:06:52 -07:00
|
|
|
err := clients.check(&c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
old, ok := clients.list[name]
|
|
|
|
if !ok {
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("client not found")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// check Name index
|
|
|
|
if old.Name != c.Name {
|
|
|
|
_, ok = clients.list[c.Name]
|
|
|
|
if ok {
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("client already exists")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check IP index
|
2019-09-26 06:40:52 -07:00
|
|
|
if !arraysEqual(old.IDs, c.IDs) {
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
c2, ok := clients.idIndex[id]
|
|
|
|
if ok && c2 != old {
|
2020-04-05 08:34:43 -07:00
|
|
|
return fmt.Errorf("another client uses the same ID (%s): %s", id, c2.Name)
|
2019-09-26 06:40:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update ID index
|
|
|
|
for _, id := range old.IDs {
|
|
|
|
delete(clients.idIndex, id)
|
|
|
|
}
|
|
|
|
for _, id := range c.IDs {
|
|
|
|
clients.idIndex[id] = old
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update Name index
|
|
|
|
if old.Name != c.Name {
|
2020-01-10 05:28:58 -07:00
|
|
|
delete(clients.list, old.Name)
|
2019-09-26 06:40:52 -07:00
|
|
|
clients.list[c.Name] = old
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:31:27 -07:00
|
|
|
// update upstreams cache
|
2020-01-09 07:29:53 -07:00
|
|
|
c.upstreamObjects = nil
|
2019-12-23 09:31:27 -07:00
|
|
|
|
2019-09-26 06:40:52 -07:00
|
|
|
*old = c
|
2019-04-26 05:10:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:44:27 -07:00
|
|
|
// SetWhoisInfo - associate WHOIS information with a client
|
|
|
|
func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2019-12-23 10:02:06 -07:00
|
|
|
_, ok := clients.findByIP(ip)
|
2019-09-18 08:44:27 -07:00
|
|
|
if ok {
|
2019-12-23 10:02:06 -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
|
|
|
}
|
|
|
|
|
|
|
|
ch, ok := clients.ipHost[ip]
|
|
|
|
if ok {
|
|
|
|
ch.WhoisInfo = info
|
|
|
|
log.Debug("Clients: set WHOIS info for auto-client %s: %v", ch.Host, ch.WhoisInfo)
|
2019-09-23 10:41:14 -07:00
|
|
|
return
|
2019-09-18 08:44:27 -07:00
|
|
|
}
|
|
|
|
|
2019-12-23 07:12:50 -07:00
|
|
|
// Create a ClientHost implicitly so that we don't do this check again
|
2019-10-11 06:58:10 -07:00
|
|
|
ch = &ClientHost{
|
2019-09-18 08:44:27 -07:00
|
|
|
Source: ClientSourceWHOIS,
|
|
|
|
}
|
|
|
|
ch.WhoisInfo = info
|
|
|
|
clients.ipHost[ip] = ch
|
|
|
|
log.Debug("Clients: set WHOIS info for auto-client with IP %s: %v", ip, ch.WhoisInfo)
|
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
// AddHost adds new IP -> Host pair
|
2019-06-25 07:51:53 -07:00
|
|
|
// Use priority of the source (etc/hosts > ARP > rDNS)
|
|
|
|
// so we overwrite existing entries with an equal or higher priority
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (bool, error) {
|
2019-04-26 05:10:29 -07:00
|
|
|
clients.lock.Lock()
|
2020-01-30 00:25:02 -07:00
|
|
|
b, e := clients.addHost(ip, host, source)
|
|
|
|
clients.lock.Unlock()
|
|
|
|
return b, e
|
|
|
|
}
|
2019-04-26 05:10:29 -07:00
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
func (clients *clientsContainer) addHost(ip, host string, source clientSource) (bool, error) {
|
2019-10-11 06:58:10 -07:00
|
|
|
// check auto-clients index
|
|
|
|
ch, ok := clients.ipHost[ip]
|
|
|
|
if ok && ch.Source > source {
|
2019-04-26 05:10:29 -07:00
|
|
|
return false, nil
|
2019-10-11 06:58:10 -07:00
|
|
|
} else if ok {
|
|
|
|
ch.Source = source
|
|
|
|
} else {
|
|
|
|
ch = &ClientHost{
|
|
|
|
Host: host,
|
|
|
|
Source: source,
|
|
|
|
}
|
|
|
|
clients.ipHost[ip] = ch
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2020-01-30 00:25:02 -07:00
|
|
|
log.Debug("Clients: added '%s' -> '%s' [%d]", ip, host, len(clients.ipHost))
|
2019-04-26 05:10:29 -07:00
|
|
|
return true, nil
|
|
|
|
}
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
// Remove all entries that match the specified source
|
|
|
|
func (clients *clientsContainer) rmHosts(source clientSource) int {
|
|
|
|
n := 0
|
|
|
|
for k, v := range clients.ipHost {
|
|
|
|
if v.Source == source {
|
|
|
|
delete(clients.ipHost, k)
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Debug("Clients: removed %d client aliases", n)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
// Fill clients array from system hosts-file
|
2019-07-09 08:19:50 -07:00
|
|
|
func (clients *clientsContainer) addFromHostsFile() {
|
2020-03-20 05:05:43 -07:00
|
|
|
hosts := clients.autoHosts.List()
|
2019-03-19 08:47:22 -07:00
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
_ = clients.rmHosts(ClientSourceHostsFile)
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
n := 0
|
2020-03-20 05:05:43 -07:00
|
|
|
for ip, names := range hosts {
|
|
|
|
for _, name := range names {
|
|
|
|
ok, err := clients.addHost(ip, name.String(), ClientSourceHostsFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Clients: %s", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2019-03-19 08:47:22 -07:00
|
|
|
}
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
log.Debug("Clients: added %d client aliases from system hosts-file", n)
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-06-25 07:51:53 -07:00
|
|
|
// Add IP -> Host pairs from the system's `arp -a` command output
|
|
|
|
// The command's output is:
|
|
|
|
// HOST (IP) at MAC on IFACE
|
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")
|
|
|
|
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
|
|
|
data, err := cmd.Output()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
|
|
log.Debug("command %s has failed: %v code:%d",
|
|
|
|
cmd.Path, err, cmd.ProcessState.ExitCode())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
_ = clients.rmHosts(ClientSourceARP)
|
|
|
|
|
2019-06-25 07:51:53 -07:00
|
|
|
n := 0
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
ok, e := clients.addHost(ip, host, ClientSourceARP)
|
2019-06-25 07:51:53 -07:00
|
|
|
if e != nil {
|
|
|
|
log.Tracef("%s", e)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
log.Debug("Clients: added %d client aliases from 'arp -a' command output", n)
|
2019-06-25 07:51:53 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 00:25:02 -07:00
|
|
|
// Add clients from DHCP that have non-empty Hostname property
|
2019-09-12 06:15:10 -07:00
|
|
|
func (clients *clientsContainer) addFromDHCP() {
|
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()
|
|
|
|
|
|
|
|
_ = clients.rmHosts(ClientSourceDHCP)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if len(l.Hostname) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-30 00:25:02 -07:00
|
|
|
ok, _ := clients.addHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
|
2019-10-16 02:03:51 -07:00
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-09-12 06:15:10 -07:00
|
|
|
}
|
2020-01-30 00:25:02 -07:00
|
|
|
log.Debug("Clients: added %d client aliases from DHCP", n)
|
2019-09-12 06:15:10 -07:00
|
|
|
}
|