all: add custom url to updater

This commit is contained in:
Eugene Burkov 2024-11-22 15:37:21 +03:00
parent 1d6d85cff4
commit 0452d8b356
8 changed files with 109 additions and 76 deletions

View File

@ -162,6 +162,10 @@ type configuration struct {
// SchemaVersion is the version of the configuration schema. See // SchemaVersion is the version of the configuration schema. See
// [configmigrate.LastSchemaVersion]. // [configmigrate.LastSchemaVersion].
SchemaVersion uint `yaml:"schema_version"` SchemaVersion uint `yaml:"schema_version"`
// UnsafeCustomUpdateIndexURL is the URL to the custom update index. It's
// only used in testing purposes and should not be used in release.
UnsafeCustomUpdateIndexURL bool `yaml:"unsafe_custom_update_index_url,omitempty"`
} }
// httpConfig is a block with HTTP configuration params. // httpConfig is a block with HTTP configuration params.

View File

@ -77,7 +77,10 @@ func (web *webAPI) requestVersionInfo(resp *versionResponse, recheck bool) (err
updater := web.conf.updater updater := web.conf.updater
for i := 0; i != 3; i++ { for i := 0; i != 3; i++ {
resp.VersionInfo, err = updater.VersionInfo(recheck) resp.VersionInfo, err = updater.VersionInfo(recheck)
if err != nil { if err == nil {
return nil
}
var terr temporaryError var terr temporaryError
if errors.As(err, &terr) && terr.Temporary() { if errors.As(err, &terr) && terr.Temporary() {
// Temporary network error. This case may happen while we're // Temporary network error. This case may happen while we're
@ -90,15 +93,12 @@ func (web *webAPI) requestVersionInfo(resp *versionResponse, recheck bool) (err
continue continue
} }
}
break break
} }
if err != nil { if err != nil {
vcu := updater.VersionCheckURL() return fmt.Errorf("getting version info: %w", err)
return fmt.Errorf("getting version info from %s: %w", vcu, err)
} }
return nil return nil

View File

@ -604,29 +604,9 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
execPath, err := os.Executable() execPath, err := os.Executable()
fatalOnError(errors.Annotate(err, "getting executable path: %w")) fatalOnError(errors.Annotate(err, "getting executable path: %w"))
u := &url.URL{
Scheme: urlutil.SchemeHTTPS,
// TODO(a.garipov): Make configurable.
Host: "static.adtidy.org",
Path: path.Join("adguardhome", version.Channel(), "version.json"),
}
confPath := configFilePath() confPath := configFilePath()
log.Debug("using config path %q for updater", confPath)
upd := updater.NewUpdater(&updater.Config{ upd := newUpdater(Context.workDir, confPath, execPath, config)
Client: config.Filtering.HTTPClient,
Version: version.Version(),
Channel: version.Channel(),
GOARCH: runtime.GOARCH,
GOOS: runtime.GOOS,
GOARM: version.GOARM(),
GOMIPS: version.GOMIPS(),
WorkDir: Context.workDir,
ConfName: confPath,
ExecPath: execPath,
VersionCheckURL: u.String(),
})
// TODO(e.burkov): This could be made earlier, probably as the option's // TODO(e.burkov): This could be made earlier, probably as the option's
// effect. // effect.
@ -698,6 +678,48 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
<-done <-done
} }
// newUpdater creates a new AdGuard Home updater.
func newUpdater(workDir, confPath, execPath string, config *configuration) (upd *updater.Updater) {
// envName is the name of the environment variable that can be used to
// override the default version check URL.
const envName = "ADGUARD_HOME_TEST_UPDATE_VERSION_URL"
var versionURL *url.URL
if version.Channel() == version.ChannelRelease || !config.UnsafeCustomUpdateIndexURL {
// Go on, use the default URL.
} else if versionURLStr, ok := os.LookupEnv(envName); ok {
var err error
versionURL, err = url.Parse(versionURLStr)
if err != nil {
log.Error(envName+" is not a valid URL: %s", err)
}
}
if versionURL == nil {
versionURL = &url.URL{
Scheme: urlutil.SchemeHTTPS,
Host: "static.adtidy.org",
Path: path.Join("adguardhome", version.Channel(), "version.json"),
}
}
log.Debug("using config path %q for updater", confPath)
return updater.NewUpdater(&updater.Config{
Client: config.Filtering.HTTPClient,
Version: version.Version(),
Channel: version.Channel(),
GOARCH: runtime.GOARCH,
GOOS: runtime.GOOS,
GOARM: version.GOARM(),
GOMIPS: version.GOMIPS(),
WorkDir: workDir,
ConfName: confPath,
ExecPath: execPath,
VersionCheckURL: versionURL,
})
}
// initUsers initializes context auth module. Clears config users field. // initUsers initializes context auth module. Clears config users field.
func initUsers() (auth *Auth, err error) { func initUsers() (auth *Auth, err error) {
sessFilename := filepath.Join(Context.getDataDir(), "sessions.db") sessFilename := filepath.Join(Context.getDataDir(), "sessions.db")
@ -1018,8 +1040,7 @@ func cmdlineUpdate(opts options, upd *updater.Updater, l *slog.Logger) {
info, err := upd.VersionInfo(true) info, err := upd.VersionInfo(true)
if err != nil { if err != nil {
vcu := upd.VersionCheckURL() log.Error("getting version info: %s", err)
log.Error("getting version info from %s: %s", vcu, err)
os.Exit(1) os.Exit(1)
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/ioutil" "github.com/AdguardTeam/golibs/ioutil"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/c2h5oh/datasize"
) )
// TODO(a.garipov): Make configurable. // TODO(a.garipov): Make configurable.
@ -28,8 +29,9 @@ type VersionInfo struct {
CanAutoUpdate aghalg.NullBool `json:"can_autoupdate,omitempty"` CanAutoUpdate aghalg.NullBool `json:"can_autoupdate,omitempty"`
} }
// MaxResponseSize is responses on server's requests maximum length in bytes. // maxVersionRespSize is the maximum length in bytes for version information
const MaxResponseSize = 64 * 1024 // response.
const maxVersionRespSize datasize.ByteSize = 64 * datasize.KB
// VersionInfo downloads the latest version information. If forceRecheck is // VersionInfo downloads the latest version information. If forceRecheck is
// false and there are cached results, those results are returned. // false and there are cached results, those results are returned.
@ -51,7 +53,7 @@ func (u *Updater) VersionInfo(forceRecheck bool) (vi VersionInfo, err error) {
} }
defer func() { err = errors.WithDeferred(err, resp.Body.Close()) }() defer func() { err = errors.WithDeferred(err, resp.Body.Close()) }()
r := ioutil.LimitReader(resp.Body, MaxResponseSize) r := ioutil.LimitReader(resp.Body, maxVersionRespSize.Bytes())
// This use of ReadAll is safe, because we just limited the appropriate // This use of ReadAll is safe, because we just limited the appropriate
// ReadCloser. // ReadCloser.

View File

@ -51,9 +51,11 @@ func TestUpdater_VersionInfo(t *testing.T) {
})) }))
t.Cleanup(srv.Close) t.Cleanup(srv.Close)
fakeURL, err := url.JoinPath(srv.URL, "adguardhome", version.ChannelBeta, "version.json") srvURL, err := url.Parse(srv.URL)
require.NoError(t, err) require.NoError(t, err)
fakeURL := srvURL.JoinPath("adguardhome", version.ChannelBeta, "version.json")
u := updater.NewUpdater(&updater.Config{ u := updater.NewUpdater(&updater.Config{
Client: srv.Client(), Client: srv.Client(),
Version: "v0.103.0-beta.1", Version: "v0.103.0-beta.1",
@ -134,7 +136,7 @@ func TestUpdater_VersionInfo_others(t *testing.T) {
GOARCH: tc.arch, GOARCH: tc.arch,
GOARM: tc.arm, GOARM: tc.arm,
GOMIPS: tc.mips, GOMIPS: tc.mips,
VersionCheckURL: fakeURL.String(), VersionCheckURL: fakeURL,
}) })
info, err := u.VersionInfo(false) info, err := u.VersionInfo(false)

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"io/fs" "io/fs"
"net/http" "net/http"
"net/url"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -65,6 +66,9 @@ type Updater struct {
type Config struct { type Config struct {
Client *http.Client Client *http.Client
// VersionCheckURL is url to the latest version announcement.
VersionCheckURL *url.URL
Version string Version string
Channel string Channel string
GOARCH string GOARCH string
@ -81,9 +85,6 @@ type Config struct {
// ExecPath is path to the executable file. // ExecPath is path to the executable file.
ExecPath string ExecPath string
// VersionCheckURL is url to the latest version announcement.
VersionCheckURL string
} }
// NewUpdater creates a new Updater. // NewUpdater creates a new Updater.
@ -101,7 +102,7 @@ func NewUpdater(conf *Config) *Updater {
confName: conf.ConfName, confName: conf.ConfName,
workDir: conf.WorkDir, workDir: conf.WorkDir,
execPath: conf.ExecPath, execPath: conf.ExecPath,
versionCheckURL: conf.VersionCheckURL, versionCheckURL: conf.VersionCheckURL.String(),
mu: &sync.RWMutex{}, mu: &sync.RWMutex{},
} }
@ -167,14 +168,6 @@ func (u *Updater) NewVersion() (nv string) {
return u.newVersion return u.newVersion
} }
// VersionCheckURL returns the version check URL.
func (u *Updater) VersionCheckURL() (vcu string) {
u.mu.RLock()
defer u.mu.RUnlock()
return u.versionCheckURL
}
// prepare fills all necessary fields in Updater object. // prepare fills all necessary fields in Updater object.
func (u *Updater) prepare() (err error) { func (u *Updater) prepare() (err error) {
u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion)) u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion))

View File

@ -1,6 +1,7 @@
package updater package updater
import ( import (
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -59,6 +60,9 @@ func TestUpdater_internal(t *testing.T) {
ExecPath: exePath, ExecPath: exePath,
WorkDir: wd, WorkDir: wd,
ConfName: yamlPath, ConfName: yamlPath,
// TODO(e.burkov): Rewrite the test to use a fake version check
// URL with a fake URLs for the package files.
VersionCheckURL: &url.URL{},
}) })
u.newVersion = "v0.103.1" u.newVersion = "v0.103.1"
@ -72,8 +76,9 @@ func TestUpdater_internal(t *testing.T) {
u.clean() u.clean()
// check backup files t.Run("backup", func(t *testing.T) {
d, err := os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome.yaml")) var d []byte
d, err = os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome.yaml"))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d)) assert.Equal(t, "AdGuardHome.yaml", string(d))
@ -82,8 +87,10 @@ func TestUpdater_internal(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, tc.exeName, string(d)) assert.Equal(t, tc.exeName, string(d))
})
// check updated files t.Run("updated", func(t *testing.T) {
var d []byte
d, err = os.ReadFile(exePath) d, err = os.ReadFile(exePath)
require.NoError(t, err) require.NoError(t, err)
@ -103,5 +110,6 @@ func TestUpdater_internal(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "AdGuardHome.yaml", string(d)) assert.Equal(t, "AdGuardHome.yaml", string(d))
})
} }
} }

View File

@ -65,7 +65,10 @@ func TestUpdater_Update(t *testing.T) {
srv := httptest.NewServer(mux) srv := httptest.NewServer(mux)
t.Cleanup(srv.Close) t.Cleanup(srv.Close)
versionCheckURL, err := url.JoinPath(srv.URL, versionPath) srvURL, err := url.Parse(srv.URL)
require.NoError(t, err)
versionCheckURL := srvURL.JoinPath(versionPath)
require.NoError(t, err) require.NoError(t, err)
u := updater.NewUpdater(&updater.Config{ u := updater.NewUpdater(&updater.Config{