mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-20 12:23:26 +00:00
Enables configured API keys to authenticate management API requests from localhost, making it easier to use tools like curl for local testing.
430 lines
12 KiB
Go
430 lines
12 KiB
Go
// Package management provides the management API handlers and middleware
|
|
// for configuring the server and managing auth files.
|
|
package management
|
|
|
|
import (
|
|
"crypto/subtle"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/integrations"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
|
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
|
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type attemptInfo struct {
|
|
count int
|
|
blockedUntil time.Time
|
|
}
|
|
|
|
// Handler aggregates config reference, persistence path and helpers.
|
|
type Handler struct {
|
|
cfg *config.Config
|
|
configFilePath string
|
|
mu sync.Mutex
|
|
attemptsMu sync.Mutex
|
|
failedAttempts map[string]*attemptInfo // keyed by client IP
|
|
authManager *coreauth.Manager
|
|
usageStats *usage.RequestStatistics
|
|
tokenStore coreauth.Store
|
|
localPassword string
|
|
allowRemoteOverride bool
|
|
envSecret string
|
|
logDir string
|
|
integrationManager *integrations.Manager
|
|
}
|
|
|
|
// openAICompatProviderNames returns the configured OpenAI-compat provider names from the current config.
|
|
func (h *Handler) openAICompatProviderNames() []string {
|
|
cfg := h.cfg
|
|
if cfg == nil {
|
|
return nil
|
|
}
|
|
out := make([]string, 0, len(cfg.OpenAICompatibility))
|
|
for _, c := range cfg.OpenAICompatibility {
|
|
name := strings.TrimSpace(c.Name)
|
|
if name != "" {
|
|
out = append(out, name)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// NewHandler creates a new management handler instance.
|
|
func NewHandler(cfg *config.Config, configFilePath string, manager *coreauth.Manager) *Handler {
|
|
envSecret, _ := os.LookupEnv("MANAGEMENT_PASSWORD")
|
|
envSecret = strings.TrimSpace(envSecret)
|
|
|
|
h := &Handler{
|
|
cfg: cfg,
|
|
configFilePath: configFilePath,
|
|
failedAttempts: make(map[string]*attemptInfo),
|
|
authManager: manager,
|
|
usageStats: usage.GetRequestStatistics(),
|
|
tokenStore: sdkAuth.GetTokenStore(),
|
|
allowRemoteOverride: envSecret != "",
|
|
envSecret: envSecret,
|
|
}
|
|
|
|
// Initialize Integration Manager
|
|
// We need the proxy URL. If not in config, assume localhost default.
|
|
proxyURL := cfg.ProxyURL
|
|
if proxyURL == "" {
|
|
host := cfg.Host
|
|
if host == "" {
|
|
host = "http://127.0.0.1"
|
|
} else {
|
|
if !strings.HasPrefix(host, "http") {
|
|
host = "http://" + host
|
|
}
|
|
}
|
|
proxyURL = fmt.Sprintf("%s:%d", host, cfg.Port)
|
|
}
|
|
|
|
h.integrationManager = integrations.NewManager(proxyURL)
|
|
h.integrationManager.Register(&integrations.CodexIntegration{})
|
|
h.integrationManager.Register(&integrations.ContinueIntegration{})
|
|
h.integrationManager.Register(&integrations.ClaudeIntegration{})
|
|
h.integrationManager.Register(&integrations.DroidIntegration{})
|
|
h.integrationManager.Register(&integrations.GeminiIntegration{})
|
|
|
|
return h
|
|
}
|
|
|
|
// SetConfig updates the in-memory config reference when the server hot-reloads.
|
|
func (h *Handler) SetConfig(cfg *config.Config) { h.cfg = cfg }
|
|
|
|
// SetAuthManager updates the auth manager reference used by management endpoints.
|
|
func (h *Handler) SetAuthManager(manager *coreauth.Manager) { h.authManager = manager }
|
|
|
|
// routingPreview describes how a model would be normalised and routed according to current config.
|
|
type routingPreview struct {
|
|
RequestedModel string `json:"requested_model"`
|
|
ResolvedModel string `json:"resolved_model"`
|
|
NormalizedModel string `json:"normalized_model"`
|
|
ProviderCandidates []string `json:"provider_candidates"`
|
|
PreferredProvider string `json:"preferred_provider"`
|
|
Dynamic map[string]any `json:"dynamic"`
|
|
Metadata map[string]any `json:"metadata"`
|
|
}
|
|
|
|
// computeRoutingPreview replicates BaseAPIHandler.getRequestDetails for management/debug purposes.
|
|
func (h *Handler) computeRoutingPreview(modelName string) (*routingPreview, error) {
|
|
if strings.TrimSpace(modelName) == "" {
|
|
return nil, fmt.Errorf("model name is empty")
|
|
}
|
|
|
|
requested := strings.TrimSpace(modelName)
|
|
|
|
// Droid CLI sometimes sends a "custom:*" model id; normalize it before routing.
|
|
modelName = util.NormalizeDroidCustomModel(modelName)
|
|
|
|
// Resolve "auto" model to an actual available model first
|
|
resolvedModelName := util.ResolveAutoModel(modelName)
|
|
|
|
providerName, extractedModelName, isDynamic := parseDynamicModelWithConfig(resolvedModelName, h.openAICompatProviderNames())
|
|
|
|
targetModelName := resolvedModelName
|
|
if isDynamic {
|
|
targetModelName = extractedModelName
|
|
}
|
|
|
|
// Normalize the model name to handle dynamic thinking suffixes before determining the provider.
|
|
normalizedModel, metadata := util.NormalizeThinkingModel(targetModelName)
|
|
|
|
var providers []string
|
|
if isDynamic {
|
|
providers = []string{providerName}
|
|
} else {
|
|
// For non-dynamic models, use the normalizedModel to get the provider name.
|
|
providers = util.GetProviderName(normalizedModel)
|
|
if len(providers) == 0 && metadata != nil {
|
|
if originalRaw, ok := metadata[util.ThinkingOriginalModelMetadataKey]; ok {
|
|
if originalModel, okStr := originalRaw.(string); okStr {
|
|
originalModel = strings.TrimSpace(originalModel)
|
|
if originalModel != "" && !strings.EqualFold(originalModel, normalizedModel) {
|
|
if altProviders := util.GetProviderName(originalModel); len(altProviders) > 0 {
|
|
providers = altProviders
|
|
normalizedModel = originalModel
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(providers) == 0 {
|
|
return nil, fmt.Errorf("unknown provider for model %s", requested)
|
|
}
|
|
|
|
preferred := ""
|
|
if len(providers) > 0 {
|
|
preferred = providers[0]
|
|
}
|
|
|
|
preview := &routingPreview{
|
|
RequestedModel: requested,
|
|
ResolvedModel: resolvedModelName,
|
|
NormalizedModel: normalizedModel,
|
|
ProviderCandidates: providers,
|
|
PreferredProvider: preferred,
|
|
Dynamic: map[string]any{
|
|
"is_dynamic": isDynamic,
|
|
"dynamic_provider": providerName,
|
|
"dynamic_model": extractedModelName,
|
|
},
|
|
Metadata: metadata,
|
|
}
|
|
return preview, nil
|
|
}
|
|
|
|
// parseDynamicModelWithConfig mirrors BaseAPIHandler.parseDynamicModel but uses the config provider list.
|
|
func parseDynamicModelWithConfig(modelName string, openAICompatProviders []string) (providerName, model string, isDynamic bool) {
|
|
var providerPart, modelPart string
|
|
for _, sep := range []string{"://"} {
|
|
if parts := strings.SplitN(modelName, sep, 2); len(parts) == 2 {
|
|
providerPart = parts[0]
|
|
modelPart = parts[1]
|
|
break
|
|
}
|
|
}
|
|
|
|
if providerPart == "" {
|
|
return "", modelName, false
|
|
}
|
|
|
|
for _, pName := range openAICompatProviders {
|
|
if pName == providerPart {
|
|
return providerPart, modelPart, true
|
|
}
|
|
}
|
|
|
|
return "", modelName, false
|
|
}
|
|
|
|
// SetUsageStatistics allows replacing the usage statistics reference.
|
|
func (h *Handler) SetUsageStatistics(stats *usage.RequestStatistics) { h.usageStats = stats }
|
|
|
|
// SetLocalPassword configures the runtime-local password accepted for localhost requests.
|
|
func (h *Handler) SetLocalPassword(password string) { h.localPassword = password }
|
|
|
|
// SetLogDirectory updates the directory where main.log should be looked up.
|
|
func (h *Handler) SetLogDirectory(dir string) {
|
|
if dir == "" {
|
|
return
|
|
}
|
|
if !filepath.IsAbs(dir) {
|
|
if abs, err := filepath.Abs(dir); err == nil {
|
|
dir = abs
|
|
}
|
|
}
|
|
h.logDir = dir
|
|
}
|
|
|
|
// Middleware enforces access control for management endpoints.
|
|
// All requests (local and remote) require a valid management key.
|
|
// Additionally, remote access requires allow-remote-management=true.
|
|
func (h *Handler) Middleware() gin.HandlerFunc {
|
|
const maxFailures = 5
|
|
const banDuration = 30 * time.Minute
|
|
|
|
return func(c *gin.Context) {
|
|
c.Header("X-CPA-VERSION", buildinfo.Version)
|
|
c.Header("X-CPA-COMMIT", buildinfo.Commit)
|
|
c.Header("X-CPA-BUILD-DATE", buildinfo.BuildDate)
|
|
|
|
clientIP := c.ClientIP()
|
|
localClient := clientIP == "127.0.0.1" || clientIP == "::1"
|
|
cfg := h.cfg
|
|
var (
|
|
allowRemote bool
|
|
secretHash string
|
|
)
|
|
if cfg != nil {
|
|
allowRemote = cfg.RemoteManagement.AllowRemote
|
|
secretHash = cfg.RemoteManagement.SecretKey
|
|
}
|
|
if h.allowRemoteOverride {
|
|
allowRemote = true
|
|
}
|
|
envSecret := h.envSecret
|
|
|
|
fail := func() {}
|
|
if !localClient {
|
|
h.attemptsMu.Lock()
|
|
ai := h.failedAttempts[clientIP]
|
|
if ai != nil {
|
|
if !ai.blockedUntil.IsZero() {
|
|
if time.Now().Before(ai.blockedUntil) {
|
|
remaining := time.Until(ai.blockedUntil).Round(time.Second)
|
|
h.attemptsMu.Unlock()
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("IP banned due to too many failed attempts. Try again in %s", remaining)})
|
|
return
|
|
}
|
|
// Ban expired, reset state
|
|
ai.blockedUntil = time.Time{}
|
|
ai.count = 0
|
|
}
|
|
}
|
|
h.attemptsMu.Unlock()
|
|
|
|
if !allowRemote {
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "remote management disabled"})
|
|
return
|
|
}
|
|
|
|
fail = func() {
|
|
h.attemptsMu.Lock()
|
|
aip := h.failedAttempts[clientIP]
|
|
if aip == nil {
|
|
aip = &attemptInfo{}
|
|
h.failedAttempts[clientIP] = aip
|
|
}
|
|
aip.count++
|
|
if aip.count >= maxFailures {
|
|
aip.blockedUntil = time.Now().Add(banDuration)
|
|
aip.count = 0
|
|
}
|
|
h.attemptsMu.Unlock()
|
|
}
|
|
}
|
|
if secretHash == "" && envSecret == "" {
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "remote management key not set"})
|
|
return
|
|
}
|
|
|
|
// Accept either Authorization: Bearer <key> or X-Management-Key
|
|
var provided string
|
|
if ah := c.GetHeader("Authorization"); ah != "" {
|
|
parts := strings.SplitN(ah, " ", 2)
|
|
if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" {
|
|
provided = parts[1]
|
|
} else {
|
|
provided = ah
|
|
}
|
|
}
|
|
if provided == "" {
|
|
provided = c.GetHeader("X-Management-Key")
|
|
}
|
|
|
|
if provided == "" {
|
|
if !localClient {
|
|
fail()
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing management key"})
|
|
return
|
|
}
|
|
|
|
if localClient {
|
|
if lp := h.localPassword; lp != "" {
|
|
if subtle.ConstantTimeCompare([]byte(provided), []byte(lp)) == 1 {
|
|
c.Next()
|
|
return
|
|
}
|
|
}
|
|
// For localhost, also accept configured API keys (same keys that work for main API)
|
|
if cfg != nil {
|
|
for _, apiKey := range cfg.APIKeys {
|
|
if apiKey != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(apiKey)) == 1 {
|
|
c.Next()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if envSecret != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(envSecret)) == 1 {
|
|
if !localClient {
|
|
h.attemptsMu.Lock()
|
|
if ai := h.failedAttempts[clientIP]; ai != nil {
|
|
ai.count = 0
|
|
ai.blockedUntil = time.Time{}
|
|
}
|
|
h.attemptsMu.Unlock()
|
|
}
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
if secretHash == "" || bcrypt.CompareHashAndPassword([]byte(secretHash), []byte(provided)) != nil {
|
|
if !localClient {
|
|
fail()
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid management key"})
|
|
return
|
|
}
|
|
|
|
if !localClient {
|
|
h.attemptsMu.Lock()
|
|
if ai := h.failedAttempts[clientIP]; ai != nil {
|
|
ai.count = 0
|
|
ai.blockedUntil = time.Time{}
|
|
}
|
|
h.attemptsMu.Unlock()
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// persist saves the current in-memory config to disk.
|
|
func (h *Handler) persist(c *gin.Context) bool {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
// Preserve comments when writing
|
|
if err := config.SaveConfigPreserveComments(h.configFilePath, h.cfg); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to save config: %v", err)})
|
|
return false
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
return true
|
|
}
|
|
|
|
// Helper methods for simple types
|
|
func (h *Handler) updateBoolField(c *gin.Context, set func(bool)) {
|
|
var body struct {
|
|
Value *bool `json:"value"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || body.Value == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
|
return
|
|
}
|
|
set(*body.Value)
|
|
h.persist(c)
|
|
}
|
|
|
|
func (h *Handler) updateIntField(c *gin.Context, set func(int)) {
|
|
var body struct {
|
|
Value *int `json:"value"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || body.Value == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
|
return
|
|
}
|
|
set(*body.Value)
|
|
h.persist(c)
|
|
}
|
|
|
|
func (h *Handler) updateStringField(c *gin.Context, set func(string)) {
|
|
var body struct {
|
|
Value *string `json:"value"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || body.Value == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
|
return
|
|
}
|
|
set(*body.Value)
|
|
h.persist(c)
|
|
}
|