mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-21 15:21:33 +03:00
c6888326b0
Closes #2576. Updates #2275. Updates #2419. Updates #2443. Squashed commit of the following: commit b1a4809ada298d675de12740051ba26fb9945957 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri May 21 14:01:40 2021 +0300 all: add --local-frontend, upd docker commit 619ee7c82f27e3405753003dbec556ffb056d025 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu May 20 15:02:33 2021 +0300 bamboo-specs: bump docker version commit 5c2b2fbce80afdcc81fd0cb83674dc3d64facbf1 Merge: 6536b32d9c60aef6
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu May 20 15:01:47 2021 +0300 Merge branch 'master' into 2275-upd-go commit 6536b32dd4580425f7dedde6765463a79b9bd699 Merge: 9bb32bc46f7fd33a
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 20:38:48 2021 +0300 Merge branch 'master' into 2275-upd-go commit 9bb32bc4c0ac0f3a97195adc75359e48c9c58897 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 18:48:50 2021 +0300 all: fix build, imp err handling commit 6868eac7f7d2980fb706881f53e72afe5f7c3447 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 18:09:32 2021 +0300 all: fix github lint commit ebbb9c55f32fbd57e34e8b161016aa6b291c097c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 17:36:56 2021 +0300 all: update go and backend tools
161 lines
3.4 KiB
Go
161 lines
3.4 KiB
Go
// +build windows
|
|
|
|
//go:build windows
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// systemResolvers implementation differs for Windows since Go's resolver
|
|
// doesn't work there.
|
|
//
|
|
// See https://github.com/golang/go/issues/33097.
|
|
type systemResolvers struct {
|
|
// addrs is the slice of cached local resolvers' addresses.
|
|
addrs []string
|
|
addrsLock sync.RWMutex
|
|
}
|
|
|
|
func newSystemResolvers(refreshIvl time.Duration, _ HostGenFunc) (sr SystemResolvers) {
|
|
return &systemResolvers{}
|
|
}
|
|
|
|
func (sr *systemResolvers) Get() (rs []string) {
|
|
sr.addrsLock.RLock()
|
|
defer sr.addrsLock.RUnlock()
|
|
|
|
addrs := sr.addrs
|
|
rs = make([]string, len(addrs))
|
|
copy(rs, addrs)
|
|
|
|
return rs
|
|
}
|
|
|
|
// getAddrs gets local resolvers' addresses from OS in a special Windows way.
|
|
//
|
|
// TODO(e.burkov): This whole function needs more detailed research on getting
|
|
// local resolvers addresses on Windows. We execute the external command for
|
|
// now that is not the most accurate way.
|
|
func (sr *systemResolvers) getAddrs() (addrs []string, err error) {
|
|
cmd := exec.Command("nslookup")
|
|
|
|
var stdin io.WriteCloser
|
|
stdin, err = cmd.StdinPipe()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting the command's stdin pipe: %w", err)
|
|
}
|
|
|
|
var stdout io.ReadCloser
|
|
stdout, err = cmd.StdoutPipe()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting the command's stdout pipe: %w", err)
|
|
}
|
|
|
|
var stdoutLimited io.ReadCloser
|
|
stdoutLimited, err = aghio.LimitReadCloser(stdout, aghos.MaxCmdOutputSize)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("limiting stdout reader: %w", err)
|
|
}
|
|
|
|
go func() {
|
|
defer agherr.LogPanic("systemResolvers")
|
|
defer func() {
|
|
derr := stdin.Close()
|
|
if derr != nil {
|
|
log.Error("systemResolvers: closing stdin pipe: %s", derr)
|
|
}
|
|
}()
|
|
|
|
_, werr := io.WriteString(stdin, "exit")
|
|
if werr != nil {
|
|
log.Error("systemResolvers: writing to command pipe: %s", werr)
|
|
}
|
|
}()
|
|
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("start command executing: %w", err)
|
|
}
|
|
|
|
// The output of nslookup looks like this:
|
|
//
|
|
// Default Server: 192-168-1-1.qualified.domain.ru
|
|
// Address: 192.168.1.1
|
|
|
|
var possibleIPs []string
|
|
s := bufio.NewScanner(stdoutLimited)
|
|
for s.Scan() {
|
|
line := s.Text()
|
|
if len(line) == 0 {
|
|
continue
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
if len(fields) != 2 || fields[0] != "Address:" {
|
|
continue
|
|
}
|
|
|
|
// If the address contains port then it is separated with '#'.
|
|
ipStrs := strings.Split(fields[1], "#")
|
|
if len(ipStrs) == 0 {
|
|
continue
|
|
}
|
|
|
|
possibleIPs = append(possibleIPs, ipStrs[0])
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("executing the command: %w", err)
|
|
}
|
|
|
|
// Don't close StdoutPipe since Wait do it for us in ¿most? cases.
|
|
//
|
|
// See go doc os/exec.Cmd.StdoutPipe.
|
|
|
|
for _, addr := range possibleIPs {
|
|
if net.ParseIP(addr) == nil {
|
|
log.Debug("systemResolvers: %q is not a valid ip", addr)
|
|
|
|
continue
|
|
}
|
|
|
|
addrs = append(addrs, addr)
|
|
}
|
|
|
|
return addrs, nil
|
|
}
|
|
|
|
func (sr *systemResolvers) refresh() (err error) {
|
|
defer agherr.Annotate("systemResolvers: %w", &err)
|
|
|
|
got, err := sr.getAddrs()
|
|
if err != nil {
|
|
return fmt.Errorf("can't get addresses: %w", err)
|
|
}
|
|
if len(got) == 0 {
|
|
return nil
|
|
}
|
|
|
|
sr.addrsLock.Lock()
|
|
defer sr.addrsLock.Unlock()
|
|
|
|
sr.addrs = got
|
|
|
|
return nil
|
|
}
|