refactor: optimize docker, robust config & country handling
All checks were successful
🚀 Docker Build and Push / build-and-push (push) Successful in 59s

- optimize(docker): use distroless/python3-debian12 & multi-stage build
- feat(app): replace Gunicorn with Waitress (pure python)
- feat(countries): replace static list with pycountry (dynamic & complete)
- feat(config): enforce mandatory APPRISE_URL (fatal if missing)
- feat(logging): add structured logging and graceful shutdown signal handling
- docs: update README with required env var and correct usage
This commit is contained in:
2025-12-31 18:46:21 +01:00
parent eef8c3835e
commit d180cb700c
4 changed files with 106 additions and 29 deletions

96
app.py
View File

@@ -6,34 +6,55 @@ Author: Maxime Killinger
"""
import os
import logging
from flask import Flask, request, jsonify
import requests
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
app = Flask(__name__)
APPRISE_URL = os.environ.get("APPRISE_URL", "http://192.168.1.118:8000/notify/telegram_alerts")
import pycountry
# Country codes to full names
COUNTRIES = {
"CN": "Chine", "RU": "Russie", "US": "États-Unis", "FR": "France",
"DE": "Allemagne", "GB": "Royaume-Uni", "NL": "Pays-Bas", "BR": "Brésil",
"IN": "Inde", "KR": "Corée du Sud", "JP": "Japon", "VN": "Vietnam",
"ID": "Indonésie", "TW": "Taïwan", "HK": "Hong Kong", "SG": "Singapour",
"TH": "Thaïlande", "PH": "Philippines", "MY": "Malaisie", "PK": "Pakistan",
"BD": "Bangladesh", "IR": "Iran", "TR": "Turquie", "UA": "Ukraine",
"PL": "Pologne", "RO": "Roumanie", "IT": "Italie", "ES": "Espagne",
"AR": "Argentine", "MX": "Mexique", "CO": "Colombie", "CL": "Chili",
"ZA": "Afrique du Sud", "EG": "Égypte", "NG": "Nigeria", "KE": "Kenya",
"AU": "Australie", "NZ": "Nouvelle-Zélande", "CA": "Canada", "SE": "Suède",
"NO": "Norvège", "FI": "Finlande", "DK": "Danemark", "BE": "Belgique",
"CH": "Suisse", "AT": "Autriche", "CZ": "Tchéquie", "HU": "Hongrie",
"BG": "Bulgarie", "GR": "Grèce", "PT": "Portugal", "IE": "Irlande",
}
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
app = Flask(__name__)
APPRISE_URL = os.environ.get("APPRISE_URL")
def get_country_name(code):
if not code:
return "Inconnu"
return COUNTRIES.get(code.upper(), code)
if not code or code.lower() == 'unknown':
logger.warning(f"Country code is unknown or empty: {code}")
return "Unknown"
try:
# Standardize code to alpha_2
# Clean string just in case
clean_code = code.strip().upper()
if len(clean_code) != 2:
logger.warning(f"Invalid country code format: {code}")
return clean_code
country = pycountry.countries.get(alpha_2=clean_code)
if country:
return country.name
logger.warning(f"Country code not found in pycountry: {clean_code}")
return clean_code
except Exception as e:
logger.warning(f"Error resolving country code {code}: {e}")
return code
def format_scenario(scenario):
if not scenario:
@@ -45,6 +66,7 @@ def handle_crowdsec():
try:
alerts = request.json
if not alerts:
logger.debug("Received empty alert payload")
return jsonify({"status": "no data"}), 200
# Group by IP
@@ -74,10 +96,19 @@ def handle_crowdsec():
body = "\n".join(lines).strip()
title = f"🛡️ CrowdSec - {num_ips} IP{'s' if num_ips > 1 else ''} bannie{'s' if num_ips > 1 else ''}"
requests.post(APPRISE_URL, json={"title": title, "body": body}, timeout=10)
# Send to Apprise
response = requests.post(APPRISE_URL, json={"title": title, "body": body}, timeout=10)
if response.ok:
ips_list = ", ".join(ip_groups.keys())
logger.info(f"Notification sent: {num_ips} IP(s) banned [{ips_list}]")
else:
logger.error(f"Apprise error: {response.status_code} - {response.text}")
return jsonify({"status": "sent", "ips": num_ips}), 200
except Exception as e:
logger.exception(f"Error processing CrowdSec alert: {e}")
return jsonify({"status": "error", "message": str(e)}), 500
@app.route('/health', methods=['GET'])
@@ -85,4 +116,25 @@ def health():
return jsonify({"status": "ok"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
from waitress import serve
import sys
import signal
if not APPRISE_URL:
logger.critical("APPRISE_URL environment variable is not set")
sys.exit(1)
def handle_signal(signum, frame):
logger.info(f"Received signal {signum}. Shutting down gracefully...")
# Waitress does not have a public stop method when run this way easily,
# but sys.exit(0) from a signal handler usually works to terminate.
# However, waitress catches signals if passed to serve?
# Actually waitress handles signals by itself if running in main.
# But we want to LOG it.
sys.exit(0)
signal.signal(signal.SIGTERM, handle_signal)
signal.signal(signal.SIGINT, handle_signal)
logger.info(f"Starting CrowdSec Notify - Apprise URL: {APPRISE_URL}")
serve(app, host='0.0.0.0', port=5000)