2 Commits

Author SHA1 Message Date
043fb3db85 upstream asis 2023-09-16 10:52:33 +08:00
1fcdc59b63 fix: cors in resp 2023-09-15 19:55:05 +08:00
3 changed files with 22 additions and 35 deletions

View File

@@ -20,7 +20,8 @@ func handleAuth(c *gin.Context) error {
authorization = strings.Trim(authorization[len("Bearer"):], " ") authorization = strings.Trim(authorization[len("Bearer"):], " ")
log.Println("Received authorization", authorization) log.Println("Received authorization", authorization)
if authorization != authConfig.Value {
if authConfig.Value != "asis" && authorization != authConfig.Value {
err = errors.New("wrong authorization header") err = errors.New("wrong authorization header")
c.AbortWithError(403, err) c.AbortWithError(403, err)
return err return err

31
cors.go
View File

@@ -1,31 +0,0 @@
package main
import (
"github.com/gin-gonic/gin"
)
// Middleware function to handle CORS requests
func handleCORS(c *gin.Context) {
if c.Request.Method == "OPTIONS" {
header := c.Writer.Header()
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
c.AbortWithStatus(200)
return
}
c.Next()
header := c.Writer.Header()
header.Del("Access-Control-Allow-Origin")
header.Del("Access-Control-Allow-Methods")
header.Del("Access-Control-Allow-Headers")
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
}

23
main.go
View File

@@ -90,7 +90,13 @@ func main() {
}) })
// CORS handler // CORS handler
engine.Use(handleCORS) engine.OPTIONS("/v1/*any", func(ctx *gin.Context) {
header := ctx.Writer.Header()
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
ctx.AbortWithStatus(200)
})
// get authorization config from db // get authorization config from db
db.Take(&authConfig, "key = ?", "authorization") db.Take(&authConfig, "key = ?", "authorization")
@@ -179,13 +185,24 @@ func main() {
out.URL.Path = in.URL.Path out.URL.Path = in.URL.Path
out.Header = http.Header{} out.Header = http.Header{}
out.Header.Set("Host", remote.Host) out.Header.Set("Host", remote.Host)
out.Header.Set("Authorization", "Bearer "+upstream.SK) if upstream.SK == "asis" {
out.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
} else {
out.Header.Set("Authorization", "Bearer "+upstream.SK)
}
out.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) out.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
} }
var buf bytes.Buffer var buf bytes.Buffer
var contentType string var contentType string
proxy.ModifyResponse = func(r *http.Response) error { proxy.ModifyResponse = func(r *http.Response) error {
record.Status = r.StatusCode record.Status = r.StatusCode
r.Header.Del("Access-Control-Allow-Origin")
r.Header.Del("Access-Control-Allow-Methods")
r.Header.Del("Access-Control-Allow-Headers")
r.Header.Set("Access-Control-Allow-Origin", "*")
r.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
r.Header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
if r.StatusCode != 200 { if r.StatusCode != 200 {
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
@@ -217,7 +234,7 @@ func main() {
} }
content := fmt.Sprintf("[%s] OpenAI 转发出错 ID: %d... 密钥: [%s] 上游: [%s] 错误: %s\n---\n%s", content := fmt.Sprintf("[%s] OpenAI 转发出错 ID: %d... 密钥: [%s] 上游: [%s] 错误: %s\n---\n%s",
c.ClientIP(), c.ClientIP(),
upstream.ID, upstream.SK[:10], upstream.Endpoint, err.Error(), upstream.ID, upstream.SK, upstream.Endpoint, err.Error(),
strings.Join(upstreamDescriptions, "\n"), strings.Join(upstreamDescriptions, "\n"),
) )
go sendMatrixMessage(content) go sendMatrixMessage(content)