diff --git a/app.py b/app.py index 11ce0bf..e6eb75e 100644 --- a/app.py +++ b/app.py @@ -1,15 +1,22 @@ -from flask import Flask, request, Response +from flask import Flask, request, Response, render_template import requests import re app = Flask(__name__) -FONT_STATIC_URL = 'https://fonts.gstatic.com' -FONT_API_URL = 'https://fonts.googleapis.com' +FONT_STATIC_URL = "https://fonts.gstatic.com" +FONT_API_URL = "https://fonts.googleapis.com" -PATTERN = rb'fonts\.gstatic\.com' +PATTERN = rb"fonts\.gstatic\.com" -@app.route('/s/', methods=['GET']) + +@app.route("/") +def home(): + domain = request.url_root + return render_template("index.html", domain=domain) + + +@app.route("/s/", methods=["GET"]) def proxy_fonts_static(path): url = f"{FONT_STATIC_URL}/s/{path}" response = requests.get(url, stream=True) @@ -19,34 +26,81 @@ def proxy_fonts_static(path): 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']} + 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('/', methods=['GET']) + +@app.route("/", methods=["GET"]) def proxy_fonts_api(path): - query_string = request.query_string.decode('utf-8') + query_string = request.query_string.decode("utf-8") url = f"{FONT_API_URL}/{path}" if query_string: url += f"?{query_string}" - headers = {key: value for (key, value) in request.headers.items() if key.lower() != 'host'} - headers['Accept-Encoding'] = '' # Disable Accept-Encoding to avoid compressed responses + headers = { + key: value for (key, value) in request.headers.items() if key.lower() != "host" + } + headers["Accept-Encoding"] = ( + "" # Disable Accept-Encoding to avoid compressed responses + ) response = requests.get(url, headers=headers) - replacement = request.host.encode('utf-8') # Convert the host to bytes for replacement + replacement = request.host.encode( + "utf-8" + ) # Convert the host to bytes for replacement - if 'text/css' in response.headers.get('Content-Type', ''): + 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']} + 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']} + 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=5678) \ No newline at end of file + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5678) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6bcaa1e --- /dev/null +++ b/templates/index.html @@ -0,0 +1,65 @@ + + + + + + GoogleDonts + + + + +
+
+

Welcome to GoogleDonts

+

+ GoogleDonts is a proxy service to Google Fonts. It allows you to use + Google Fonts without any data being sent to Google. +

+
+
+
+

+ To use the proxy, simply replace + https://fonts.googleapis.com/ with {{ domain }} in + the URL. +

+

Example:

+
+ {{ domain }}css?family=Montserrat:400,700 +
+
+ + + +