forked from acheong08/ChatGPTProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (183 loc) · 5.38 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
package main
import (
"io"
"log"
"os"
"time"
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.Safari_IOS_16_0),
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
http_proxy = os.Getenv("http_proxy")
authorizations auth_struct
)
func admin(c *gin.Context) {
if c.GetHeader("Authorization") != os.Getenv("PASSWORD") {
c.String(401, "Unauthorized")
c.Abort()
return
}
c.Next()
}
func init() {
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)
time.Sleep(24 * time.Hour * 7)
}
}()
}
}
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) {
// Remove _cfuvid cookie from session
jar.SetCookies(c.Request.URL, []*http.Cookie{})
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://chat.openai.com/backend-api" + c.Param("path") + "?" + c.Request.URL.RawQuery
} else {
url = "https://chat.openai.com/backend-api" + c.Param("path")
}
request_method = c.Request.Method
request, err = http.NewRequest(request_method, url, c.Request.Body)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
request.Header.Set("Host", "chat.openai.com")
request.Header.Set("Origin", "https://chat.openai.com/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 os.Getenv("PUID") != "" {
request.Header.Set("cookie", "_puid="+os.Getenv("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 {
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
}
}
}