haproxy map

This commit is contained in:
Mateusz Gruszczyński
2026-02-23 15:15:39 +01:00
parent 166b55a632
commit 98acbc0119

View File

@@ -1147,21 +1147,26 @@ class ConfigGenerator:
@staticmethod @staticmethod
def generate_haproxy_map(country_networks: dict, aggregate: bool = True, redis_ips: set = None) -> str: def generate_haproxy_map(country_networks: dict, aggregate: bool = True, redis_ips: set = None) -> str:
""" """
Generate HAProxy MAP file (IP COUNTRY format) Generate HAProxy MAP file (CIDR COUNTRY format)
""" """
# Get metadata
countries = sorted(country_networks.keys()) countries = sorted(country_networks.keys())
redis_stats = None
redisstats = None
if redis_ips: if redis_ips:
redisstats = {"total": len(redis_ips), "unique": len(redis_ips), "deduped": 0} redis_stats = {
'total': len(redis_ips),
'unique': len(redis_ips),
'deduped': 0
}
handler = GeoIPHandler() handler = GeoIPHandler()
metadata = generate_metadata(countries, country_networks, redisstats, handler) metadata = generate_metadata(countries, country_networks, redis_stats, handler)
# Aggregate networks # Aggregate networks (for header stats only, same style as ACL)
all_networks = [] all_networks = []
for networks in country_networks.values(): for nets in country_networks.values():
all_networks.extend(networks) all_networks.extend(nets)
if redis_ips: if redis_ips:
all_networks.extend(redis_ips) all_networks.extend(redis_ips)
@@ -1170,7 +1175,7 @@ class ConfigGenerator:
else: else:
all_networks = sorted(list(set(all_networks))) all_networks = sorted(list(set(all_networks)))
# Generate header # Generate header (same style as ACL)
config = "# " + "="*77 + "\n" config = "# " + "="*77 + "\n"
config += "# HAProxy MAP Configuration\n" config += "# HAProxy MAP Configuration\n"
config += f"# Generated: {metadata['timestamp']}\n" config += f"# Generated: {metadata['timestamp']}\n"
@@ -1192,20 +1197,37 @@ class ConfigGenerator:
config += f"# Database: {metadata['cache_db_path']}\n" config += f"# Database: {metadata['cache_db_path']}\n"
config += "# \n" config += "# \n"
config += "# Usage in HAProxy:\n" config += "# Usage in HAProxy:\n"
config += "# acl banned_ips src -f /path/to/this_file.acl\n" config += "# map_beg(/path/to/geo.map) -m ip $src var(txn.country)\n"
config += "# http-request deny if banned_ips\n"
config += "# \n" config += "# \n"
config += "# " + "="*77 + "\n" config += "# " + "="*77 + "\n"
config += "\n" config += "\n"
# MAP BODY # MAP BODY (per-country aggregation => poprawny country, brak XX, brak pustych wyników)
for network in all_networks: for country_code, nets in sorted(country_networks.items()):
country = next((c for c, nets in country_networks.items() if network in nets), 'XX') if not nets:
config += f"{network} {country}\n" continue
if aggregate:
nets = ConfigGenerator._aggregate_networks(nets)
else:
nets = sorted(list(set(nets)))
for network in nets:
config += f"{network} {country_code}\n"
# Redis IPs (opcjonalnie jako osobna etykieta)
if redis_ips:
redis_list = list(redis_ips)
if aggregate:
redis_list = ConfigGenerator._aggregate_networks(redis_list)
else:
redis_list = sorted(list(set(redis_list)))
for network in redis_list:
config += f"{network} REDIS\n"
return config return config
@staticmethod @staticmethod
def generate_haproxy_lua(country_networks: dict, aggregate: bool = True, redis_ips: set = None) -> str: def generate_haproxy_lua(country_networks: dict, aggregate: bool = True, redis_ips: set = None) -> str:
"""Generate HAProxy Lua script with detailed metadata header""" """Generate HAProxy Lua script with detailed metadata header"""