mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-17 21:31:43 +03:00
7fab31beae
Merge in DNS/adguard-home from 2508-ip-conversion-vol2 to master Closes #2508. Squashed commit of the following: commit 5b9d33f9cd352756831f63e34c4aea48674628c1 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 20 17:15:17 2021 +0300 util: replace net.IPNet with pointer commit 680126de7d59464077f9edf1bbaa925dd3fcee19 Merge: d3ba6a6c5a50efad
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 20 17:02:41 2021 +0300 Merge branch 'master' into 2508-ip-conversion-vol2 commit d3ba6a6cdd01c0aa736418fdb86ed40120169fe9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 19 18:29:54 2021 +0300 all: remove last conversion commit 88b63f11a6c3f8705d7fa0c448c50dd646cc9214 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 19 14:12:45 2021 +0300 all: improve code quality commit 71af60c70a0dbaf55e2221023d6d2e4993c9e9a7 Merge: 98af37849f75725d
Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 18 17:13:27 2021 +0300 Merge branch 'master' into 2508-ip-conversion-vol2 commit 98af3784ce44d0993d171653c13d6e83bb8d1e6a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 18 16:32:53 2021 +0300 all: log changes commit e99595a172bae1e844019d344544be84ddd65e4e Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 18 16:06:49 2021 +0300 all: fix or remove remaining net.IP <-> string conversions commit 7fd0634ce945f7e4c9b856684c5199f8a84a543e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Jan 15 15:36:17 2021 +0300 all: remove redundant net.IP <-> string converions commit 5df8af030421237d41b67ed659f83526cc258199 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jan 14 16:35:25 2021 +0300 stats: remove redundant net.IP <-> string conversion commit fbe4e3fc015e6898063543a90c04401d76dbb18f Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jan 14 16:20:35 2021 +0300 querylog: remove redundant net.IP <-> string conversion
165 lines
3.7 KiB
Go
165 lines
3.7 KiB
Go
// +build linux
|
|
|
|
package sysutil
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/util"
|
|
"github.com/AdguardTeam/golibs/file"
|
|
)
|
|
|
|
// maxConfigFileSize is the maximum length of interfaces configuration file.
|
|
const maxConfigFileSize = 1024 * 1024
|
|
|
|
func ifaceHasStaticIP(ifaceName string) (has bool, err error) {
|
|
var f *os.File
|
|
for _, check := range []struct {
|
|
checker func(io.Reader, string) (bool, error)
|
|
filePath string
|
|
}{{
|
|
checker: dhcpcdStaticConfig,
|
|
filePath: "/etc/dhcpcd.conf",
|
|
}, {
|
|
checker: ifacesStaticConfig,
|
|
filePath: "/etc/network/interfaces",
|
|
}} {
|
|
f, err = os.Open(check.filePath)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer f.Close()
|
|
|
|
fileReadCloser, err := aghio.LimitReadCloser(f, maxConfigFileSize)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer fileReadCloser.Close()
|
|
|
|
has, err = check.checker(fileReadCloser, ifaceName)
|
|
if has || err != nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
return has, err
|
|
}
|
|
|
|
// dhcpcdStaticConfig checks if interface is configured by /etc/dhcpcd.conf to
|
|
// have a static IP.
|
|
func dhcpcdStaticConfig(r io.Reader, ifaceName string) (has bool, err error) {
|
|
s := bufio.NewScanner(r)
|
|
var withinInterfaceCtx bool
|
|
|
|
for s.Scan() {
|
|
line := strings.TrimSpace(s.Text())
|
|
|
|
if withinInterfaceCtx && len(line) == 0 {
|
|
// An empty line resets our state.
|
|
withinInterfaceCtx = false
|
|
}
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
continue
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if withinInterfaceCtx {
|
|
if len(fields) >= 2 && fields[0] == "static" && strings.HasPrefix(fields[1], "ip_address=") {
|
|
return true, nil
|
|
}
|
|
if len(fields) > 0 && fields[0] == "interface" {
|
|
// Another interface found.
|
|
withinInterfaceCtx = false
|
|
}
|
|
continue
|
|
}
|
|
|
|
if len(fields) == 2 && fields[0] == "interface" && fields[1] == ifaceName {
|
|
// The interface found.
|
|
withinInterfaceCtx = true
|
|
}
|
|
}
|
|
|
|
return false, s.Err()
|
|
}
|
|
|
|
// ifacesStaticConfig checks if interface is configured by
|
|
// /etc/network/interfaces to have a static IP.
|
|
func ifacesStaticConfig(r io.Reader, ifaceName string) (has bool, err error) {
|
|
s := bufio.NewScanner(r)
|
|
for s.Scan() {
|
|
line := strings.TrimSpace(s.Text())
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
continue
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
// Man page interfaces(5) declares that interface definition
|
|
// should consist of the key word "iface" followed by interface
|
|
// name, and method at fourth field.
|
|
if len(fields) >= 4 && fields[0] == "iface" && fields[1] == ifaceName && fields[3] == "static" {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, s.Err()
|
|
}
|
|
|
|
func ifaceSetStaticIP(ifaceName string) (err error) {
|
|
ipNet := util.GetSubnet(ifaceName)
|
|
if ipNet.IP == nil {
|
|
return errors.New("can't get IP address")
|
|
}
|
|
|
|
gatewayIP := GatewayIP(ifaceName)
|
|
add := updateStaticIPdhcpcdConf(ifaceName, ipNet.String(), gatewayIP, ipNet.IP)
|
|
|
|
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
|
|
}
|
|
|
|
// updateStaticIPdhcpcdConf sets static IP address for the interface by writing
|
|
// into dhcpd.conf.
|
|
func updateStaticIPdhcpcdConf(ifaceName, ip string, gatewayIP, dnsIP net.IP) string {
|
|
var body []byte
|
|
|
|
add := fmt.Sprintf("\ninterface %s\nstatic ip_address=%s\n",
|
|
ifaceName, ip)
|
|
body = append(body, []byte(add)...)
|
|
|
|
if gatewayIP != nil {
|
|
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)
|
|
}
|