This commit is contained in:
Sakurasan
2023-03-31 21:55:24 +08:00
parent 89f6e0df6a
commit a452469b51
2 changed files with 14 additions and 14 deletions

View File

@@ -41,9 +41,10 @@ func main() {
// 初始化用户
r.POST("/1/users/init", router.Handleinit)
r.POST("/v1/chat/completions", router.HandleReverseProxy)
r.GET("/v1/models", router.HandleReverseProxy)
r.GET("/v1/dashboard/billing/credit_grants", router.HandleReverseProxy)
r.POST("/v1/chat/completions", router.HandleProy)
r.GET("/v1/models", router.HandleProy)
r.GET("/v1/dashboard/billing/credit_grants", router.HandleProy)
r.Run(":80")
}

View File

@@ -234,9 +234,7 @@ func HandleProy(c *gin.Context) {
var localuser bool
auth := c.Request.Header.Get("Authorization")
if len(auth) > 7 && auth[:7] == "Bearer " {
if store.IsExistAuthCache(auth[7:]) {
localuser = true
}
localuser = store.IsExistAuthCache(auth[7:])
}
client := http.DefaultClient
tr := &http.Transport{
@@ -346,20 +344,21 @@ func HandleReverseProxy(c *gin.Context) {
localuser = store.IsExistAuthCache(auth[7:])
}
// req, err := http.NewRequest(c.Request.Method, baseUrl+c.Request.URL.Path, c.Request.Body)
// if err != nil {
// c.JSON(http.StatusOK, gin.H{"error": err.Error()})
// return
// }
// req.Header = c.Request.Header
req, err := http.NewRequest(c.Request.Method, c.Request.URL.Path, c.Request.Body)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
req.Header = c.Request.Header
if localuser {
if store.KeysCache.ItemCount() == 0 {
c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"})
return
}
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem()))
// c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem()))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem()))
}
proxy.ServeHTTP(c.Writer, c.Request)
proxy.ServeHTTP(c.Writer, req)
}