Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, Response
|
2 |
+
import requests
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
GITHUB_URL = "https://github.com"
|
7 |
+
|
8 |
+
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
9 |
+
def proxy(path):
|
10 |
+
"""
|
11 |
+
一个非常基础的 GitHub 反向代理。
|
12 |
+
"""
|
13 |
+
# 构造完整的 GitHub URL
|
14 |
+
url = f"{GITHUB_URL}/{path}"
|
15 |
+
|
16 |
+
# 复制请求头,特别是对于私有仓库的认证头
|
17 |
+
headers = {key: value for (key, value) in request.headers if key != 'Host'}
|
18 |
+
|
19 |
+
try:
|
20 |
+
# 使用流式传输,以处理大文件
|
21 |
+
resp = requests.request(
|
22 |
+
method=request.method,
|
23 |
+
url=url,
|
24 |
+
params=request.args,
|
25 |
+
headers=headers,
|
26 |
+
data=request.get_data(),
|
27 |
+
cookies=request.cookies,
|
28 |
+
allow_redirects=True,
|
29 |
+
stream=True
|
30 |
+
)
|
31 |
+
|
32 |
+
# 构造并返回响应
|
33 |
+
# 注意:需要仔细处理响应头,这里只是一个简化示例
|
34 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
35 |
+
response_headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
|
36 |
+
|
37 |
+
return Response(resp.iter_content(chunk_size=8192), status=resp.status_code, headers=response_headers)
|
38 |
+
|
39 |
+
except requests.exceptions.RequestException as e:
|
40 |
+
return f"An error occurred: {e}", 502
|
41 |
+
|
42 |
+
if __name__ == '__main__':
|
43 |
+
app.run(host='0.0.0.0', port=7860)
|