Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 修复使用空指针bug,增加前置请求拦截。

* 修复uuid401失效,更换uuid

* 移除随机ua,优化错误返回码。
  • Loading branch information
learnLi authored Apr 9, 2024
1 parent 6baafd3 commit 3ba65c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
32 changes: 2 additions & 30 deletions initialize/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"aurora/internal/proxys"
"aurora/internal/tokens"
officialtypes "aurora/typings/official"
"aurora/util"
browser "github.com/EDDYCJY/fake-useragent"
fhttp "github.com/bogdanfinn/fhttp"
"os"
"strings"

Expand Down Expand Up @@ -40,10 +37,6 @@ func (h *Handler) refresh(c *gin.Context) {
}
proxyUrl := h.proxy.GetProxyIP()
client := bogdanfinn.NewStdClient()
client.ReqBefore = func(r *fhttp.Request) error {
r.Header.Set("User-Agent", browser.Random())
return nil
}
openaiRefreshToken, status, err := chatgpt.GETTokenForRefreshToken(client, refreshToken.RefreshToken, proxyUrl)
if err != nil {
c.JSON(400, gin.H{
Expand Down Expand Up @@ -71,10 +64,6 @@ func (h *Handler) session(c *gin.Context) {
}
proxy_url := h.proxy.GetProxyIP()
client := bogdanfinn.NewStdClient()
client.ReqBefore = func(r *fhttp.Request) error {
r.Header.Set("User-Agent", browser.Random())
return nil
}
openaiSessionToken, status, err := chatgpt.GETTokenForSessionToken(client, sessionToken.SessionToken, proxy_url)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
Expand Down Expand Up @@ -113,10 +102,6 @@ func (h *Handler) refresh_handler(c *gin.Context) {

proxy_url := h.proxy.GetProxyIP()
client := bogdanfinn.NewStdClient()
client.ReqBefore = func(r *fhttp.Request) error {
r.Header.Set("User-Agent", browser.Random())
return nil
}
openaiRefreshToken, status, err := chatgpt.GETTokenForRefreshToken(client, refresh_token.RefreshToken, proxy_url)
if err != nil {
c.JSON(400, gin.H{
Expand Down Expand Up @@ -144,10 +129,6 @@ func (h *Handler) session_handler(c *gin.Context) {
}
proxy_url := h.proxy.GetProxyIP()
client := bogdanfinn.NewStdClient()
client.ReqBefore = func(r *fhttp.Request) error {
r.Header.Set("User-Agent", browser.Random())
return nil
}
openaiSessionToken, status, err := chatgpt.GETTokenForSessionToken(client, session_token.SessionToken, proxy_url)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
Expand Down Expand Up @@ -190,11 +171,6 @@ func (h *Handler) nightmare(c *gin.Context) {

uid := uuid.NewString()
client := bogdanfinn.NewStdClient()
client.ReqBefore = func(r *fhttp.Request) error {
r.Header.Set("User-Agent", browser.Random())
r.Header.Set("oai-language", util.RandomLanguage())
return nil
}
turnStile, status, err := chatgpt.InitTurnStile(client, secret, proxyUrl)
if err != nil {
c.JSON(status, gin.H{
Expand All @@ -221,7 +197,7 @@ func (h *Handler) nightmare(c *gin.Context) {

if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
"error": "request conversion error",
})
return
}
Expand Down Expand Up @@ -254,7 +230,7 @@ func (h *Handler) nightmare(c *gin.Context) {

if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
"error": "request conversion error",
})
return
}
Expand Down Expand Up @@ -290,10 +266,6 @@ func (h *Handler) engines(c *gin.Context) {
return
}
client := bogdanfinn.NewStdClient()
client.ReqBefore = func(r *fhttp.Request) error {
r.Header.Set("User-Agent", browser.Random())
return nil
}
resp, status, err := chatgpt.GETengines(client, secret, proxyUrl)
if err != nil {
c.JSON(500, gin.H{
Expand Down
12 changes: 7 additions & 5 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func InitTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, pr
defer poolMutex.Unlock()
currTurnToken := TurnStilePool[secret.Token]
if currTurnToken == nil || currTurnToken.ExpireAt.Before(time.Now()) {
response, err := POSTTurnStile(client, secret, proxy)
response, err := POSTTurnStile(client, secret, proxy, 0)
if err != nil {
return nil, http.StatusInternalServerError, err
}
Expand All @@ -240,13 +240,12 @@ func InitTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, pr
}
return currTurnToken, 0, nil
}
func POSTTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, proxy string) (*http.Response, error) {
func POSTTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, proxy string, retry int) (*http.Response, error) {
if proxy != "" {
client.SetProxy(proxy)
}
apiUrl := BaseURL + "/sentinel/chat-requirements"
payload := strings.NewReader(`{"conversation_mode_kind":"primary_assistant"}`)

header := make(httpclient.AuroraHeaders)
header.Set("Content-Type", "application/json")
header.Set("User-Agent", userAgent)
Expand All @@ -265,9 +264,12 @@ func POSTTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, pr
return &http.Response{}, err
}
if response.StatusCode == 401 && secret.IsFree {
if retry > 3 {
return response, err
}
time.Sleep(time.Second * 2)
secret.Token = uuid.NewString()
return response, err
return POSTTurnStile(client, secret, proxy, retry+1)
}
return response, err
}
Expand Down Expand Up @@ -416,7 +418,7 @@ func Handle_request_error(c *gin.Context, response *http.Response) bool {
if err != nil {
// Read response body
body, _ := io.ReadAll(response.Body)
c.JSON(500, gin.H{"error": gin.H{
c.JSON(response.StatusCode, gin.H{"error": gin.H{
"message": "Unknown error",
"type": "internal_server_error",
"param": nil,
Expand Down

0 comments on commit 3ba65c0

Please sign in to comment.