commit f77ec2c177b13f00aa1963691a56be65360bbae4 Author: Kumi Date: Mon Aug 5 16:10:59 2024 +0200 feat: initialize Flask proxy app with gitignore and license - Added .gitignore to exclude virtual environment and Python cache files. - Included MIT License to allow use, distribution, and modification. - Implemented Flask app for proxying font requests to Google Fonts. - Ensured specific headers are handled correctly to support CSS proxying. - Added necessary dependencies in requirements.txt for Flask and requests. This setup allows for efficient font request handling and helps establish project structure with appropriate licensing and dependency management. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f64719 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv/ +__pyache__/ +*.pyc \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3788438 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Private.coffee Team + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/app.py b/app.py new file mode 100644 index 0000000..11ce0bf --- /dev/null +++ b/app.py @@ -0,0 +1,52 @@ +from flask import Flask, request, Response +import requests +import re + +app = Flask(__name__) + +FONT_STATIC_URL = 'https://fonts.gstatic.com' +FONT_API_URL = 'https://fonts.googleapis.com' + +PATTERN = rb'fonts\.gstatic\.com' + +@app.route('/s/', 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('/', 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 = {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 + + 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=5678) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5eaf725 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +requests \ No newline at end of file