Skip to content

Commit

Permalink
Test vercel1 (aurora-develop#151)
Browse files Browse the repository at this point in the history
* 正式接入抽离的客户端库。

* 整理main包,把所有初始化任务抽离出来。尝试集成vercel的api请求模式。

* 整理main包,把所有初始化任务抽离出来。尝试集成vercel的api请求模式。将funcaptcha、tls-client、endless包更到最新。

* 可以设置是否支持流式传输。

---------

Co-authored-by: xiaozhou26 <[email protected]>
  • Loading branch information
learnLi and xiaozhou26 authored Apr 6, 2024
1 parent 1dd8a15 commit ca2fbf4
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 127 deletions.
18 changes: 18 additions & 0 deletions api/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package api

import (
"aurora/initialize"
"github.com/gin-gonic/gin"
"net/http"
)

var router *gin.Engine

func init() {
// 初始化gin
router = initialize.RegisterRouter()
}

func Listen(w http.ResponseWriter, r *http.Request) {
router.ServeHTTP(w, r)
}
Empty file modified build.sh
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions conversion/requests/chatgpt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ConvertAPIRequest(api_request official_types.APIRequest, secret *tokens.Sec
}
}
if requireArk {
token, err := arkose.GetOpenAIToken(api_version, secret.PUID, proxy)
token, err := arkose.GetOpenAIToken(api_version, secret.PUID, "", proxy)
if err == nil {
chatgpt_request.ArkoseToken = token
} else {
Expand Down Expand Up @@ -57,7 +57,7 @@ func RenewTokenForRequest(request *chatgpt_types.ChatGPTRequest, puid string, pr
} else {
api_version = 3
}
token, err := arkose.GetOpenAIToken(api_version, puid, proxy)
token, err := arkose.GetOpenAIToken(api_version, puid, "", proxy)
if err == nil {
request.ArkoseToken = token
} else {
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ require (
github.com/EDDYCJY/fake-useragent v0.2.0
github.com/acheong08/endless v0.0.0-20230615162514-90545c7793fd
github.com/aurorax-neo/go-logger v0.0.0-20240331061645-18b054649449
github.com/bogdanfinn/fhttp v0.5.24
github.com/bogdanfinn/tls-client v1.6.1
github.com/bogdanfinn/fhttp v0.5.27
github.com/bogdanfinn/tls-client v1.7.2
github.com/gin-gonic/gin v1.9.1
github.com/go-resty/resty/v2 v2.12.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.1
github.com/joho/godotenv v1.5.1
github.com/xqdoo00o/funcaptcha v0.0.0-20240313153914-4ab805804232
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c
)

require (
github.com/PuerkitoBio/goquery v1.9.1 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/bogdanfinn/utls v1.5.16 // indirect
github.com/bogdanfinn/utls v1.6.1 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/cloudflare/circl v1.3.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -39,6 +40,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/quic-go/quic-go v0.37.4 // indirect
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ github.com/aurorax-neo/go-logger v0.0.0-20240331061645-18b054649449 h1:rDAVeb3Vw
github.com/aurorax-neo/go-logger v0.0.0-20240331061645-18b054649449/go.mod h1:BJsRG1ECcXTHwiz2zaMYxFkeXh+MpQVs6nWYphLT244=
github.com/bogdanfinn/fhttp v0.5.24 h1:OlyBKjvJp6a3TotN3wuj4mQHHRbfK7QUMrzCPOZGhRc=
github.com/bogdanfinn/fhttp v0.5.24/go.mod h1:brqi5woc5eSCVHdKYBV8aZLbO7HGqpwyDLeXW+fT18I=
github.com/bogdanfinn/fhttp v0.5.27 h1:+glR3k8v5nxfUSk7+J3M246zEQ2yadhS0vLq1utK71A=
github.com/bogdanfinn/fhttp v0.5.27/go.mod h1:oJiYPG3jQTKzk/VFmogH8jxjH5yiv2rrOH48Xso2lrE=
github.com/bogdanfinn/tls-client v1.6.1 h1:GTIqQssFoIvLaDf4btoYRzDhUzudLqYD4axvfUCXl3I=
github.com/bogdanfinn/tls-client v1.6.1/go.mod h1:FtwQ3DndVZ0xAOO704v4iNAgbHOcEc5kPk9tjICTNQ0=
github.com/bogdanfinn/tls-client v1.7.2 h1:vpL5qBYUfT9ueygEf1yLfymrXyUEZQatL25amfqGV8M=
github.com/bogdanfinn/tls-client v1.7.2/go.mod h1:pOGa2euqTbEkGNqE5idx5jKKfs9ytlyn3fwEw8RSP+g=
github.com/bogdanfinn/utls v1.5.16 h1:NhhWkegEcYETBMj9nvgO4lwvc6NcLH+znrXzO3gnw4M=
github.com/bogdanfinn/utls v1.5.16/go.mod h1:mHeRCi69cUiEyVBkKONB1cAbLjRcZnlJbGzttmiuK4o=
github.com/bogdanfinn/utls v1.6.1 h1:dKDYAcXEyFFJ3GaWaN89DEyjyRraD1qb4osdEK89ass=
github.com/bogdanfinn/utls v1.6.1/go.mod h1:VXIbRZaiY/wHZc6Hu+DZ4O2CgTzjhjCg/Ou3V4r/39Y=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc=
Expand All @@ -26,6 +32,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/cloudflare/circl v1.3.6 h1:/xbKIqSHbZXHwkhbrhrt2YOHIwYJlXH94E3tI/gDlUg=
github.com/cloudflare/circl v1.3.6/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -78,6 +86,8 @@ github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/quic-go/quic-go v0.37.4 h1:ke8B73yMCWGq9MfrCCAw0Uzdm7GaViC3i39dsIdDlH4=
github.com/quic-go/quic-go v0.37.4/go.mod h1:YsbH1r4mSHPJcLF4k4zruUkLBqctEMBDR6VPvcYjIsU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -97,6 +107,8 @@ github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4d
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/xqdoo00o/funcaptcha v0.0.0-20240313153914-4ab805804232 h1:K8+8YdWdDzmmhVqNflEXV4RLpZPf56wwxo0k0nAtQO4=
github.com/xqdoo00o/funcaptcha v0.0.0-20240313153914-4ab805804232/go.mod h1:7aCyoW5MHDUsoooMVLqKe0F7W9HMPUvDG3bXqw++8XA=
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c h1:nj17XsSTwprsZUDXLldOUZmqz7VlHsLCeXXFOE6Q+Mk=
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c/go.mod h1:7aCyoW5MHDUsoooMVLqKe0F7W9HMPUvDG3bXqw++8XA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
Expand Down
2 changes: 1 addition & 1 deletion auth.go → initialize/auth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package initialize

import (
"bufio"
Expand Down
10 changes: 8 additions & 2 deletions handlers.go → initialize/handlers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package initialize

import (
chatgptrequestconverter "aurora/conversion/requests/chatgpt"
Expand All @@ -7,9 +7,11 @@ import (
"aurora/internal/proxys"
"aurora/internal/tokens"
officialtypes "aurora/typings/official"
"os"
"strings"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"strings"
)

type Handler struct {
Expand Down Expand Up @@ -205,6 +207,10 @@ func (h *Handler) nightmare(c *gin.Context) {
return
}
var full_response string

if os.Getenv("STREAM_MODE") == "false" {
original_request.Stream = false
}
for i := 3; i > 0; i-- {
var continue_info *chatgpt.ContinueInfo
var response_part string
Expand Down
48 changes: 48 additions & 0 deletions initialize/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package initialize

import (
"aurora/internal/proxys"
"bufio"
"log/slog"
"net/url"
"os"
)

func checkProxy() *proxys.IProxy {
var proxies []string
proxyUrl := os.Getenv("PROXY_URL")
if proxyUrl != "" {
proxies = append(proxies, proxyUrl)
}

if _, err := os.Stat("proxies.txt"); err == nil {
file, _ := os.Open("proxies.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
proxy := scanner.Text()
parsedURL, err := url.Parse(proxy)
if err != nil {
slog.Warn("proxy url is invalid", "url", proxy, "err", err)
continue
}

// 如果缺少端口信息,不是完整的代理链接
if parsedURL.Port() != "" {
proxies = append(proxies, proxy)
} else {
continue
}
}
}

if len(proxies) == 0 {
proxy := os.Getenv("http_proxy")
if proxy != "" {
proxies = append(proxies, proxy)
}
}

proxyIP := proxys.NewIProxyIP(proxies)
return &proxyIP
}
38 changes: 38 additions & 0 deletions initialize/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package initialize

import (
"aurora/middlewares"

"github.com/gin-gonic/gin"
)

func RegisterRouter() *gin.Engine {
handler := NewHandle(
checkProxy(),
readAccessToken(),
)

router := gin.Default()
router.Use(middlewares.Cors)

router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello, world!",
})
})

router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})

router.POST("/auth/session", handler.session)
router.POST("/auth/refresh", handler.refresh)
router.OPTIONS("/v1/chat/completions", optionsHandler)

authGroup := router.Group("").Use(middlewares.Authorization)
authGroup.POST("/v1/chat/completions", handler.nightmare)
authGroup.GET("/v1/models", handler.engines)
return router
}
6 changes: 3 additions & 3 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
connPool = map[string][]*connInfo{}
poolMutex = sync.Mutex{}
TurnStilePool = map[string]*TurnStile{}
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.0.0 Safari/537.36"
)

func getWSURL(client httpclient.AuroraHttpClient, token string, retry int) (string, error) {
Expand Down Expand Up @@ -701,15 +701,15 @@ func Handler(c *gin.Context, response *http.Response, client httpclient.AuroraHt
waitSource = true
continue
}
endProcess:
isRole = false
if stream {
_, err = c.Writer.WriteString(response_string)
if err != nil {
return "", nil
}
c.Writer.Flush()
}
endProcess:
c.Writer.Flush()

if original_response.Message.Metadata.FinishDetails != nil {
if original_response.Message.Metadata.FinishDetails.Type == "max_tokens" {
Expand Down
94 changes: 4 additions & 90 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,104 +1,18 @@
package main

import (
"aurora/internal/proxys"
"bufio"
"embed"
"io/fs"
"log"
"log/slog"
"net/http"
"net/url"
"aurora/initialize"
"os"

"github.com/acheong08/endless"
"github.com/gin-gonic/gin"

"github.com/acheong08/endless"
"github.com/joho/godotenv"
)

func checkProxy() *proxys.IProxy {
var proxies []string
proxyUrl := os.Getenv("PROXY_URL")
if proxyUrl != "" {
proxies = append(proxies, proxyUrl)
}

if _, err := os.Stat("proxies.txt"); err == nil {
file, _ := os.Open("proxies.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
proxy := scanner.Text()
parsedURL, err := url.Parse(proxy)
if err != nil {
slog.Warn("proxy url is invalid", "url", proxy, "err", err)
continue
}

// 如果缺少端口信息,不是完整的代理链接
if parsedURL.Port() != "" {
proxies = append(proxies, proxy)
} else {
continue
}
}
}

if len(proxies) == 0 {
proxy := os.Getenv("http_proxy")
if proxy != "" {
proxies = append(proxies, proxy)
}
}

proxyIP := proxys.NewIProxyIP(proxies)
return &proxyIP
}

//go:embed web/*
var staticFiles embed.FS

func registerRouter() *gin.Engine {
handler := NewHandle(
checkProxy(),
readAccessToken(),
)

router := gin.Default()
router.Use(cors)

router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello, world!",
})
})

router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})

router.POST("/auth/session", handler.session)
router.POST("/auth/refresh", handler.refresh)
router.OPTIONS("/v1/chat/completions", optionsHandler)

authGroup := router.Group("").Use(Authorization)
authGroup.POST("/v1/chat/completions", handler.nightmare)
authGroup.GET("/v1/models", handler.engines)

subFS, err := fs.Sub(staticFiles, "web")
if err != nil {
log.Fatal(err)
}
router.StaticFS("/web", http.FS(subFS))

return router
}

func main() {
gin.SetMode(gin.ReleaseMode)
router := registerRouter()
router := initialize.RegisterRouter()

_ = godotenv.Load(".env")
host := os.Getenv("SERVER_HOST")
Expand Down
Loading

0 comments on commit ca2fbf4

Please sign in to comment.