2021-05-21 04:55:42 -07:00
|
|
|
//go:build darwin
|
2021-06-15 09:42:41 -07:00
|
|
|
// +build darwin
|
2021-05-21 04:55:42 -07:00
|
|
|
|
2021-03-16 09:42:15 -07:00
|
|
|
package aghnet
|
2020-12-07 09:48:24 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-05-21 04:55:42 -07:00
|
|
|
"os"
|
2020-12-07 09:48:24 -07:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2021-03-22 06:46:36 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2021-05-24 07:28:11 -07:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-12-07 09:48:24 -07:00
|
|
|
)
|
|
|
|
|
2021-06-18 08:13:36 -07:00
|
|
|
// hardwarePortInfo contains information about the current state of the internet
|
|
|
|
// connection obtained from macOS networksetup.
|
2020-12-07 09:48:24 -07:00
|
|
|
type hardwarePortInfo struct {
|
|
|
|
name string
|
|
|
|
ip string
|
|
|
|
subnet string
|
|
|
|
gatewayIP string
|
|
|
|
static bool
|
|
|
|
}
|
|
|
|
|
2021-06-28 07:02:45 -07:00
|
|
|
func canBindPrivilegedPorts() (can bool, err error) {
|
|
|
|
return aghos.HaveAdminRights()
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:48:24 -07:00
|
|
|
func ifaceHasStaticIP(ifaceName string) (bool, error) {
|
|
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return portInfo.static, nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:00:47 -07:00
|
|
|
// getCurrentHardwarePortInfo gets information for the specified network interface.
|
2020-12-07 09:48:24 -07:00
|
|
|
func getCurrentHardwarePortInfo(ifaceName string) (hardwarePortInfo, error) {
|
|
|
|
// First of all we should find hardware port name
|
|
|
|
m := getNetworkSetupHardwareReports()
|
|
|
|
hardwarePort, ok := m[ifaceName]
|
|
|
|
if !ok {
|
|
|
|
return hardwarePortInfo{}, fmt.Errorf("could not find hardware port for %s", ifaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getHardwarePortInfo(hardwarePort)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func getNetworkSetupHardwareReports() map[string]string {
|
2021-03-22 06:46:36 -07:00
|
|
|
_, out, err := aghos.RunCommand("networksetup", "-listallhardwareports")
|
2020-12-07 09:48:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
re, err := regexp.Compile("Hardware Port: (.*?)\nDevice: (.*?)\n")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHardwarePortInfo(hardwarePort string) (hardwarePortInfo, error) {
|
|
|
|
h := hardwarePortInfo{}
|
|
|
|
|
2021-03-22 06:46:36 -07:00
|
|
|
_, out, err := aghos.RunCommand("networksetup", "-getinfo", hardwarePort)
|
2020-12-07 09:48:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return h, err
|
|
|
|
}
|
|
|
|
|
|
|
|
re := regexp.MustCompile("IP address: (.*?)\nSubnet mask: (.*?)\nRouter: (.*?)\n")
|
|
|
|
|
|
|
|
match := re.FindStringSubmatch(out)
|
|
|
|
if len(match) == 0 {
|
2021-05-24 07:28:11 -07:00
|
|
|
return h, errors.Error("could not find hardware port info")
|
2020-12-07 09:48:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func ifaceSetStaticIP(ifaceName string) (err error) {
|
|
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if portInfo.static {
|
2021-05-24 07:28:11 -07:00
|
|
|
return errors.Error("IP address is already static")
|
2020-12-07 09:48:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dnsAddrs, err := getEtcResolvConfServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
args := make([]string, 0)
|
|
|
|
args = append(args, "-setdnsservers", portInfo.name)
|
|
|
|
args = append(args, dnsAddrs...)
|
|
|
|
|
|
|
|
// Setting DNS servers is necessary when configuring a static IP
|
2021-03-22 06:46:36 -07:00
|
|
|
code, _, err := aghos.RunCommand("networksetup", args...)
|
2020-12-07 09:48:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if code != 0 {
|
|
|
|
return fmt.Errorf("failed to set DNS servers, code=%d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually configures hardware port to have static IP
|
2021-03-22 06:46:36 -07:00
|
|
|
code, _, err = aghos.RunCommand("networksetup", "-setmanual",
|
2020-12-07 09:48:24 -07:00
|
|
|
portInfo.name, portInfo.ip, portInfo.subnet, portInfo.gatewayIP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if code != 0 {
|
|
|
|
return fmt.Errorf("failed to set DNS servers, code=%d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getEtcResolvConfServers returns a list of nameservers configured in
|
|
|
|
// /etc/resolv.conf.
|
|
|
|
func getEtcResolvConfServers() ([]string, error) {
|
2021-05-21 04:55:42 -07:00
|
|
|
body, err := os.ReadFile("/etc/resolv.conf")
|
2020-12-07 09:48:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
re := regexp.MustCompile("nameserver ([a-zA-Z0-9.:]+)")
|
|
|
|
|
|
|
|
matches := re.FindAllStringSubmatch(string(body), -1)
|
|
|
|
if len(matches) == 0 {
|
2021-05-24 07:28:11 -07:00
|
|
|
return nil, errors.Error("found no DNS servers in /etc/resolv.conf")
|
2020-12-07 09:48:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
addrs := make([]string, 0)
|
|
|
|
for i := range matches {
|
|
|
|
addrs = append(addrs, matches[i][1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|