Skip to content

Commit

Permalink
chore: use inner for upgrade core
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Mar 29, 2023
1 parent 1fdd1f7 commit d730fee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
6 changes: 3 additions & 3 deletions constant/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *path) MMDB() string {
// 目录则直接跳过
continue
} else {
if strings.EqualFold(strings.ToLower(fi.Name()), "country.mmdb") {
if strings.EqualFold(fi.Name(), "Country.mmdb") {
GeoipName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
Expand All @@ -93,7 +93,7 @@ func (p *path) GeoIP() string {
// 目录则直接跳过
continue
} else {
if strings.EqualFold(strings.ToLower(fi.Name()), "geoip.dat") {
if strings.EqualFold(fi.Name(), "GeoIP.dat") {
GeoipName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
Expand All @@ -112,7 +112,7 @@ func (p *path) GeoSite() string {
// 目录则直接跳过
continue
} else {
if strings.EqualFold(strings.ToLower(fi.Name()), "geosite.dat") {
if strings.EqualFold(fi.Name(), "GeoSite.dat") {
GeositeName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
Expand Down
55 changes: 27 additions & 28 deletions hub/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package updater
import (
"archive/zip"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
Expand All @@ -11,29 +12,26 @@ import (
"runtime"
"strings"
"sync"
"time"

clashHttp "github.com/Dreamacro/clash/component/http"
"github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
)

// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/updater/updater.go
// Updater is the Clash.Meta updater.
var (
client http.Client

goarch string
goos string
goarm string
gomips string

workDir string
versionCheckURL string
workDir string

// mu protects all fields below.
mu sync.RWMutex

// TODO(a.garipov): See if all of these fields actually have to be in
// this struct.
currentExeName string // 当前可执行文件
updateDir string // 更新目录
packageName string // 更新压缩文件
Expand All @@ -42,8 +40,8 @@ var (
updateExeName string // 更新后的可执行文件
unpackedFile string

baseURL string = "https://ghproxy.com/https://github.com/MetaCubeX/Clash.Meta/releases/download/Prerelease-Alpha/clash.meta"
versionURL string = "https://github.com/MetaCubeX/Clash.Meta/releases/download/Prerelease-Alpha/version.txt"
baseURL string = "https://testingcf.jsdelivr.net/gh/MetaCubeX/Clash.Meta@release/clash.meta"
versionURL string = "https://raw.githubusercontent.com/MetaCubeX/Clash.Meta/release/version.txt"
packageURL string
latestVersion string
)
Expand All @@ -61,7 +59,13 @@ func (e *updateError) Error() string {
func Update() (err error) {
goos = runtime.GOOS
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 {
err := &updateError{Message: "Already using latest version"}
Expand All @@ -72,8 +76,6 @@ func Update() (err error) {
mu.Lock()
defer mu.Unlock()

log.Infoln("current version alpha-%s", constant.Version)

defer func() {
if err != nil {
log.Errorln("updater: failed: %v", err)
Expand Down Expand Up @@ -115,14 +117,6 @@ func Update() (err error) {
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.
func prepare(exePath string) (err error) {
updateDir = filepath.Join(workDir, "meta-update")
Expand Down Expand Up @@ -226,8 +220,11 @@ const MaxPackageFileSize = 32 * 1024 * 1024

// Download package file and save it to disk
func downloadPackageFile() (err error) {
var resp *http.Response
resp, err = client.Get(packageURL)
// var resp *http.Response
// 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 {
return fmt.Errorf("http request failed: %w", err)
}
Expand Down Expand Up @@ -255,11 +252,11 @@ func downloadPackageFile() (err error) {
log.Debugln("updateDir %s", updateDir)
err = os.Mkdir(updateDir, 0o755)
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)
err = os.WriteFile(packageName, body, 0o755)
err = os.WriteFile(packageName, body, 0o644)
if err != nil {
return fmt.Errorf("os.WriteFile() failed: %w", err)
}
Expand Down Expand Up @@ -405,10 +402,12 @@ func copyFile(src, dst string) error {
return nil
}

func getLatestVersion() string {
resp, err := http.Get(versionURL)
func getLatestVersion() (version string, err error) {
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 {
return ""
return "", fmt.Errorf("get Latest Version fail: %w", err)
}
defer func() {
closeErr := resp.Body.Close()
Expand All @@ -419,11 +418,11 @@ func getLatestVersion() string {

body, err := io.ReadAll(resp.Body)
if err != nil {
return ""
return "", fmt.Errorf("get Latest Version fail: %w", err)
}
content := strings.TrimRight(string(body), "\n")
log.Infoln("latest:%s", content)
return content
return content, nil
}

func updateDownloadURL() {
Expand Down

0 comments on commit d730fee

Please sign in to comment.