generated from nathanwoodburn/python-webserver-template
feat: Add proxy pac generator
All checks were successful
Build Docker / BuildImage (push) Successful in 54s
All checks were successful
Build Docker / BuildImage (push) Successful in 54s
This commit is contained in:
109
server.py
109
server.py
@@ -13,13 +13,101 @@ from flask import (
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import dotenv
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Proxy PAC configuration
|
||||
PROXY_ADDRESS = os.getenv('PROXY_ADDRESS', '127.0.0.1:9590')
|
||||
# URL for IANA TLD list
|
||||
IANA_TLD_URL = "https://data.iana.org/TLD/tlds-alpha-by-domain.txt"
|
||||
# Default TLDs to always skip (in addition to IANA list)
|
||||
DEFAULT_SKIP_TLDS = [
|
||||
'localhost', 'test', 'invalid', 'example', 'local',
|
||||
'arpa', 'onion', 'internal', 'private', 'home'
|
||||
]
|
||||
# Additional TLDs to skip specified in environment
|
||||
ADDITIONAL_SKIP_TLDS = os.getenv('ADDITIONAL_SKIP_TLDS', '').split(',')
|
||||
if ADDITIONAL_SKIP_TLDS == ['']:
|
||||
ADDITIONAL_SKIP_TLDS = []
|
||||
|
||||
# IANA TLD list cache
|
||||
tld_list_cache = {
|
||||
'tlds': [],
|
||||
'last_updated': None,
|
||||
'initialized': False
|
||||
}
|
||||
|
||||
def get_iana_tlds():
|
||||
"""Download and parse the IANA TLD list."""
|
||||
now = datetime.now()
|
||||
|
||||
# Check if we need to refresh the cache (daily)
|
||||
if (tld_list_cache['last_updated'] is None or
|
||||
now - tld_list_cache['last_updated'] > timedelta(days=1)):
|
||||
try:
|
||||
response = requests.get(IANA_TLD_URL)
|
||||
if response.status_code == 200:
|
||||
# Parse the TLD list (skip the header line, convert to lowercase)
|
||||
tlds = [line.strip().lower() for line in response.text.splitlines()
|
||||
if line.strip() and not line.startswith('#')]
|
||||
tld_list_cache['tlds'] = tlds
|
||||
tld_list_cache['last_updated'] = now
|
||||
tld_list_cache['initialized'] = True
|
||||
print(f"Downloaded {len(tlds)} TLDs from IANA")
|
||||
else:
|
||||
print(f"Failed to download IANA TLD list: {response.status_code}")
|
||||
# If we failed but have a previous cache, keep using it
|
||||
if not tld_list_cache['tlds']:
|
||||
# Otherwise use an empty list
|
||||
tld_list_cache['tlds'] = []
|
||||
except Exception as e:
|
||||
print(f"Error downloading IANA TLD list: {e}")
|
||||
# If exception occurs but we have a previous cache, keep using it
|
||||
if not tld_list_cache['tlds']:
|
||||
# Otherwise use an empty list
|
||||
tld_list_cache['tlds'] = []
|
||||
|
||||
# Combine IANA TLDs with our default skip TLDs and additional skip TLDs
|
||||
all_tlds = list(set(tld_list_cache['tlds'] + DEFAULT_SKIP_TLDS + ADDITIONAL_SKIP_TLDS))
|
||||
return all_tlds
|
||||
|
||||
def generate_pac_script(proxy_addr, skip_tlds):
|
||||
"""Generate a Proxy Auto-Configuration script."""
|
||||
skipped_tlds = "', '".join(skip_tlds)
|
||||
return f"""
|
||||
function FindProxyForURL(url, host) {{
|
||||
var skipped = ['{skipped_tlds}'];
|
||||
|
||||
// skip any TLD in the list
|
||||
var tld = host;
|
||||
var lastDot = tld.lastIndexOf('.');
|
||||
if (lastDot != -1) {{
|
||||
tld = tld.substr(lastDot+1);
|
||||
}}
|
||||
tld = tld.toLowerCase();
|
||||
|
||||
if (skipped.includes(tld)) {{
|
||||
return 'DIRECT';
|
||||
}}
|
||||
|
||||
// skip IP addresses
|
||||
var isIpV4Addr = /^(\\d+.)(\\d+.)(\\d+.)(\\d+)$/;
|
||||
if (isIpV4Addr.test(host)) {{
|
||||
return "DIRECT";
|
||||
}}
|
||||
|
||||
// loosely check if IPv6
|
||||
if (lastDot == -1 && host.split(':').length > 2) {{
|
||||
return "DIRECT";
|
||||
}}
|
||||
|
||||
return "PROXY {proxy_addr}";
|
||||
}}
|
||||
"""
|
||||
|
||||
def find(name, path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
@@ -59,6 +147,16 @@ def faviconPNG():
|
||||
return send_from_directory("templates/assets/img", "favicon.png")
|
||||
|
||||
|
||||
@app.route("/proxy.pac")
|
||||
def proxy_pac():
|
||||
"""Serve the Proxy Auto-Configuration file."""
|
||||
proxy_addr = PROXY_ADDRESS
|
||||
skip_tlds = get_iana_tlds()
|
||||
|
||||
response = make_response(generate_pac_script(proxy_addr, skip_tlds))
|
||||
response.headers["Content-Type"] = "application/x-ns-proxy-autoconfig"
|
||||
return response
|
||||
|
||||
@app.route("/.well-known/<path:path>")
|
||||
def wellknown(path):
|
||||
# Try to proxy to https://nathan.woodburn.au/.well-known/
|
||||
@@ -110,5 +208,14 @@ def not_found(e):
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# Replace before_first_request with a startup initialization
|
||||
# This runs once at server startup
|
||||
with app.app_context():
|
||||
get_iana_tlds()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Initialize TLD list at startup if running directly
|
||||
if not tld_list_cache['initialized']:
|
||||
get_iana_tlds()
|
||||
app.run(debug=True, port=5000, host="0.0.0.0")
|
||||
|
||||
Reference in New Issue
Block a user