2021-05-21 11:55:42 +00:00
|
|
|
//go:build darwin
|
2021-06-15 16:42:41 +00:00
|
|
|
// +build darwin
|
2021-05-21 11:55:42 +00:00
|
|
|
|
2021-03-16 16:42:15 +00:00
|
|
|
package aghnet
|
2020-12-07 16:48:24 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-30 12:11:57 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2020-12-07 16:48:24 +00:00
|
|
|
"fmt"
|
2022-03-30 12:11:57 +00:00
|
|
|
"io"
|
2020-12-07 16:48:24 +00:00
|
|
|
"regexp"
|
|
|
|
|
2021-03-22 13:46:36 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2021-05-24 14:28:11 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-12-07 16:48:24 +00:00
|
|
|
)
|
|
|
|
|
2021-06-18 15:13:36 +00:00
|
|
|
// hardwarePortInfo contains information about the current state of the internet
|
|
|
|
// connection obtained from macOS networksetup.
|
2020-12-07 16:48:24 +00:00
|
|
|
type hardwarePortInfo struct {
|
|
|
|
name string
|
|
|
|
ip string
|
|
|
|
subnet string
|
|
|
|
gatewayIP string
|
|
|
|
static bool
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
|
2020-12-07 16:48:24 +00:00
|
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return portInfo.static, nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
// getCurrentHardwarePortInfo gets information for the specified network
|
|
|
|
// interface.
|
2020-12-07 16:48:24 +00:00
|
|
|
func getCurrentHardwarePortInfo(ifaceName string) (hardwarePortInfo, error) {
|
2022-03-30 12:11:57 +00:00
|
|
|
// First of all we should find hardware port name.
|
2020-12-07 16:48:24 +00:00
|
|
|
m := getNetworkSetupHardwareReports()
|
|
|
|
hardwarePort, ok := m[ifaceName]
|
|
|
|
if !ok {
|
|
|
|
return hardwarePortInfo{}, fmt.Errorf("could not find hardware port for %s", ifaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getHardwarePortInfo(hardwarePort)
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
// 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")
|
|
|
|
|
2021-08-12 14:33:53 +00:00
|
|
|
// 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
|
|
|
|
// to parse the output
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): There should be more proper approach than parsing the
|
|
|
|
// command output. For example, see
|
|
|
|
// https://developer.apple.com/documentation/systemconfiguration.
|
2022-03-30 12:11:57 +00:00
|
|
|
func getNetworkSetupHardwareReports() (reports map[string]string) {
|
|
|
|
_, out, err := aghosRunCommand("networksetup", "-listallhardwareports")
|
2020-12-07 16:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
reports = make(map[string]string)
|
2020-12-07 16:48:24 +00:00
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
matches := hardwareReportsReg.FindAllSubmatch(out, -1)
|
|
|
|
for _, m := range matches {
|
|
|
|
reports[string(m[2])] = string(m[1])
|
2020-12-07 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
return reports
|
2020-12-07 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
// 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")
|
2020-12-07 16:48:24 +00:00
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
func getHardwarePortInfo(hardwarePort string) (h hardwarePortInfo, err error) {
|
|
|
|
_, out, err := aghosRunCommand("networksetup", "-getinfo", hardwarePort)
|
2020-12-07 16:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return h, err
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
match := hardwarePortReg.FindSubmatch(out)
|
|
|
|
if len(match) != 4 {
|
2021-05-24 14:28:11 +00:00
|
|
|
return h, errors.Error("could not find hardware port info")
|
2020-12-07 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
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
|
2020-12-07 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ifaceSetStaticIP(ifaceName string) (err error) {
|
|
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if portInfo.static {
|
2022-03-30 12:11:57 +00:00
|
|
|
return errors.Error("ip address is already static")
|
2020-12-07 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dnsAddrs, err := getEtcResolvConfServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
args := append([]string{"-setdnsservers", portInfo.name}, dnsAddrs...)
|
2020-12-07 16:48:24 +00:00
|
|
|
|
|
|
|
// Setting DNS servers is necessary when configuring a static IP
|
2022-03-30 12:11:57 +00:00
|
|
|
code, _, err := aghosRunCommand("networksetup", args...)
|
2020-12-07 16:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-30 12:11:57 +00:00
|
|
|
} else if code != 0 {
|
2020-12-07 16:48:24 +00:00
|
|
|
return fmt.Errorf("failed to set DNS servers, code=%d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually configures hardware port to have static IP
|
2022-03-30 12:11:57 +00:00
|
|
|
code, _, err = aghosRunCommand(
|
|
|
|
"networksetup",
|
|
|
|
"-setmanual",
|
|
|
|
portInfo.name,
|
|
|
|
portInfo.ip,
|
|
|
|
portInfo.subnet,
|
|
|
|
portInfo.gatewayIP,
|
|
|
|
)
|
2020-12-07 16:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-30 12:11:57 +00:00
|
|
|
} else if code != 0 {
|
2020-12-07 16:48:24 +00:00
|
|
|
return fmt.Errorf("failed to set DNS servers, code=%d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
// 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.:]+)")
|
|
|
|
|
2020-12-07 16:48:24 +00:00
|
|
|
// getEtcResolvConfServers returns a list of nameservers configured in
|
|
|
|
// /etc/resolv.conf.
|
2022-03-30 12:11:57 +00:00
|
|
|
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)
|
2020-12-07 16:48:24 +00:00
|
|
|
if err != nil {
|
2022-03-30 12:11:57 +00:00
|
|
|
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)
|
2020-12-07 16:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|