File size: 1,459 Bytes
f8390d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, Response
import requests

app = Flask(__name__)

GITHUB_URL = "https://github.com"

@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
    """
    一个非常基础的 GitHub 反向代理。
    """
    # 构造完整的 GitHub URL
    url = f"{GITHUB_URL}/{path}"

    # 复制请求头,特别是对于私有仓库的认证头
    headers = {key: value for (key, value) in request.headers if key != 'Host'}
    
    try:
        # 使用流式传输,以处理大文件
        resp = requests.request(
            method=request.method,
            url=url,
            params=request.args,
            headers=headers,
            data=request.get_data(),
            cookies=request.cookies,
            allow_redirects=True,
            stream=True
        )

        # 构造并返回响应
        # 注意:需要仔细处理响应头,这里只是一个简化示例
        excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
        response_headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]

        return Response(resp.iter_content(chunk_size=8192), status=resp.status_code, headers=response_headers)

    except requests.exceptions.RequestException as e:
        return f"An error occurred: {e}", 502

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)