mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 10:58:29 -07:00
28f34ca399
Merge in DNS/adguard-home from 3257-ifaces-source to master
Updates #3257.
Squashed commit of the following:
commit 0b9b42bab731bbd048e97893cf209794ea014dfe
Merge: 530a1a23 e25a5329
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Mon Jun 28 16:53:36 2021 +0300
Merge branch 'master' into 3257-ifaces-source
commit 530a1a23a601c5575c8dc5f6f97cd84801cf911b
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Fri Jun 25 19:43:55 2021 +0300
aghnet: imp code, add docs
commit 58de84821b93bcbb3df1834ba33fbad817201b1d
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Fri Jun 25 13:34:43 2021 +0300
aghnet: sup "source" directive
commit c0901abd5212902295e8ee546fad652092fdb5a8
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jun 24 16:46:03 2021 +0300
aghos: mv func to aghnet
167 lines
3.9 KiB
Go
167 lines
3.9 KiB
Go
//go:build darwin
|
|
// +build darwin
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
)
|
|
|
|
// hardwarePortInfo contains information about the current state of the internet
|
|
// connection obtained from macOS networksetup.
|
|
type hardwarePortInfo struct {
|
|
name string
|
|
ip string
|
|
subnet string
|
|
gatewayIP string
|
|
static bool
|
|
}
|
|
|
|
func canBindPrivilegedPorts() (can bool, err error) {
|
|
return aghos.HaveAdminRights()
|
|
}
|
|
|
|
func ifaceHasStaticIP(ifaceName string) (bool, error) {
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return portInfo.static, nil
|
|
}
|
|
|
|
// getCurrentHardwarePortInfo gets information for the specified network interface.
|
|
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 {
|
|
_, out, err := aghos.RunCommand("networksetup", "-listallhardwareports")
|
|
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{}
|
|
|
|
_, out, err := aghos.RunCommand("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 {
|
|
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
|
|
}
|
|
|
|
func ifaceSetStaticIP(ifaceName string) (err error) {
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if portInfo.static {
|
|
return errors.Error("IP address is already static")
|
|
}
|
|
|
|
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
|
|
code, _, err := aghos.RunCommand("networksetup", args...)
|
|
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
|
|
code, _, err = aghos.RunCommand("networksetup", "-setmanual",
|
|
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) {
|
|
body, err := os.ReadFile("/etc/resolv.conf")
|
|
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 addrs, nil
|
|
}
|