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

app = Flask(__name__)

@app.route('/proxy/<path:url>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(url):
    req_method = request.method
    req_headers = {key: value for key, value in request.headers if key != 'Host'}
    req_data = request.get_data()

    resp = requests.request(method=req_method,
                            url=f'https://{url}',
                            headers=req_headers,
                            data=req_data,
                            stream=True)

    excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
    headers = [(name, value) for (name, value) in resp.raw.headers.items()
               if name.lower() not in excluded_headers]

    def generate():
        for chunk in resp.iter_content(chunk_size=4096):
            yield chunk

    return Response(generate(), status=resp.status_code, headers=headers)

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