fail2ban_discord.py aktualisiert

This commit is contained in:
2025-05-15 07:06:31 +00:00
parent b0778cb024
commit 8190d6c863

View File

@ -4,13 +4,17 @@ import os
import re
import sqlite3
from dotenv import load_dotenv
import ipinfo
# .env laden
load_dotenv()
LOGFILE = "/var/log/fail2ban.log"
WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL")
GEO_API_URL = "https://ipapi.co/{ip}/json/"
TOKEN = os.getenv("IPINFO_TOKEN")
#ipinfo handler
handler = ipinfo.getHandler(TOKEN)
# SQLite-Datenbank
DB_FILE = "bans.db"
@ -46,29 +50,26 @@ def save_ban(ip, jail, country):
def get_country(ip):
try:
resp = requests.get(GEO_API_URL.format(ip=ip), timeout=5)
data = resp.json()
country_name = data.get("country_name", "Unbekannt")
country_code = data.get("country_code", "").upper()
return country_name, country_code
details = handler.getDetails(ip)
return details.country_name, details.country
except Exception as e:
print(f"[!] Fehler bei Geo-Abfrage: {e}")
return "Unbekannt", ""
def country_code_to_flag(country_code):
if not country_code or len(country_code) != 2:
return "🏳️" # neutrale Flagge
return chr(ord(country_code[0].upper()) + 127397) + chr(ord(country_code[1].upper()) + 127397)
def country_code_to_flag(country):
if not country or len(country) != 2:
return "No_Flag" # Discord-kompatibles Fallback
return f":flag_{country.lower()}:"
def send_discord_embed(ip, jail, country_name, country_code):
flag = country_code_to_flag(country_code)
def send_discord_embed(ip, jail, country_name, country):
flag = country_code_to_flag(country)
embed = {
"title": "🚨 Neue IP gesperrt durch Fail2Ban",
"title": "🚨 Neue IP gesperrt",
"color": 16711680,
"fields": [
{"name": "📛 Jail", "value": jail, "inline": True},
{"name": "🌍 IP-Adresse", "value": ip, "inline": True},
{"name": f"{flag} Herkunftsland", "value": f"{country_name} (`{country_code}`)", "inline": True}
{"name": f"{flag} Herkunftsland", "value": f"{country_name} (`{country}`)", "inline": True}
],
"footer": {"text": "Fail2Ban Benachrichtigung"}
}
@ -112,4 +113,4 @@ def monitor_fail2ban():
if __name__ == "__main__":
init_db()
monitor_fail2ban()
monitor_fail2ban()