diff --git a/internal/upgrader/binary.go b/internal/upgrader/binary.go index 96042608..0cdeeffc 100644 --- a/internal/upgrader/binary.go +++ b/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, diff --git a/internal/upgrader/upgrade.go b/internal/upgrader/upgrade.go index 19bb73e9..80f3829f 100644 --- a/internal/upgrader/upgrade.go +++ b/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) diff --git a/internal/version/dev_build.go b/internal/version/dev_build.go index a32bc636..f3ffdef3 100644 --- a/internal/version/dev_build.go +++ b/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 } diff --git a/internal/version/release.go b/internal/version/release.go index 3acd55d1..ad9b667d 100644 --- a/internal/version/release.go +++ b/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 diff --git a/internal/version/url.go b/internal/version/url.go new file mode 100644 index 00000000..ba15c54b --- /dev/null +++ b/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 +}