mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-16 11:52:58 +03:00
7a3eda02ce
Squashed commit of the following: commit57466233cb
Merge:2df5f281
867bf545
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 18:39:15 2020 +0300 Merge branch 'master' into 1069-install-static-ip commit2df5f281c4
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 18:35:54 2020 +0300 *: lang fix commitb4649a6b27
Merge:c2785253
f61d5f0f
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 16:47:30 2020 +0300 *(home): fixed issues with setting static IP on Mac commitc27852537d
Author: Andrey Meshkov <am@adguard.com> Date: Thu Feb 13 14:14:30 2020 +0300 +(dhcpd): added static IP for MacOS commitf61d5f0f85
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Thu Feb 13 14:13:35 2020 +0300 + client: show confirm before setting static IP commit7afa16fbe7
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Thu Feb 13 13:51:52 2020 +0300 - client: fix text commit019bff0851
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Thu Feb 13 13:49:16 2020 +0300 - client: pass all params to the check_config request commit194bed72f5
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 17:12:16 2020 +0300 *: fix home_test commit9359f6b55f
Merge:ae299058
c5ca2a77
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 15:54:54 2020 +0300 Merge with master commitae2990582d
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 15:53:36 2020 +0300 *(global): refactoring - moved runtime properties to Context commitd8d48c5386
Author: Andrey Meshkov <am@adguard.com> Date: Wed Feb 12 15:04:25 2020 +0300 *(dhcpd): refactoring, use dhcpd/network_utils where possible commit8d039c572f
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Fri Feb 7 18:37:39 2020 +0300 - client: fix button position commit26c47e59dd
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Fri Feb 7 18:08:56 2020 +0300 - client: fix static ip description commitcb12babc46
Author: Andrey Meshkov <am@adguard.com> Date: Fri Feb 7 17:08:39 2020 +0300 *: lower log level for some commands commitd9001ff848
Author: Andrey Meshkov <am@adguard.com> Date: Fri Feb 7 16:17:59 2020 +0300 *(documentation): updated openapi commit1d213d53c8
Merge:8406d7d2
80861860
Author: Andrey Meshkov <am@adguard.com> Date: Fri Feb 7 15:16:46 2020 +0300 *: merge with master commit8406d7d288
Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Fri Jan 31 16:52:22 2020 +0300 - client: fix locales commitfb476b0117
Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jan 31 13:29:03 2020 +0300 linter commit84b5708e71
Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jan 31 13:27:53 2020 +0300 linter commit143a86a28a
Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jan 31 13:26:47 2020 +0300 linter ... and 7 more commits
313 lines
7.3 KiB
Go
313 lines
7.3 KiB
Go
package dhcpd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"os/exec"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/util"
|
|
|
|
"github.com/AdguardTeam/golibs/file"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// Check if network interface has a static IP configured
|
|
// Supports: Raspbian.
|
|
func HasStaticIP(ifaceName string) (bool, error) {
|
|
if runtime.GOOS == "linux" {
|
|
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return hasStaticIPDhcpcdConf(string(body), ifaceName), nil
|
|
}
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
return hasStaticIPDarwin(ifaceName)
|
|
}
|
|
|
|
return false, fmt.Errorf("Cannot check if IP is static: not supported on %s", runtime.GOOS)
|
|
}
|
|
|
|
// Set a static IP for the specified network interface
|
|
func SetStaticIP(ifaceName string) error {
|
|
if runtime.GOOS == "linux" {
|
|
return setStaticIPDhcpdConf(ifaceName)
|
|
}
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
return setStaticIPDarwin(ifaceName)
|
|
}
|
|
|
|
return fmt.Errorf("Cannot set static IP on %s", runtime.GOOS)
|
|
}
|
|
|
|
// for dhcpcd.conf
|
|
func hasStaticIPDhcpcdConf(dhcpConf, ifaceName string) bool {
|
|
lines := strings.Split(dhcpConf, "\n")
|
|
nameLine := fmt.Sprintf("interface %s", ifaceName)
|
|
withinInterfaceCtx := false
|
|
|
|
for _, line := range lines {
|
|
line = strings.TrimSpace(line)
|
|
|
|
if withinInterfaceCtx && len(line) == 0 {
|
|
// an empty line resets our state
|
|
withinInterfaceCtx = false
|
|
}
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
continue
|
|
}
|
|
line = strings.TrimSpace(line)
|
|
|
|
if !withinInterfaceCtx {
|
|
if line == nameLine {
|
|
// we found our interface
|
|
withinInterfaceCtx = true
|
|
}
|
|
|
|
} else {
|
|
if strings.HasPrefix(line, "interface ") {
|
|
// we found another interface - reset our state
|
|
withinInterfaceCtx = false
|
|
continue
|
|
}
|
|
if strings.HasPrefix(line, "static ip_address=") {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Get gateway IP address
|
|
func getGatewayIP(ifaceName string) string {
|
|
cmd := exec.Command("ip", "route", "show", "dev", ifaceName)
|
|
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
|
d, err := cmd.Output()
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
return ""
|
|
}
|
|
|
|
fields := strings.Fields(string(d))
|
|
if len(fields) < 3 || fields[0] != "default" {
|
|
return ""
|
|
}
|
|
|
|
ip := net.ParseIP(fields[2])
|
|
if ip == nil {
|
|
return ""
|
|
}
|
|
|
|
return fields[2]
|
|
}
|
|
|
|
// setStaticIPDhcpdConf - updates /etc/dhcpd.conf and sets the current IP address to be static
|
|
func setStaticIPDhcpdConf(ifaceName string) error {
|
|
ip := util.GetSubnet(ifaceName)
|
|
if len(ip) == 0 {
|
|
return errors.New("Can't get IP address")
|
|
}
|
|
|
|
ip4, _, err := net.ParseCIDR(ip)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
gatewayIP := getGatewayIP(ifaceName)
|
|
add := updateStaticIPDhcpcdConf(ifaceName, ip, gatewayIP, ip4.String())
|
|
|
|
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
body = append(body, []byte(add)...)
|
|
err = file.SafeWrite("/etc/dhcpcd.conf", body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// updates dhcpd.conf content -- sets static IP address there
|
|
// for dhcpcd.conf
|
|
func updateStaticIPDhcpcdConf(ifaceName, ip, gatewayIP, dnsIP string) string {
|
|
var body []byte
|
|
|
|
add := fmt.Sprintf("\ninterface %s\nstatic ip_address=%s\n",
|
|
ifaceName, ip)
|
|
body = append(body, []byte(add)...)
|
|
|
|
if len(gatewayIP) != 0 {
|
|
add = fmt.Sprintf("static routers=%s\n",
|
|
gatewayIP)
|
|
body = append(body, []byte(add)...)
|
|
}
|
|
|
|
add = fmt.Sprintf("static domain_name_servers=%s\n\n",
|
|
dnsIP)
|
|
body = append(body, []byte(add)...)
|
|
|
|
return string(body)
|
|
}
|
|
|
|
// Check if network interface has a static IP configured
|
|
// Supports: MacOS.
|
|
func hasStaticIPDarwin(ifaceName string) (bool, error) {
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return portInfo.static, nil
|
|
}
|
|
|
|
// setStaticIPDarwin - uses networksetup util to set the current IP address to be static
|
|
// Additionally it configures the current DNS servers as well
|
|
func setStaticIPDarwin(ifaceName string) error {
|
|
portInfo, err := getCurrentHardwarePortInfo(ifaceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if portInfo.static {
|
|
return errors.New("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 := util.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 = util.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
|
|
}
|
|
|
|
// getCurrentHardwarePortInfo gets information 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 := util.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, 0)
|
|
|
|
matches := re.FindAllStringSubmatch(out, -1)
|
|
for i := range matches {
|
|
port := matches[i][1]
|
|
device := matches[i][2]
|
|
m[device] = port
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// hardwarePortInfo - information obtained using MacOS networksetup
|
|
// about the current state of the internet connection
|
|
type hardwarePortInfo struct {
|
|
name string
|
|
ip string
|
|
subnet string
|
|
gatewayIP string
|
|
static bool
|
|
}
|
|
|
|
func getHardwarePortInfo(hardwarePort string) (hardwarePortInfo, error) {
|
|
h := hardwarePortInfo{}
|
|
|
|
_, out, err := util.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.New("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
|
|
}
|
|
|
|
// Gets a list of nameservers currently configured in the /etc/resolv.conf
|
|
func getEtcResolvConfServers() ([]string, error) {
|
|
body, err := ioutil.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.New("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
|
|
}
|