Skip to content

Commit

Permalink
Pow2 (#236)
Browse files Browse the repository at this point in the history
* 提取基础请求头,普号TurnStile不做缓存。

* Update tag_name in build.yml to v2.0.6

* 补全伪装环境参数

* Fix content-type header in POSTconversation function

* fix

* fix

* chore: Update go.mod dependencies

---------

Co-authored-by: xiaozhou26 <xiaoxiaofeihh@foxmail.com>
Co-authored-by: xiaofei <122727418+xiaozhou26@users.noreply.github.com>
  • Loading branch information
3 people authored May 14, 2024
1 parent 5e257b7 commit 0fa6cf0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
6 changes: 3 additions & 3 deletions conversion/requests/chatgpt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (
func ConvertAPIRequest(api_request official_types.APIRequest, secret *tokens.Secret, requireArk bool, proxy string) chatgpt_types.ChatGPTRequest {
chatgpt_request := chatgpt_types.NewChatGPTRequest()
var api_version int
if secret.PUID == "" {
api_request.Model = "gpt-3.5"
}
if strings.HasPrefix(api_request.Model, "gpt-3.5") {
api_version = 3
chatgpt_request.Model = "text-davinci-002-render-sha"
} else if strings.HasPrefix(api_request.Model, "gpt-4") {
api_version = 4
chatgpt_request.Model = api_request.Model
// Cover some models like gpt-4-32k
if api_request.Model == "gpt-4o" {
chatgpt_request.Model = "gpt-4o"
}
if len(api_request.Model) >= 7 && api_request.Model[6] >= 48 && api_request.Model[6] <= 57 {
chatgpt_request.Model = "gpt-4"
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/EDDYCJY/fake-useragent v0.2.0
github.com/PuerkitoBio/goquery v1.9.1
github.com/acheong08/endless v0.0.0-20230615162514-90545c7793fd
github.com/bogdanfinn/fhttp v0.5.27
github.com/bogdanfinn/tls-client v1.7.2
Expand All @@ -14,10 +15,10 @@ require (
github.com/joho/godotenv v1.5.1
github.com/pkoukk/tiktoken-go v0.1.6
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c
golang.org/x/crypto v0.21.0
)

require (
github.com/PuerkitoBio/goquery v1.9.1 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/bogdanfinn/utls v1.6.1 // indirect
Expand Down Expand Up @@ -47,7 +48,6 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions httpclient/Iaurorahttpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
type AuroraHttpClient interface {
Request(method HttpMethod, url string, headers AuroraHeaders, cookies []*http.Cookie, body io.Reader) (*http.Response, error)
SetProxy(url string) error
SetCookies(rawUrl string, cookies []*http.Cookie)
GetCookies(rawUrl string) []*http.Cookie
}

type HttpMethod string
Expand Down
27 changes: 26 additions & 1 deletion httpclient/bogdanfinn/tls_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewStdClient() *TlsClient {
client, _ := tls_client.NewHttpClient(tls_client.NewNoopLogger(), []tls_client.HttpClientOption{
tls_client.WithCookieJar(tls_client.NewCookieJar()),
tls_client.WithTimeoutSeconds(600),
tls_client.WithClientProfile(profiles.Safari_15_6_1),
tls_client.WithClientProfile(profiles.Chrome_117),
}...)

stdClient := &TlsClient{Client: client}
Expand Down Expand Up @@ -128,3 +128,28 @@ func (t *TlsClient) SetCookies(rawUrl string, cookies []*http.Cookie) {
}
t.Client.GetCookieJar().SetCookies(u, fcookies)
}

func (t *TlsClient) GetCookies(rawUrl string) []*http.Cookie {
currUrl, err := url.Parse(rawUrl)
if err != nil {
return nil
}

var cookies []*http.Cookie
for _, c := range t.Client.GetCookies(currUrl) {
cookies = append(cookies, &http.Cookie{
Name: c.Name,
Value: c.Value,
Path: c.Path,
Domain: c.Domain,
Expires: c.Expires,
RawExpires: c.RawExpires,
MaxAge: c.MaxAge,
Secure: c.Secure,
HttpOnly: c.HttpOnly,
SameSite: http.SameSite(c.SameSite),
})
}
return cookies
}

9 changes: 0 additions & 9 deletions initialize/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,6 @@ func (h *Handler) engines(c *gin.Context) {
Object: "list",
}
var resModelList []ResData
if len(resp.Models) > 2 {
res_data := ResData{
ID: "gpt-4-mobile",
Object: "model",
Created: 1685474247,
OwnedBy: "openai",
}
resModelList = append(resModelList, res_data)
}
for _, model := range resp.Models {
res_data := ResData{
ID: model.Slug,
Expand Down
12 changes: 10 additions & 2 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var (

func GetDpl(client httpclient.AuroraHttpClient, proxy string) {
requestURL := strings.Replace(BaseURL, "/backend-anon", "", 1) + "/?oai-dm=1"

if len(cachedScripts) > 0 {
return
}
Expand All @@ -85,6 +86,7 @@ func GetDpl(client httpclient.AuroraHttpClient, proxy string) {
}
header := createBaseHeader()
response, err := client.Request(http.MethodGet, requestURL, header, nil, nil)

if err != nil {
return
}
Expand All @@ -104,10 +106,13 @@ func GetDpl(client httpclient.AuroraHttpClient, proxy string) {
}
})
if BasicCookies == nil {
for _, cookie := range response.Cookies() {
for _, cookie := range client.GetCookies("https://chatgpt.com") {
if cookie.Name == "oai-did" {
continue
}
if cookie.Name == "__Secure-next-auth.callback-url" {
cookie.Value = "https://chatgpt.com"
}
BasicCookies = append(BasicCookies, cookie)
}
}
Expand Down Expand Up @@ -277,7 +282,8 @@ func getParseTime() string {
func getConfig() []interface{} {
rand.New(rand.NewSource(time.Now().UnixNano()))
script := cachedScripts[rand.Intn(len(cachedScripts))]
return []interface{}{cachedHardware, getParseTime(), int64(4294705152), 0, userAgent, script, cachedDpl, "zh-CN", "zh-CN,en,en-GB,en-US", 0}

return []interface{}{cachedHardware, getParseTime(), int64(4294705152), 0, userAgent, script, cachedDpl, "zh-CN", "zh-CN,en,en-GB,en-US", 0, "userAgentData−[object NavigatorUAData]", "_reactListening4pw0k9ttxw", "onpopstate"}

}
func CalcProofToken(client httpclient.AuroraHttpClient, require *ChatRequire, proxy string) string {
Expand Down Expand Up @@ -952,11 +958,13 @@ func createBaseHeader() httpclient.AuroraHeaders {
header.Set("origin", "https://chatgpt.com")
header.Set("referer", "https://chatgpt.com/")
header.Set("sec-ch-ua", `"Google Chrome";v="120", "Not:A-Brand";v="120", "Chromium";v="99"`)
header.Set("priority", "u=1, i")
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("Connection", "close")
header.Set("user-agent", userAgent)
return header
}

0 comments on commit 0fa6cf0

Please sign in to comment.