|
|
from flask import Flask, Response, request, abort, render_template_string |
|
|
import requests |
|
|
from bs4 import BeautifulSoup |
|
|
from urllib.parse import urljoin, urlparse |
|
|
import os |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
def create_download_directory(): |
|
|
if not os.path.exists('downloads'): |
|
|
os.makedirs('downloads') |
|
|
|
|
|
|
|
|
def download_and_replace_links(url): |
|
|
try: |
|
|
response = requests.get(url) |
|
|
response.raise_for_status() |
|
|
except requests.RequestException as e: |
|
|
return f"Error fetching the URL: {e}" |
|
|
|
|
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
|
|
|
|
create_download_directory() |
|
|
|
|
|
for tag in soup.find_all(['link', 'img', 'script']): |
|
|
url_attr = 'href' if tag.name == 'link' else 'src' |
|
|
if tag.has_attr(url_attr): |
|
|
resource_url = tag[url_attr] |
|
|
if resource_url.startswith(('http', 'https')): |
|
|
try: |
|
|
filename = os.path.basename(urlparse(resource_url).path) |
|
|
filepath = os.path.join('downloads', filename) |
|
|
if not os.path.exists(filepath): |
|
|
resource_response = requests.get(resource_url) |
|
|
resource_response.raise_for_status() |
|
|
with open(filepath, 'wb') as f: |
|
|
f.write(resource_response.content) |
|
|
tag[url_attr] = urljoin('/downloads/', filename) |
|
|
except requests.RequestException as e: |
|
|
print(f"Error downloading {resource_url}: {e}") |
|
|
|
|
|
return str(soup) |
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return render_template_string(''' |
|
|
<!doctype html> |
|
|
<html lang="en"> |
|
|
<head> |
|
|
<meta charset="utf-8"> |
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> |
|
|
<title>Proxy Service</title> |
|
|
</head> |
|
|
<body> |
|
|
<div class="container"> |
|
|
<h1 class="mt-5">Welcome to the Proxy Service</h1> |
|
|
<p class="lead">To use the proxy, append the URL parameter like this:</p> |
|
|
<p><code>/proxy?url=http://example.com</code></p> |
|
|
</div> |
|
|
</body> |
|
|
</html> |
|
|
''') |
|
|
|
|
|
|
|
|
@app.route('/proxy') |
|
|
def proxy(): |
|
|
url = request.args.get('url') |
|
|
if not url: |
|
|
abort(400, description="URL parameter is required.") |
|
|
|
|
|
parsed_url = urlparse(url) |
|
|
if not parsed_url.scheme: |
|
|
url = 'http://' + url |
|
|
|
|
|
modified_html = download_and_replace_links(url) |
|
|
return Response(modified_html, mimetype='text/html') |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=True, host="0.0.0.0", port=7860) |
|
|
|