Skip to content

Commit

Permalink
Merge pull request #21 from xqdoo00o/master
Browse files Browse the repository at this point in the history
New openai domain
  • Loading branch information
bi1101 authored May 6, 2024
2 parents 6417dab + 85aafb4 commit e34333c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
69 changes: 38 additions & 31 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,23 @@ 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"
cores = []int{8, 12, 16, 24}
screens = []int{3000, 4000, 6000}
timeLocation, _ = time.LoadLocation("Asia/Shanghai")
timeLayout = "Mon Jan 2 2006 15:04:05"
cachedHardware = 0
cachedScripts = []string{}
cachedDpl = ""
cachedRequireProof = ""
)

func init() {
cores := []int{8, 12, 16, 24}
screens := []int{3000, 4000, 6000}
rand.New(rand.NewSource(time.Now().UnixNano()))
core := cores[rand.Intn(4)]
rand.New(rand.NewSource(time.Now().UnixNano()))
screen := screens[rand.Intn(3)]
cachedHardware = core + screen
}
func newRequest(method string, url string, body io.Reader, secret *tokens.Secret, deviceId string) (*http.Request, error) {
request, err := http.NewRequest(method, url, body)
if err != nil {
Expand Down Expand Up @@ -262,22 +271,23 @@ func getParseTime() string {
now = now.In(timeLocation)
return now.Format(timeLayout) + " GMT+0800 (中国标准时间)"
}
func getDpl(proxy string) bool {
if len(cachedScripts) != 0 {
return true
func GetDpl(proxy string) {
if len(cachedScripts) > 0 {
return
}
if proxy != "" {
client.SetProxy(proxy)
}
request, err := http.NewRequest(http.MethodGet, "https://chat.openai.com/", nil)
request, err := http.NewRequest(http.MethodGet, "https://chatgpt.com/?oai-dm=1", nil)
request.Header.Set("User-Agent", userAgent)
request.Header.Set("Accept", "*/*")
request.Header.Set("Cookie", "oai-dm-tgt-c-240329=2024-04-02")
if err != nil {
return false
return
}
response, err := client.Do(request)
if err != nil {
return false
return
}
defer response.Body.Close()
doc, _ := goquery.NewDocumentFromReader(response.Body)
Expand All @@ -294,45 +304,40 @@ func getDpl(proxy string) bool {
}
}
})
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
if len(cachedScripts) == 0 {
cachedScripts = append(cachedScripts, "https://cdn.oaistatic.com/_next/static/chunks/polyfills-78c92fac7aa8fdd8.js?dpl=baf36960d05dde6d8b941194fa4093fb5cb78c6a")
cachedDpl = "dpl=baf36960d05dde6d8b941194fa4093fb5cb78c6a"
}
}
func getConfig() []interface{} {
rand.New(rand.NewSource(time.Now().UnixNano()))
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"}
return []interface{}{cachedHardware, getParseTime(), int64(4294705152), 0, userAgent, script, cachedDpl, "zh-CN", "zh-CN,en,en-GB,en-US", 0}

}
func CalcProofToken(require *ChatRequire, proxy string) string {
proof, _ := generateAnswer(require.Proof.Seed, require.Proof.Difficulty, require.Hardware, proxy)
proof := generateAnswer(require.Proof.Seed, require.Proof.Difficulty, 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(hardware)
func generateAnswer(seed string, diff string, proxy string) string {
GetDpl(proxy)
config := getConfig()
diffLen := len(diff)
hasher := sha3.New512()
for i := 0; i < 1000000; i++ {
for i := 0; i < 500000; i++ {
config[3] = i
config[9] = (i + 2) / 2
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])[:diffLen] <= diff {
return base, config[0].(int)
return base
}
}
return "wQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + base64.StdEncoding.EncodeToString([]byte(`"`+seed+`"`)), config[0].(int)
return "wQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + base64.StdEncoding.EncodeToString([]byte(`"`+seed+`"`))
}

type ChatRequire struct {
Expand All @@ -342,15 +347,16 @@ 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)
}
proof, hardware := generateAnswer(strconv.FormatFloat(rand.Float64(), 'f', -1, 64), "0", 0, proxy)
body := bytes.NewBuffer([]byte(`{"p":"` + "gAAAAAC" + proof + `"}`))
if cachedRequireProof == "" {
cachedRequireProof = "gAAAAAC" + generateAnswer(strconv.FormatFloat(rand.Float64(), 'f', -1, 64), "0", proxy)
}
body := bytes.NewBuffer([]byte(`{"p":"` + cachedRequireProof + `"}`))
var apiUrl string
if secret.Token == "" {
apiUrl = "https://chat.openai.com/backend-anon/sentinel/chat-requirements"
Expand All @@ -372,7 +378,6 @@ func CheckRequire(secret *tokens.Secret, deviceId string, proxy string) *ChatReq
if err != nil {
return nil
}
require.Hardware = hardware
return &require
}

Expand Down Expand Up @@ -444,6 +449,8 @@ func POSTconversation(message chatgpt_types.ChatGPTRequest, secret *tokens.Secre
if proofToken != "" {
request.Header.Set("Openai-Sentinel-Proof-Token", proofToken)
}
request.Header.Set("Origin", "https://chat.openai.com")
request.Header.Set("Referer", "https://chat.openai.com/c/"+message.ConversationID)
if err != nil {
return &http.Response{}, err
}
Expand Down
2 changes: 2 additions & 0 deletions typings/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ChatGPTRequest struct {
Model string `json:"model"`
HistoryAndTrainingDisabled bool `json:"history_and_training_disabled"`
ArkoseToken string `json:"arkose_token,omitempty"`
WebsocketRequestId string `json:"websocket_request_id"`
}
type FileResp struct {
File_id string `json:"file_id"`
Expand Down Expand Up @@ -230,6 +231,7 @@ func NewChatGPTRequest() ChatGPTRequest {
Model: "text-davinci-002-render-sha",
HistoryAndTrainingDisabled: disable_history,
ConversationMode: ChatGPTConvMode{Kind: "primary_assistant"},
WebsocketRequestId: uuid.NewString(),
}
}

Expand Down

0 comments on commit e34333c

Please sign in to comment.