This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathproxy.py
105 lines (91 loc) · 3.27 KB
/
proxy.py
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
"""
Fetches cookies from chat.openai.com and returns them (Flask)
"""
import json
import os
import tls_client
import uvicorn
from asgiref.wsgi import WsgiToAsgi
from flask import Flask
from flask import jsonify
from flask import request
from OpenAIAuth.Cloudflare import Cloudflare
GPT_PROXY = os.getenv('GPT_PROXY')
GPT_HOST = os.getenv('GPT_HOST', '0.0.0.0')
GPT_PORT = int(os.getenv('GPT_PORT', 5000))
app = Flask(__name__)
session = tls_client.Session(client_identifier="chrome_108", )
if GPT_PROXY:
session.proxies.update(http=GPT_PROXY, https=GPT_PROXY)
authentication = {}
context = {"blocked": False}
# Get cloudflare cookies
(
authentication["cf_clearance"],
authentication["user_agent"],
) = Cloudflare(proxy=GPT_PROXY).get_cf_cookies()
@app.route("/<path:subpath>", methods=["POST", "GET"])
def conversation(subpath: str):
if request.headers.get("Authorization") is None:
return jsonify({"error": "Missing Authorization header"})
try:
if context.get("blocked"):
return jsonify({"error": "Blocking operation in progress"})
# Get cookies from request
cookies = {
"cf_clearance":
authentication["cf_clearance"],
"__Secure-next-auth.session-token":
request.cookies.get("__Secure-next-auth.session-token"),
}
# Set user agent
headers = {
"Accept": "text/event-stream",
"Authorization": request.headers.get("Authorization"),
"User-Agent": authentication["user_agent"],
"Content-Type": "application/json",
"X-Openai-Assistant-App-Id": "",
"Connection": "close",
"Accept-Language": "en-US,en;q=0.9",
"Referer": "https://chat.openai.com/" + "chat",
}
# Send request to OpenAI
if request.method == "POST":
response = session.post(
url="https://chat.openai.com/" + subpath,
headers=headers,
cookies=cookies,
data=json.dumps(request.get_json()),
timeout_seconds=360,
)
elif request.method == "GET":
response = session.get(
url="https://chat.openai.com/" + subpath,
headers=headers,
cookies=cookies,
timeout_seconds=360,
)
# Check status code
if response.status_code == 403:
# Get cf_clearance again
context["blocked"] = True
(
authentication["cf_clearance"],
authentication["user_agent"],
) = Cloudflare(proxy=GPT_PROXY).get_cf_cookies()
context["blocked"] = False
# return error
return jsonify({
"error":
"Cloudflare token expired. Please wait a few minutes while I refresh"
})
# Return response
return response.text
except Exception as exc:
return jsonify({"error": str(exc)})
if __name__ == "__main__":
uvicorn.run(
WsgiToAsgi(app),
host=GPT_HOST,
port=GPT_PORT,
server_header=False) # start a high-performance server with Uvicorn