Pull request: all: mv IsOpenWrt

Updates #2829.

Squashed commit of the following:

commit a284a26ba5df101c78e6f866d1bdfa540d205666
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 8 20:34:20 2021 +0300

    aghos: fix darwin

commit 9dbd42d75ebb048c83dbd06a1f09d950b3d12181
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 8 20:11:56 2021 +0300

    all: mv IsOpenWrt
This commit is contained in:
Ainar Garipov
2021-04-08 20:42:04 +03:00
parent 8c7f2b77d5
commit 6a8f6357e2
10 changed files with 78 additions and 62 deletions

View File

@@ -1,3 +1,7 @@
// Package util contains various utilities.
//
// TODO(a.garipov): Such packages are widely considered an antipattern. Remove
// this when we refactor our project structure.
package util
import (
@@ -13,6 +17,7 @@ import (
"sync"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/log"
"github.com/fsnotify/fsnotify"
"github.com/miekg/dns"
@@ -68,7 +73,7 @@ func (a *AutoHosts) Init(hostsFn string) {
a.hostsFn = hostsFn
}
if IsOpenWrt() {
if aghos.IsOpenWrt() {
// OpenWrt: "/tmp/hosts/dhcp.cfg01411c".
a.hostsDirs = append(a.hostsDirs, "/tmp/hosts")
}

View File

@@ -1,55 +0,0 @@
// Package util contains various utilities.
//
// TODO(a.garipov): Such packages are widely considered an antipattern. Remove
// this when we refactor our project structure.
package util
import (
"bytes"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
)
// IsOpenWrt returns true if host OS is OpenWrt.
func IsOpenWrt() bool {
if runtime.GOOS != "linux" {
return false
}
const etcDir = "/etc"
// TODO(e.burkov): Take care of dealing with fs package after updating
// Go version to 1.16.
fileInfos, err := ioutil.ReadDir(etcDir)
if err != nil {
return false
}
// fNameSubstr is a part of a name of the desired file.
const fNameSubstr = "release"
osNameData := []byte("OpenWrt")
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}
if !strings.Contains(fileInfo.Name(), fNameSubstr) {
continue
}
var body []byte
body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
if err != nil {
continue
}
if bytes.Contains(body, osNameData) {
return true
}
}
return false
}