Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,95 @@
|
|
|
|
1 |
from flask import Flask, request, Response
|
2 |
import requests
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def proxy(path):
|
10 |
"""
|
11 |
-
|
|
|
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=
|
24 |
-
params=request.args,
|
25 |
headers=headers,
|
26 |
data=request.get_data(),
|
27 |
cookies=request.cookies,
|
28 |
-
allow_redirects=
|
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)
|
|
|
1 |
+
import re
|
2 |
from flask import Flask, request, Response
|
3 |
import requests
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# --- 白名单过滤规则 (保持不变) ---
|
8 |
+
# 这些规则现在将应用到从路径中解析出的完整URL上
|
9 |
+
ALLOWED_PATTERNS = [
|
10 |
+
re.compile(r'^https://github\.com/[^/]+/[^/]+/(?:releases|archive)/.*$', re.IGNORECASE),
|
11 |
+
re.compile(r'^https://github\.com/[^/]+/[^/]+/(?:blob|raw)/.*$', re.IGNORECASE),
|
12 |
+
re.compile(r'^https://github\.com/[^/]+/[^/]+/(?:info|git-).*/.*$', re.IGNORECASE),
|
13 |
+
re.compile(r'^https://raw\.(?:githubusercontent|github)\.com/[^/]+/[^/]+/.*/.*$', re.IGNORECASE),
|
14 |
+
re.compile(r'^https://gist\.(?:githubusercontent|github)\.com/[^/]+/[^/]+/.*/.*$', re.IGNORECASE),
|
15 |
+
re.compile(r'^https://github\.com/[^/]+/[^/]+/tags.*$', re.IGNORECASE),
|
16 |
+
re.compile(r'^https://avatars\.githubusercontent\.com/.*$', re.IGNORECASE),
|
17 |
+
re.compile(r'^https://github\.githubassets\.com/.*$', re.IGNORECASE),
|
18 |
+
re.compile(r'^https://github\.com/[^/]+/?$', re.IGNORECASE),
|
19 |
+
re.compile(r'^https://github\.com/[^/]+/[^/]+/?$', re.IGNORECASE),
|
20 |
+
]
|
21 |
|
22 |
+
def is_url_allowed(url):
|
23 |
+
"""检查给定的URL是否匹配白名单中的任何一个模式。"""
|
24 |
+
for pattern in ALLOWED_PATTERNS:
|
25 |
+
if pattern.match(url):
|
26 |
+
return True
|
27 |
+
return False
|
28 |
+
|
29 |
+
# --- 核心代理逻辑 ---
|
30 |
+
|
31 |
+
# 我们现在使用一个更通用的路由来捕获所有请求
|
32 |
+
@app.route('/', defaults={'path': ''})
|
33 |
+
@app.route('/<path:path>')
|
34 |
def proxy(path):
|
35 |
"""
|
36 |
+
一个通用的反向代理,它将目标URL作为路径的一部分。
|
37 |
+
例如: /https://github.com/user/repo
|
38 |
"""
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# --- 1. 从请求路径中构建目标URL ---
|
41 |
+
# 使用 request.full_path 来获取完整的路径和查询参数, e.g., /https://github.com/user/repo?service=...
|
42 |
+
target_path = request.full_path
|
43 |
+
|
44 |
+
# 移除开头的斜杠
|
45 |
+
if target_path.startswith('/'):
|
46 |
+
target_path = target_path[1:]
|
47 |
+
|
48 |
+
# 如果路径本身不是一个完整的URL,则为其添加 https://
|
49 |
+
if not target_path.startswith(('http://', 'https://')):
|
50 |
+
target_url = 'https://' + target_path
|
51 |
+
else:
|
52 |
+
target_url = target_path
|
53 |
+
|
54 |
+
# --- 2. 执行安全过滤检查 ---
|
55 |
+
if not is_url_allowed(target_url):
|
56 |
+
error_message = (
|
57 |
+
"<h1>403 Forbidden</h1>"
|
58 |
+
"<p>This request is blocked by the proxy's security policy.</p>"
|
59 |
+
f"<p>Blocked URL: {target_url}</p>"
|
60 |
+
)
|
61 |
+
return error_message, 403
|
62 |
+
|
63 |
+
# --- 3. 转发请求 ---
|
64 |
+
# 从目标URL中解析出Host头
|
65 |
+
try:
|
66 |
+
from urllib.parse import urlparse
|
67 |
+
target_host = urlparse(target_url).hostname
|
68 |
+
except Exception:
|
69 |
+
return "Invalid target URL in path", 400
|
70 |
+
|
71 |
+
headers = {key: value for (key, value) in request.headers if key.lower() != 'host'}
|
72 |
+
headers['Host'] = target_host
|
73 |
+
|
74 |
try:
|
|
|
75 |
resp = requests.request(
|
76 |
method=request.method,
|
77 |
+
url=target_url,
|
|
|
78 |
headers=headers,
|
79 |
data=request.get_data(),
|
80 |
cookies=request.cookies,
|
81 |
+
allow_redirects=False,
|
82 |
stream=True
|
83 |
)
|
84 |
|
|
|
|
|
85 |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
86 |
response_headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
|
87 |
|
88 |
return Response(resp.iter_content(chunk_size=8192), status=resp.status_code, headers=response_headers)
|
89 |
|
90 |
except requests.exceptions.RequestException as e:
|
91 |
+
return f"An error occurred while proxying: {e}", 502
|
92 |
+
|
93 |
|
94 |
if __name__ == '__main__':
|
95 |
+
app.run(host='0.0.0.0', port=7860)
|