Skip to content

Commit

Permalink
Merge branch 'xqdoo00o-master' into Add-Gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
bi1101 committed May 1, 2024
2 parents 293e688 + ab70c5a commit 56f92c4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.1
toolchain go1.22.0

require (
github.com/PuerkitoBio/goquery v1.9.1
github.com/acheong08/endless v0.0.0-20230615162514-90545c7793fd
github.com/bogdanfinn/fhttp v0.5.28
github.com/bogdanfinn/tls-client v1.7.4
Expand All @@ -17,7 +18,7 @@ require (
github.com/joho/godotenv v1.5.1
github.com/sashabaranov/go-openai v1.21.0
github.com/tidwall/gjson v1.17.1
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240420184134-706a5d60f6c1
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240425072142-e599e4049d02
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c
github.com/zhu327/gemini-openai-proxy v0.0.0-20240328042054-d2c6c4cdff01
golang.org/x/crypto v0.22.0
Expand All @@ -32,6 +33,7 @@ require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/longrunning v0.5.6 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/bogdanfinn/utls v1.6.1 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
Expand Down
11 changes: 9 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.112.2 h1:ZaGT6LiG7dBzi6zNOvVZwacaXlmf3lRqnC4DQzqyRQw=
cloud.google.com/go v0.112.2/go.mod h1:iEqjp//KquGIJV/m+Pk3xecgKNhV+ry+vVTsy4TbDms=
Expand All @@ -12,6 +14,8 @@ github.com/acheong08/endless v0.0.0-20230615162514-90545c7793fd h1:oIpfrRhD7Jus4
github.com/acheong08/endless v0.0.0-20230615162514-90545c7793fd/go.mod h1:0yO7neMeJLvKk/B/fq5votDY8rByrOPDubpvU+6saKo=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/bogdanfinn/fhttp v0.5.28 h1:G6thT8s8v6z1IuvXMUsX9QKy3ZHseTQTzxuIhSiaaAw=
github.com/bogdanfinn/fhttp v0.5.28/go.mod h1:oJiYPG3jQTKzk/VFmogH8jxjH5yiv2rrOH48Xso2lrE=
github.com/bogdanfinn/tls-client v1.7.4 h1:8cn2/egs0LmqA7RFgrQh9Ww074lyQkeMKmsYl9JluTQ=
Expand Down Expand Up @@ -176,8 +180,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240420184134-706a5d60f6c1 h1:pSzLMgRZ8G9f6qytVm/OL97hD+qSTvhceSzDhW7SRbY=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240420184134-706a5d60f6c1/go.mod h1:XHxG7wI3Erk4nS0rwjXVF3FllYW5QxsRynnJ0GqAMCQ=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240425072142-e599e4049d02 h1:l+ADAnV1KDjZkY0ccAvVYzSCGbA90xitA1aP+7yR0Yc=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240425072142-e599e4049d02/go.mod h1:XHxG7wI3Erk4nS0rwjXVF3FllYW5QxsRynnJ0GqAMCQ=
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c h1:nj17XsSTwprsZUDXLldOUZmqz7VlHsLCeXXFOE6Q+Mk=
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c/go.mod h1:7aCyoW5MHDUsoooMVLqKe0F7W9HMPUvDG3bXqw++8XA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down Expand Up @@ -225,6 +229,7 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
Expand All @@ -250,6 +255,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand All @@ -258,6 +264,7 @@ golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
Expand Down
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func nightmare(c *gin.Context) {
}
var proofToken string
if chat_require.Proof.Required {
proofToken = chatgpt.CalcProofToken(chat_require.Proof.Seed, chat_require.Proof.Difficulty)
proofToken = chatgpt.CalcProofToken(chat_require, proxy_url)
}
// Convert the chat request to a ChatGPT request
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request, account, &secret, deviceId, chat_require.Arkose.Required, chat_require.Arkose.DX, proxy_url)
Expand Down
82 changes: 65 additions & 17 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gorilla/websocket"
"golang.org/x/crypto/sha3"

"github.com/PuerkitoBio/goquery"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/bogdanfinn/tls-client/profiles"
Expand Down Expand Up @@ -53,11 +54,12 @@ var (
FILES_REVERSE_PROXY = os.Getenv("FILES_REVERSE_PROXY")
connPool = map[string][]*connInfo{}
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
answers = map[string]string{}
cores = []int{8, 12, 16, 24}
screens = []int{3000, 4000, 6000}
timeLocation, _ = time.LoadLocation("Asia/Shanghai")
timeLayout = "Mon Jan 2 2006 15:04:05"
cachedScripts = []string{}
cachedDpl = ""
)

func newRequest(method string, url string, body io.Reader, secret *tokens.Secret, deviceId string) (*http.Request, error) {
Expand Down Expand Up @@ -236,34 +238,77 @@ func getParseTime() string {
now = now.In(timeLocation)
return now.Format(timeLayout) + " GMT+0800 (中国标准时间)"
}
func getConfig() []interface{} {
rand.New(rand.NewSource(time.Now().UnixNano()))
core := cores[rand.Intn(4)]
func getDpl(proxy string) bool {
if len(cachedScripts) != 0 {
return true
}
if proxy != "" {
client.SetProxy(proxy)
}
request, err := http.NewRequest(http.MethodGet, "https://chat.openai.com/", nil)
request.Header.Set("User-Agent", userAgent)
request.Header.Set("Accept", "*/*")
if err != nil {
return false
}
response, err := client.Do(request)
if err != nil {
return false
}
defer response.Body.Close()
doc, _ := goquery.NewDocumentFromReader(response.Body)
cachedScripts = nil
doc.Find("script[src]").Each(func(i int, s *goquery.Selection) {
src, exists := s.Attr("src")
if exists {
cachedScripts = append(cachedScripts, src)
if cachedDpl == "" {
idx := strings.Index(src, "dpl")
if idx >= 0 {
cachedDpl = src[idx:]
}
}
}
})
return len(cachedScripts) != 0
}
func getConfig(hardware int) []interface{} {
if hardware == 0 {
rand.New(rand.NewSource(time.Now().UnixNano()))
core := cores[rand.Intn(4)]
rand.New(rand.NewSource(time.Now().UnixNano()))
screen := screens[rand.Intn(3)]
hardware = core + screen
}
rand.New(rand.NewSource(time.Now().UnixNano()))
screen := screens[rand.Intn(3)]
return []interface{}{core + screen, getParseTime(), int64(4294705152), 0, userAgent}
script := cachedScripts[rand.Intn(len(cachedScripts))]
return []interface{}{hardware, getParseTime(), int64(4294705152), 0, userAgent, script, cachedDpl, "zh-CN", "zh-CN,en,en-GB,en-US"}

}
func CalcProofToken(seed string, diff string) string {
if answers[seed] != "" {
return answers[seed]
func CalcProofToken(require *ChatRequire, proxy string) string {
proof, _ := generateAnswer(require.Proof.Seed, require.Proof.Difficulty, require.Hardware, proxy)
return "gAAAAAB" + proof
}

func generateAnswer(seed string, diff string, hardware int, proxy string) (string, int) {
if !getDpl(proxy) {
return "wQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + base64.StdEncoding.EncodeToString([]byte(`"`+seed+`"`)), 0
}
config := getConfig()
diffLen := len(diff) / 2
config := getConfig(hardware)
diffLen := len(diff)
hasher := sha3.New512()
for i := 0; i < 100000; i++ {
for i := 0; i < 1000000; i++ {
config[3] = i
json, _ := json.Marshal(config)
base := base64.StdEncoding.EncodeToString(json)
hasher.Write([]byte(seed + base))
hash := hasher.Sum(nil)
hasher.Reset()
if hex.EncodeToString(hash[:diffLen]) <= diff {
answers[seed] = "gAAAAAB" + base
return answers[seed]
if hex.EncodeToString(hash[:diffLen])[:diffLen] <= diff {
return base, config[0].(int)
}
}
return "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + base64.StdEncoding.EncodeToString([]byte(`"`+seed+`"`))
return "wQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + base64.StdEncoding.EncodeToString([]byte(`"`+seed+`"`)), config[0].(int)
}

type ChatRequire struct {
Expand All @@ -273,13 +318,15 @@ type ChatRequire struct {
Required bool `json:"required"`
DX string `json:"dx,omitempty"`
} `json:"arkose"`
Hardware int
}

func CheckRequire(secret *tokens.Secret, deviceId string, proxy string) *ChatRequire {
if proxy != "" {
client.SetProxy(proxy)
}
body := bytes.NewBuffer([]byte(`{}`))
proof, hardware := generateAnswer(strconv.FormatFloat(rand.Float64(), 'f', -1, 64), "0", 0, proxy)
body := bytes.NewBuffer([]byte(`{"p":"` + "gAAAAAC" + proof + `"}`))
var apiUrl string
if secret.Token == "" {
apiUrl = "https://chat.openai.com/backend-anon/sentinel/chat-requirements"
Expand All @@ -301,6 +348,7 @@ func CheckRequire(secret *tokens.Secret, deviceId string, proxy string) *ChatReq
if err != nil {
return nil
}
require.Hardware = hardware
return &require
}

Expand Down

0 comments on commit 56f92c4

Please sign in to comment.