2019-06-10 09:33:19 +01:00
|
|
|
package home
|
2019-03-19 15:47:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-04-26 13:10:29 +01:00
|
|
|
"fmt"
|
2019-03-19 15:47:22 +00:00
|
|
|
"io/ioutil"
|
2019-04-26 13:10:29 +01:00
|
|
|
"net"
|
2019-03-19 15:47:22 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-06-25 15:51:53 +01:00
|
|
|
"os/exec"
|
2019-03-19 15:47:22 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2019-04-26 13:10:29 +01:00
|
|
|
"sync"
|
2019-06-25 15:51:53 +01:00
|
|
|
"time"
|
2019-03-19 15:47:22 +00:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2019-06-25 15:51:53 +01:00
|
|
|
"github.com/AdguardTeam/golibs/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
clientsUpdatePeriod = 1 * time.Hour
|
2019-03-19 15:47:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client information
|
|
|
|
type Client struct {
|
2019-04-26 13:10:29 +01:00
|
|
|
IP string
|
|
|
|
MAC string
|
|
|
|
Name string
|
|
|
|
UseOwnSettings bool // false: use global settings
|
|
|
|
FilteringEnabled bool
|
|
|
|
SafeSearchEnabled bool
|
|
|
|
SafeBrowsingEnabled bool
|
|
|
|
ParentalEnabled bool
|
2019-09-18 16:44:27 +01:00
|
|
|
WhoisInfo [][]string // [[key,value], ...]
|
2019-07-18 10:27:10 +01:00
|
|
|
|
|
|
|
UseOwnBlockedServices bool // false: use global settings
|
|
|
|
BlockedServices []string
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientJSON struct {
|
2019-04-26 13:10:29 +01:00
|
|
|
IP string `json:"ip"`
|
|
|
|
MAC string `json:"mac"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
UseGlobalSettings bool `json:"use_global_settings"`
|
|
|
|
FilteringEnabled bool `json:"filtering_enabled"`
|
|
|
|
ParentalEnabled bool `json:"parental_enabled"`
|
|
|
|
SafeSearchEnabled bool `json:"safebrowsing_enabled"`
|
|
|
|
SafeBrowsingEnabled bool `json:"safesearch_enabled"`
|
2019-07-18 10:27:10 +01:00
|
|
|
|
2019-09-18 16:44:27 +01:00
|
|
|
WhoisInfo map[string]interface{} `json:"whois_info"`
|
|
|
|
|
2019-07-18 10:27:10 +01:00
|
|
|
UseGlobalBlockedServices bool `json:"use_global_blocked_services"`
|
|
|
|
BlockedServices []string `json:"blocked_services"`
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
type clientSource uint
|
|
|
|
|
2019-09-18 16:44:27 +01:00
|
|
|
// Client sources
|
2019-04-26 13:10:29 +01:00
|
|
|
const (
|
2019-09-18 16:44:27 +01: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 13:10:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientHost information
|
|
|
|
type ClientHost struct {
|
2019-09-18 16:44:27 +01:00
|
|
|
Host string
|
|
|
|
Source clientSource
|
|
|
|
WhoisInfo [][]string // [[key,value], ...]
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientsContainer struct {
|
2019-09-18 16:44:27 +01:00
|
|
|
list map[string]*Client // name -> client
|
|
|
|
ipIndex map[string]*Client // IP -> client
|
2019-04-26 13:10:29 +01:00
|
|
|
ipHost map[string]ClientHost // IP -> Hostname
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
// Init initializes clients container
|
|
|
|
// Note: this function must be called only once
|
|
|
|
func (clients *clientsContainer) Init() {
|
2019-04-26 13:10:29 +01:00
|
|
|
if clients.list != nil {
|
|
|
|
log.Fatal("clients.list != nil")
|
|
|
|
}
|
|
|
|
clients.list = make(map[string]*Client)
|
|
|
|
clients.ipIndex = make(map[string]*Client)
|
|
|
|
clients.ipHost = make(map[string]ClientHost)
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
go clients.periodicUpdate()
|
2019-06-25 15:51:53 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
func (clients *clientsContainer) periodicUpdate() {
|
2019-06-25 15:51:53 +01:00
|
|
|
for {
|
2019-07-09 16:19:50 +01:00
|
|
|
clients.addFromHostsFile()
|
|
|
|
clients.addFromSystemARP()
|
2019-09-12 14:15:10 +01:00
|
|
|
clients.addFromDHCP()
|
2019-06-25 15:51:53 +01:00
|
|
|
time.Sleep(clientsUpdatePeriod)
|
|
|
|
}
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
// GetList returns the pointer to clients list
|
|
|
|
func (clients *clientsContainer) GetList() map[string]*Client {
|
2019-04-26 13:10:29 +01:00
|
|
|
return clients.list
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
// Exists checks if client with this IP already exists
|
2019-09-18 16:44:27 +01:00
|
|
|
func (clients *clientsContainer) Exists(ip string, source clientSource) bool {
|
2019-04-26 13:10:29 +01:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
_, ok := clients.ipIndex[ip]
|
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:44:27 +01: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 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
// Find searches for a client by IP
|
|
|
|
func (clients *clientsContainer) Find(ip string) (Client, bool) {
|
2019-04-26 13:10:29 +01:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
c, ok := clients.ipIndex[ip]
|
|
|
|
if ok {
|
2019-06-07 09:37:55 +01:00
|
|
|
return *c, true
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c = range clients.list {
|
|
|
|
if len(c.MAC) != 0 {
|
|
|
|
mac, err := net.ParseMAC(c.MAC)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-09 16:57:41 +01:00
|
|
|
ipAddr := config.dhcpServer.FindIPbyMAC(mac)
|
2019-04-26 13:10:29 +01:00
|
|
|
if ipAddr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ip == ipAddr.String() {
|
2019-06-07 09:37:55 +01:00
|
|
|
return *c, true
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:37:55 +01:00
|
|
|
return Client{}, false
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Client object's fields are correct
|
2019-07-09 16:19:50 +01:00
|
|
|
func (c *Client) check() error {
|
2019-04-26 13:10:29 +01:00
|
|
|
if len(c.Name) == 0 {
|
|
|
|
return fmt.Errorf("Invalid Name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len(c.IP) == 0 && len(c.MAC) == 0) ||
|
|
|
|
(len(c.IP) != 0 && len(c.MAC) != 0) {
|
|
|
|
return fmt.Errorf("IP or MAC required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.IP) != 0 {
|
|
|
|
ip := net.ParseIP(c.IP)
|
|
|
|
if ip == nil {
|
|
|
|
return fmt.Errorf("Invalid IP")
|
|
|
|
}
|
|
|
|
c.IP = ip.String()
|
|
|
|
} else {
|
|
|
|
_, err := net.ParseMAC(c.MAC)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid MAC: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new client object
|
|
|
|
// Return true: success; false: client exists.
|
2019-07-09 16:19:50 +01:00
|
|
|
func (clients *clientsContainer) Add(c Client) (bool, error) {
|
|
|
|
e := c.check()
|
2019-04-26 13:10:29 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// check IP index
|
|
|
|
if len(c.IP) != 0 {
|
|
|
|
c2, ok := clients.ipIndex[c.IP]
|
|
|
|
if ok {
|
|
|
|
return false, fmt.Errorf("Another client uses the same IP address: %s", c2.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 19:25:17 +01:00
|
|
|
ch, ok := clients.ipHost[c.IP]
|
|
|
|
if ok {
|
|
|
|
c.WhoisInfo = ch.WhoisInfo
|
|
|
|
delete(clients.ipHost, c.IP)
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
clients.list[c.Name] = &c
|
|
|
|
if len(c.IP) != 0 {
|
|
|
|
clients.ipIndex[c.IP] = &c
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("'%s': '%s' | '%s' -> [%d]", c.Name, c.IP, c.MAC, len(clients.list))
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
// Del removes a client
|
|
|
|
func (clients *clientsContainer) Del(name string) bool {
|
2019-04-26 13:10:29 +01:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
c, ok := clients.list[name]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(clients.list, name)
|
|
|
|
delete(clients.ipIndex, c.IP)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update a client
|
2019-07-09 16:19:50 +01:00
|
|
|
func (clients *clientsContainer) Update(name string, c Client) error {
|
|
|
|
err := c.check()
|
2019-04-26 13:10:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
old, ok := clients.list[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Client not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check Name index
|
|
|
|
if old.Name != c.Name {
|
|
|
|
_, ok = clients.list[c.Name]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("Client already exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check IP index
|
|
|
|
if old.IP != c.IP && len(c.IP) != 0 {
|
|
|
|
c2, ok := clients.ipIndex[c.IP]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("Another client uses the same IP address: %s", c2.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update Name index
|
|
|
|
if old.Name != c.Name {
|
|
|
|
delete(clients.list, old.Name)
|
|
|
|
}
|
|
|
|
clients.list[c.Name] = &c
|
|
|
|
|
|
|
|
// update IP index
|
|
|
|
if old.IP != c.IP {
|
|
|
|
delete(clients.ipIndex, old.IP)
|
|
|
|
}
|
|
|
|
if len(c.IP) != 0 {
|
|
|
|
clients.ipIndex[c.IP] = &c
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:44:27 +01:00
|
|
|
// SetWhoisInfo - associate WHOIS information with a client
|
|
|
|
func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
|
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
|
|
|
c, ok := clients.ipIndex[ip]
|
|
|
|
if ok {
|
|
|
|
c.WhoisInfo = info
|
|
|
|
log.Debug("Clients: set WHOIS info for client %s: %v", c.Name, c.WhoisInfo)
|
2019-09-23 18:41:14 +01:00
|
|
|
return
|
2019-09-18 16:44:27 +01: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 18:41:14 +01:00
|
|
|
return
|
2019-09-18 16:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ch = ClientHost{
|
|
|
|
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 16:19:50 +01:00
|
|
|
// AddHost adds new IP -> Host pair
|
2019-06-25 15:51:53 +01:00
|
|
|
// Use priority of the source (etc/hosts > ARP > rDNS)
|
|
|
|
// so we overwrite existing entries with an equal or higher priority
|
2019-07-09 16:19:50 +01:00
|
|
|
func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (bool, error) {
|
2019-04-26 13:10:29 +01:00
|
|
|
clients.lock.Lock()
|
|
|
|
defer clients.lock.Unlock()
|
|
|
|
|
2019-09-23 19:25:17 +01:00
|
|
|
// check index
|
|
|
|
_, ok := clients.ipIndex[ip]
|
|
|
|
if ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
// check index
|
2019-06-25 15:51:53 +01:00
|
|
|
c, ok := clients.ipHost[ip]
|
|
|
|
if ok && c.Source > source {
|
2019-04-26 13:10:29 +01:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.ipHost[ip] = ClientHost{
|
2019-09-18 16:44:27 +01:00
|
|
|
Host: host,
|
|
|
|
Source: source,
|
|
|
|
WhoisInfo: c.WhoisInfo,
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
2019-06-25 15:51:53 +01:00
|
|
|
log.Tracef("'%s' -> '%s' [%d]", ip, host, len(clients.ipHost))
|
2019-04-26 13:10:29 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
2019-03-19 15:47:22 +00:00
|
|
|
|
|
|
|
// Parse system 'hosts' file and fill clients array
|
2019-07-09 16:19:50 +01:00
|
|
|
func (clients *clientsContainer) addFromHostsFile() {
|
2019-03-19 15:47:22 +00:00
|
|
|
hostsFn := "/etc/hosts"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
|
|
|
|
}
|
|
|
|
|
|
|
|
d, e := ioutil.ReadFile(hostsFn)
|
|
|
|
if e != nil {
|
|
|
|
log.Info("Can't read file %s: %v", hostsFn, e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(d), "\n")
|
2019-04-26 13:10:29 +01:00
|
|
|
n := 0
|
2019-03-19 15:47:22 +00:00
|
|
|
for _, ln := range lines {
|
|
|
|
ln = strings.TrimSpace(ln)
|
|
|
|
if len(ln) == 0 || ln[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
ok, e := clients.AddHost(fields[0], fields[1], ClientSourceHostsFile)
|
2019-04-26 13:10:29 +01:00
|
|
|
if e != nil {
|
|
|
|
log.Tracef("%s", e)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
log.Info("Added %d client aliases from %s", n, hostsFn)
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:51:53 +01: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 16:19:50 +01:00
|
|
|
func (clients *clientsContainer) addFromSystemARP() {
|
2019-06-25 15:51:53 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
ok, e := clients.AddHost(ip, host, ClientSourceARP)
|
2019-06-25 15:51:53 +01:00
|
|
|
if e != nil {
|
|
|
|
log.Tracef("%s", e)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Added %d client aliases from 'arp -a' command output", n)
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:15:10 +01:00
|
|
|
// add clients from DHCP that have non-empty Hostname property
|
|
|
|
func (clients *clientsContainer) addFromDHCP() {
|
|
|
|
leases := config.dhcpServer.Leases()
|
|
|
|
for _, l := range leases {
|
|
|
|
if len(l.Hostname) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-17 22:26:58 +01:00
|
|
|
_, _ = config.clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
|
2019-09-12 14:15:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
type clientHostJSON struct {
|
|
|
|
IP string `json:"ip"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Source string `json:"source"`
|
2019-09-18 16:44:27 +01:00
|
|
|
|
|
|
|
WhoisInfo map[string]interface{} `json:"whois_info"`
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientListJSON struct {
|
|
|
|
Clients []clientJSON `json:"clients"`
|
|
|
|
AutoClients []clientHostJSON `json:"auto_clients"`
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// respond with information about configured clients
|
|
|
|
func handleGetClients(w http.ResponseWriter, r *http.Request) {
|
2019-04-26 13:10:29 +01:00
|
|
|
data := clientListJSON{}
|
2019-03-19 15:47:22 +00:00
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
config.clients.lock.Lock()
|
|
|
|
for _, c := range config.clients.list {
|
2019-03-19 15:47:22 +00:00
|
|
|
cj := clientJSON{
|
2019-04-26 13:10:29 +01:00
|
|
|
IP: c.IP,
|
|
|
|
MAC: c.MAC,
|
|
|
|
Name: c.Name,
|
|
|
|
UseGlobalSettings: !c.UseOwnSettings,
|
|
|
|
FilteringEnabled: c.FilteringEnabled,
|
|
|
|
ParentalEnabled: c.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: c.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: c.SafeBrowsingEnabled,
|
2019-07-18 10:27:10 +01:00
|
|
|
|
|
|
|
UseGlobalBlockedServices: !c.UseOwnBlockedServices,
|
|
|
|
BlockedServices: c.BlockedServices,
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|
2019-04-26 13:10:29 +01:00
|
|
|
|
|
|
|
if len(c.MAC) != 0 {
|
|
|
|
hwAddr, _ := net.ParseMAC(c.MAC)
|
2019-07-09 16:57:41 +01:00
|
|
|
ipAddr := config.dhcpServer.FindIPbyMAC(hwAddr)
|
2019-04-26 13:10:29 +01:00
|
|
|
if ipAddr != nil {
|
|
|
|
cj.IP = ipAddr.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:44:27 +01:00
|
|
|
cj.WhoisInfo = make(map[string]interface{})
|
|
|
|
for _, wi := range c.WhoisInfo {
|
|
|
|
cj.WhoisInfo[wi[0]] = wi[1]
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
data.Clients = append(data.Clients, cj)
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
for ip, ch := range config.clients.ipHost {
|
2019-04-26 13:10:29 +01:00
|
|
|
cj := clientHostJSON{
|
|
|
|
IP: ip,
|
|
|
|
Name: ch.Host,
|
|
|
|
}
|
2019-09-18 16:44:27 +01:00
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
cj.Source = "etc/hosts"
|
2019-06-25 15:51:53 +01:00
|
|
|
switch ch.Source {
|
2019-09-12 14:15:10 +01:00
|
|
|
case ClientSourceDHCP:
|
|
|
|
cj.Source = "DHCP"
|
2019-06-25 15:51:53 +01:00
|
|
|
case ClientSourceRDNS:
|
2019-04-26 13:10:29 +01:00
|
|
|
cj.Source = "rDNS"
|
2019-06-25 15:51:53 +01:00
|
|
|
case ClientSourceARP:
|
|
|
|
cj.Source = "ARP"
|
2019-09-18 16:44:27 +01:00
|
|
|
case ClientSourceWHOIS:
|
|
|
|
cj.Source = "WHOIS"
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
2019-09-18 16:44:27 +01:00
|
|
|
|
|
|
|
cj.WhoisInfo = make(map[string]interface{})
|
|
|
|
for _, wi := range ch.WhoisInfo {
|
|
|
|
cj.WhoisInfo[wi[0]] = wi[1]
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
data.AutoClients = append(data.AutoClients, cj)
|
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
config.clients.lock.Unlock()
|
2019-04-26 13:10:29 +01:00
|
|
|
|
2019-03-19 15:47:22 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
e := json.NewEncoder(w).Encode(data)
|
|
|
|
if e != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Failed to encode to json: %v", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
// Convert JSON object to Client object
|
|
|
|
func jsonToClient(cj clientJSON) (*Client, error) {
|
|
|
|
c := Client{
|
|
|
|
IP: cj.IP,
|
|
|
|
MAC: cj.MAC,
|
|
|
|
Name: cj.Name,
|
|
|
|
UseOwnSettings: !cj.UseGlobalSettings,
|
|
|
|
FilteringEnabled: cj.FilteringEnabled,
|
|
|
|
ParentalEnabled: cj.ParentalEnabled,
|
|
|
|
SafeSearchEnabled: cj.SafeSearchEnabled,
|
|
|
|
SafeBrowsingEnabled: cj.SafeBrowsingEnabled,
|
2019-07-18 10:27:10 +01:00
|
|
|
|
|
|
|
UseOwnBlockedServices: !cj.UseGlobalBlockedServices,
|
|
|
|
BlockedServices: cj.BlockedServices,
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new client
|
|
|
|
func handleAddClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cj := clientJSON{}
|
|
|
|
err = json.Unmarshal(body, &cj)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "JSON parse: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := jsonToClient(cj)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
ok, err := config.clients.Add(*c)
|
2019-04-26 13:10:29 +01:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
httpError(w, http.StatusBadRequest, "Client already exists")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = writeAllConfigsAndReloadDNS()
|
|
|
|
returnOK(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove client
|
|
|
|
func handleDelClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cj := clientJSON{}
|
|
|
|
err = json.Unmarshal(body, &cj)
|
|
|
|
if err != nil || len(cj.Name) == 0 {
|
|
|
|
httpError(w, http.StatusBadRequest, "JSON parse: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
if !config.clients.Del(cj.Name) {
|
2019-04-26 13:10:29 +01:00
|
|
|
httpError(w, http.StatusBadRequest, "Client not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = writeAllConfigsAndReloadDNS()
|
|
|
|
returnOK(w)
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
type updateJSON struct {
|
2019-04-26 13:10:29 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Data clientJSON `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update client's properties
|
|
|
|
func handleUpdateClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
var dj updateJSON
|
2019-04-26 13:10:29 +01:00
|
|
|
err = json.Unmarshal(body, &dj)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "JSON parse: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(dj.Name) == 0 {
|
|
|
|
httpError(w, http.StatusBadRequest, "Invalid request")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := jsonToClient(dj.Data)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
err = config.clients.Update(dj.Name, *c)
|
2019-04-26 13:10:29 +01:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = writeAllConfigsAndReloadDNS()
|
|
|
|
returnOK(w)
|
|
|
|
}
|
|
|
|
|
2019-03-19 15:47:22 +00:00
|
|
|
// RegisterClientsHandlers registers HTTP handlers
|
|
|
|
func RegisterClientsHandlers() {
|
2019-08-21 12:39:37 +01:00
|
|
|
httpRegister(http.MethodGet, "/control/clients", handleGetClients)
|
|
|
|
httpRegister(http.MethodPost, "/control/clients/add", handleAddClient)
|
|
|
|
httpRegister(http.MethodPost, "/control/clients/delete", handleDelClient)
|
|
|
|
httpRegister(http.MethodPost, "/control/clients/update", handleUpdateClient)
|
2019-03-19 15:47:22 +00:00
|
|
|
}
|