Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Add CORS middleware to handle cross-origin requests (aurora-develop#72)

* Revert "Add CORS middleware to handle cross-origin requests (aurora-develop#72)" (aurora-develop#73)

This reverts commit 03173fe.

* Test au (aurora-develop#75)

* Add CORS middleware to handle cross-origin requests

* chroe: 抽离 baseurl

* Update main.go

* update: 更新 api 路径为/backend-anon

* Refactor code to improve performance and readability

* uuid (aurora-develop#74)

* Add CORS middleware to handle cross-origin requests (aurora-develop#72)

* Revert "Add CORS middleware to handle cross-origin requests (aurora-develop#72)" (aurora-develop#73)

This reverts commit 03173fe.

* 修复uuid失效,替换失效uuid

---------

Co-authored-by: xiaofei <[email protected]>

---------

Co-authored-by: aurorax-neo <[email protected]>
Co-authored-by: learnLi <[email protected]>

* 全局变量私有化 & 重排序导入包 (aurora-develop#76)

* style: sort imports

* refactor: 私有化全局变量

* Update build_docker.yml (aurora-develop#77)

---------

Co-authored-by: aurorax-neo <[email protected]>
Co-authored-by: learnLi <[email protected]>
Co-authored-by: icpd <[email protected]>
  • Loading branch information
4 people authored Apr 4, 2024
1 parent bf3d252 commit 0b13f49
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 104 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/build_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ jobs:
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build and push to GHCR
uses: docker/build-push-action@v2
Expand All @@ -42,6 +44,8 @@ jobs:
platforms: linux/amd64,linux/arm64
file: Dockerfile
push: true
tags: ${{ env.GHCR_REPO }}:latest
tags: |
${{ env.GHCR_REPO }}:latest
${{ env.GHCR_REPO }}:${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
8 changes: 5 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"aurora/internal/tokens"
"bufio"
"fmt"
"os"
"strconv"

"aurora/internal/tokens"

"github.com/google/uuid"
)

func readAccessToken() {
func readAccessToken() *tokens.AccessToken {
var Secrets []*tokens.Secret
// Read accounts.txt and create a list of accounts
if _, err := os.Stat("access_tokens.txt"); err == nil {
Expand Down Expand Up @@ -62,5 +63,6 @@ func readAccessToken() {
}
}

ACCESS_TOKENS = tokens.NewAccessToken(Secrets)
token := tokens.NewAccessToken(Secrets)
return &token
}
6 changes: 4 additions & 2 deletions conversion/requests/chatgpt/convert.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package chatgpt

import (
"fmt"
"strings"

"aurora/internal/tokens"
chatgpt_types "aurora/typings/chatgpt"
official_types "aurora/typings/official"
"fmt"

arkose "github.com/xqdoo00o/funcaptcha"
"strings"
)

func ConvertAPIRequest(api_request official_types.APIRequest, secret *tokens.Secret, requireArk bool, proxy string) chatgpt_types.ChatGPTRequest {
Expand Down
3 changes: 2 additions & 1 deletion conversion/response/chatgpt/convert.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package chatgpt

import (
"strings"

"aurora/typings"
chatgpt_types "aurora/typings/chatgpt"
official_types "aurora/typings/official"
"strings"
)

func ConvertToString(chatgpt_response *chatgpt_types.ChatGPTResponse, previous_text *typings.StringStruct, role bool) string {
Expand Down
75 changes: 43 additions & 32 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package main

import (
chatgpt_request_converter "aurora/conversion/requests/chatgpt"
"strings"

chatgptrequestconverter "aurora/conversion/requests/chatgpt"
"aurora/httpclient"
"aurora/internal/chatgpt"

official_types "aurora/typings/official"
"strings"
"aurora/internal/proxys"
"aurora/internal/tokens"
officialtypes "aurora/typings/official"

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

type Handler struct {
proxy *proxys.IProxy
token *tokens.AccessToken
}

func NewHandle(proxy *proxys.IProxy, token *tokens.AccessToken) *Handler {
return &Handler{proxy: proxy, token: token}
}

func optionsHandler(c *gin.Context) {
// Set headers for CORS
c.Header("Access-Control-Allow-Origin", "*")
Expand All @@ -22,9 +33,9 @@ func optionsHandler(c *gin.Context) {
})
}

func refresh_handler(c *gin.Context) {
var refresh_token official_types.OpenAIRefreshToken
err := c.BindJSON(&refresh_token)
func (h *Handler) refresh(c *gin.Context) {
var refreshToken officialtypes.OpenAIRefreshToken
err := c.BindJSON(&refreshToken)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must be proper JSON",
Expand All @@ -34,8 +45,8 @@ func refresh_handler(c *gin.Context) {
}})
return
}
proxy_url := ProxyIP.GetProxyIP()
openaiRefreshToken, status, err := chatgpt.GETTokenForRefreshToken(refresh_token.RefreshToken, proxy_url)
proxyUrl := h.proxy.GetProxyIP()
openaiRefreshToken, status, err := chatgpt.GETTokenForRefreshToken(refreshToken.RefreshToken, proxyUrl)
if err != nil {
c.JSON(400, gin.H{
"message": "Request must be proper JSON",
Expand All @@ -48,9 +59,9 @@ func refresh_handler(c *gin.Context) {
c.JSON(status, openaiRefreshToken)
}

func session_handler(c *gin.Context) {
var session_token official_types.OpenAISessionToken
err := c.BindJSON(&session_token)
func (h *Handler) session(c *gin.Context) {
var sessionToken officialtypes.OpenAISessionToken
err := c.BindJSON(&sessionToken)
if err != nil {
c.JSON(400, gin.H{
"message": "Request must be proper JSON",
Expand All @@ -60,8 +71,8 @@ func session_handler(c *gin.Context) {
})
return
}
proxy_url := ProxyIP.GetProxyIP()
openaiSessionToken, status, err := chatgpt.GETTokenForSessionToken(session_token.SessionToken, proxy_url)
proxy_url := h.proxy.GetProxyIP()
openaiSessionToken, status, err := chatgpt.GETTokenForSessionToken(sessionToken.SessionToken, proxy_url)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must be proper JSON",
Expand All @@ -74,8 +85,8 @@ func session_handler(c *gin.Context) {
c.JSON(status, openaiSessionToken)
}

func nightmare(c *gin.Context) {
var original_request official_types.APIRequest
func (h *Handler) nightmare(c *gin.Context) {
var original_request officialtypes.APIRequest
err := c.BindJSON(&original_request)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
Expand All @@ -87,13 +98,13 @@ func nightmare(c *gin.Context) {
return
}

proxy_url := ProxyIP.GetProxyIP()
secret := ACCESS_TOKENS.GetSecret()
proxyUrl := h.proxy.GetProxyIP()
secret := h.token.GetSecret()
authHeader := c.GetHeader("Authorization")
if authHeader != "" {
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
if strings.HasPrefix(customAccessToken, "eyJhbGciOiJSUzI1NiI") {
secret = ACCESS_TOKENS.GenerateTempToken(customAccessToken)
secret = h.token.GenerateTempToken(customAccessToken)
}
}
if secret == nil {
Expand All @@ -104,7 +115,7 @@ func nightmare(c *gin.Context) {

client := httpclient.NewStdClient()
uid := uuid.NewString()
turnStile, status, err := chatgpt.InitTurnStile(client, secret, proxy_url)
turnStile, status, err := chatgpt.InitTurnStile(client, secret, proxyUrl)
if err != nil {
c.JSON(status, gin.H{
"message": err.Error(),
Expand All @@ -115,17 +126,17 @@ func nightmare(c *gin.Context) {
return
}
if !secret.IsFree {
err = chatgpt.InitWSConn(secret.Token, uid, proxy_url)
err = chatgpt.InitWSConn(secret.Token, uid, proxyUrl)
if err != nil {
c.JSON(500, gin.H{"error": "unable to create ws tunnel"})
return
}
}

// Convert the chat request to a ChatGPT request
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request, secret, turnStile.Arkose, proxy_url)
translated_request := chatgptrequestconverter.ConvertAPIRequest(original_request, secret, turnStile.Arkose, proxyUrl)

response, err := chatgpt.POSTconversation(client, translated_request, secret, turnStile, proxy_url)
response, err := chatgpt.POSTconversation(client, translated_request, secret, turnStile, proxyUrl)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand All @@ -151,9 +162,9 @@ func nightmare(c *gin.Context) {
translated_request.ParentMessageID = continue_info.ParentID

if turnStile.Arkose {
chatgpt_request_converter.RenewTokenForRequest(&translated_request, secret.PUID, proxy_url)
chatgptrequestconverter.RenewTokenForRequest(&translated_request, secret.PUID, proxyUrl)
}
response, err = chatgpt.POSTconversation(client, translated_request, secret, turnStile, proxy_url)
response, err = chatgpt.POSTconversation(client, translated_request, secret, turnStile, proxyUrl)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand All @@ -169,30 +180,30 @@ func nightmare(c *gin.Context) {
return
}
if !original_request.Stream {
c.JSON(200, official_types.NewChatCompletion(full_response))
c.JSON(200, officialtypes.NewChatCompletion(full_response))
} else {
c.String(200, "data: [DONE]\n\n")
}
chatgpt.UnlockSpecConn(secret.Token, uid)
}

func engines_handler(c *gin.Context) {
proxy_url := ProxyIP.GetProxyIP()
secret := ACCESS_TOKENS.GetSecret()
func (h *Handler) engines(c *gin.Context) {
proxyUrl := h.proxy.GetProxyIP()
secret := h.token.GetSecret()
authHeader := c.GetHeader("Authorization")
if authHeader != "" {
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
// Check if customAccessToken starts with sk-
if strings.HasPrefix(customAccessToken, "eyJhbGciOiJSUzI1NiI") {
secret = ACCESS_TOKENS.GenerateTempToken(customAccessToken)
secret = h.token.GenerateTempToken(customAccessToken)
}
}
if secret.Token == "" || secret == nil {
if secret == nil || secret.Token == "" {
c.JSON(400, gin.H{"error": "Not Account Found."})
return
}

resp, status, err := chatgpt.GETengines(secret, proxy_url)
resp, status, err := chatgpt.GETengines(secret, proxyUrl)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand Down
8 changes: 5 additions & 3 deletions httpclient/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package httpclient

import (
"aurora/util"
"crypto/tls"
browser "github.com/EDDYCJY/fake-useragent"
"github.com/go-resty/resty/v2"
"net/http"
"time"

"aurora/util"

browser "github.com/EDDYCJY/fake-useragent"
"github.com/go-resty/resty/v2"
)

type RestyClient struct {
Expand Down
19 changes: 8 additions & 11 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package chatgpt

import (
"aurora/httpclient"
"aurora/internal/tokens"
"aurora/typings"
chatgpt_types "aurora/typings/chatgpt"
"bufio"
"bytes"
"context"
Expand All @@ -22,18 +18,19 @@ import (
"sync"
"time"

"github.com/aurorax-neo/go-logger"

"github.com/gorilla/websocket"
chatgpt_response_converter "aurora/conversion/response/chatgpt"
"aurora/httpclient"
"aurora/internal/tokens"
"aurora/typings"
chatgpt_types "aurora/typings/chatgpt"
official_types "aurora/typings/official"

"github.com/aurorax-neo/go-logger"
fhttp "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/bogdanfinn/tls-client/profiles"
"github.com/gin-gonic/gin"

chatgpt_response_converter "aurora/conversion/response/chatgpt"

official_types "aurora/typings/official"
"github.com/gorilla/websocket"
)

type connInfo struct {
Expand Down
6 changes: 6 additions & 0 deletions internal/proxys/proxys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ func (p *IProxy) GetIPS() int {
}

func (p *IProxy) GetProxyIP() string {
if p == nil {
return ""
}

p.lock.Lock()
defer p.lock.Unlock()

if len(p.ips) == 0 {
return ""
}

proxyIp := p.ips[0]
p.ips = append(p.ips[1:], proxyIp)
return proxyIp
Expand Down
4 changes: 4 additions & 0 deletions internal/tokens/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func NewAccessToken(tokens []*Secret) AccessToken {
}

func (a *AccessToken) GetSecret() *Secret {
if a == nil {
return &Secret{}
}

a.lock.Lock()
defer a.lock.Unlock()

Expand Down
Loading

0 comments on commit 0b13f49

Please sign in to comment.