2024-08-05 16:16:37 +02:00
|
|
|
from flask import Flask, request, Response, render_template
|
2024-08-05 16:10:59 +02:00
|
|
|
import requests
|
|
|
|
import re
|
2024-11-27 12:43:40 +01:00
|
|
|
import os
|
2024-08-05 16:10:59 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2024-11-25 17:29:03 +01:00
|
|
|
FONT_URL = "https://use.typekit.net"
|
2024-08-05 16:10:59 +02:00
|
|
|
|
2024-11-25 17:29:03 +01:00
|
|
|
PATTERN = r"use\.typekit\.net"
|
2024-08-05 16:10:59 +02:00
|
|
|
|
2024-11-25 17:29:03 +01:00
|
|
|
# P_PATTERN: Any *line* that contains p.typekit.net will be removed
|
|
|
|
P_PATTERN = r".*p\.typekit\.net.*\n"
|
2024-08-05 16:16:37 +02:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def home():
|
|
|
|
domain = request.url_root
|
|
|
|
return render_template("index.html", domain=domain)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/<path:path>", methods=["GET"])
|
2024-11-25 17:29:03 +01:00
|
|
|
def proxy_fonts(path):
|
2024-08-05 16:16:37 +02:00
|
|
|
query_string = request.query_string.decode("utf-8")
|
2024-11-25 17:29:03 +01:00
|
|
|
url = f"{FONT_URL}/{path}"
|
2024-08-05 16:10:59 +02:00
|
|
|
if query_string:
|
|
|
|
url += f"?{query_string}"
|
2024-11-25 17:42:50 +01:00
|
|
|
|
2024-08-05 16:16:37 +02:00
|
|
|
headers = {
|
2024-11-25 17:42:50 +01:00
|
|
|
"Accept-Encoding": "",
|
2024-08-05 16:16:37 +02:00
|
|
|
}
|
2024-08-05 16:10:59 +02:00
|
|
|
|
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
|
2024-08-05 16:16:37 +02:00
|
|
|
if "text/css" in response.headers.get("Content-Type", ""):
|
2024-11-25 17:29:03 +01:00
|
|
|
content = response.content.decode("utf-8")
|
|
|
|
content = re.sub(PATTERN, request.host, content)
|
|
|
|
content = re.sub(P_PATTERN, "", content)
|
2024-08-05 16:10:59 +02:00
|
|
|
|
2024-08-05 16:16:37 +02:00
|
|
|
headers = {
|
|
|
|
key: value
|
|
|
|
for key, value in response.headers.items()
|
|
|
|
if key.lower()
|
|
|
|
not in [
|
|
|
|
"connection",
|
|
|
|
"keep-alive",
|
|
|
|
"proxy-authenticate",
|
|
|
|
"proxy-authorization",
|
|
|
|
"te",
|
|
|
|
"trailers",
|
|
|
|
"transfer-encoding",
|
|
|
|
"upgrade",
|
|
|
|
]
|
|
|
|
}
|
2024-08-05 16:10:59 +02:00
|
|
|
return Response(content, status=response.status_code, headers=headers)
|
|
|
|
else:
|
|
|
|
# For non-CSS content, return as is
|
2024-08-05 16:16:37 +02:00
|
|
|
headers = {
|
|
|
|
key: value
|
|
|
|
for key, value in response.headers.items()
|
|
|
|
if key.lower()
|
|
|
|
not in [
|
|
|
|
"connection",
|
|
|
|
"keep-alive",
|
|
|
|
"proxy-authenticate",
|
|
|
|
"proxy-authorization",
|
|
|
|
"te",
|
|
|
|
"trailers",
|
|
|
|
"transfer-encoding",
|
|
|
|
"upgrade",
|
|
|
|
]
|
|
|
|
}
|
2024-08-05 16:10:59 +02:00
|
|
|
return Response(response.content, status=response.status_code, headers=headers)
|
|
|
|
|
2024-08-05 16:16:37 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-11-27 12:43:40 +01:00
|
|
|
app.run(host="0.0.0.0", port=os.environ.get("PORT", 5690))
|