typenot/app.py

77 lines
2.1 KiB
Python
Raw Permalink Normal View History

from flask import Flask, request, Response, render_template
import requests
import re
import os
app = Flask(__name__)
FONT_URL = "https://use.typekit.net"
PATTERN = r"use\.typekit\.net"
# P_PATTERN: Any *line* that contains p.typekit.net will be removed
P_PATTERN = r".*p\.typekit\.net.*\n"
@app.route("/")
def home():
domain = request.url_root
return render_template("index.html", domain=domain)
@app.route("/<path:path>", methods=["GET"])
def proxy_fonts(path):
query_string = request.query_string.decode("utf-8")
url = f"{FONT_URL}/{path}"
if query_string:
url += f"?{query_string}"
2024-11-25 17:42:50 +01:00
headers = {
2024-11-25 17:42:50 +01:00
"Accept-Encoding": "",
}
response = requests.get(url, headers=headers)
if "text/css" in response.headers.get("Content-Type", ""):
content = response.content.decode("utf-8")
content = re.sub(PATTERN, request.host, content)
content = re.sub(P_PATTERN, "", content)
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",
]
}
return Response(content, status=response.status_code, headers=headers)
else:
# For non-CSS content, return as is
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",
]
}
return Response(response.content, status=response.status_code, headers=headers)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=os.environ.get("PORT", 5690))