+ use per-client DNS servers

This commit is contained in:
Simon Zolin
2019-11-06 17:24:15 +03:00
parent b6b26c0681
commit 7313c3bc53
7 changed files with 58 additions and 6 deletions

View File

@@ -13,6 +13,7 @@ import (
"time"
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/utils"
)
@@ -34,6 +35,8 @@ type Client struct {
UseOwnBlockedServices bool // false: use global settings
BlockedServices []string
Upstreams []string // list of upstream servers to be used for the client's requests
}
type clientSource uint
@@ -96,6 +99,8 @@ type clientObject struct {
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
BlockedServices []string `yaml:"blocked_services"`
Upstreams []string `yaml:"upstreams"`
}
func (clients *clientsContainer) addFromConfig(objects []clientObject) {
@@ -111,6 +116,8 @@ func (clients *clientsContainer) addFromConfig(objects []clientObject) {
UseOwnBlockedServices: !cy.UseGlobalBlockedServices,
BlockedServices: cy.BlockedServices,
Upstreams: cy.Upstreams,
}
_, err := clients.Add(cli)
if err != nil {
@@ -134,6 +141,8 @@ func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) {
UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
BlockedServices: cli.BlockedServices,
Upstreams: cli.Upstreams,
}
*objects = append(*objects, cy)
}
@@ -268,6 +277,14 @@ func (c *Client) check() error {
return fmt.Errorf("Invalid ID: %s", id)
}
if len(c.Upstreams) != 0 {
err := dnsforward.ValidateUpstreams(c.Upstreams)
if err != nil {
return fmt.Errorf("Invalid upstream servers: %s", err)
}
}
return nil
}

View File

@@ -20,6 +20,8 @@ type clientJSON struct {
UseGlobalBlockedServices bool `json:"use_global_blocked_services"`
BlockedServices []string `json:"blocked_services"`
Upstreams []string `json:"upstreams"`
}
type clientHostJSON struct {
@@ -92,6 +94,8 @@ func jsonToClient(cj clientJSON) (*Client, error) {
UseOwnBlockedServices: !cj.UseGlobalBlockedServices,
BlockedServices: cj.BlockedServices,
Upstreams: cj.Upstreams,
}
return &c, nil
}
@@ -109,6 +113,8 @@ func clientToJSON(c *Client) clientJSON {
UseGlobalBlockedServices: !c.UseOwnBlockedServices,
BlockedServices: c.BlockedServices,
Upstreams: c.Upstreams,
}
cj.WhoisInfo = make(map[string]interface{})

View File

@@ -170,9 +170,19 @@ func generateServerConfig() (dnsforward.ServerConfig, error) {
}
newconfig.FilterHandler = applyAdditionalFiltering
newconfig.GetUpstreamsByClient = getUpstreamsByClient
return newconfig, nil
}
func getUpstreamsByClient(clientAddr string) []string {
c, ok := config.clients.Find(clientAddr)
if !ok {
return []string{}
}
log.Debug("Using upstreams %v for client %s (IP: %s)", c.Upstreams, c.Name, clientAddr)
return c.Upstreams
}
// If a client has his own settings, apply them
func applyAdditionalFiltering(clientAddr string, setts *dnsfilter.RequestFilteringSettings) {