2024-08-03 14:33:32 +00:00
|
|
|
from flask import Flask, render_template, request, Response, abort
|
|
|
|
from werkzeug.exceptions import InternalServerError
|
2024-08-03 14:26:09 +00:00
|
|
|
|
|
|
|
from urllib.request import urlopen
|
2024-08-03 14:33:32 +00:00
|
|
|
from urllib.error import HTTPError
|
|
|
|
from urllib.parse import unquote
|
2024-08-03 06:40:59 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2024-08-03 14:33:32 +00:00
|
|
|
|
|
|
|
@app.route("/")
|
2024-08-03 06:40:59 +00:00
|
|
|
def home():
|
|
|
|
domain = request.url_root
|
2024-08-03 14:33:32 +00:00
|
|
|
return render_template("index.html", domain=domain)
|
|
|
|
|
2024-08-03 06:40:59 +00:00
|
|
|
|
2024-08-03 14:33:32 +00:00
|
|
|
@app.route("/<path:url>")
|
2024-08-03 06:40:59 +00:00
|
|
|
def proxy(url):
|
2024-08-03 14:39:46 +00:00
|
|
|
cdnjs_url = f"https://cdnjs.cloudflare.com/{url}"
|
2024-08-03 14:33:32 +00:00
|
|
|
|
|
|
|
def generate():
|
|
|
|
# Subfunction to allow streaming the data instead of
|
|
|
|
# downloading all of it at once
|
|
|
|
try:
|
2024-08-03 14:39:46 +00:00
|
|
|
with urlopen(unquote(cdnjs_url)) as data:
|
2024-08-03 14:33:32 +00:00
|
|
|
while True:
|
|
|
|
chunk = data.read(1024 * 1024)
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
yield chunk
|
|
|
|
except HTTPError as e:
|
|
|
|
abort(e.code)
|
|
|
|
|
|
|
|
try:
|
2024-08-03 14:39:46 +00:00
|
|
|
with urlopen(unquote(cdnjs_url)) as data:
|
2024-08-03 14:33:32 +00:00
|
|
|
content_type = data.headers["content-type"]
|
|
|
|
except HTTPError as e:
|
|
|
|
abort(e.code)
|
|
|
|
except KeyError:
|
|
|
|
raise InternalServerError()
|
|
|
|
|
|
|
|
headers = dict()
|
|
|
|
filename = url.split("/")[-1]
|
|
|
|
headers["Content-Disposition"] = f'attachment; filename="{filename}"'
|
2024-08-03 14:26:09 +00:00
|
|
|
|
2024-08-03 14:33:32 +00:00
|
|
|
return Response(generate(), content_type=content_type, headers=headers)
|
2024-08-03 14:26:09 +00:00
|
|
|
|
2024-08-03 06:40:59 +00:00
|
|
|
|
2024-08-03 14:33:32 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run()
|