Skip to content

Commit 0003530

Browse files
committed
chore: let /upgrade support channel and force as parameters in restful api
Leaving `channel` blank will automatically determine the channel. Other valid values are `alpha`/`release`. Setting `force` to `true` will bypass the version check and force the update.
1 parent 578e659 commit 0003530

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

component/updater/update_core.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ const (
3333
MaxPackageFileSize = 32 * 1024 * 1024
3434
)
3535

36+
const (
37+
ReleaseChannel = "release"
38+
AlphaChannel = "alpha"
39+
)
40+
3641
// CoreUpdater is the mihomo updater.
3742
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/updater/updater.go
3843
type CoreUpdater struct {
@@ -69,7 +74,7 @@ func (u *CoreUpdater) CoreBaseName() string {
6974
}
7075
}
7176

72-
func (u *CoreUpdater) Update(currentExePath string) (err error) {
77+
func (u *CoreUpdater) Update(currentExePath string, channel string, force bool) (err error) {
7378
u.mu.Lock()
7479
defer u.mu.Unlock()
7580

@@ -80,9 +85,17 @@ func (u *CoreUpdater) Update(currentExePath string) (err error) {
8085

8186
baseURL := baseAlphaURL
8287
versionURL := versionAlphaURL
83-
if !strings.HasPrefix(C.Version, "alpha") {
88+
switch strings.ToLower(channel) {
89+
case ReleaseChannel:
8490
baseURL = baseReleaseURL
8591
versionURL = versionReleaseURL
92+
case AlphaChannel:
93+
break
94+
default: // auto
95+
if !strings.HasPrefix(C.Version, "alpha") {
96+
baseURL = baseReleaseURL
97+
versionURL = versionReleaseURL
98+
}
8699
}
87100

88101
latestVersion, err := u.getLatestVersion(versionURL)
@@ -91,7 +104,7 @@ func (u *CoreUpdater) Update(currentExePath string) (err error) {
91104
}
92105
log.Infoln("current version %s, latest version %s", C.Version, latestVersion)
93106

94-
if latestVersion == C.Version {
107+
if latestVersion == C.Version && !force {
95108
// don't change this output, some downstream dependencies on the upgrader's output fields
96109
return fmt.Errorf("update error: already using latest version %s", C.Version)
97110
}

hub/route/upgrade.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func upgradeCore(w http.ResponseWriter, r *http.Request) {
3232
return
3333
}
3434

35-
err = updater.DefaultCoreUpdater.Update(execPath)
35+
query := r.URL.Query()
36+
channel := query.Get("channel")
37+
force := query.Get("force") == "true"
38+
39+
err = updater.DefaultCoreUpdater.Update(execPath, channel, force)
3640
if err != nil {
3741
log.Warnln("%s", err)
3842
render.Status(r, http.StatusInternalServerError)

0 commit comments

Comments
 (0)