AdGuardHome/internal/updater/check.go

143 lines
3.8 KiB
Go
Raw Normal View History

package updater
import (
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/ioutil"
"github.com/AdguardTeam/golibs/log"
"golang.org/x/exp/maps"
)
// TODO(a.garipov): Make configurable.
const versionCheckPeriod = 8 * time.Hour
// VersionInfo contains information about a new version.
type VersionInfo struct {
NewVersion string `json:"new_version,omitempty"`
Announcement string `json:"announcement,omitempty"`
AnnouncementURL string `json:"announcement_url,omitempty"`
// TODO(a.garipov): See if the frontend actually still cares about
// nullability.
CanAutoUpdate aghalg.NullBool `json:"can_autoupdate,omitempty"`
}
// MaxResponseSize is responses on server's requests maximum length in bytes.
const MaxResponseSize = 64 * 1024
// VersionInfo downloads the latest version information. If forceRecheck is
// false and there are cached results, those results are returned.
func (u *Updater) VersionInfo(forceRecheck bool) (vi VersionInfo, err error) {
u.mu.Lock()
defer u.mu.Unlock()
now := time.Now()
recheckTime := u.prevCheckTime.Add(versionCheckPeriod)
if !forceRecheck && now.Before(recheckTime) {
return u.prevCheckResult, u.prevCheckError
}
var resp *http.Response
vcu := u.versionCheckURL
resp, err = u.client.Get(vcu)
if err != nil {
return VersionInfo{}, fmt.Errorf("updater: HTTP GET %s: %w", vcu, err)
}
defer func() { err = errors.WithDeferred(err, resp.Body.Close()) }()
r := ioutil.LimitReader(resp.Body, MaxResponseSize)
// This use of ReadAll is safe, because we just limited the appropriate
// ReadCloser.
body, err := io.ReadAll(r)
if err != nil {
return VersionInfo{}, fmt.Errorf("updater: HTTP GET %s: %w", vcu, err)
}
Pull request: 5191-update-flag Merge in DNS/adguard-home from 5191-update-flag to master Updates #5191. Updates #4223. Squashed commit of the following: commit fbace4942844dc67f2467479385e06843c3abb6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Jan 9 12:05:16 2023 +0400 all: imp code, docs commit 8237dceb771ba95f545f79565d76cbb4ebd0d805 Merge: ca9518f2 bbdcc673 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Dec 30 14:45:55 2022 +0400 Merge branch 'master' into 5191-update-flag commit ca9518f20e5643572adf9734b93a5436ba30c865 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 20:36:33 2022 +0400 home: imp code commit 1dc6c7c3480df3df4a5f3f923f1feab7761a7945 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 18:26:08 2022 +0400 all: imp code, docs commit 7bbe893e98063b956482fd6f1c6be95a4f1956cf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:44:48 2022 +0400 home: restart service on update commit e0d3c287a7e1c05b1e397f4727c447a1fcd9f7f6 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 03:22:49 2022 +0400 all: update on first run commit 0aa4e78f03bf3819425accb468ce59e747506ef3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 02:47:30 2022 +0400 all: move some code to init less commit 68aebfa050b9965afef26653e9b699ff4aaf5b8b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 29 00:36:00 2022 +0400 WIP commit 2c7fb97d701ac158613c5a3a4d4d35c5b79b3d59 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Dec 28 14:15:59 2022 +0400 home: imp logs commit 4b06d089da835d6d187803bbb5ca1caf9973e2d3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Dec 27 19:21:17 2022 +0400 all: fix update flag
2023-01-09 10:38:31 +00:00
u.prevCheckTime = now
u.prevCheckResult, u.prevCheckError = u.parseVersionResponse(body)
return u.prevCheckResult, u.prevCheckError
}
func (u *Updater) parseVersionResponse(data []byte) (VersionInfo, error) {
info := VersionInfo{
CanAutoUpdate: aghalg.NBFalse,
}
versionJSON := map[string]string{
"version": "",
"announcement": "",
"announcement_url": "",
}
err := json.Unmarshal(data, &versionJSON)
if err != nil {
return info, fmt.Errorf("version.json: %w", err)
}
for k, v := range versionJSON {
if v == "" {
return info, fmt.Errorf("version.json: bad data: value for key %q is empty", k)
}
}
info.NewVersion = versionJSON["version"]
info.Announcement = versionJSON["announcement"]
info.AnnouncementURL = versionJSON["announcement_url"]
packageURL, key, found := u.downloadURL(versionJSON)
if !found {
return info, fmt.Errorf("version.json: no package URL: key %q not found in object", key)
}
info.CanAutoUpdate = aghalg.BoolToNullBool(info.NewVersion != u.version)
u.newVersion = info.NewVersion
u.packageURL = packageURL
return info, nil
}
// downloadURL returns the download URL for current build as well as its key in
// versionObj. If the key is not found, it additionally prints an informative
// log message.
func (u *Updater) downloadURL(versionObj map[string]string) (dlURL, key string, ok bool) {
if u.goarch == "arm" && u.goarm != "" {
key = fmt.Sprintf("download_%s_%sv%s", u.goos, u.goarch, u.goarm)
} else if isMIPS(u.goarch) && u.gomips != "" {
key = fmt.Sprintf("download_%s_%s_%s", u.goos, u.goarch, u.gomips)
} else {
key = fmt.Sprintf("download_%s_%s", u.goos, u.goarch)
}
dlURL, ok = versionObj[key]
if ok {
return dlURL, key, true
}
keys := maps.Keys(versionObj)
slices.Sort(keys)
log.Error("updater: key %q not found; got keys %q", key, keys)
return "", key, false
}
// isMIPS returns true if arch is any MIPS architecture.
func isMIPS(arch string) (ok bool) {
switch arch {
case
"mips",
"mips64",
"mips64le",
"mipsle":
return true
default:
return false
}
}