chore: use inner for upgrade core

This commit is contained in:
Larvan2 2023-03-29 06:03:13 +00:00
parent 1fdd1f702e
commit d730feecb4
2 changed files with 30 additions and 31 deletions

View file

@ -66,7 +66,7 @@ func (p *path) MMDB() string {
// 目录则直接跳过 // 目录则直接跳过
continue continue
} else { } else {
if strings.EqualFold(strings.ToLower(fi.Name()), "country.mmdb") { if strings.EqualFold(fi.Name(), "Country.mmdb") {
GeoipName = fi.Name() GeoipName = fi.Name()
return P.Join(p.homeDir, fi.Name()) return P.Join(p.homeDir, fi.Name())
} }
@ -93,7 +93,7 @@ func (p *path) GeoIP() string {
// 目录则直接跳过 // 目录则直接跳过
continue continue
} else { } else {
if strings.EqualFold(strings.ToLower(fi.Name()), "geoip.dat") { if strings.EqualFold(fi.Name(), "GeoIP.dat") {
GeoipName = fi.Name() GeoipName = fi.Name()
return P.Join(p.homeDir, fi.Name()) return P.Join(p.homeDir, fi.Name())
} }
@ -112,7 +112,7 @@ func (p *path) GeoSite() string {
// 目录则直接跳过 // 目录则直接跳过
continue continue
} else { } else {
if strings.EqualFold(strings.ToLower(fi.Name()), "geosite.dat") { if strings.EqualFold(fi.Name(), "GeoSite.dat") {
GeositeName = fi.Name() GeositeName = fi.Name()
return P.Join(p.homeDir, fi.Name()) return P.Join(p.homeDir, fi.Name())
} }

View file

@ -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"
) )
@ -19,21 +22,16 @@ import (
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/updater/updater.go // modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/updater/updater.go
// Updater is the Clash.Meta updater. // Updater is the Clash.Meta updater.
var ( var (
client http.Client
goarch string goarch string
goos string goos string
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
// TODO(a.garipov): See if all of these fields actually have to be in
// this struct.
currentExeName string // 当前可执行文件 currentExeName string // 当前可执行文件
updateDir string // 更新目录 updateDir string // 更新目录
packageName string // 更新压缩文件 packageName string // 更新压缩文件
@ -42,8 +40,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 +59,13 @@ 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
}
log.Infoln("current version alpha-%s, latest version alpha-%s", constant.Version, latestVersion)
if latestVersion == constant.Version { if latestVersion == constant.Version {
err := &updateError{Message: "Already using latest version"} err := &updateError{Message: "Already using latest version"}
@ -72,8 +76,6 @@ func Update() (err error) {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
log.Infoln("current version alpha-%s", constant.Version)
defer func() { defer func() {
if err != nil { if err != nil {
log.Errorln("updater: failed: %v", err) log.Errorln("updater: failed: %v", err)
@ -115,14 +117,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 +220,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)
} }
@ -255,11 +252,11 @@ func downloadPackageFile() (err error) {
log.Debugln("updateDir %s", updateDir) log.Debugln("updateDir %s", updateDir)
err = os.Mkdir(updateDir, 0o755) err = os.Mkdir(updateDir, 0o755)
if err != nil { if err != nil {
fmt.Errorf("mkdir error: %w", err) return fmt.Errorf("mkdir error: %w", err)
} }
log.Debugln("updater: saving package to file %s", packageName) log.Debugln("updater: saving package to file %s", packageName)
err = os.WriteFile(packageName, body, 0o755) err = os.WriteFile(packageName, body, 0o644)
if err != nil { if err != nil {
return fmt.Errorf("os.WriteFile() failed: %w", err) return fmt.Errorf("os.WriteFile() failed: %w", err)
} }
@ -405,10 +402,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 +418,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() {