File size: 1,357 Bytes
6d8044f
 
 
 
 
 
d7e6a25
 
6d8044f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, jsonify
from bs4 import BeautifulSoup
import requests

app = Flask(__name__)


@app.route('/')
def proxy():
    url = request.args.get('url')
    if not url:
        return jsonify({'error': 'URL parameter is missing'}), 400
    
    try:
        headers = {key: value for (key, value) in request.headers if key != 'Host'}
        cookies = request.cookies
        method = request.method
        data = request.get_data()

        response = requests.request(
            method=method,
            url=url,
            headers=headers,
            cookies=cookies,
            data=data,
            allow_redirects=False
        )

        if 'text/html' in response.headers.get('content-type', ''):
            soup = BeautifulSoup(response.content, 'html.parser')
            for tag in soup.find_all(href=True):
                tag['href'] = '/proxy?url=' + tag['href']
            for tag in soup.find_all(src=True):
                tag['src'] = '/proxy?url=' + tag['src']
            response.content = soup.encode()

        return response.content, response.status_code, response.headers.items()
    except requests.RequestException as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(host="0.0.0.0", ssl_context='adhoc', port=7680)  # Use 'adhoc' to enable HTTPS locally