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 }