Skip to content

Commit

Permalink
Pull request: 3435 openwrt detect
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 3435-openwrt-detect to master

Updates AdguardTeam#3435.

Squashed commit of the following:

commit 04b10f4
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 19:02:55 2021 +0300

    aghos: fix windows build

commit d387cec
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 18:22:12 2021 +0300

    aghos: imp code, tests

commit 2450b98
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 13:43:46 2021 +0300

    all: imp code, docs

commit 7fabba3
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 04:04:09 2021 +0300

    all: log changes

commit 7cc1235
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 03:33:13 2021 +0300

    querylog: repl with golibs

commit 8459208
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 03:16:37 2021 +0300

    aghos: use filewalker

commit e4f2964
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Aug 13 00:34:20 2021 +0300

    aghos: mv recurrentchecker from aghnet
  • Loading branch information
EugeneOne1 committed Aug 13, 2021
1 parent e3ad468 commit 394c2f6
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 476 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ and this project adheres to

### Changed

- Better OpenWrt detection ([#3435]).
- DNS-over-HTTPS queries that come from HTTP proxies in the `trusted_proxies`
list now use the real IP address of the client instead of the address of the
proxy ([#2799]).
Expand Down Expand Up @@ -123,6 +124,7 @@ and this project adheres to
[#3351]: https://github.com/AdguardTeam/AdGuardHome/issues/3351
[#3372]: https://github.com/AdguardTeam/AdGuardHome/issues/3372
[#3417]: https://github.com/AdguardTeam/AdGuardHome/issues/3417
[#3435]: https://github.com/AdguardTeam/AdGuardHome/issues/3435
[#3437]: https://github.com/AdguardTeam/AdGuardHome/issues/3437


Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/AdguardTeam/dnsproxy v0.39.2
github.com/AdguardTeam/golibs v0.9.0
github.com/AdguardTeam/golibs v0.9.1
github.com/AdguardTeam/urlfilter v0.14.6
github.com/NYTimes/gziphandler v1.1.1
github.com/ameshkov/dnscrypt/v2 v2.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/AdguardTeam/dnsproxy v0.39.2/go.mod h1:aNXKNdTyKfgAG2OS712SYSaGIM9Aas
github.com/AdguardTeam/golibs v0.4.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.4.2/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.8.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.9.0 h1:QwmHqeZOVs9XpkmPb2iYpZ35OBArjgTesE8gLtEFRFg=
github.com/AdguardTeam/golibs v0.9.0/go.mod h1:fCAMwPBJ8S7YMYbTWvYS+eeTLblP5E04IDtNAo7y7IY=
github.com/AdguardTeam/golibs v0.9.1 h1:mHSN4LfaY1uGmHPsl97paAND/VeSnM5r9XQ7pSYx93o=
github.com/AdguardTeam/golibs v0.9.1/go.mod h1:fCAMwPBJ8S7YMYbTWvYS+eeTLblP5E04IDtNAo7y7IY=
github.com/AdguardTeam/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU=
github.com/AdguardTeam/urlfilter v0.14.6 h1:emqoKZElooHACYehRBYENeKVN1a/rspxiqTIMYLuoIo=
github.com/AdguardTeam/urlfilter v0.14.6/go.mod h1:klx4JbOfc4EaNb5lWLqOwfg+pVcyRukmoJRvO55lL5U=
Expand Down
46 changes: 12 additions & 34 deletions internal/aghnet/net_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
continue
}

eqIdx := len(ifaceLinePref)
if line[eqIdx] != '=' {
if !strings.HasPrefix(line, pref) {
continue
}

fieldsStart, fieldsEnd := eqIdx+2, len(line)-1
if fieldsStart >= fieldsEnd {
cfgLeft, cfgRight := len(pref)+1, len(line)-1
if cfgLeft >= cfgRight {
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) {
Expand Down
19 changes: 9 additions & 10 deletions internal/aghnet/net_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Loading

0 comments on commit 394c2f6

Please sign in to comment.