Pull request: 2757 fix OpenWRT detection

Merge in DNS/adguard-home from 2757-openwrt to master

Updates #2757.

Squashed commit of the following:

commit 8e94e6a67ae702bd1b281b306555a4ce9ecc6391
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 17:02:24 2021 +0300

    util: convert only once

commit f1c74f4d18898f286d70c58f93b2fa21de6b5780
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 16:22:51 2021 +0300

    util: log changes, imp docs

commit 0a4558d044602058255db71f825a730642cc9b07
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 15:53:26 2021 +0300

    util: imp os detection
This commit is contained in:
Eugene Burkov 2021-03-01 17:20:16 +03:00
parent d6a059e395
commit 91403d0b95
2 changed files with 35 additions and 4 deletions

View File

@ -14,11 +14,12 @@ and this project adheres to
--> -->
<!-- <!--
## [v0.105.2] - 2021-02-24 ## [v0.105.2] - 2021-03-04
--> -->
### Fixed ### Fixed
- Incomplete OpenWRT detection ([#2757]).
- DHCP lease's `expired` field incorrect time format ([#2692]). - DHCP lease's `expired` field incorrect time format ([#2692]).
- Incomplete DNS upstreams validation ([#2674]). - Incomplete DNS upstreams validation ([#2674]).
- Wrong parsing of DHCP options of the `ip` type ([#2688]). - Wrong parsing of DHCP options of the `ip` type ([#2688]).
@ -26,6 +27,7 @@ and this project adheres to
[#2674]: https://github.com/AdguardTeam/AdGuardHome/issues/2674 [#2674]: https://github.com/AdguardTeam/AdGuardHome/issues/2674
[#2688]: https://github.com/AdguardTeam/AdGuardHome/issues/2688 [#2688]: https://github.com/AdguardTeam/AdGuardHome/issues/2688
[#2692]: https://github.com/AdguardTeam/AdGuardHome/issues/2692 [#2692]: https://github.com/AdguardTeam/AdGuardHome/issues/2692
[#2757]: https://github.com/AdguardTeam/AdGuardHome/issues/2757

View File

@ -5,10 +5,12 @@
package util package util
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
) )
@ -64,16 +66,43 @@ func SplitNext(str *string, splitBy byte) string {
return strings.TrimSpace(s) return strings.TrimSpace(s)
} }
// IsOpenWRT checks if OS is OpenWRT. // IsOpenWRT returns true if host OS is OpenWRT.
func IsOpenWRT() bool { func IsOpenWRT() bool {
if runtime.GOOS != "linux" { if runtime.GOOS != "linux" {
return false return false
} }
body, err := ioutil.ReadFile("/etc/os-release") 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 { if err != nil {
return false return false
} }
return strings.Contains(string(body), "OpenWrt") // 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
}
body, err := ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
if err != nil {
continue
}
if bytes.Contains(body, osNameData) {
return true
}
}
return false
} }