Browse Source

fix(upgrader): use cloud.nginxui.com as proxy by default

Jacky 1 month ago
parent
commit
5a34d5ca9a

+ 0 - 1
internal/upgrader/binary.go

@@ -22,7 +22,6 @@ func BinaryUpgrade(ws *websocket.Conn, control *Control) {
 	})
 
 	u, err := NewUpgrader(control.Channel)
-
 	if err != nil {
 		_ = ws.WriteJSON(CoreUpgradeResp{
 			Status:  UpgradeStatusError,

+ 6 - 13
internal/upgrader/upgrade.go

@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"io"
 	"net/http"
-	"net/url"
 	"os"
 	"path/filepath"
 	"strconv"
@@ -36,6 +35,7 @@ type CoreUpgradeResp struct {
 }
 
 type Upgrader struct {
+	Channel string
 	Release version.TRelease
 	version.RuntimeInfo
 }
@@ -50,6 +50,7 @@ func NewUpgrader(channel string) (u *Upgrader, err error) {
 		return
 	}
 	u = &Upgrader{
+		Channel:     channel,
 		Release:     data,
 		RuntimeInfo: runtimeInfo,
 	}
@@ -153,12 +154,8 @@ func (u *Upgrader) DownloadLatestRelease(progressChan chan float64) (tarName str
 	}
 
 	githubProxy := settings.HTTPSettings.GithubProxy
-	if githubProxy != "" {
-		digest.BrowserDownloadUrl, err = url.JoinPath(githubProxy, digest.BrowserDownloadUrl)
-		if err != nil {
-			err = errors.Wrap(err, "service.DownloadLatestRelease url.JoinPath error")
-			return
-		}
+	if githubProxy != "" && u.Channel != string(version.ReleaseTypeDev) {
+		digest.BrowserDownloadUrl = version.GetUrl(digest.BrowserDownloadUrl)
 	}
 
 	resp, err := http.Get(digest.BrowserDownloadUrl)
@@ -171,12 +168,8 @@ func (u *Upgrader) DownloadLatestRelease(progressChan chan float64) (tarName str
 
 	dir := filepath.Dir(u.ExPath)
 
-	if githubProxy != "" {
-		downloadUrl, err = url.JoinPath(githubProxy, downloadUrl)
-		if err != nil {
-			err = errors.Wrap(err, "service.DownloadLatestRelease url.JoinPath error")
-			return
-		}
+	if githubProxy != "" && u.Channel != string(version.ReleaseTypeDev) {
+		downloadUrl = version.GetUrl(downloadUrl)
 	}
 
 	tarName, err = downloadRelease(downloadUrl, dir, progressChan)

+ 2 - 7
internal/version/dev_build.go

@@ -10,11 +10,6 @@ import (
 	"github.com/pkg/errors"
 )
 
-const (
-	GithubDevCommitAPI  = "https://cloud.nginxui.com/https://api.github.com/repos/0xJacky/nginx-ui/commits/dev?per_page=1"
-	CloudflareWorkerAPI = "https://cloud.nginxui.com"
-)
-
 type TCommit struct {
 	SHA    string `json:"sha"`
 	Commit struct {
@@ -26,7 +21,7 @@ type TCommit struct {
 }
 
 func getDevBuild() (data TRelease, err error) {
-	resp, err := http.Get(GithubDevCommitAPI)
+	resp, err := http.Get(GetGithubDevCommitAPIUrl())
 	if err != nil {
 		return
 	}
@@ -47,7 +42,7 @@ func getDevBuild() (data TRelease, err error) {
 	}
 	shortSHA := commit.SHA[:7]
 
-	resp, err = http.Get(fmt.Sprintf("%s/dev-builds", CloudflareWorkerAPI))
+	resp, err = http.Get(fmt.Sprintf("%sdev-builds", CloudflareWorkerAPI))
 	if err != nil {
 		return
 	}

+ 2 - 9
internal/version/release.go

@@ -9,11 +9,6 @@ import (
 	"github.com/pkg/errors"
 )
 
-const (
-	GithubLatestReleaseAPI = "https://cloud.nginxui.com/https://api.github.com/repos/0xJacky/nginx-ui/releases/latest"
-	GithubReleasesListAPI  = "https://cloud.nginxui.com/https://api.github.com/repos/0xJacky/nginx-ui/releases"
-)
-
 type ReleaseType string
 
 const (
@@ -47,10 +42,8 @@ func (t *TRelease) GetAssetsMap() (m map[string]TReleaseAsset) {
 }
 
 func getLatestRelease() (data TRelease, err error) {
-	resp, err := http.Get(GithubLatestReleaseAPI)
+	resp, err := http.Get(GetGithubLatestReleaseAPIUrl())
 	if err != nil {
-		err = errors.Wrap(err, "service.getLatestRelease http.Get err")
-		return
 	}
 	body, err := io.ReadAll(resp.Body)
 	if err != nil {
@@ -72,7 +65,7 @@ func getLatestRelease() (data TRelease, err error) {
 }
 
 func getLatestPrerelease() (data TRelease, err error) {
-	resp, err := http.Get(GithubReleasesListAPI)
+	resp, err := http.Get(GetGithubReleasesListAPIUrl())
 	if err != nil {
 		err = errors.Wrap(err, "service.getLatestPrerelease http.Get err")
 		return

+ 40 - 0
internal/version/url.go

@@ -0,0 +1,40 @@
+package version
+
+import (
+	"strings"
+
+	"github.com/0xJacky/Nginx-UI/settings"
+)
+
+const (
+	GithubDevCommitAPI     = "https://api.github.com/repos/0xJacky/nginx-ui/commits/dev?per_page=1"
+	CloudflareWorkerAPI    = "https://cloud.nginxui.com/"
+	GithubLatestReleaseAPI = "https://api.github.com/repos/0xJacky/nginx-ui/releases/latest"
+	GithubReleasesListAPI  = "https://api.github.com/repos/0xJacky/nginx-ui/releases"
+)
+
+func GetGithubDevCommitAPIUrl() string {
+	return GetUrl(GithubDevCommitAPI)
+}
+
+func GetGithubLatestReleaseAPIUrl() string {
+	return GetUrl(GithubLatestReleaseAPI)
+}
+
+func GetGithubReleasesListAPIUrl() string {
+	return GetUrl(GithubReleasesListAPI)
+}
+
+func GetCloudflareWorkerAPIUrl() string {
+	return GetUrl(CloudflareWorkerAPI)
+}
+
+func GetUrl(path string) string {
+	githubProxy := settings.HTTPSettings.GithubProxy
+	if githubProxy == "" {
+		githubProxy = CloudflareWorkerAPI
+	}
+	githubProxy = strings.TrimSuffix(githubProxy, "/")
+
+	return githubProxy + "/" + path
+}