diff --git a/geoip_handler.py b/geoip_handler.py index 09e1115..1177aab 100644 --- a/geoip_handler.py +++ b/geoip_handler.py @@ -1198,13 +1198,27 @@ class ConfigGenerator: config += "# " + "="*77 + "\n" config += "\n" - # MAP BODY - for network in all_networks: - country = next((c for c, nets in country_networks.items() if network in nets), 'XX') - config += f"{network} {country}\n" + # MAP BODY (aggregate per country to preserve country mapping) + for country, nets in sorted(country_networks.items()): + if aggregate: + nets = ConfigGenerator._aggregate_networks(nets) + else: + nets = sorted(set(nets)) - return config + for network in nets: + config += f"{network} {country}\n" + if redis_ips: + redis_list = list(redis_ips) + if aggregate: + redis_list = ConfigGenerator._aggregate_networks(redis_list) + else: + redis_list = sorted(set(redis_list)) + + for network in redis_list: + config += f"{network} REDIS\n" + + return config @staticmethod def generate_haproxy_lua(country_networks: dict, aggregate: bool = True, redis_ips: set = None) -> str: