Skip to content

Commit

Permalink
Pull request: 2546 updater fix
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 2546-updater-fix to master

Closes AdguardTeam#2546.

Squashed commit of the following:

commit af243c9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Jan 13 14:33:37 2021 +0300

    updater: fix go 1.14 compat

commit 742fba2
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Jan 13 13:58:27 2021 +0300

    util: imp error check

commit c2bdbce
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Jan 11 18:51:26 2021 +0300

    all: fix and refactor update checking
  • Loading branch information
ainar-g authored and heyxkhoa committed Mar 17, 2023
1 parent bd41b76 commit 9d87ee9
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 464 deletions.
8 changes: 8 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ The rules are mostly sorted in the alphabetical order.
* Avoid `new`, especially with structs.
* Check against empty strings like this:
```go
if s == "" {
// …
}
```
* Constructors should validate their arguments and return meaningful errors.
As a corollary, avoid lazy initialization.
Expand Down
3 changes: 2 additions & 1 deletion internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -177,7 +178,7 @@ func initConfig() {
config.DHCP.Conf4.ICMPTimeout = 1000
config.DHCP.Conf6.LeaseDuration = 86400

if updateChannel == "none" || updateChannel == "edge" || updateChannel == "development" {
if ch := version.Channel(); ch == "edge" || ch == "development" {
config.BetaBindPort = 3001
}
}
Expand Down
5 changes: 3 additions & 2 deletions internal/home/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/log"
"github.com/NYTimes/gziphandler"
)
Expand Down Expand Up @@ -53,7 +54,7 @@ func handleStatus(w http.ResponseWriter, r *http.Request) {
"http_port": config.BindPort,
"dns_port": config.DNS.Port,
"running": isRunning(),
"version": versionString,
"version": version.Version(),
"language": config.Language,

"protection_enabled": c.ProtectionEnabled,
Expand Down Expand Up @@ -118,7 +119,7 @@ func registerControlHandlers() {
}

func httpRegister(method, url string, handler func(http.ResponseWriter, *http.Request)) {
if len(method) == 0 {
if method == "" {
// "/dns-query" handler doesn't need auth, gzip and isn't restricted by 1 HTTP method
Context.mux.HandleFunc(url, postInstall(handler))
return
Expand Down
16 changes: 9 additions & 7 deletions internal/home/controlupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/update"
"github.com/AdguardTeam/AdGuardHome/internal/updater"
"github.com/AdguardTeam/golibs/log"
)

Expand Down Expand Up @@ -47,13 +47,13 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
}
}

var info update.VersionInfo
var info updater.VersionInfo
for i := 0; i != 3; i++ {
func() {
Context.controlLock.Lock()
defer Context.controlLock.Unlock()

info, err = Context.updater.GetVersionResponse(req.RecheckNow)
info, err = Context.updater.VersionInfo(req.RecheckNow)
}()

if err != nil {
Expand All @@ -75,7 +75,9 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
break
}
if err != nil {
httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err)
vcu := Context.updater.VersionCheckURL()
httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", vcu, err, err)

return
}

Expand All @@ -88,12 +90,12 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {

// Perform an update procedure to the latest available version
func handleUpdate(w http.ResponseWriter, _ *http.Request) {
if len(Context.updater.NewVersion) == 0 {
if Context.updater.NewVersion() == "" {
httpError(w, http.StatusBadRequest, "/update request isn't allowed now")
return
}

err := Context.updater.DoUpdate()
err := Context.updater.Update()
if err != nil {
httpError(w, http.StatusInternalServerError, "%s", err)
return
Expand All @@ -108,7 +110,7 @@ func handleUpdate(w http.ResponseWriter, _ *http.Request) {
}

// Convert version.json data to our JSON response
func getVersionResp(info update.VersionInfo) []byte {
func getVersionResp(info updater.VersionInfo) []byte {
ret := make(map[string]interface{})
ret["can_autoupdate"] = false
ret["new_version"] = info.NewVersion
Expand Down
63 changes: 18 additions & 45 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/update"
"github.com/AdguardTeam/AdGuardHome/internal/updater"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/log"
"gopkg.in/natefinch/lumberjack.v2"
)
Expand All @@ -38,15 +39,6 @@ const (
configSyslog = "syslog"
)

// Update-related variables
var (
versionString = "dev"
updateChannel = "none"
versionCheckURL = ""
ARMVersion = ""
MIPSVersion = ""
)

// Global context
type homeContext struct {
// Modules
Expand All @@ -65,7 +57,7 @@ type homeContext struct {
web *Web // Web (HTTP, HTTPS) module
tls *TLSMod // TLS module
autoHosts util.AutoHosts // IP-hostname pairs taken from system configuration (e.g. /etc/hosts) files
updater *update.Updater
updater *updater.Updater

ipDetector *ipDetector

Expand Down Expand Up @@ -99,14 +91,7 @@ func (c *homeContext) getDataDir() string {
var Context homeContext

// Main is the entry point
func Main(version, channel, armVer, mipsVer string) {
// Init update-related global variables
versionString = version
updateChannel = channel
ARMVersion = armVer
MIPSVersion = mipsVer
versionCheckURL = "https://static.adguard.com/adguardhome/" + updateChannel + "/version.json"

func Main() {
// config can be specified, which reads options from there, but other command line flags have to override config values
// therefore, we must do it manually instead of using a lib
args := loadOptions()
Expand Down Expand Up @@ -139,20 +124,6 @@ func Main(version, channel, armVer, mipsVer string) {
run(args)
}

// version - returns the current version string
func version() string {
// TODO(a.garipov): I'm pretty sure we can extract some of this stuff
// from the build info.
msg := "AdGuard Home, version %s, channel %s, arch %s %s"
if ARMVersion != "" {
msg = msg + " v" + ARMVersion
} else if MIPSVersion != "" {
msg = msg + " " + MIPSVersion
}

return fmt.Sprintf(msg, versionString, updateChannel, runtime.GOOS, runtime.GOARCH)
}

func setupContext(args options) {
Context.runningAsService = args.runningAsService
Context.disableUpdate = args.disableUpdate
Expand Down Expand Up @@ -214,15 +185,16 @@ func setupConfig(args options) {

Context.autoHosts.Init("")

Context.updater = update.NewUpdater(update.Config{
Client: Context.client,
WorkDir: Context.workDir,
VersionURL: versionCheckURL,
VersionString: versionString,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
ARMVersion: ARMVersion,
ConfigName: config.getConfigFilename(),
Context.updater = updater.NewUpdater(&updater.Config{
Client: Context.client,
Version: version.Version(),
Channel: version.Channel(),
GOARCH: runtime.GOARCH,
GOOS: runtime.GOOS,
GOARM: version.GOARM(),
GOMIPS: version.GOMIPS(),
WorkDir: Context.workDir,
ConfName: config.getConfigFilename(),
})

Context.clients.Init(config.Clients, Context.dhcpServer, &Context.autoHosts)
Expand Down Expand Up @@ -260,7 +232,7 @@ func run(args options) {
memoryUsage(args)

// print the first message after logger is configured
log.Println(version())
log.Println(version.Full())
log.Debug("Current working directory is %s", Context.workDir)
if args.runningAsService {
log.Info("AdGuard Home is running as a service")
Expand Down Expand Up @@ -690,10 +662,11 @@ func customDialContext(ctx context.Context, network, addr string) (net.Conn, err
return nil, agherr.Many(fmt.Sprintf("couldn't dial to %s", addr), dialErrs...)
}

func getHTTPProxy(req *http.Request) (*url.URL, error) {
if len(config.ProxyURL) == 0 {
func getHTTPProxy(_ *http.Request) (*url.URL, error) {
if config.ProxyURL == "" {
return nil, nil
}

return url.Parse(config.ProxyURL)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/home/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"strconv"

"github.com/AdguardTeam/AdGuardHome/internal/version"
)

// options passed from command-line arguments
Expand Down Expand Up @@ -180,7 +182,7 @@ var versionArg = arg{
"Show the version and exit",
"version", "",
nil, nil, func(o options, exec string) (effect, error) {
return func() error { fmt.Println(version()); os.Exit(0); return nil }, nil
return func() error { fmt.Println(version.Full()); os.Exit(0); return nil }, nil
},
func(o options) []string { return nil },
}
Expand Down
114 changes: 0 additions & 114 deletions internal/update/check.go

This file was deleted.

Loading

0 comments on commit 9d87ee9

Please sign in to comment.