googledonts/app.py
Kumi 5a4d3aa9e7
feat: Add README and dynamic port configuration
Introduces a README with setup and usage instructions for the GoogleDonts app, enhancing accessibility and guidance for users.

Adapts the app to use an environment variable for port configuration, allowing flexibility in deployment environments, which defaults to port 5678 when unset.

Refactors request headers to exclude "Accept-Encoding", ensuring compatibility with font responses.

This update aims to support user autonomy in hosting and increase privacy assurance.

Relates to potential improvements in user deployment experiences and privacy measures.
2024-11-27 12:50:44 +01:00

104 lines
2.8 KiB
Python

from flask import Flask, request, Response, render_template
import requests
import re
import os
app = Flask(__name__)
FONT_STATIC_URL = "https://fonts.gstatic.com"
FONT_API_URL = "https://fonts.googleapis.com"
PATTERN = rb"fonts\.gstatic\.com"
@app.route("/")
def home():
domain = request.url_root
return render_template("index.html", domain=domain)
@app.route("/s/<path:path>", methods=["GET"])
def proxy_fonts_static(path):
url = f"{FONT_STATIC_URL}/s/{path}"
response = requests.get(url, stream=True)
def generate():
for chunk in response.iter_content(chunk_size=8192):
yield chunk
# Remove hop-by-hop headers
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(generate(), status=response.status_code, headers=headers)
@app.route("/<path:path>", methods=["GET"])
def proxy_fonts_api(path):
query_string = request.query_string.decode("utf-8")
url = f"{FONT_API_URL}/{path}"
if query_string:
url += f"?{query_string}"
headers = {
"Accept-Encoding": ""
}
response = requests.get(url, headers=headers)
replacement = request.host.encode(
"utf-8"
) # Convert the host to bytes for replacement
if "text/css" in response.headers.get("Content-Type", ""):
content = response.content
content = re.sub(PATTERN, replacement, 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", 5678))