Skip to content

Commit

Permalink
Revert "Pow2,补全伪装环境参数 (aurora-develop#218)" (aurora-develop#224)
Browse files Browse the repository at this point in the history
This reverts commit a018193.
  • Loading branch information
xiaozhou26 authored Apr 24, 2024
1 parent 896e94e commit 8b68be0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 107 deletions.
6 changes: 0 additions & 6 deletions initialize/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ 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
82 changes: 6 additions & 76 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ 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 @@ -65,14 +63,8 @@ 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 @@ -156,7 +148,6 @@ 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 @@ -269,22 +260,6 @@ 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 Down Expand Up @@ -334,10 +309,7 @@ func POSTTurnStile(client httpclient.AuroraHttpClient, secret *tokens.Secret, pr
if secret.IsFree {
header.Set("oai-device-id", secret.Token)
}
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)
response, err := client.Request(http.MethodPost, apiUrl, header, nil, payload)
if err != nil {
return &http.Response{}, err
}
Expand Down Expand Up @@ -383,45 +355,6 @@ 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 @@ -439,12 +372,11 @@ func POSTconversation(client httpclient.AuroraHttpClient, message chatgpt_types.
return &http.Response{}, err
}
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")
if chat_token.Arkose {
header.Set("openai-sentinel-arkose-token", arkoseToken)
}
Expand All @@ -460,10 +392,7 @@ func POSTconversation(client httpclient.AuroraHttpClient, message chatgpt_types.
if secret.IsFree {
header.Set("oai-device-id", secret.Token)
}
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))
response, err := client.Request(http.MethodPost, apiUrl, header, nil, bytes.NewBuffer(body_json))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -936,10 +865,11 @@ func createBaseHeader() httpclient.AuroraHeaders {
// 完善补充完整的请求头
header.Set("accept", "*/*")
header.Set("accept-language", "en-US,en;q=0.9")
header.Set("oai-language", util.RandomLanguage())
//header.Set("content-type", "application/json")
header.Set("oai-language", "en-US")
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", `"Google Chrome";v="", "Not:A-Brand";v="8", "Chromium";v=""`)
header.Set("sec-ch-ua-mobile", "?0")
header.Set("sec-ch-ua-platform", `"Linux"`)
header.Set("sec-fetch-dest", "empty")
Expand Down
17 changes: 3 additions & 14 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,17 +16,6 @@ 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 @@ -36,4 +25,4 @@ func CountToken(input string) int {
}
token := tkm.Encode(input, nil, nil)
return len(token)
}
}
11 changes: 0 additions & 11 deletions util/utils_test.go

This file was deleted.

0 comments on commit 8b68be0

Please sign in to comment.