mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 02:48:28 -07:00
7a3eda02ce
Squashed commit of the following: commit57466233cb
Merge:2df5f281
867bf545
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 18:39:15 2020 +0300 Merge branch 'master' into 1069-install-static-ip commit2df5f281c4
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 18:35:54 2020 +0300 *: lang fix commitb4649a6b27
Merge:c2785253
f61d5f0f
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 16:47:30 2020 +0300 *(home): fixed issues with setting static IP on Mac commitc27852537d
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 14:14:30 2020 +0300 +(dhcpd): added static IP for MacOS commitf61d5f0f85
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Thu Feb 13 14:13:35 2020 +0300 + client: show confirm before setting static IP commit7afa16fbe7
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Thu Feb 13 13:51:52 2020 +0300 - client: fix text commit019bff0851
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Thu Feb 13 13:49:16 2020 +0300 - client: pass all params to the check_config request commit194bed72f5
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 17:12:16 2020 +0300 *: fix home_test commit9359f6b55f
Merge:ae299058
c5ca2a77
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 15:54:54 2020 +0300 Merge with master commitae2990582d
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 15:53:36 2020 +0300 *(global): refactoring - moved runtime properties to Context commitd8d48c5386
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 15:04:25 2020 +0300 *(dhcpd): refactoring, use dhcpd/network_utils where possible commit8d039c572f
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Fri Feb 7 18:37:39 2020 +0300 - client: fix button position commit26c47e59dd
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Fri Feb 7 18:08:56 2020 +0300 - client: fix static ip description commitcb12babc46
Author: Andrey Meshkov <am@adguard.com> Date: Fri Feb 7 17:08:39 2020 +0300 *: lower log level for some commands commitd9001ff848
Author: Andrey Meshkov <am@adguard.com> Date: Fri Feb 7 16:17:59 2020 +0300 *(documentation): updated openapi commit1d213d53c8
Merge:8406d7d2
80861860
Author: Andrey Meshkov <am@adguard.com> Date: Fri Feb 7 15:16:46 2020 +0300 *: merge with master commit8406d7d288
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Fri Jan 31 16:52:22 2020 +0300 - client: fix locales commitfb476b0117
Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jan 31 13:29:03 2020 +0300 linter commit84b5708e71
Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jan 31 13:27:53 2020 +0300 linter commit143a86a28a
Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jan 31 13:26:47 2020 +0300 linter ... and 7 more commits
304 lines
7.3 KiB
Go
304 lines
7.3 KiB
Go
package home
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
|
"github.com/AdguardTeam/AdGuardHome/dnsforward"
|
|
"github.com/AdguardTeam/AdGuardHome/querylog"
|
|
"github.com/AdguardTeam/AdGuardHome/stats"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/joomcode/errorx"
|
|
)
|
|
|
|
// Called by other modules when configuration is changed
|
|
func onConfigModified() {
|
|
_ = config.write()
|
|
}
|
|
|
|
// initDNSServer creates an instance of the dnsforward.Server
|
|
// Please note that we must do it even if we don't start it
|
|
// so that we had access to the query log and the stats
|
|
func initDNSServer() error {
|
|
baseDir := Context.getDataDir()
|
|
|
|
err := os.MkdirAll(baseDir, 0755)
|
|
if err != nil {
|
|
return fmt.Errorf("Cannot create DNS data dir at %s: %s", baseDir, err)
|
|
}
|
|
|
|
statsConf := stats.Config{
|
|
Filename: filepath.Join(baseDir, "stats.db"),
|
|
LimitDays: config.DNS.StatsInterval,
|
|
ConfigModified: onConfigModified,
|
|
HTTPRegister: httpRegister,
|
|
}
|
|
Context.stats, err = stats.New(statsConf)
|
|
if err != nil {
|
|
return fmt.Errorf("Couldn't initialize statistics module")
|
|
}
|
|
conf := querylog.Config{
|
|
Enabled: config.DNS.QueryLogEnabled,
|
|
BaseDir: baseDir,
|
|
Interval: config.DNS.QueryLogInterval,
|
|
MemSize: config.DNS.QueryLogMemSize,
|
|
ConfigModified: onConfigModified,
|
|
HTTPRegister: httpRegister,
|
|
}
|
|
Context.queryLog = querylog.New(conf)
|
|
|
|
filterConf := config.DNS.DnsfilterConf
|
|
bindhost := config.DNS.BindHost
|
|
if config.DNS.BindHost == "0.0.0.0" {
|
|
bindhost = "127.0.0.1"
|
|
}
|
|
filterConf.ResolverAddress = fmt.Sprintf("%s:%d", bindhost, config.DNS.Port)
|
|
filterConf.ConfigModified = onConfigModified
|
|
filterConf.HTTPRegister = httpRegister
|
|
Context.dnsFilter = dnsfilter.New(&filterConf, nil)
|
|
|
|
Context.dnsServer = dnsforward.NewServer(Context.dnsFilter, Context.stats, Context.queryLog)
|
|
dnsConfig := generateServerConfig()
|
|
err = Context.dnsServer.Prepare(&dnsConfig)
|
|
if err != nil {
|
|
closeDNSServer()
|
|
return fmt.Errorf("dnsServer.Prepare: %s", err)
|
|
}
|
|
|
|
sessFilename := filepath.Join(baseDir, "sessions.db")
|
|
Context.auth = InitAuth(sessFilename, config.Users, config.WebSessionTTLHours*60*60)
|
|
if Context.auth == nil {
|
|
closeDNSServer()
|
|
return fmt.Errorf("Couldn't initialize Auth module")
|
|
}
|
|
config.Users = nil
|
|
|
|
Context.rdns = InitRDNS(Context.dnsServer, &Context.clients)
|
|
Context.whois = initWhois(&Context.clients)
|
|
|
|
initFiltering()
|
|
return nil
|
|
}
|
|
|
|
func isRunning() bool {
|
|
return Context.dnsServer != nil && Context.dnsServer.IsRunning()
|
|
}
|
|
|
|
// nolint (gocyclo)
|
|
// Return TRUE if IP is within public Internet IP range
|
|
func isPublicIP(ip net.IP) bool {
|
|
ip4 := ip.To4()
|
|
if ip4 != nil {
|
|
switch ip4[0] {
|
|
case 0:
|
|
return false //software
|
|
case 10:
|
|
return false //private network
|
|
case 127:
|
|
return false //loopback
|
|
case 169:
|
|
if ip4[1] == 254 {
|
|
return false //link-local
|
|
}
|
|
case 172:
|
|
if ip4[1] >= 16 && ip4[1] <= 31 {
|
|
return false //private network
|
|
}
|
|
case 192:
|
|
if (ip4[1] == 0 && ip4[2] == 0) || //private network
|
|
(ip4[1] == 0 && ip4[2] == 2) || //documentation
|
|
(ip4[1] == 88 && ip4[2] == 99) || //reserved
|
|
(ip4[1] == 168) { //private network
|
|
return false
|
|
}
|
|
case 198:
|
|
if (ip4[1] == 18 || ip4[2] == 19) || //private network
|
|
(ip4[1] == 51 || ip4[2] == 100) { //documentation
|
|
return false
|
|
}
|
|
case 203:
|
|
if ip4[1] == 0 && ip4[2] == 113 { //documentation
|
|
return false
|
|
}
|
|
case 224:
|
|
if ip4[1] == 0 && ip4[2] == 0 { //multicast
|
|
return false
|
|
}
|
|
case 255:
|
|
if ip4[1] == 255 && ip4[2] == 255 && ip4[3] == 255 { //subnet
|
|
return false
|
|
}
|
|
}
|
|
} else {
|
|
if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func onDNSRequest(d *proxy.DNSContext) {
|
|
ip := dnsforward.GetIPString(d.Addr)
|
|
if ip == "" {
|
|
// This would be quite weird if we get here
|
|
return
|
|
}
|
|
|
|
ipAddr := net.ParseIP(ip)
|
|
if !ipAddr.IsLoopback() {
|
|
Context.rdns.Begin(ip)
|
|
}
|
|
if isPublicIP(ipAddr) {
|
|
Context.whois.Begin(ip)
|
|
}
|
|
}
|
|
|
|
func generateServerConfig() dnsforward.ServerConfig {
|
|
newconfig := dnsforward.ServerConfig{
|
|
UDPListenAddr: &net.UDPAddr{IP: net.ParseIP(config.DNS.BindHost), Port: config.DNS.Port},
|
|
TCPListenAddr: &net.TCPAddr{IP: net.ParseIP(config.DNS.BindHost), Port: config.DNS.Port},
|
|
FilteringConfig: config.DNS.FilteringConfig,
|
|
ConfigModified: onConfigModified,
|
|
HTTPRegister: httpRegister,
|
|
OnDNSRequest: onDNSRequest,
|
|
}
|
|
|
|
if config.TLS.Enabled {
|
|
newconfig.TLSConfig = config.TLS.TLSConfig
|
|
if config.TLS.PortDNSOverTLS != 0 {
|
|
newconfig.TLSListenAddr = &net.TCPAddr{IP: net.ParseIP(config.DNS.BindHost), Port: config.TLS.PortDNSOverTLS}
|
|
}
|
|
}
|
|
|
|
newconfig.FilterHandler = applyAdditionalFiltering
|
|
newconfig.GetUpstreamsByClient = getUpstreamsByClient
|
|
return newconfig
|
|
}
|
|
|
|
func getUpstreamsByClient(clientAddr string) []upstream.Upstream {
|
|
return Context.clients.FindUpstreams(clientAddr)
|
|
}
|
|
|
|
// If a client has his own settings, apply them
|
|
func applyAdditionalFiltering(clientAddr string, setts *dnsfilter.RequestFilteringSettings) {
|
|
ApplyBlockedServices(setts, config.DNS.BlockedServices)
|
|
|
|
if len(clientAddr) == 0 {
|
|
return
|
|
}
|
|
|
|
c, ok := Context.clients.Find(clientAddr)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
log.Debug("Using settings for client with IP %s", clientAddr)
|
|
|
|
if c.UseOwnBlockedServices {
|
|
ApplyBlockedServices(setts, c.BlockedServices)
|
|
}
|
|
|
|
setts.ClientTags = c.Tags
|
|
|
|
if !c.UseOwnSettings {
|
|
return
|
|
}
|
|
|
|
setts.FilteringEnabled = c.FilteringEnabled
|
|
setts.SafeSearchEnabled = c.SafeSearchEnabled
|
|
setts.SafeBrowsingEnabled = c.SafeBrowsingEnabled
|
|
setts.ParentalEnabled = c.ParentalEnabled
|
|
}
|
|
|
|
func startDNSServer() error {
|
|
if isRunning() {
|
|
return fmt.Errorf("unable to start forwarding DNS server: Already running")
|
|
}
|
|
|
|
enableFilters(false)
|
|
|
|
err := Context.dnsServer.Start()
|
|
if err != nil {
|
|
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
|
|
}
|
|
|
|
Context.dnsFilter.Start()
|
|
startFiltering()
|
|
Context.stats.Start()
|
|
Context.queryLog.Start()
|
|
|
|
const topClientsNumber = 100 // the number of clients to get
|
|
topClients := Context.stats.GetTopClientsIP(topClientsNumber)
|
|
for _, ip := range topClients {
|
|
ipAddr := net.ParseIP(ip)
|
|
if !ipAddr.IsLoopback() {
|
|
Context.rdns.Begin(ip)
|
|
}
|
|
if isPublicIP(ipAddr) {
|
|
Context.whois.Begin(ip)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func reconfigureDNSServer() error {
|
|
newconfig := generateServerConfig()
|
|
err := Context.dnsServer.Reconfigure(&newconfig)
|
|
if err != nil {
|
|
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func stopDNSServer() error {
|
|
if !isRunning() {
|
|
return nil
|
|
}
|
|
|
|
err := Context.dnsServer.Stop()
|
|
if err != nil {
|
|
return errorx.Decorate(err, "Couldn't stop forwarding DNS server")
|
|
}
|
|
|
|
closeDNSServer()
|
|
return nil
|
|
}
|
|
|
|
func closeDNSServer() {
|
|
// DNS forward module must be closed BEFORE stats or queryLog because it depends on them
|
|
if Context.dnsServer != nil {
|
|
Context.dnsServer.Close()
|
|
Context.dnsServer = nil
|
|
}
|
|
|
|
if Context.dnsFilter != nil {
|
|
Context.dnsFilter.Close()
|
|
Context.dnsFilter = nil
|
|
}
|
|
|
|
if Context.stats != nil {
|
|
Context.stats.Close()
|
|
Context.stats = nil
|
|
}
|
|
|
|
if Context.queryLog != nil {
|
|
Context.queryLog.Close()
|
|
Context.queryLog = nil
|
|
}
|
|
|
|
if Context.auth != nil {
|
|
Context.auth.Close()
|
|
Context.auth = nil
|
|
}
|
|
|
|
log.Debug("Closed all DNS modules")
|
|
}
|