|
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 反向代理。 |
|
""" |
|
|
|
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) |