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.

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

---------

Co-authored-by: xiaofei <[email protected]>
  • Loading branch information
learnLi and xiaozhou26 authored Apr 4, 2024
1 parent 83f85e6 commit bf3d252
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -220,7 +221,7 @@ func InitTurnStile(client *httpclient.RestyClient, secret *tokens.Secret, proxy
defer poolMutex.Unlock()
currTurnToken := TurnStilePool[secret.Token]
if currTurnToken == nil || currTurnToken.ExpireAt.Before(time.Now()) {
result, err := POSTTurnStile(client, secret, proxy)
result, err := POSTTurnStile(client, secret, proxy, 0)
if err != nil {
return nil, 500, err
}
Expand All @@ -235,7 +236,7 @@ func InitTurnStile(client *httpclient.RestyClient, secret *tokens.Secret, proxy
return currTurnToken, 0, nil
}

func POSTTurnStile(client *httpclient.RestyClient, secret *tokens.Secret, proxy string) (*chatgpt_types.RequirementsResponse, error) {
func POSTTurnStile(client *httpclient.RestyClient, secret *tokens.Secret, proxy string, retry int) (*chatgpt_types.RequirementsResponse, error) {
if proxy != "" {
client.Client.SetProxy(proxy)
}
Expand All @@ -256,9 +257,18 @@ func POSTTurnStile(client *httpclient.RestyClient, secret *tokens.Secret, proxy
return nil, err
}
if response.StatusCode() != 200 {
if response.StatusCode() == 401 {
if retry > 3 {
return nil, errors.New("error sending request")
}
time.Sleep(time.Second * 5)
secret.Token = uuid.NewString()
return POSTTurnStile(client, secret, proxy, retry+1)
}
logger.Logger.Debug(fmt.Sprint("POSTTurnStile: ", response.String()))
return nil, errors.New("error sending request")
}

return result, err
}

Expand Down

0 comments on commit bf3d252

Please sign in to comment.