feat(pluginhost, api): add support for "X-CPA-SUPPORT-PLUGIN" header with CGO detection

- Introduced `SupportPluginHeaderValue` to indicate CGO build status (`1` for enabled, `0` for disabled).
- Updated API response headers in `handler.go` to include "X-CPA-SUPPORT-PLUGIN".
- Added unit tests to verify proper header behavior under varying conditions.
This commit is contained in:
Luis Pater
2026-06-12 23:54:26 +08:00
parent 69b7462860
commit 049ced5c3f
5 changed files with 68 additions and 0 deletions
@@ -171,6 +171,7 @@ func (h *Handler) Middleware() gin.HandlerFunc {
c.Header("X-CPA-VERSION", buildinfo.Version)
c.Header("X-CPA-COMMIT", buildinfo.Commit)
c.Header("X-CPA-BUILD-DATE", buildinfo.BuildDate)
c.Header("X-CPA-SUPPORT-PLUGIN", pluginhost.SupportPluginHeaderValue())
clientIP := c.ClientIP()
localClient := clientIP == "127.0.0.1" || clientIP == "::1"
@@ -2,10 +2,13 @@ package management
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
"github.com/router-for-me/CLIProxyAPI/v7/internal/pluginhost"
)
func TestAuthenticateManagementKey_LocalhostIPBan_BlocksCorrectKeyDuringBan(t *testing.T) {
@@ -36,3 +39,51 @@ func TestAuthenticateManagementKey_LocalhostIPBan_BlocksCorrectKeyDuringBan(t *t
t.Fatalf("unexpected banned message: %q", errMsg)
}
}
func TestMiddlewareSetsSupportPluginHeader(t *testing.T) {
gin.SetMode(gin.TestMode)
h := &Handler{
cfg: &config.Config{},
failedAttempts: make(map[string]*attemptInfo),
envSecret: "test-secret",
}
middleware := h.Middleware()
t.Run("invalid key", func(t *testing.T) {
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodGet, "/v0/management/config", nil)
c.Request.RemoteAddr = "127.0.0.1:12345"
c.Request.Header.Set("X-Management-Key", "wrong-secret")
middleware(c)
if rec.Code != http.StatusUnauthorized {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusUnauthorized)
}
if got := rec.Header().Get("X-CPA-SUPPORT-PLUGIN"); got != pluginhost.SupportPluginHeaderValue() {
t.Fatalf("X-CPA-SUPPORT-PLUGIN = %q, want %q", got, pluginhost.SupportPluginHeaderValue())
}
})
t.Run("valid key", func(t *testing.T) {
engine := gin.New()
engine.GET("/v0/management/config", middleware, func(c *gin.Context) {
c.Status(http.StatusOK)
})
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/v0/management/config", nil)
req.RemoteAddr = "127.0.0.1:12345"
req.Header.Set("X-Management-Key", "test-secret")
engine.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
if got := rec.Header().Get("X-CPA-SUPPORT-PLUGIN"); got != pluginhost.SupportPluginHeaderValue() {
t.Fatalf("X-CPA-SUPPORT-PLUGIN = %q, want %q", got, pluginhost.SupportPluginHeaderValue())
}
})
}
+6
View File
@@ -0,0 +1,6 @@
package pluginhost
// SupportPluginHeaderValue reports whether the current binary was built with CGO enabled.
func SupportPluginHeaderValue() string {
return supportPluginValue
}
+5
View File
@@ -0,0 +1,5 @@
//go:build cgo
package pluginhost
const supportPluginValue = "1"
+5
View File
@@ -0,0 +1,5 @@
//go:build !cgo
package pluginhost
const supportPluginValue = "0"