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.
This commit is contained in:
Kumi 2024-08-05 16:10:59 +02:00
commit f77ec2c177
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 76 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
venv/
__pyache__/
*.pyc

19
LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) 2024 Private.coffee Team <support@private.coffee>
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.

52
app.py Normal file
View file

@ -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/<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 = {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)

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
flask
requests