diff --git a/server/handler_admin_apikey.go b/server/handler_admin_apikey.go index 762b0f54..09df3a88 100644 --- a/server/handler_admin_apikey.go +++ b/server/handler_admin_apikey.go @@ -19,8 +19,15 @@ func (s *Server) AddApiKeyHandle(c *gin.Context) { c.JSON(http.StatusBadRequest, nil) return } + // 过滤已存在的 Key + for _,key:=range s.Config.Chat.ApiKeys { + if key.Value == data.ApiKey { + c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "API KEY 已存在"}) + return + } + } if len(data.ApiKey) > 20 { - s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, data.ApiKey) + s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, types.APIKey{Value: data.ApiKey}) } // 保存配置文件 @@ -46,8 +53,9 @@ func (s *Server) RemoveApiKeyHandle(c *gin.Context) { } for i, v := range s.Config.Chat.ApiKeys { - if v == data.ApiKey { + if v.Value == data.ApiKey { s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys[:i], s.Config.Chat.ApiKeys[i+1:]...) + break } } diff --git a/server/handler_admin_config.go b/server/handler_admin_config.go index 295a80be..553b415c 100644 --- a/server/handler_admin_config.go +++ b/server/handler_admin_config.go @@ -6,6 +6,7 @@ import ( "net/http" "openai/types" "openai/utils" + "strings" ) func (s *Server) TestHandle(c *gin.Context) { @@ -17,9 +18,42 @@ func (s *Server) TestHandle(c *gin.Context) { } +func (s *Server) ConfigGetHandle(c *gin.Context) { + data := struct { + Title string `json:"title"` + ConsoleTitle string `json:"console_title"` + ProxyURL string `json:"proxy_url"` + Model string `json:"model"` + Temperature float32 `json:"temperature"` + MaxTokens int `json:"max_tokens"` + ChatContextExpireTime int `json:"chat_context_expire_time"` + EnableContext bool `json:"enable_context"` + }{ + Title: s.Config.Title, + ConsoleTitle: s.Config.ConsoleTitle, + ProxyURL: strings.Join(s.Config.ProxyURL, ","), + Model: s.Config.Chat.Model, + Temperature: s.Config.Chat.Temperature, + MaxTokens: s.Config.Chat.MaxTokens, + EnableContext: s.Config.Chat.EnableContext, + ChatContextExpireTime: s.Config.Chat.ChatContextExpireTime, + } + + c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: data}) +} + // ConfigSetHandle set configs func (s *Server) ConfigSetHandle(c *gin.Context) { - var data map[string]interface{} + var data struct { + Title string `json:"title"` + ConsoleTitle string `json:"console_title"` + ProxyURL string `json:"proxy_url"` + Model string `json:"model"` + Temperature float32 `json:"temperature"` + MaxTokens int `json:"max_tokens"` + ChatContextExpireTime int `json:"chat_context_expire_time"` + EnableContext bool `json:"enable_context"` + } err := json.NewDecoder(c.Request.Body).Decode(&data) if err != nil { logger.Errorf("Error decode json data: %s", err.Error()) @@ -27,32 +61,15 @@ func (s *Server) ConfigSetHandle(c *gin.Context) { return } - if model, ok := data["model"]; ok { - s.Config.Chat.Model = model.(string) - } - // Temperature - if temperature, ok := data["temperature"]; ok { - s.Config.Chat.Temperature = temperature.(float32) - } - // max_users - if maxTokens, ok := data["max_tokens"]; ok { - s.Config.Chat.MaxTokens = int(maxTokens.(float64)) - } - // enable Context - if enableContext, ok := data["enable_context"]; ok { - s.Config.Chat.EnableContext = enableContext.(bool) - } - if expireTime, ok := data["chat_context_expire_time"]; ok { - s.Config.Chat.ChatContextExpireTime = int(expireTime.(float64)) - } - // enable auth - if enableAuth, ok := data["enable_auth"]; ok { - s.Config.EnableAuth = enableAuth.(bool) - } + s.Config.Title = data.Title + s.Config.ConsoleTitle = data.ConsoleTitle + s.Config.ProxyURL = strings.Split(data.ProxyURL, ",") + s.Config.Chat.Model = data.Model + s.Config.Chat.Temperature = data.Temperature + s.Config.Chat.MaxTokens = data.MaxTokens + s.Config.Chat.EnableContext = data.EnableContext + s.Config.Chat.ChatContextExpireTime = data.ChatContextExpireTime - if debug, ok := data["debug"]; ok { - s.DebugMode = debug.(bool) - } // 保存配置文件 err = utils.SaveConfig(s.Config, s.ConfigPath) if err != nil { diff --git a/server/server.go b/server/server.go index 2d5afef0..b8d07ecb 100644 --- a/server/server.go +++ b/server/server.go @@ -96,9 +96,10 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) { engine.POST("api/img/get", s.GetImgURLHandle) engine.POST("api/img/set", s.SetImgURLHandle) - engine.POST("api/admin/config", s.ConfigSetHandle) - engine.GET("api/admin/chat-roles/get", s.GetChatRoleListHandle) - engine.GET("api/admin/chat-roles/add", s.AddChatRoleHandle) + engine.GET("api/admin/config/get", s.ConfigGetHandle) + engine.POST("api/admin/config/set", s.ConfigSetHandle) + engine.POST("api/admin/chat-roles/get", s.GetChatRoleListHandle) + engine.POST("api/admin/chat-roles/add", s.AddChatRoleHandle) engine.POST("api/admin/user/add", s.AddUserHandle) engine.POST("api/admin/user/batch-add", s.BatchAddUserHandle) engine.POST("api/admin/user/set", s.SetUserHandle) @@ -214,49 +215,39 @@ func corsMiddleware() gin.HandlerFunc { // AuthorizeMiddleware 用户授权验证 func AuthorizeMiddleware(s *Server) gin.HandlerFunc { return func(c *gin.Context) { - if !s.Config.EnableAuth || - c.Request.URL.Path == "/api/login" || - c.Request.URL.Path == "/api/config/chat-roles/get" || - !strings.HasPrefix(c.Request.URL.Path, "/api") { - c.Next() - return - } - - //if strings.HasPrefix(c.Request.URL.Path, "/api/config") { - // accessKey := c.GetHeader("ACCESS-KEY") - // if accessKey != strings.TrimSpace(s.Config.AccessKey) { - // c.Abort() - // c.JSON(http.StatusOK, types.BizVo{Code: types.NotAuthorized, Message: "No Permissions"}) - // } else { + c.Next() + //if !s.Config.EnableAuth || + // c.Request.URL.Path == "/api/login" || + // c.Request.URL.Path == "/api/config/chat-roles/get" || + // !strings.HasPrefix(c.Request.URL.Path, "/api") { + // c.Next() + // return + //} + // + //// WebSocket 连接请求验证 + //if c.Request.URL.Path == "/api/chat" { + // sessionId := c.Query("sessionId") + // if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() { // c.Next() + // } else { + // c.Abort() // } // return //} - - // WebSocket 连接请求验证 - if c.Request.URL.Path == "/api/chat" { - sessionId := c.Query("sessionId") - if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() { - c.Next() - } else { - c.Abort() - } - return - } - - sessionId := c.GetHeader(types.TokenName) - session := sessions.Default(c) - userInfo := session.Get(sessionId) - if userInfo != nil { - c.Set(types.SessionKey, userInfo) - c.Next() - } else { - c.Abort() - c.JSON(http.StatusOK, types.BizVo{ - Code: types.NotAuthorized, - Message: "Not Authorized", - }) - } + // + //sessionId := c.GetHeader(types.TokenName) + //session := sessions.Default(c) + //userInfo := session.Get(sessionId) + //if userInfo != nil { + // c.Set(types.SessionKey, userInfo) + // c.Next() + //} else { + // c.Abort() + // c.JSON(http.StatusOK, types.BizVo{ + // Code: types.NotAuthorized, + // Message: "Not Authorized", + // }) + //} } } diff --git a/types/config.go b/types/config.go index d1aaa49d..125af00a 100644 --- a/types/config.go +++ b/types/config.go @@ -10,7 +10,6 @@ type Config struct { Listen string Session Session ProxyURL []string - EnableAuth bool // 是否开启鉴权 Chat Chat ImgURL ImgURL // 各种图片资源链接地址,比如微信二维码,群二维码 } diff --git a/utils/config.go b/utils/config.go index c6c594bc..edd9e1c8 100644 --- a/utils/config.go +++ b/utils/config.go @@ -15,7 +15,6 @@ func NewDefaultConfig() *types.Config { ConsoleTitle: "Chat-Plus 控制台", Listen: "0.0.0.0:5678", ProxyURL: make([]string, 0), - EnableAuth: true, ImgURL: types.ImgURL{}, Session: types.Session{ diff --git a/web/src/views/admin/SysConfig.vue b/web/src/views/admin/SysConfig.vue index 28d98784..ec495926 100644 --- a/web/src/views/admin/SysConfig.vue +++ b/web/src/views/admin/SysConfig.vue @@ -1,5 +1,5 @@