Skip to content

Commit

Permalink
Merge branch 'aurora-develop:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
taurusduan authored Apr 22, 2024
2 parents 16fe4cd + 896e94e commit a1589a0
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GHCR_PAT }}
with:
tag_name: v2.0.5
tag_name: v2.0.7
files: |
aurora-linux-amd64.tar.gz
aurora-windows-amd64.tar.gz
Expand Down
3 changes: 1 addition & 2 deletions initialize/auth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package initialize

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

"aurora/internal/tokens"

"github.com/google/uuid"
)

Expand Down
6 changes: 6 additions & 0 deletions initialize/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ func (h *Handler) nightmare(c *gin.Context) {

uid := uuid.NewString()
client := bogdanfinn.NewStdClient()
err = chatgpt.InitCfClearance(proxyUrl)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
c.Abort()
return
}
turnStile, status, err := chatgpt.InitTurnStile(client, secret, proxyUrl)
if err != nil {
c.JSON(status, gin.H{
Expand Down
130 changes: 105 additions & 25 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"aurora/typings"
chatgpt_types "aurora/typings/chatgpt"
official_types "aurora/typings/official"
"aurora/util"
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
Expand Down Expand Up @@ -63,8 +65,14 @@ var (
screens = []int{3000, 4000, 6000}
timeLocation, _ = time.LoadLocation("Asia/Shanghai")
timeLayout = "Mon Jan 2 2006 15:04:05"
cf_clearance *CfClearance
)

type CfClearance struct {
cfClearance string
ExpireAt time.Time
}

func getWSURL(client httpclient.AuroraHttpClient, token string, retry int) (string, error) {
requestURL := BaseURL + "/register-websocket"
header := make(httpclient.AuroraHeaders)
Expand Down Expand Up @@ -148,6 +156,7 @@ func UnlockSpecConn(token string, uuid string) {
}
}
}

func InitWSConn(client httpclient.AuroraHttpClient, token string, uuid string, proxy string) error {
connInfo := findAvailConn(token, uuid)
conn := connInfo.conn
Expand Down Expand Up @@ -260,6 +269,22 @@ func CalcProofToken(seed string, diff string) string {
return "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + base64.StdEncoding.EncodeToString([]byte(`"`+seed+`"`))
}

func InitCfClearance(proxy string) error {
poolMutex.Lock()
defer poolMutex.Unlock()
if cf_clearance == nil || cf_clearance.ExpireAt.Before(time.Now()) {
resp, err := getCf(proxy)
if err != nil {
return err
}
cf_clearance = &CfClearance{
cfClearance: resp,
ExpireAt: time.Now().Add(30 * time.Minute),
}
}
return nil
}

func InitTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, proxy string) (*TurnStile, int, error) {
poolMutex.Lock()
defer poolMutex.Unlock()
Expand All @@ -281,12 +306,16 @@ func InitTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, pr
currTurnToken = &TurnStile{
TurnStileToken: result.Token,
Arkose: result.Arkose.Required,
ExpireAt: time.Now().Add(1 * time.Second),
ExpireAt: time.Now().Add(5 * time.Second),
}
if result.Proof.Required == true {
currTurnToken.ProofOfWorkToken = CalcProofToken(result.Proof.Seed, result.Proof.Difficulty)
}
TurnStilePool[secret.Token] = currTurnToken

// 如果是免登账号,将其放入池子
if secret.IsFree {
TurnStilePool[secret.Token] = currTurnToken
}
}
return currTurnToken, 0, nil
}
Expand All @@ -296,20 +325,19 @@ func POSTTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, pr
}
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)
header.Set("Accept", "*/*")
header.Set("oai-language", "en-US")
header.Set("origin", "https://chat.openai.com")
header.Set("referer", "https://chat.openai.com/")

header := createBaseHeader()
header.Set("content-type", "application/json")
if !secret.IsFree && secret.Token != "" {
header.Set("Authorization", "Bearer "+secret.Token)
}
if secret.IsFree {
header.Set("oai-device-id", secret.Token)
}
response, err := client.Request(http.MethodPost, apiUrl, header, nil, payload)
var cookie *http.Cookie
// 补全伪装环境参数,无实质用处。
cookie = &http.Cookie{Name: "cf_clearance", Value: cf_clearance.cfClearance}
response, err := client.Request(http.MethodPost, apiUrl, header, []*http.Cookie{cookie}, payload)
if err != nil {
return &http.Response{}, err
}
Expand All @@ -334,15 +362,11 @@ type urlAttr struct {
func getURLAttribution(client httpclient.AuroraHttpClient, access_token string, puid string, url string) string {
requestURL := BaseURL + "/attributions"
payload := bytes.NewBuffer([]byte(`{"urls":["` + url + `"]}`))
header := make(httpclient.AuroraHeaders)
header := createBaseHeader()
if puid != "" {
header.Set("Cookie", "_puid="+puid+";")
}
header.Set("Content-Type", "application/json")
header.Set("User-Agent", userAgent)
header.Set("Oai-Language", "en-US")
header.Set("Origin", "https://chat.openai.com")
header.Set("Referer", "https://chat.openai.com/")
if access_token != "" {
header.Set("Authorization", "Bearer "+access_token)
}
Expand All @@ -359,6 +383,45 @@ func getURLAttribution(client httpclient.AuroraHttpClient, access_token string,
return attr.Attribution
}

func getCf(proxy string) (string, error) {
client := &http.Client{}
if proxy != "" {
client.Transport = &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(proxy)
},
}
}

var data = strings.NewReader(`{}`)
req, err := http.NewRequest("POST", "https://chat.openai.com/cdn-cgi/challenge-platform/h/b/jsd/r/"+util.RandomHexadecimalString(), data)
if err != nil {
return "", err
}
req.Header.Set("accept", "*/*")
req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("content-type", "application/json")
req.Header.Set("origin", "https://chat.openai.com")
req.Header.Set("sec-ch-ua", `"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.0.0 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.New("failed to get cf clearance")
}
for _, cookie := range resp.Cookies() {
if cookie.Name == "cf_clearance" {
return cookie.Value, nil
}
}
return "", errors.New("ailed to get cf clearance")
}

func POSTconversation(client httpclient.AuroraHttpClient, message chatgpt_types.ChatGPTRequest, secret *tokens.Secret, chat_token *TurnStile, proxy string) (*http.Response, error) {
if proxy != "" {
client.SetProxy(proxy)
Expand All @@ -375,17 +438,13 @@ func POSTconversation(client httpclient.AuroraHttpClient, message chatgpt_types.
if err != nil {
return &http.Response{}, err
}
header := make(httpclient.AuroraHeaders)
header := createBaseHeader()
header.Set("content-type", "application/json")

// Clear cookies
if secret.PUID != "" {
header.Set("Cookie", "_puid="+secret.PUID+";")
}
header.Set("Content-Type", "application/json")
header.Set("User-Agent", userAgent)
header.Set("Accept", "*/*")
header.Set("oai-language", "en-US")
header.Set("origin", "https://chat.openai.com")
header.Set("referer", "https://chat.openai.com/")
if chat_token.Arkose {
header.Set("openai-sentinel-arkose-token", arkoseToken)
}
Expand All @@ -401,7 +460,10 @@ func POSTconversation(client httpclient.AuroraHttpClient, message chatgpt_types.
if secret.IsFree {
header.Set("oai-device-id", secret.Token)
}
response, err := client.Request(http.MethodPost, apiUrl, header, nil, bytes.NewBuffer(body_json))
var cookie *http.Cookie
// 补全伪装环境参数,无实质用处。
cookie = &http.Cookie{Name: "cf_clearance", Value: cf_clearance.cfClearance}
response, err := client.Request(http.MethodPost, apiUrl, header, []*http.Cookie{cookie}, bytes.NewBuffer(body_json))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -797,7 +859,7 @@ func GETTokenForRefreshToken(client httpclient.AuroraHttpClient, refresh_token s
if proxy != "" {
client.SetProxy(proxy)
}
url := "https://auth0.openai.com/oauth/token"
rawUrl := "https://auth0.openai.com/oauth/token"

data := map[string]interface{}{
"redirect_uri": "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
Expand All @@ -818,7 +880,7 @@ func GETTokenForRefreshToken(client httpclient.AuroraHttpClient, refresh_token s
header.Set("Content-Type", "application/json")
header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
header.Set("Accept", "*/*")
resp, err := client.Request(http.MethodPost, url, header, nil, bytes.NewBuffer(reqBody))
resp, err := client.Request(http.MethodPost, rawUrl, header, nil, bytes.NewBuffer(reqBody))
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -868,3 +930,21 @@ func parseCookies(cookies []*http.Cookie) map[string]string {
}
return cookieDict
}

func createBaseHeader() httpclient.AuroraHeaders {
header := make(httpclient.AuroraHeaders)
// 完善补充完整的请求头
header.Set("accept", "*/*")
header.Set("accept-language", "en-US,en;q=0.9")
header.Set("oai-language", util.RandomLanguage())
header.Set("origin", "https://chat.openai.com")
header.Set("referer", "https://chat.openai.com/")
header.Set("sec-ch-ua", `"Google Chrome";v="44", "Not:A-Brand";v="8", "Chromium";v="44"`)
header.Set("sec-ch-ua-mobile", "?0")
header.Set("sec-ch-ua-platform", `"Linux"`)
header.Set("sec-fetch-dest", "empty")
header.Set("sec-fetch-mode", "cors")
header.Set("sec-fetch-site", "same-origin")
header.Set("user-agent", userAgent)
return header
}
17 changes: 14 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package util

import (
"github.com/pkoukk/tiktoken-go"
"log/slog"
"math/rand"
"time"
"log/slog"
"github.com/pkoukk/tiktoken-go"
)

func RandomLanguage() string {
Expand All @@ -16,6 +16,17 @@ func RandomLanguage() string {
randomIndex := rand.Intn(len(languages))
return languages[randomIndex]
}

func RandomHexadecimalString() string {
rand.Seed(time.Now().UnixNano())
const charset = "0123456789abcdef"
const length = 16 // The length of the string you want to generate
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
func CountToken(input string) int {
encoding := "gpt-3.5-turbo"
tkm, err := tiktoken.EncodingForModel(encoding)
Expand All @@ -25,4 +36,4 @@ func CountToken(input string) int {
}
token := tkm.Encode(input, nil, nil)
return len(token)
}
}
11 changes: 11 additions & 0 deletions util/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

import (
"fmt"
"testing"
)

func TestRandomHexadecimalString(t *testing.T) {
var str = RandomHexadecimalString()
fmt.Println(str)
}

0 comments on commit a1589a0

Please sign in to comment.