Update main.py
Browse files
main.py
CHANGED
|
@@ -3,47 +3,45 @@ import requests
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def proxy(path):
|
| 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 |
-
proxy_response = Response(response.content, response.status_code)
|
| 39 |
-
for key, value in response.headers.items():
|
| 40 |
-
proxy_response.headers[key] = value
|
| 41 |
-
|
| 42 |
-
return proxy_response
|
| 43 |
-
|
| 44 |
-
except requests.exceptions.RequestException as e:
|
| 45 |
-
print(f"Request failed: {e}")
|
| 46 |
-
return Response("Bad Gateway", status=502)
|
| 47 |
|
| 48 |
if __name__ == '__main__':
|
| 49 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
PROXY_TARGET = "https://chatgpt.vvqq.top/"
|
| 7 |
+
|
| 8 |
+
@app.route('/')
|
| 9 |
+
def home():
|
| 10 |
+
global PROXY_TARGET
|
| 11 |
+
# 获取原始HTTP GET请求的参数
|
| 12 |
+
resp = requests.get(f"{PROXY_TARGET}{request.full_path}")
|
| 13 |
+
# 返回从目标网站获取的响应数据
|
| 14 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 15 |
+
headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
| 16 |
+
if name.lower() not in excluded_headers]
|
| 17 |
+
response = Response(resp.content, resp.status_code, headers)
|
| 18 |
+
return response
|
| 19 |
+
|
| 20 |
+
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
| 21 |
def proxy(path):
|
| 22 |
+
global PROXY_TARGET
|
| 23 |
+
# 构造目标URL
|
| 24 |
+
url = f"{PROXY_TARGET}/{path}"
|
| 25 |
+
# 根据原始请求的方法来发送对应的请求到目标服务器
|
| 26 |
+
if request.method == 'GET':
|
| 27 |
+
resp = requests.get(url, stream=True)
|
| 28 |
+
elif request.method == 'POST':
|
| 29 |
+
resp = requests.post(url, json=request.json, stream=True)
|
| 30 |
+
elif request.method == 'PUT':
|
| 31 |
+
resp = requests.put(url, json=request.json, stream=True)
|
| 32 |
+
elif request.method == 'DELETE':
|
| 33 |
+
resp = requests.delete(url, stream=True)
|
| 34 |
+
|
| 35 |
+
# 将从目标网站获取的响应返回给客户端
|
| 36 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 37 |
+
headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
| 38 |
+
if name.lower() not in excluded_headers]
|
| 39 |
+
|
| 40 |
+
def generate():
|
| 41 |
+
for chunk in resp.iter_content(chunk_size=8192):
|
| 42 |
+
yield chunk
|
| 43 |
+
|
| 44 |
+
return Response(generate(), resp.status_code, headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if __name__ == '__main__':
|
| 47 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|