forked from acheong08/ChatGPTProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (150 loc) · 4.4 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
package main
import (
"io"
"os"
"time"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)
var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(360),
tls_client.WithClientProfile(tls_client.Chrome_110),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
}
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
access_token = os.Getenv("ACCESS_TOKEN")
puid = os.Getenv("PUID")
http_proxy = os.Getenv("http_proxy")
)
func main() {
if access_token == "" && puid == "" {
println("Error: ACCESS_TOKEN and PUID are not set")
return
}
if http_proxy != "" {
client.SetProxy(http_proxy)
println("Proxy set:" + http_proxy)
}
// Automatically refresh the puid cookie
if access_token != "" {
go func() {
url := "https://chat.openai.com/backend-api/models"
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Host", "chat.openai.com")
req.Header.Set("origin", "https://chat.openai.com/chat")
req.Header.Set("referer", "https://chat.openai.com/chat")
req.Header.Set("sec-ch-ua", `Chromium";v="110", "Not A(Brand";v="24", "Brave";v="110`)
req.Header.Set("sec-ch-ua-platform", "Linux")
req.Header.Set("content-type", "application/json")
req.Header.Set("content-type", "application/json")
req.Header.Set("accept", "text/event-stream")
req.Header.Set("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36")
// Set authorization header
req.Header.Set("Authorization", "Bearer "+access_token)
// Initial puid cookie
req.AddCookie(
&http.Cookie{
Name: "_puid",
Value: puid,
},
)
for {
resp, err := client.Do(req)
if err != nil {
break
}
defer resp.Body.Close()
println("Got response: " + resp.Status)
if resp.StatusCode != 200 {
println("Error: " + resp.Status)
// Print response body
body, _ := io.ReadAll(resp.Body)
println(string(body))
break
}
// Get cookies from response
cookies := resp.Cookies()
// Find _puid cookie
for _, cookie := range cookies {
if cookie.Name == "_puid" {
puid = cookie.Value
println("puid: " + puid)
break
}
}
// Sleep for 6 hour
time.Sleep(6 * time.Hour)
}
println("Error: Failed to refresh puid cookie")
}()
}
PORT := os.Getenv("PORT")
if PORT == "" {
PORT = "8080"
}
handler := gin.Default()
handler.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
handler.Any("/api/*path", proxy)
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://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("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
request.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
if c.Request.Header.Get("Puid") == "" {
request.AddCookie(
&http.Cookie{
Name: "_puid",
Value: puid,
},
)
} else {
request.AddCookie(
&http.Cookie{
Name: "_puid",
Value: 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()
c.Header("Content-Type", response.Header.Get("Content-Type"))
// Get status code
c.Status(response.StatusCode)
c.Stream(func(w io.Writer) bool {
// Write data to client
io.Copy(w, response.Body)
return false
})
}