From 91403d0b95e30e071bdf425a95e0f3746c76d359 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Mon, 1 Mar 2021 17:20:16 +0300 Subject: [PATCH] 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 Date: Mon Mar 1 17:02:24 2021 +0300 util: convert only once commit f1c74f4d18898f286d70c58f93b2fa21de6b5780 Author: Eugene Burkov Date: Mon Mar 1 16:22:51 2021 +0300 util: log changes, imp docs commit 0a4558d044602058255db71f825a730642cc9b07 Author: Eugene Burkov Date: Mon Mar 1 15:53:26 2021 +0300 util: imp os detection --- CHANGELOG.md | 4 +++- internal/util/helpers.go | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d47edd7..7d078d4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,11 +14,12 @@ and this project adheres to --> ### Fixed +- Incomplete OpenWRT detection ([#2757]). - DHCP lease's `expired` field incorrect time format ([#2692]). - Incomplete DNS upstreams validation ([#2674]). - 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 [#2688]: https://github.com/AdguardTeam/AdGuardHome/issues/2688 [#2692]: https://github.com/AdguardTeam/AdGuardHome/issues/2692 +[#2757]: https://github.com/AdguardTeam/AdGuardHome/issues/2757 diff --git a/internal/util/helpers.go b/internal/util/helpers.go index b575b269..1b759f45 100644 --- a/internal/util/helpers.go +++ b/internal/util/helpers.go @@ -5,10 +5,12 @@ package util import ( + "bytes" "fmt" "io/ioutil" "os" "os/exec" + "path/filepath" "runtime" "strings" ) @@ -64,16 +66,43 @@ func SplitNext(str *string, splitBy byte) string { return strings.TrimSpace(s) } -// IsOpenWRT checks if OS is OpenWRT. +// IsOpenWRT returns true if host OS is OpenWRT. func IsOpenWRT() bool { if runtime.GOOS != "linux" { 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 { 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 }