公开代理

This commit is contained in:
Sakurasan
2023-03-31 16:59:25 +08:00
parent 074a8e4267
commit 8c75f9575b
2 changed files with 8 additions and 7 deletions

View File

@@ -41,7 +41,8 @@ func main() {
// 初始化用户 // 初始化用户
r.POST("/1/users/init", router.Handleinit) r.POST("/1/users/init", router.Handleinit)
r.POST("/v1/chat/completions", router.HandleProy) r.Any("/v1/chat/completions", router.HandleProy)
r.Any("/", router.HandleProy)
r.Run(":80") r.Run(":80")
} }

View File

@@ -229,6 +229,7 @@ func GenerateToken() string {
} }
func HandleProy(c *gin.Context) { func HandleProy(c *gin.Context) {
var localuser bool
auth := c.Request.Header.Get("Authorization") auth := c.Request.Header.Get("Authorization")
if auth[:7] == "Bearer " { if auth[:7] == "Bearer " {
if len(auth[7:]) < 1 { if len(auth[7:]) < 1 {
@@ -236,12 +237,9 @@ func HandleProy(c *gin.Context) {
return return
} }
if !store.IsExistAuthCache(auth[7:]) { if !store.IsExistAuthCache(auth[7:]) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) localuser = false
return
} }
} else { localuser = true
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
} }
client := http.DefaultClient client := http.DefaultClient
tr := &http.Transport{ tr := &http.Transport{
@@ -270,7 +268,9 @@ func HandleProy(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"}) c.JSON(http.StatusOK, gin.H{"error": "No Api-Key Available"})
return return
} }
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem())) if localuser {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem()))
}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {