Pull request: 2846 cover aghnet vol.3
Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master Updates #2846. Squashed commit of the following: commit cb22987c43c17bbc8d098e65639cc84e2284bc7b Merge: cf995e9df31ffcc5Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 30 15:01:10 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit cf995e9dce635f16e10406a61e2ab12f06407f1f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:29:50 2022 +0300 aghnet: imp tests commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:03:06 2022 +0300 aghnet: imp tests commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56 Merge: f80812490d562a7bAuthor: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:39:13 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit f80812490c49f69655d409c6f015b069affa2f19 Merge: edccaa793603b1fcAuthor: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:29:20 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit edccaa79fca061ffeea1985c293eed123b16a09c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 13:53:40 2022 +0300 aghnet: imp tests commit 7c5028c92f0a6680516bda67c73e794182c9b825 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 18:01:28 2022 +0300 aghnet: imp code & docs commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 17:11:46 2022 +0300 aghnet: imp coverage commit 1eef110af3bf721a0275c695bf27c31815abff04 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 23 21:10:29 2022 +0300 all: return byte slice
This commit is contained in:
@@ -4,10 +4,11 @@
|
||||
package aghnet
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
@@ -27,7 +28,7 @@ func canBindPrivilegedPorts() (can bool, err error) {
|
||||
return aghos.HaveAdminRights()
|
||||
}
|
||||
|
||||
func ifaceHasStaticIP(ifaceName string) (bool, error) {
|
||||
func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
|
||||
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -36,9 +37,10 @@ func ifaceHasStaticIP(ifaceName string) (bool, error) {
|
||||
return portInfo.static, nil
|
||||
}
|
||||
|
||||
// getCurrentHardwarePortInfo gets information for the specified network interface.
|
||||
// getCurrentHardwarePortInfo gets information for the specified network
|
||||
// interface.
|
||||
func getCurrentHardwarePortInfo(ifaceName string) (hardwarePortInfo, error) {
|
||||
// First of all we should find hardware port name
|
||||
// First of all we should find hardware port name.
|
||||
m := getNetworkSetupHardwareReports()
|
||||
hardwarePort, ok := m[ifaceName]
|
||||
if !ok {
|
||||
@@ -48,6 +50,10 @@ func getCurrentHardwarePortInfo(ifaceName string) (hardwarePortInfo, error) {
|
||||
return getHardwarePortInfo(hardwarePort)
|
||||
}
|
||||
|
||||
// hardwareReportsReg is the regular expression matching the lines of
|
||||
// networksetup command output lines containing the interface information.
|
||||
var hardwareReportsReg = regexp.MustCompile("Hardware Port: (.*?)\nDevice: (.*?)\n")
|
||||
|
||||
// getNetworkSetupHardwareReports parses the output of the `networksetup
|
||||
// -listallhardwareports` command it returns a map where the key is the
|
||||
// interface name, and the value is the "hardware port" returns nil if it fails
|
||||
@@ -56,54 +62,44 @@ func getCurrentHardwarePortInfo(ifaceName string) (hardwarePortInfo, error) {
|
||||
// TODO(e.burkov): There should be more proper approach than parsing the
|
||||
// command output. For example, see
|
||||
// https://developer.apple.com/documentation/systemconfiguration.
|
||||
func getNetworkSetupHardwareReports() map[string]string {
|
||||
_, out, err := aghos.RunCommand("networksetup", "-listallhardwareports")
|
||||
func getNetworkSetupHardwareReports() (reports map[string]string) {
|
||||
_, out, err := aghosRunCommand("networksetup", "-listallhardwareports")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
re, err := regexp.Compile("Hardware Port: (.*?)\nDevice: (.*?)\n")
|
||||
if err != nil {
|
||||
return nil
|
||||
reports = make(map[string]string)
|
||||
|
||||
matches := hardwareReportsReg.FindAllSubmatch(out, -1)
|
||||
for _, m := range matches {
|
||||
reports[string(m[2])] = string(m[1])
|
||||
}
|
||||
|
||||
m := make(map[string]string)
|
||||
|
||||
matches := re.FindAllStringSubmatch(out, -1)
|
||||
for i := range matches {
|
||||
port := matches[i][1]
|
||||
device := matches[i][2]
|
||||
m[device] = port
|
||||
}
|
||||
|
||||
return m
|
||||
return reports
|
||||
}
|
||||
|
||||
func getHardwarePortInfo(hardwarePort string) (hardwarePortInfo, error) {
|
||||
h := hardwarePortInfo{}
|
||||
// hardwarePortReg is the regular expression matching the lines of networksetup
|
||||
// command output lines containing the port information.
|
||||
var hardwarePortReg = regexp.MustCompile("IP address: (.*?)\nSubnet mask: (.*?)\nRouter: (.*?)\n")
|
||||
|
||||
_, out, err := aghos.RunCommand("networksetup", "-getinfo", hardwarePort)
|
||||
func getHardwarePortInfo(hardwarePort string) (h hardwarePortInfo, err error) {
|
||||
_, out, err := aghosRunCommand("networksetup", "-getinfo", hardwarePort)
|
||||
if err != nil {
|
||||
return h, err
|
||||
}
|
||||
|
||||
re := regexp.MustCompile("IP address: (.*?)\nSubnet mask: (.*?)\nRouter: (.*?)\n")
|
||||
|
||||
match := re.FindStringSubmatch(out)
|
||||
if len(match) == 0 {
|
||||
match := hardwarePortReg.FindSubmatch(out)
|
||||
if len(match) != 4 {
|
||||
return h, errors.Error("could not find hardware port info")
|
||||
}
|
||||
|
||||
h.name = hardwarePort
|
||||
h.ip = match[1]
|
||||
h.subnet = match[2]
|
||||
h.gatewayIP = match[3]
|
||||
|
||||
if strings.Index(out, "Manual Configuration") == 0 {
|
||||
h.static = true
|
||||
}
|
||||
|
||||
return h, nil
|
||||
return hardwarePortInfo{
|
||||
name: hardwarePort,
|
||||
ip: string(match[1]),
|
||||
subnet: string(match[2]),
|
||||
gatewayIP: string(match[3]),
|
||||
static: bytes.Index(out, []byte("Manual Configuration")) == 0,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ifaceSetStaticIP(ifaceName string) (err error) {
|
||||
@@ -113,7 +109,7 @@ func ifaceSetStaticIP(ifaceName string) (err error) {
|
||||
}
|
||||
|
||||
if portInfo.static {
|
||||
return errors.Error("IP address is already static")
|
||||
return errors.Error("ip address is already static")
|
||||
}
|
||||
|
||||
dnsAddrs, err := getEtcResolvConfServers()
|
||||
@@ -121,50 +117,62 @@ func ifaceSetStaticIP(ifaceName string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
args = append(args, "-setdnsservers", portInfo.name)
|
||||
args = append(args, dnsAddrs...)
|
||||
args := append([]string{"-setdnsservers", portInfo.name}, dnsAddrs...)
|
||||
|
||||
// Setting DNS servers is necessary when configuring a static IP
|
||||
code, _, err := aghos.RunCommand("networksetup", args...)
|
||||
code, _, err := aghosRunCommand("networksetup", args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if code != 0 {
|
||||
} else if code != 0 {
|
||||
return fmt.Errorf("failed to set DNS servers, code=%d", code)
|
||||
}
|
||||
|
||||
// Actually configures hardware port to have static IP
|
||||
code, _, err = aghos.RunCommand("networksetup", "-setmanual",
|
||||
portInfo.name, portInfo.ip, portInfo.subnet, portInfo.gatewayIP)
|
||||
code, _, err = aghosRunCommand(
|
||||
"networksetup",
|
||||
"-setmanual",
|
||||
portInfo.name,
|
||||
portInfo.ip,
|
||||
portInfo.subnet,
|
||||
portInfo.gatewayIP,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if code != 0 {
|
||||
} else if code != 0 {
|
||||
return fmt.Errorf("failed to set DNS servers, code=%d", code)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// etcResolvConfReg is the regular expression matching the lines of resolv.conf
|
||||
// file containing a name server information.
|
||||
var etcResolvConfReg = regexp.MustCompile("nameserver ([a-zA-Z0-9.:]+)")
|
||||
|
||||
// getEtcResolvConfServers returns a list of nameservers configured in
|
||||
// /etc/resolv.conf.
|
||||
func getEtcResolvConfServers() ([]string, error) {
|
||||
body, err := os.ReadFile("/etc/resolv.conf")
|
||||
func getEtcResolvConfServers() (addrs []string, err error) {
|
||||
const filename = "etc/resolv.conf"
|
||||
|
||||
_, err = aghos.FileWalker(func(r io.Reader) (_ []string, _ bool, err error) {
|
||||
sc := bufio.NewScanner(r)
|
||||
for sc.Scan() {
|
||||
matches := etcResolvConfReg.FindAllStringSubmatch(sc.Text(), -1)
|
||||
if len(matches) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, m := range matches {
|
||||
addrs = append(addrs, m[1])
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false, sc.Err()
|
||||
}).Walk(rootDirFS, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
re := regexp.MustCompile("nameserver ([a-zA-Z0-9.:]+)")
|
||||
|
||||
matches := re.FindAllStringSubmatch(string(body), -1)
|
||||
if len(matches) == 0 {
|
||||
return nil, errors.Error("found no DNS servers in /etc/resolv.conf")
|
||||
}
|
||||
|
||||
addrs := make([]string, 0)
|
||||
for i := range matches {
|
||||
addrs = append(addrs, matches[i][1])
|
||||
return nil, fmt.Errorf("parsing etc/resolv.conf file: %w", err)
|
||||
} else if len(addrs) == 0 {
|
||||
return nil, fmt.Errorf("found no dns servers in %s", filename)
|
||||
}
|
||||
|
||||
return addrs, nil
|
||||
|
||||
Reference in New Issue
Block a user