Pull request: 3435 openwrt detect
Merge in DNS/adguard-home from 3435-openwrt-detect to master Updates #3435. Squashed commit of the following: commit 04b10f407ced1c85ac8089f980d79e9bbfe14e95 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 19:02:55 2021 +0300 aghos: fix windows build commit d387cec5f9cae9256dccef8c666c02f2fb7449a2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 18:22:12 2021 +0300 aghos: imp code, tests commit 2450b98522eb032ec8658f3ef2384fc77b627cc6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 13:43:46 2021 +0300 all: imp code, docs commit 7fabba3a8dc70fe61dbaa8fd5445453816fe9ac7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 04:04:09 2021 +0300 all: log changes commit 7cc1235308caf09eb4c80c05a4f328b8d6909ec7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 03:33:13 2021 +0300 querylog: repl with golibs commit 84592087d3b2aca23613950bb203ff3c862624dc Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 03:16:37 2021 +0300 aghos: use filewalker commit e4f2964b0e031c7a9a053e85c0ff7c792c772929 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Aug 13 00:34:20 2021 +0300 aghos: mv recurrentchecker from aghnet
This commit is contained in:
@@ -8,69 +8,47 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
)
|
||||
|
||||
func canBindPrivilegedPorts() (can bool, err error) {
|
||||
return aghos.HaveAdminRights()
|
||||
}
|
||||
|
||||
// maxCheckedFileSize is the maximum acceptable length of the /etc/rc.conf file.
|
||||
const maxCheckedFileSize = 1024 * 1024
|
||||
|
||||
func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
|
||||
const filename = "/etc/rc.conf"
|
||||
|
||||
var f *os.File
|
||||
f, err = os.Open(filename)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() { err = errors.WithDeferred(err, f.Close()) }()
|
||||
|
||||
var r io.Reader
|
||||
r, err = aghio.LimitReader(f, maxCheckedFileSize)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return rcConfStaticConfig(r, ifaceName)
|
||||
return aghos.FileWalker(interfaceName(ifaceName).rcConfStaticConfig).Walk(filename)
|
||||
}
|
||||
|
||||
// rcConfStaticConfig checks if the interface is configured by /etc/rc.conf to
|
||||
// have a static IP.
|
||||
func rcConfStaticConfig(r io.Reader, ifaceName string) (has bool, err error) {
|
||||
func (n interfaceName) rcConfStaticConfig(r io.Reader) (_ []string, cont bool, err error) {
|
||||
s := bufio.NewScanner(r)
|
||||
for ifaceLinePref := fmt.Sprintf("ifconfig_%s", ifaceName); s.Scan(); {
|
||||
for pref := fmt.Sprintf("ifconfig_%s=", n); s.Scan(); {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if !strings.HasPrefix(line, ifaceLinePref) {
|
||||
if !strings.HasPrefix(line, pref) {
|
||||
continue
|
||||
}
|
||||
|
||||
eqIdx := len(ifaceLinePref)
|
||||
if line[eqIdx] != '=' {
|
||||
cfgLeft, cfgRight := len(pref)+1, len(line)-1
|
||||
if cfgLeft >= cfgRight {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldsStart, fieldsEnd := eqIdx+2, len(line)-1
|
||||
if fieldsStart >= fieldsEnd {
|
||||
continue
|
||||
}
|
||||
|
||||
fields := strings.Fields(line[fieldsStart:fieldsEnd])
|
||||
// TODO(e.burkov): Expand the check to cover possible
|
||||
// configurations from man rc.conf(5).
|
||||
fields := strings.Fields(line[cfgLeft:cfgRight])
|
||||
if len(fields) >= 2 &&
|
||||
strings.ToLower(fields[0]) == "inet" &&
|
||||
strings.EqualFold(fields[0], "inet") &&
|
||||
net.ParseIP(fields[1]) != nil {
|
||||
return true, s.Err()
|
||||
return nil, false, s.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return false, s.Err()
|
||||
return nil, true, s.Err()
|
||||
}
|
||||
|
||||
func ifaceSetStaticIP(string) (err error) {
|
||||
|
||||
@@ -12,49 +12,48 @@ import (
|
||||
)
|
||||
|
||||
func TestRcConfStaticConfig(t *testing.T) {
|
||||
const ifaceName = `em0`
|
||||
const iface interfaceName = `em0`
|
||||
const nl = "\n"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
rcconfData string
|
||||
wantHas bool
|
||||
wantCont bool
|
||||
}{{
|
||||
name: "simple",
|
||||
rcconfData: `ifconfig_em0="inet 127.0.0.253 netmask 0xffffffff"` + nl,
|
||||
wantHas: true,
|
||||
wantCont: false,
|
||||
}, {
|
||||
name: "case_insensitiveness",
|
||||
rcconfData: `ifconfig_em0="InEt 127.0.0.253 NeTmAsK 0xffffffff"` + nl,
|
||||
wantHas: true,
|
||||
wantCont: false,
|
||||
}, {
|
||||
name: "comments_and_trash",
|
||||
rcconfData: `# comment 1` + nl +
|
||||
`` + nl +
|
||||
`# comment 2` + nl +
|
||||
`ifconfig_em0="inet 127.0.0.253 netmask 0xffffffff"` + nl,
|
||||
wantHas: true,
|
||||
wantCont: false,
|
||||
}, {
|
||||
name: "aliases",
|
||||
rcconfData: `ifconfig_em0_alias="inet 127.0.0.1/24"` + nl +
|
||||
`ifconfig_em0="inet 127.0.0.253 netmask 0xffffffff"` + nl,
|
||||
wantHas: true,
|
||||
wantCont: false,
|
||||
}, {
|
||||
name: "incorrect_config",
|
||||
rcconfData: `ifconfig_em0="inet6 127.0.0.253 netmask 0xffffffff"` + nl +
|
||||
`ifconfig_em0="inet 127.0.0.253 net-mask 0xffffffff"` + nl +
|
||||
`ifconfig_em0="inet 256.256.256.256 netmask 0xffffffff"` + nl +
|
||||
`ifconfig_em0=""` + nl,
|
||||
wantHas: false,
|
||||
wantCont: true,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
r := strings.NewReader(tc.rcconfData)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
has, err := rcConfStaticConfig(r, ifaceName)
|
||||
_, cont, err := iface.rcConfStaticConfig(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tc.wantHas, has)
|
||||
assert.Equal(t, tc.wantCont, cont)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,130 +9,72 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/stringutil"
|
||||
"github.com/google/renameio/maybe"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// recurrentChecker is used to check all the files which may include references
|
||||
// for other ones.
|
||||
type recurrentChecker struct {
|
||||
// checker is the function to check if r's stream contains the desired
|
||||
// attribute. It must return all the patterns for files which should
|
||||
// also be checked and each of them should be valid for filepath.Glob
|
||||
// function.
|
||||
checker func(r io.Reader, desired string) (patterns []string, has bool, err error)
|
||||
// initPath is the path of the first member in the sequence of checked
|
||||
// files.
|
||||
initPath string
|
||||
// dhcpcdStaticConfig checks if interface is configured by /etc/dhcpcd.conf to
|
||||
// have a static IP.
|
||||
func (n interfaceName) dhcpcdStaticConfig(r io.Reader) (subsources []string, cont bool, err error) {
|
||||
s := bufio.NewScanner(r)
|
||||
ifaceFound := findIfaceLine(s, string(n))
|
||||
if !ifaceFound {
|
||||
return nil, true, s.Err()
|
||||
}
|
||||
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 2 &&
|
||||
fields[0] == "static" &&
|
||||
strings.HasPrefix(fields[1], "ip_address=") {
|
||||
return nil, false, s.Err()
|
||||
}
|
||||
|
||||
if len(fields) > 0 && fields[0] == "interface" {
|
||||
// Another interface found.
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil, true, s.Err()
|
||||
}
|
||||
|
||||
// maxCheckedFileSize is the maximum length of the file that recurrentChecker
|
||||
// may check.
|
||||
const maxCheckedFileSize = 1024 * 1024
|
||||
|
||||
// checkFile tries to open and to check single file located on the sourcePath.
|
||||
func (rc *recurrentChecker) checkFile(sourcePath, desired string) (
|
||||
subsources []string,
|
||||
has bool,
|
||||
err error,
|
||||
) {
|
||||
var f *os.File
|
||||
f, err = os.Open(sourcePath)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer func() { err = errors.WithDeferred(err, f.Close()) }()
|
||||
|
||||
var r io.Reader
|
||||
r, err = aghio.LimitReader(f, maxCheckedFileSize)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
subsources, has, err = rc.checker(r, desired)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if has {
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
return subsources, has, nil
|
||||
}
|
||||
|
||||
// handlePatterns parses the patterns and takes care of duplicates.
|
||||
func (rc *recurrentChecker) handlePatterns(sourcesSet *stringutil.Set, patterns []string) (
|
||||
subsources []string,
|
||||
err error,
|
||||
) {
|
||||
subsources = make([]string, 0, len(patterns))
|
||||
for _, p := range patterns {
|
||||
var matches []string
|
||||
matches, err = filepath.Glob(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid pattern %q: %w", p, err)
|
||||
// ifacesStaticConfig checks if the interface is configured by any file of
|
||||
// /etc/network/interfaces format to have a static IP.
|
||||
func (n interfaceName) ifacesStaticConfig(r io.Reader) (sub []string, cont bool, err error) {
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if len(line) == 0 || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, m := range matches {
|
||||
if sourcesSet.Has(m) {
|
||||
continue
|
||||
}
|
||||
// TODO(e.burkov): As man page interfaces(5) says, a line may be
|
||||
// extended across multiple lines by making the last character a
|
||||
// backslash. Provide extended lines support.
|
||||
|
||||
sourcesSet.Add(m)
|
||||
subsources = append(subsources, m)
|
||||
fields := strings.Fields(line)
|
||||
fieldsNum := len(fields)
|
||||
|
||||
// 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 fieldsNum >= 4 &&
|
||||
fields[0] == "iface" && fields[1] == string(n) && fields[3] == "static" {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
if fieldsNum >= 2 && fields[0] == "source" {
|
||||
sub = append(sub, fields[1])
|
||||
}
|
||||
}
|
||||
|
||||
return subsources, nil
|
||||
}
|
||||
|
||||
// check walks through all the files searching for the desired attribute.
|
||||
func (rc *recurrentChecker) check(desired string) (has bool, err error) {
|
||||
var i int
|
||||
sources := []string{rc.initPath}
|
||||
|
||||
defer func() {
|
||||
if i >= len(sources) {
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.Annotate(err, "checking %q: %w", sources[i])
|
||||
}()
|
||||
|
||||
var patterns, subsources []string
|
||||
// The slice of sources is separate from the set of sources to keep the
|
||||
// order in which the files are walked.
|
||||
for sourcesSet := stringutil.NewSet(rc.initPath); i < len(sources); i++ {
|
||||
patterns, has, err = rc.checkFile(sources[i], desired)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
if has {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
subsources, err = rc.handlePatterns(sourcesSet, patterns)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
sources = append(sources, subsources...)
|
||||
}
|
||||
|
||||
return false, nil
|
||||
return sub, true, s.Err()
|
||||
}
|
||||
|
||||
func ifaceHasStaticIP(ifaceName string) (has bool, err error) {
|
||||
@@ -141,14 +83,19 @@ func ifaceHasStaticIP(ifaceName string) (has bool, err error) {
|
||||
// /etc/network/interfaces doesn't, it will return true. Perhaps this
|
||||
// is not the most desirable behavior.
|
||||
|
||||
for _, rc := range []*recurrentChecker{{
|
||||
checker: dhcpcdStaticConfig,
|
||||
initPath: "/etc/dhcpcd.conf",
|
||||
iface := interfaceName(ifaceName)
|
||||
|
||||
for _, pair := range []struct {
|
||||
aghos.FileWalker
|
||||
filename string
|
||||
}{{
|
||||
FileWalker: iface.dhcpcdStaticConfig,
|
||||
filename: "/etc/dhcpcd.conf",
|
||||
}, {
|
||||
checker: ifacesStaticConfig,
|
||||
initPath: "/etc/network/interfaces",
|
||||
FileWalker: iface.ifacesStaticConfig,
|
||||
filename: "/etc/network/interfaces",
|
||||
}} {
|
||||
has, err = rc.check(ifaceName)
|
||||
has, err = pair.Walk(pair.filename)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -183,67 +130,6 @@ func findIfaceLine(s *bufio.Scanner, name string) (ok bool) {
|
||||
return false
|
||||
}
|
||||
|
||||
// dhcpcdStaticConfig checks if interface is configured by /etc/dhcpcd.conf to
|
||||
// have a static IP.
|
||||
func dhcpcdStaticConfig(r io.Reader, ifaceName string) (subsources []string, has bool, err error) {
|
||||
s := bufio.NewScanner(r)
|
||||
ifaceFound := findIfaceLine(s, ifaceName)
|
||||
if !ifaceFound {
|
||||
return nil, false, s.Err()
|
||||
}
|
||||
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 2 &&
|
||||
fields[0] == "static" &&
|
||||
strings.HasPrefix(fields[1], "ip_address=") {
|
||||
return nil, true, s.Err()
|
||||
}
|
||||
|
||||
if len(fields) > 0 && fields[0] == "interface" {
|
||||
// Another interface found.
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false, s.Err()
|
||||
}
|
||||
|
||||
// ifacesStaticConfig checks if the interface is configured by any file of
|
||||
// /etc/network/interfaces format to have a static IP.
|
||||
func ifacesStaticConfig(r io.Reader, ifaceName string) (subsources []string, has bool, err error) {
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if len(line) == 0 || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO(e.burkov): As man page interfaces(5) says, a line may be
|
||||
// extended across multiple lines by making the last character a
|
||||
// backslash. Provide extended lines and "source-directory"
|
||||
// stanzas support.
|
||||
|
||||
fields := strings.Fields(line)
|
||||
fieldsNum := len(fields)
|
||||
|
||||
// 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 fieldsNum >= 4 &&
|
||||
fields[0] == "iface" && fields[1] == ifaceName && fields[3] == "static" {
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
if fieldsNum >= 2 && fields[0] == "source" {
|
||||
subsources = append(subsources, fields[1])
|
||||
}
|
||||
}
|
||||
|
||||
return subsources, false, s.Err()
|
||||
}
|
||||
|
||||
// ifaceSetStaticIP configures the system to retain its current IP on the
|
||||
// interface through dhcpdc.conf.
|
||||
func ifaceSetStaticIP(ifaceName string) (err error) {
|
||||
|
||||
@@ -12,101 +12,90 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRecurrentChecker(t *testing.T) {
|
||||
c := &recurrentChecker{
|
||||
checker: ifacesStaticConfig,
|
||||
initPath: "./testdata/include-subsources",
|
||||
}
|
||||
|
||||
has, err := c.check("sample_name")
|
||||
require.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
|
||||
has, err = c.check("another_name")
|
||||
require.NoError(t, err)
|
||||
assert.False(t, has)
|
||||
}
|
||||
|
||||
const nl = "\n"
|
||||
|
||||
func TestDHCPCDStaticConfig(t *testing.T) {
|
||||
const iface interfaceName = `wlan0`
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
data []byte
|
||||
want bool
|
||||
name string
|
||||
data []byte
|
||||
wantCont bool
|
||||
}{{
|
||||
name: "has_not",
|
||||
data: []byte(`#comment` + nl +
|
||||
`# comment` + nl +
|
||||
`interface eth0` + nl +
|
||||
`static ip_address=192.168.0.1/24` + nl +
|
||||
`# interface wlan0` + nl +
|
||||
`# interface ` + iface + nl +
|
||||
`static ip_address=192.168.1.1/24` + nl +
|
||||
`# comment` + nl,
|
||||
),
|
||||
want: false,
|
||||
wantCont: true,
|
||||
}, {
|
||||
name: "has",
|
||||
data: []byte(`#comment` + nl +
|
||||
`# comment` + nl +
|
||||
`interface eth0` + nl +
|
||||
`static ip_address=192.168.0.1/24` + nl +
|
||||
`# interface wlan0` + nl +
|
||||
`# interface ` + iface + nl +
|
||||
`static ip_address=192.168.1.1/24` + nl +
|
||||
`# comment` + nl +
|
||||
`interface wlan0` + nl +
|
||||
`interface ` + iface + nl +
|
||||
`# comment` + nl +
|
||||
`static ip_address=192.168.2.1/24` + nl,
|
||||
),
|
||||
want: true,
|
||||
wantCont: false,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := bytes.NewReader(tc.data)
|
||||
_, has, err := dhcpcdStaticConfig(r, "wlan0")
|
||||
_, cont, err := iface.dhcpcdStaticConfig(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tc.want, has)
|
||||
assert.Equal(t, tc.wantCont, cont)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIfacesStaticConfig(t *testing.T) {
|
||||
const iface interfaceName = `enp0s3`
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
data []byte
|
||||
want bool
|
||||
wantCont bool
|
||||
wantPatterns []string
|
||||
}{{
|
||||
name: "has_not",
|
||||
data: []byte(`allow-hotplug enp0s3` + nl +
|
||||
data: []byte(`allow-hotplug ` + iface + nl +
|
||||
`#iface enp0s3 inet static` + nl +
|
||||
`# address 192.168.0.200` + nl +
|
||||
`# netmask 255.255.255.0` + nl +
|
||||
`# gateway 192.168.0.1` + nl +
|
||||
`iface enp0s3 inet dhcp` + nl,
|
||||
`iface ` + iface + ` inet dhcp` + nl,
|
||||
),
|
||||
want: false,
|
||||
wantCont: true,
|
||||
wantPatterns: []string{},
|
||||
}, {
|
||||
name: "has",
|
||||
data: []byte(`allow-hotplug enp0s3` + nl +
|
||||
`iface enp0s3 inet static` + nl +
|
||||
data: []byte(`allow-hotplug ` + iface + nl +
|
||||
`iface ` + iface + ` inet static` + nl +
|
||||
` address 192.168.0.200` + nl +
|
||||
` netmask 255.255.255.0` + nl +
|
||||
` gateway 192.168.0.1` + nl +
|
||||
`#iface enp0s3 inet dhcp` + nl,
|
||||
`#iface ` + iface + ` inet dhcp` + nl,
|
||||
),
|
||||
want: true,
|
||||
wantCont: false,
|
||||
wantPatterns: []string{},
|
||||
}, {
|
||||
name: "return_patterns",
|
||||
data: []byte(`source hello` + nl +
|
||||
`source world` + nl +
|
||||
`#iface enp0s3 inet static` + nl,
|
||||
`#iface ` + iface + ` inet static` + nl,
|
||||
),
|
||||
want: false,
|
||||
wantCont: true,
|
||||
wantPatterns: []string{"hello", "world"},
|
||||
}, {
|
||||
// This one tests if the first found valid interface prevents
|
||||
@@ -114,19 +103,19 @@ func TestIfacesStaticConfig(t *testing.T) {
|
||||
name: "ignore_patterns",
|
||||
data: []byte(`source hello` + nl +
|
||||
`source world` + nl +
|
||||
`iface enp0s3 inet static` + nl,
|
||||
`iface ` + iface + ` inet static` + nl,
|
||||
),
|
||||
want: true,
|
||||
wantCont: false,
|
||||
wantPatterns: []string{},
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
r := bytes.NewReader(tc.data)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := bytes.NewReader(tc.data)
|
||||
patterns, has, err := ifacesStaticConfig(r, "enp0s3")
|
||||
patterns, has, err := iface.ifacesStaticConfig(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tc.want, has)
|
||||
assert.Equal(t, tc.wantCont, has)
|
||||
assert.ElementsMatch(t, tc.wantPatterns, patterns)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,61 +8,34 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
)
|
||||
|
||||
func canBindPrivilegedPorts() (can bool, err error) {
|
||||
return aghos.HaveAdminRights()
|
||||
}
|
||||
|
||||
// maxCheckedFileSize is the maximum acceptable length of the /etc/hostname.*
|
||||
// files.
|
||||
const maxCheckedFileSize = 1024 * 1024
|
||||
|
||||
func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
|
||||
const filenameFmt = "/etc/hostname.%s"
|
||||
filename := fmt.Sprintf("/etc/hostname.%s", ifaceName)
|
||||
|
||||
filename := fmt.Sprintf(filenameFmt, ifaceName)
|
||||
var f *os.File
|
||||
if f, err = os.Open(filename); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
defer func() { err = errors.WithDeferred(err, f.Close()) }()
|
||||
|
||||
var r io.Reader
|
||||
r, err = aghio.LimitReader(f, maxCheckedFileSize)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return hostnameIfStaticConfig(r)
|
||||
return aghos.FileWalker(hostnameIfStaticConfig).Walk(filename)
|
||||
}
|
||||
|
||||
// hostnameIfStaticConfig checks if the interface is configured by
|
||||
// /etc/hostname.* to have a static IP.
|
||||
//
|
||||
// TODO(e.burkov): The platform-dependent functions to check the static IP
|
||||
// address configured are rather similar. Think about unifying common parts.
|
||||
func hostnameIfStaticConfig(r io.Reader) (has bool, err error) {
|
||||
func hostnameIfStaticConfig(r io.Reader) (_ []string, ok bool, err error) {
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 2 && fields[0] == "inet" && net.ParseIP(fields[1]) != nil {
|
||||
return true, s.Err()
|
||||
return nil, true, s.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return false, s.Err()
|
||||
return nil, false, s.Err()
|
||||
}
|
||||
|
||||
func ifaceSetStaticIP(string) (err error) {
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestHostnameIfStaticConfig(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
r := strings.NewReader(tc.rcconfData)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
has, err := hostnameIfStaticConfig(r)
|
||||
_, has, err := hostnameIfStaticConfig(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tc.wantHas, has)
|
||||
|
||||
8
internal/aghnet/net_unix.go
Normal file
8
internal/aghnet/net_unix.go
Normal file
@@ -0,0 +1,8 @@
|
||||
//go:build openbsd || freebsd || linux
|
||||
// +build openbsd freebsd linux
|
||||
|
||||
package aghnet
|
||||
|
||||
// interfaceName is a string containing network interface's name. The name is
|
||||
// used in file walking methods.
|
||||
type interfaceName string
|
||||
Reference in New Issue
Block a user