Skip to content

Commit

Permalink
may no need wss further
Browse files Browse the repository at this point in the history
  • Loading branch information
xqdoo00o committed May 20, 2024
1 parent 750495c commit cfe288f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 61 deletions.
28 changes: 1 addition & 27 deletions conversion/requests/chatgpt/convert.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package chatgpt

import (
"fmt"
"freechatgpt/internal/tokens"
chatgpt_types "freechatgpt/typings/chatgpt"
official_types "freechatgpt/typings/official"
"regexp"
"strings"

arkose "github.com/xqdoo00o/funcaptcha"
)

var gpt4Regexp = regexp.MustCompile(`^(gpt-4|gpt-4o)(?:-gizmo-g-(\w+))?$`)

func ConvertAPIRequest(api_request official_types.APIRequest, account string, secret *tokens.Secret, deviceId string, requireArk bool, dx string, proxy string) chatgpt_types.ChatGPTRequest {
func ConvertAPIRequest(api_request official_types.APIRequest, account string, secret *tokens.Secret, deviceId string, proxy string) chatgpt_types.ChatGPTRequest {
chatgpt_request := chatgpt_types.NewChatGPTRequest()
var api_version int
if strings.HasPrefix(api_request.Model, "gpt-3.5") {
Expand All @@ -32,14 +29,6 @@ func ConvertAPIRequest(api_request official_types.APIRequest, account string, se
chatgpt_request.ConversationMode.GizmoId = "g-" + matches[2]
}
}
if requireArk {
token, err := arkose.GetOpenAIToken(api_version, secret.PUID, dx, proxy)
if err == nil {
chatgpt_request.ArkoseToken = token
} else {
fmt.Println("Error getting Arkose token: ", err)
}
}
ifMultimodel := api_version == 4
for _, api_message := range api_request.Messages {
if api_message.Role == "system" {
Expand All @@ -49,18 +38,3 @@ func ConvertAPIRequest(api_request official_types.APIRequest, account string, se
}
return chatgpt_request
}

func RenewTokenForRequest(request *chatgpt_types.ChatGPTRequest, puid string, dx string, proxy string) {
var api_version int
if strings.HasPrefix(request.Model, "gpt-4") {
api_version = 4
} else {
api_version = 3
}
token, err := arkose.GetOpenAIToken(api_version, puid, dx, proxy)
if err == nil {
request.ArkoseToken = token
} else {
fmt.Println("Error getting Arkose token: ", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/joho/godotenv v1.5.1
github.com/tidwall/gjson v1.17.1
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240518064833-c9bef8c55476
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240520071626-7f9e463abed0
github.com/xqdoo00o/funcaptcha v0.0.0-20240507071758-6c32cfc34bdc
golang.org/x/crypto v0.23.0
golang.org/x/image v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,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-20240518064833-c9bef8c55476 h1:YoCK2wxV5zqhmOiXkYyeKckV4YGlE3YBeRUQ4LZkqxo=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240518064833-c9bef8c55476/go.mod h1:TOXe2RBzDx5/w2RYzP7C1WNfNJx1wEVOSkCBcGSaNrM=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240520071626-7f9e463abed0 h1:pZSFokQ7e5a86B4jD8zn6Js4kLNeTN7sh3InxRSivEs=
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240520071626-7f9e463abed0/go.mod h1:TOXe2RBzDx5/w2RYzP7C1WNfNJx1wEVOSkCBcGSaNrM=
github.com/xqdoo00o/funcaptcha v0.0.0-20240507071758-6c32cfc34bdc h1:GA82q8G/mh5LLaBAo3UWw4zvxk/BlarGSuonyK4tz3Y=
github.com/xqdoo00o/funcaptcha v0.0.0-20240507071758-6c32cfc34bdc/go.mod h1:MmsYdzgYjO6NQYNS8vtecBWUFWwD4TApVzMp8iwKiR0=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
43 changes: 17 additions & 26 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
official_types "freechatgpt/typings/official"
"os"
"strings"
"sync"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
arkose "github.com/xqdoo00o/funcaptcha"
)

var (
Expand Down Expand Up @@ -119,26 +119,7 @@ func nightmare(c *gin.Context) {
deviceId = generateUUID(account)
chatgpt.SetOAICookie(deviceId)
}
var chat_require *chatgpt.ChatRequire
var wg sync.WaitGroup
if secret.Token == "" {
wg.Add(1)
} else {
wg.Add(2)
go func() {
defer wg.Done()
err = chatgpt.InitWSConn(&secret, deviceId, uid, proxy_url)
}()
}
go func() {
defer wg.Done()
chat_require = chatgpt.CheckRequire(&secret, deviceId, proxy_url)
}()
wg.Wait()
if err != nil {
c.JSON(500, gin.H{"error": "unable to create ws tunnel"})
return
}
chat_require := chatgpt.CheckRequire(&secret, deviceId, proxy_url)
if chat_require == nil {
c.JSON(500, gin.H{"error": "unable to check chat requirement"})
return
Expand All @@ -147,10 +128,17 @@ func nightmare(c *gin.Context) {
if chat_require.Proof.Required {
proofToken = chatgpt.CalcProofToken(chat_require, proxy_url)
}
var arkoseToken string
if chat_require.Arkose.Required {
arkoseToken, err = arkose.GetOpenAIToken(4, secret.PUID, chat_require.Arkose.DX, proxy_url)
if err != nil {
println("Error getting Arkose token: ", err)
}
}
// 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)
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request, account, &secret, deviceId, proxy_url)

response, err := chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, proofToken, proxy_url)
response, err := chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, arkoseToken, proofToken, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand Down Expand Up @@ -180,9 +168,12 @@ func nightmare(c *gin.Context) {
proofToken = chatgpt.CalcProofToken(chat_require, proxy_url)
}
if chat_require.Arkose.Required {
chatgpt_request_converter.RenewTokenForRequest(&translated_request, secret.PUID, chat_require.Arkose.DX, proxy_url)
arkoseToken, err = arkose.GetOpenAIToken(4, secret.PUID, chat_require.Arkose.DX, proxy_url)
if err != nil {
println("Error getting Arkose token: ", err)
}
}
response, err = chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, proofToken, proxy_url)
response, err = chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, arkoseToken, proofToken, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand All @@ -202,5 +193,5 @@ func nightmare(c *gin.Context) {
} else {
c.String(200, "data: [DONE]\n\n")
}
chatgpt.UnlockSpecConn(secret.Token+secret.TeamUserID, uid)
// chatgpt.UnlockSpecConn(secret.Token+secret.TeamUserID, uid)
}
5 changes: 1 addition & 4 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func getURLAttribution(secret *tokens.Secret, deviceId string, url string) strin
return attr.Attribution
}

func POSTconversation(message chatgpt_types.ChatGPTRequest, secret *tokens.Secret, deviceId string, chat_token string, proofToken string, proxy string) (*http.Response, error) {
func POSTconversation(message chatgpt_types.ChatGPTRequest, secret *tokens.Secret, deviceId string, chat_token string, arkoseToken string, proofToken string, proxy string) (*http.Response, error) {
if proxy != "" {
client.SetProxy(proxy)
}
Expand All @@ -432,9 +432,6 @@ func POSTconversation(message chatgpt_types.ChatGPTRequest, secret *tokens.Secre
if API_REVERSE_PROXY != "" {
apiUrl = API_REVERSE_PROXY
}

arkoseToken := message.ArkoseToken
message.ArkoseToken = ""
// JSONify the body and add it to the request
body_json, err := json.Marshal(message)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion typings/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ type ChatGPTRequest struct {
ConversationID string `json:"conversation_id,omitempty"`
Model string `json:"model"`
HistoryAndTrainingDisabled bool `json:"history_and_training_disabled"`
ArkoseToken string `json:"arkose_token,omitempty"`
WebsocketRequestId string `json:"websocket_request_id"`
ForceSSE bool `json:"force_use_sse"`
}
type FileResp struct {
File_id string `json:"file_id"`
Expand Down Expand Up @@ -237,6 +237,7 @@ func NewChatGPTRequest() ChatGPTRequest {
HistoryAndTrainingDisabled: disable_history,
ConversationMode: ChatGPTConvMode{Kind: "primary_assistant"},
WebsocketRequestId: uuid.NewString(),
ForceSSE: true,
}
}

Expand Down

0 comments on commit cfe288f

Please sign in to comment.