From 361443db10d64d5ff80c0aebe3e3352a3d362fa3 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Fri, 5 Dec 2025 10:29:12 +0800 Subject: [PATCH] **feat(api): add GetLatestVersion endpoint to fetch latest release version from GitHub** --- .../api/handlers/management/config_basic.go | 72 +++++++++++++++++++ internal/api/server.go | 1 + 2 files changed, 73 insertions(+) diff --git a/internal/api/handlers/management/config_basic.go b/internal/api/handlers/management/config_basic.go index 5843c5b8..ae292982 100644 --- a/internal/api/handlers/management/config_basic.go +++ b/internal/api/handlers/management/config_basic.go @@ -1,16 +1,28 @@ package management import ( + "encoding/json" + "fmt" "io" "net/http" "os" "path/filepath" + "strings" + "time" "github.com/gin-gonic/gin" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" + log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" ) +const ( + latestReleaseURL = "https://api.github.com/repos/router-for-me/CLIProxyAPI/releases/latest" + latestReleaseUserAgent = "CLIProxyAPI" +) + func (h *Handler) GetConfig(c *gin.Context) { if h == nil || h.cfg == nil { c.JSON(200, gin.H{}) @@ -20,6 +32,66 @@ func (h *Handler) GetConfig(c *gin.Context) { c.JSON(200, &cfgCopy) } +type releaseInfo struct { + TagName string `json:"tag_name"` + Name string `json:"name"` +} + +// GetLatestVersion returns the latest release version from GitHub without downloading assets. +func (h *Handler) GetLatestVersion(c *gin.Context) { + client := &http.Client{Timeout: 10 * time.Second} + proxyURL := "" + if h != nil && h.cfg != nil { + proxyURL = strings.TrimSpace(h.cfg.ProxyURL) + } + if proxyURL != "" { + sdkCfg := &sdkconfig.SDKConfig{ProxyURL: proxyURL} + util.SetProxy(sdkCfg, client) + } + + req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodGet, latestReleaseURL, nil) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "request_create_failed", "message": err.Error()}) + return + } + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("User-Agent", latestReleaseUserAgent) + + resp, err := client.Do(req) + if err != nil { + c.JSON(http.StatusBadGateway, gin.H{"error": "request_failed", "message": err.Error()}) + return + } + defer func() { + if errClose := resp.Body.Close(); errClose != nil { + log.WithError(errClose).Debug("failed to close latest version response body") + } + }() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) + c.JSON(http.StatusBadGateway, gin.H{"error": "unexpected_status", "message": fmt.Sprintf("status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))}) + return + } + + var info releaseInfo + if errDecode := json.NewDecoder(resp.Body).Decode(&info); errDecode != nil { + c.JSON(http.StatusBadGateway, gin.H{"error": "decode_failed", "message": errDecode.Error()}) + return + } + + version := strings.TrimSpace(info.TagName) + if version == "" { + version = strings.TrimSpace(info.Name) + } + if version == "" { + c.JSON(http.StatusBadGateway, gin.H{"error": "invalid_response", "message": "missing release version"}) + return + } + + c.JSON(http.StatusOK, gin.H{"latest-version": version}) +} + func WriteConfig(path string, data []byte) error { data = config.NormalizeCommentIndentation(data) f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) diff --git a/internal/api/server.go b/internal/api/server.go index c4d6ad8f..9e1c5848 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -472,6 +472,7 @@ func (s *Server) registerManagementRoutes() { mgmt.GET("/config", s.mgmt.GetConfig) mgmt.GET("/config.yaml", s.mgmt.GetConfigYAML) mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML) + mgmt.GET("/latest-version", s.mgmt.GetLatestVersion) mgmt.GET("/debug", s.mgmt.GetDebug) mgmt.PUT("/debug", s.mgmt.PutDebug)