mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-01-07 09:53:58 +08:00
chore: use inner for upgrade core
This commit is contained in:
parent
1fdd1f702e
commit
56e525114d
@ -3,6 +3,7 @@ package updater
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -11,7 +12,9 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
||||||
"github.com/Dreamacro/clash/constant"
|
"github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
)
|
)
|
||||||
@ -26,8 +29,7 @@ var (
|
|||||||
goarm string
|
goarm string
|
||||||
gomips string
|
gomips string
|
||||||
|
|
||||||
workDir string
|
workDir string
|
||||||
versionCheckURL string
|
|
||||||
|
|
||||||
// mu protects all fields below.
|
// mu protects all fields below.
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
@ -42,8 +44,8 @@ var (
|
|||||||
updateExeName string // 更新后的可执行文件
|
updateExeName string // 更新后的可执行文件
|
||||||
unpackedFile string
|
unpackedFile string
|
||||||
|
|
||||||
baseURL string = "https://ghproxy.com/https://github.com/MetaCubeX/Clash.Meta/releases/download/Prerelease-Alpha/clash.meta"
|
baseURL string = "https://testingcf.jsdelivr.net/gh/MetaCubeX/Clash.Meta@release/clash.meta"
|
||||||
versionURL string = "https://github.com/MetaCubeX/Clash.Meta/releases/download/Prerelease-Alpha/version.txt"
|
versionURL string = "https://raw.githubusercontent.com/MetaCubeX/Clash.Meta/release/version.txt"
|
||||||
packageURL string
|
packageURL string
|
||||||
latestVersion string
|
latestVersion string
|
||||||
)
|
)
|
||||||
@ -61,7 +63,11 @@ func (e *updateError) Error() string {
|
|||||||
func Update() (err error) {
|
func Update() (err error) {
|
||||||
goos = runtime.GOOS
|
goos = runtime.GOOS
|
||||||
goarch = runtime.GOARCH
|
goarch = runtime.GOARCH
|
||||||
latestVersion = getLatestVersion()
|
latestVersion, err = getLatestVersion()
|
||||||
|
if err != nil {
|
||||||
|
err := &updateError{Message: err.Error()}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if latestVersion == constant.Version {
|
if latestVersion == constant.Version {
|
||||||
err := &updateError{Message: "Already using latest version"}
|
err := &updateError{Message: "Already using latest version"}
|
||||||
@ -115,14 +121,6 @@ func Update() (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// VersionCheckURL returns the version check URL.
|
|
||||||
func VersionCheckURL() (vcu string) {
|
|
||||||
mu.RLock()
|
|
||||||
defer mu.RUnlock()
|
|
||||||
|
|
||||||
return versionCheckURL
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepare fills all necessary fields in Updater object.
|
// prepare fills all necessary fields in Updater object.
|
||||||
func prepare(exePath string) (err error) {
|
func prepare(exePath string) (err error) {
|
||||||
updateDir = filepath.Join(workDir, "meta-update")
|
updateDir = filepath.Join(workDir, "meta-update")
|
||||||
@ -226,8 +224,11 @@ const MaxPackageFileSize = 32 * 1024 * 1024
|
|||||||
|
|
||||||
// Download package file and save it to disk
|
// Download package file and save it to disk
|
||||||
func downloadPackageFile() (err error) {
|
func downloadPackageFile() (err error) {
|
||||||
var resp *http.Response
|
// var resp *http.Response
|
||||||
resp, err = client.Get(packageURL)
|
// resp, err = client.Get(packageURL)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
||||||
|
defer cancel()
|
||||||
|
resp, err := clashHttp.HttpRequest(ctx, packageURL, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("http request failed: %w", err)
|
return fmt.Errorf("http request failed: %w", err)
|
||||||
}
|
}
|
||||||
@ -405,10 +406,12 @@ func copyFile(src, dst string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLatestVersion() string {
|
func getLatestVersion() (version string, err error) {
|
||||||
resp, err := http.Get(versionURL)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
|
defer cancel()
|
||||||
|
resp, err := clashHttp.HttpRequest(ctx, versionURL, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return "", fmt.Errorf("get Latest Version fail: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
closeErr := resp.Body.Close()
|
closeErr := resp.Body.Close()
|
||||||
@ -419,11 +422,11 @@ func getLatestVersion() string {
|
|||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return "", fmt.Errorf("get Latest Version fail: %w", err)
|
||||||
}
|
}
|
||||||
content := strings.TrimRight(string(body), "\n")
|
content := strings.TrimRight(string(body), "\n")
|
||||||
log.Infoln("latest:%s", content)
|
log.Infoln("latest:%s", content)
|
||||||
return content
|
return content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateDownloadURL() {
|
func updateDownloadURL() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user