This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
/
main.go
251 lines (234 loc) · 6.69 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/url"
"os"
"strings"
"time"
arkose "github.com/acheong08/funcaptcha"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/acheong08/OpenAIAuth/auth"
"github.com/acheong08/endless"
"github.com/gin-gonic/gin"
)
type auth_struct struct {
OpenAI_Email string `json:"openai_email"`
OpenAI_Password string `json:"openai_password"`
}
var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(360),
tls_client.WithClientProfile(tls_client.Firefox_110),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
}
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:114.0) Gecko/20100101 Firefox/114.0"
http_proxy = os.Getenv("http_proxy")
authorizations auth_struct
OpenAI_HOST = os.Getenv("OPENAI_HOST")
)
func admin(c *gin.Context) {
if c.GetHeader("Authorization") != os.Getenv("PASSWORD") {
c.String(401, "Unauthorized")
c.Abort()
return
}
c.Next()
}
func init() {
if OpenAI_HOST == "" {
OpenAI_HOST = "chat.openai.com"
}
authorizations.OpenAI_Email = os.Getenv("OPENAI_EMAIL")
authorizations.OpenAI_Password = os.Getenv("OPENAI_PASSWORD")
if authorizations.OpenAI_Email != "" && authorizations.OpenAI_Password != "" {
go func() {
for {
authenticator := auth.NewAuthenticator(authorizations.OpenAI_Email, authorizations.OpenAI_Password, http_proxy)
err := authenticator.Begin()
if err != nil {
log.Println(err)
break
}
puid, err := authenticator.GetPUID()
if err != nil {
break
}
os.Setenv("PUID", puid)
println(puid)
client.SetCookies(&url.URL{
Host: OpenAI_HOST,
}, []*http.Cookie{
{
Name: "_puid",
Value: puid,
},
})
time.Sleep(24 * time.Hour * 7)
}
}()
}
// arkose.SetTLSClient(&client)
}
func main() {
if http_proxy != "" {
client.SetProxy(http_proxy)
println("Proxy set:" + http_proxy)
}
PORT := os.Getenv("PORT")
if PORT == "" {
PORT = "9090"
}
handler := gin.Default()
handler.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
handler.PATCH("/admin/puid", admin, func(c *gin.Context) {
// Get the password from the request (json) and update the password
type puid_struct struct {
PUID string `json:"puid"`
}
var puid puid_struct
err := c.BindJSON(&puid)
if err != nil {
c.String(400, "puid not provided")
return
}
// Set environment variable
os.Setenv("PUID", puid.PUID)
c.String(200, "puid updated")
})
handler.PATCH("/admin/password", admin, func(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
}
// Set environment variable
os.Setenv("PASSWORD", password.PASSWORD)
c.String(200, "PASSWORD updated")
})
handler.PATCH("/admin/openai", admin, func(c *gin.Context) {
err := c.BindJSON(&authorizations)
if err != nil {
c.JSON(400, gin.H{"error": "JSON invalid"})
}
os.Setenv("OPENAI_EMAIL", authorizations.OpenAI_Email)
os.Setenv("OPENAI_PASSWORD", authorizations.OpenAI_Password)
})
handler.Any("/api/*path", proxy)
gin.SetMode(gin.ReleaseMode)
endless.ListenAndServe(os.Getenv("HOST")+":"+PORT, handler)
}
func proxy(c *gin.Context) {
var url string
var err error
var request_method string
var request *http.Request
var response *http.Response
if c.Request.URL.RawQuery != "" {
url = "https://" + OpenAI_HOST + "/backend-api" + c.Param("path") + "?" + c.Request.URL.RawQuery
} else {
url = "https://" + OpenAI_HOST + "/backend-api" + c.Param("path")
}
request_method = c.Request.Method
if c.Request.URL.Path == "/api/conversation" {
var request_body map[string]interface{}
if c.Request.Body != nil {
err := json.NewDecoder(c.Request.Body).Decode(&request_body)
if err != nil {
c.JSON(400, gin.H{"error": "JSON invalid"})
return
}
}
// Check if "model" is in the request json
if _, ok := request_body["model"]; !ok {
c.JSON(400, gin.H{"error": "model not provided"})
return
}
if strings.HasPrefix(request_body["model"].(string), "gpt-4") {
if _, ok := request_body["arkose_token"]; !ok {
log.Println("arkose token not provided")
token, _, err := arkose.GetOpenAIToken()
var arkose_token string
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
arkose_token = token
request_body["arkose_token"] = arkose_token
}
}
body_json, err := json.Marshal(request_body)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
original_body := bytes.NewReader(body_json)
request, _ = http.NewRequest(request_method, url, original_body)
} else {
request, _ = http.NewRequest(request_method, url, c.Request.Body)
}
request.Header.Set("Host", ""+OpenAI_HOST+"")
request.Header.Set("Origin", "https://"+OpenAI_HOST+"/chat")
request.Header.Set("Connection", "keep-alive")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Keep-Alive", "timeout=360")
request.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
request.Header.Set("sec-ch-ua", "\"Chromium\";v=\"112\", \"Brave\";v=\"112\", \"Not:A-Brand\";v=\"99\"")
request.Header.Set("sec-ch-ua-mobile", "?0")
request.Header.Set("sec-ch-ua-platform", "\"Linux\"")
request.Header.Set("sec-fetch-dest", "empty")
request.Header.Set("sec-fetch-mode", "cors")
request.Header.Set("sec-fetch-site", "same-origin")
request.Header.Set("sec-gpc", "1")
request.Header.Set("user-agent", user_agent)
if c.Request.Header.Get("PUID") != "" {
request.Header.Set("cookie", "_puid="+c.Request.Header.Get("PUID")+";")
}
response, err = client.Do(request)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer response.Body.Close()
// Copy headers from response
for k, v := range response.Header {
if strings.ToLower(k) == "content-encoding" {
continue
}
c.Header(k, v[0])
}
// Get status code
c.Status(response.StatusCode)
buf := make([]byte, 4096)
for {
n, err := response.Body.Read(buf)
if n > 0 {
_, writeErr := c.Writer.Write(buf[:n])
if writeErr != nil {
log.Printf("Error writing to client: %v", writeErr)
break
}
c.Writer.Flush() // flush buffer to make sure the data is sent to client in time.
}
if err == io.EOF {
break
}
if err != nil {
log.Printf("Error reading from response body: %v", err)
break
}
}
}