forked from acheong08/ChatGPT-to-API
-
Notifications
You must be signed in to change notification settings - Fork 135
/
handlers.go
348 lines (330 loc) · 9.73 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
chatgpt_request_converter "freechatgpt/conversion/requests/chatgpt"
chatgpt "freechatgpt/internal/chatgpt"
"freechatgpt/internal/tokens"
official_types "freechatgpt/typings/official"
"os"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
arkose "github.com/xqdoo00o/funcaptcha"
)
var (
uuidNamespace = uuid.MustParse("12345678-1234-5678-1234-567812345678")
)
func passwordHandler(c *gin.Context) {
// Get the password from the request (json) and update the password
type password_struct struct {
Password string `json:"password"`
}
var password password_struct
err := c.BindJSON(&password)
if err != nil {
c.String(400, "password not provided")
return
}
ADMIN_PASSWORD = password.Password
// Set environment variable
os.Setenv("ADMIN_PASSWORD", ADMIN_PASSWORD)
c.String(200, "password updated")
}
func tokensHandler(c *gin.Context) {
// Get the request_tokens from the request (json) and update the request_tokens
var request_tokens map[string]tokens.Secret
err := c.BindJSON(&request_tokens)
if err != nil {
c.String(400, "tokens not provided")
return
}
ACCESS_TOKENS = tokens.NewAccessToken(request_tokens)
ACCESS_TOKENS.Save()
validAccounts = ACCESS_TOKENS.GetKeys()
c.String(200, "tokens updated")
}
func optionsHandler(c *gin.Context) {
// Set headers for CORS
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST")
c.Header("Access-Control-Allow-Headers", "*")
c.JSON(200, gin.H{
"message": "pong",
})
}
func simulateModel(c *gin.Context) {
c.JSON(200, gin.H{
"object": "list",
"data": []gin.H{
{
"id": "gpt-3.5-turbo",
"object": "model",
"created": 1688888888,
"owned_by": "chatgpt-to-api",
},
{
"id": "gpt-4",
"object": "model",
"created": 1688888888,
"owned_by": "chatgpt-to-api",
},
{
"id": "gpt-4o",
"object": "model",
"created": 1688888888,
"owned_by": "chatgpt-to-api",
},
{
"id": "gpt-4o-mini",
"object": "model",
"created": 1688888888,
"owned_by": "chatgpt-to-api",
},
},
})
}
func generateUUID(name string) string {
return uuid.NewSHA1(uuidNamespace, []byte(name)).String()
}
func nightmare(c *gin.Context) {
var original_request official_types.APIRequest
err := c.BindJSON(&original_request)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must be proper JSON",
"type": "invalid_request_error",
"param": nil,
"code": err.Error(),
}})
return
}
account, secret := getSecret()
var proxy_url string
if len(proxies) == 0 {
proxy_url = ""
} else {
proxy_url = proxies[0]
// Push used proxy to the back of the list
proxies = append(proxies[1:], proxies[0])
}
uid := uuid.NewString()
var deviceId string
if account == "" {
deviceId = uid
chatgpt.SetOAICookie(deviceId)
} else {
deviceId = generateUUID(account)
chatgpt.SetOAICookie(deviceId)
}
chat_require, p := chatgpt.CheckRequire(&secret, deviceId, proxy_url)
if chat_require == nil {
c.JSON(500, gin.H{"error": "unable to check chat requirement"})
return
}
var proofToken string
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.Error())
}
}
var turnstileToken string
if chat_require.Turnstile.Required {
turnstileToken = chatgpt.ProcessTurnstile(chat_require.Turnstile.DX, p)
}
// Convert the chat request to a ChatGPT request
translated_request := chatgpt_request_converter.ConvertAPIRequest(original_request, account, &secret, deviceId, proxy_url)
response, err := chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, arkoseToken, proofToken, turnstileToken, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
})
return
}
defer response.Body.Close()
if chatgpt.Handle_request_error(c, response) {
return
}
var full_response string
for i := 3; i > 0; i-- {
var continue_info *chatgpt.ContinueInfo
var response_part string
response_part, continue_info = chatgpt.Handler(c, response, &secret, proxy_url, deviceId, uid, original_request.Stream)
full_response += response_part
if continue_info == nil {
break
}
println("Continuing conversation")
translated_request.Messages = nil
translated_request.Action = "continue"
translated_request.ConversationID = continue_info.ConversationID
translated_request.ParentMessageID = continue_info.ParentID
chat_require, _ = chatgpt.CheckRequire(&secret, deviceId, proxy_url)
if chat_require.Proof.Required {
proofToken = chatgpt.CalcProofToken(chat_require, proxy_url)
}
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.Error())
}
}
if chat_require.Turnstile.Required {
turnstileToken = chatgpt.ProcessTurnstile(chat_require.Turnstile.DX, p)
}
response, err = chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, arkoseToken, proofToken, turnstileToken, proxy_url)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
})
return
}
defer response.Body.Close()
if chatgpt.Handle_request_error(c, response) {
return
}
}
if c.Writer.Status() != 200 {
return
}
if !original_request.Stream {
c.JSON(200, official_types.NewChatCompletion(full_response))
} else {
c.String(200, "data: [DONE]\n\n")
}
}
var ttsFmtMap = map[string]string{
"mp3": "mp3",
"opus": "opus",
"aac": "aac",
"flac": "aac",
"wav": "aac",
"pcm": "aac",
}
var ttsTypeMap = map[string]string{
"mp3": "audio/mpeg",
"opus": "audio/ogg",
"aac": "audio/aac",
}
var ttsVoiceMap = map[string]string{
"alloy": "cove",
"echo": "ember",
"fable": "breeze",
"onyx": "cove",
"nova": "juniper",
"shimmer": "juniper",
}
func tts(c *gin.Context) {
var original_request official_types.TTSAPIRequest
err := c.BindJSON(&original_request)
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must be proper JSON",
"type": "invalid_request_error",
"param": nil,
"code": err.Error(),
}})
return
}
account, secret := getSecret()
if account == "" || secret.PUID == "" {
c.JSON(500, gin.H{"error": "Plus user only"})
return
}
var proxy_url string
if len(proxies) == 0 {
proxy_url = ""
} else {
proxy_url = proxies[0]
// Push used proxy to the back of the list
proxies = append(proxies[1:], proxies[0])
}
var deviceId = generateUUID(account)
chatgpt.SetOAICookie(deviceId)
chat_require, p := chatgpt.CheckRequire(&secret, deviceId, proxy_url)
if chat_require == nil {
c.JSON(500, gin.H{"error": "unable to check chat requirement"})
return
}
var proofToken string
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.Error())
}
}
var turnstileToken string
if chat_require.Turnstile.Required {
turnstileToken = chatgpt.ProcessTurnstile(chat_require.Turnstile.DX, p)
}
// Convert the chat request to a ChatGPT request
translated_request := chatgpt_request_converter.ConvertTTSAPIRequest(original_request.Input)
response, err := chatgpt.POSTconversation(translated_request, &secret, deviceId, chat_require.Token, arkoseToken, proofToken, turnstileToken, proxy_url)
if err != nil {
c.JSON(500, gin.H{"error": "error sending request"})
return
}
defer response.Body.Close()
if chatgpt.Handle_request_error(c, response) {
return
}
msgId, convId := chatgpt.HandlerTTS(response, original_request.Input)
format := ttsFmtMap[original_request.Format]
if format == "" {
format = "aac"
}
voice := ttsVoiceMap[original_request.Voice]
if voice == "" {
voice = "cove"
}
apiUrl := "https://chatgpt.com/backend-api/synthesize?message_id=" + msgId + "&conversation_id=" + convId + "&voice=" + voice + "&format=" + format
data := chatgpt.GetTTS(&secret, deviceId, apiUrl, proxy_url)
if data != nil {
c.Data(200, ttsTypeMap[format], data)
} else {
c.JSON(500, gin.H{"error": "synthesize error"})
}
chatgpt.RemoveConversation(&secret, deviceId, convId, proxy_url)
}
func stt(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
println(err.Error())
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must has proper file",
"type": "invalid_request_error",
"param": nil,
"code": err.Error(),
}})
return
}
defer file.Close()
lang := c.Request.FormValue("language")
account, secret := getSecret()
if account == "" {
c.JSON(500, gin.H{"error": "Logined user only"})
return
}
var proxy_url string
if len(proxies) == 0 {
proxy_url = ""
} else {
proxy_url = proxies[0]
// Push used proxy to the back of the list
proxies = append(proxies[1:], proxies[0])
}
var deviceId = generateUUID(account)
chatgpt.SetOAICookie(deviceId)
data := chatgpt.GetSTT(file, header, lang, &secret, deviceId, proxy_url)
if data != nil {
c.Data(200, "application/json", data)
} else {
c.JSON(500, gin.H{"error": "transcribe error"})
}
}