mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Add GeminiModels handler and enhance API key validation
- Introduced `GeminiModels` handler to serve Gemini model information under `/v1beta/models`. - Updated `AuthMiddleware` to validate API keys from query parameters for improved flexibility. - Adjusted route to use the new handler for model retrieval.
This commit is contained in:
@@ -14,6 +14,22 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (h *APIHandlers) GeminiModels(c *gin.Context) {
|
||||||
|
c.Status(200)
|
||||||
|
c.Header("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
_, _ = c.Writer.Write([]byte(`{"models":[{"name":"models/gemini-2.5-flash","version":"001","displayName":"Gemini `))
|
||||||
|
_, _ = c.Writer.Write([]byte(`2.5 Flash","description":"Stable version of Gemini 2.5 Flash, our mid-size multimod`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`al model that supports up to 1 million tokens, released in June of 2025.","inputTok`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`enLimit":1048576,"outputTokenLimit":65536,"supportedGenerationMethods":["generateCo`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ntent","countTokens","createCachedContent","batchGenerateContent"],"temperature":1,`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`"topP":0.95,"topK":64,"maxTemperature":2,"thinking":true},{"name":"models/gemini-2.`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`5-pro","version":"2.5","displayName":"Gemini 2.5 Pro","description":"Stable release`))
|
||||||
|
_, _ = c.Writer.Write([]byte(` (June 17th, 2025) of Gemini 2.5 Pro","inputTokenLimit":1048576,"outputTokenLimit":`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`65536,"supportedGenerationMethods":["generateContent","countTokens","createCachedCo`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ntent","batchGenerateContent"],"temperature":1,"topP":0.95,"topK":64,"maxTemperatur`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`e":2,"thinking":true}],"nextPageToken":""}`))
|
||||||
|
}
|
||||||
|
|
||||||
func (h *APIHandlers) GeminiHandler(c *gin.Context) {
|
func (h *APIHandlers) GeminiHandler(c *gin.Context) {
|
||||||
var person struct {
|
var person struct {
|
||||||
Action string `uri:"action" binding:"required"`
|
Action string `uri:"action" binding:"required"`
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func (s *Server) setupRoutes() {
|
|||||||
v1beta := s.engine.Group("/v1beta")
|
v1beta := s.engine.Group("/v1beta")
|
||||||
v1beta.Use(AuthMiddleware(s.cfg))
|
v1beta.Use(AuthMiddleware(s.cfg))
|
||||||
{
|
{
|
||||||
v1beta.GET("/models", s.handlers.Models)
|
v1beta.GET("/models", s.handlers.GeminiModels)
|
||||||
v1beta.POST("/models/:action", s.handlers.GeminiHandler)
|
v1beta.POST("/models/:action", s.handlers.GeminiHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +151,11 @@ func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|||||||
authHeader := c.GetHeader("Authorization")
|
authHeader := c.GetHeader("Authorization")
|
||||||
authHeaderGoogle := c.GetHeader("X-Goog-Api-Key")
|
authHeaderGoogle := c.GetHeader("X-Goog-Api-Key")
|
||||||
authHeaderAnthropic := c.GetHeader("X-Api-Key")
|
authHeaderAnthropic := c.GetHeader("X-Api-Key")
|
||||||
if authHeader == "" && authHeaderGoogle == "" && authHeaderAnthropic == "" {
|
|
||||||
|
// Get the API key from the query parameter
|
||||||
|
apiKeyQuery, _ := c.GetQuery("key")
|
||||||
|
|
||||||
|
if authHeader == "" && authHeaderGoogle == "" && authHeaderAnthropic == "" && apiKeyQuery == "" {
|
||||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||||
"error": "Missing API key",
|
"error": "Missing API key",
|
||||||
})
|
})
|
||||||
@@ -170,7 +174,7 @@ func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|||||||
// Find the API key in the in-memory list
|
// Find the API key in the in-memory list
|
||||||
var foundKey string
|
var foundKey string
|
||||||
for i := range cfg.ApiKeys {
|
for i := range cfg.ApiKeys {
|
||||||
if cfg.ApiKeys[i] == apiKey || cfg.ApiKeys[i] == authHeaderGoogle || cfg.ApiKeys[i] == authHeaderAnthropic {
|
if cfg.ApiKeys[i] == apiKey || cfg.ApiKeys[i] == authHeaderGoogle || cfg.ApiKeys[i] == authHeaderAnthropic || cfg.ApiKeys[i] == apiKeyQuery {
|
||||||
foundKey = cfg.ApiKeys[i]
|
foundKey = cfg.ApiKeys[i]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user