diff --git a/README.md b/README.md index c4b7eff1..f0be89e8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # F2B Discord Notification - Docker mod which allows Fail2Ban Discord embeds +>[!WARNING] +>This mod is deprecated + +
+ This mod enhances the swag container adding better Fail2Ban notifications for discord. ## Configuration @@ -36,3 +41,5 @@ Action arguments: ## Example ![Example picture](.assets/Example.png) + +
diff --git a/root/AwesomeFolder/Fail2Ban.py b/root/AwesomeFolder/Fail2Ban.py index 509eabd1..9308da11 100644 --- a/root/AwesomeFolder/Fail2Ban.py +++ b/root/AwesomeFolder/Fail2Ban.py @@ -1,98 +1,18 @@ #!/usr/bin/env python3 -import argparse -import collections -import datetime -import geoip2.database -import os import requests -class Discord: - def __init__(self, data, action): - self.action = action - self.base = "https://discordapp.com/api/webhooks/" - self.data = data - self.token = os.getenv('DISC_HOOK', "") # If not setting enviroment variables, edit this - self.you = os.getenv('DISC_ME', "120970603556503552") # If not setting enviroment variables, edit this +base = "https://discordapp.com/api/webhooks/" +token = os.getenv('DISC_HOOK', False) +you = os.getenv('DISC_ME', False) - def create_payload(self): - webhook = { - "username":"Fail2Ban", - "content": f"<@{self.you}>", - "embeds": [{}] - } - webhook["embeds"][0]["author"] = {"name": "Fail2Ban"} - webhook["embeds"][0]["timestamp"] = f"{datetime.datetime.utcnow()}" - if "ban" in self.action.action: - webhook["embeds"][0]["url"] = f"https://db-ip.com/{self.data['ip']}" - webhook["embeds"][0]["image"] = {"url": f"{self.data['map-img']}"} - webhook["embeds"][0]["fields"] = [{}] - webhook["embeds"][0]["fields"][0]["name"] = f":flag_{self.data['iso'].lower()}:" - webhook["embeds"][0]["fields"][0]["value"] = self.data["city"] or self.data["name"] - if self.action.action == "ban": - webhook["embeds"][0]["fields"].append({"name": f"Map", "value": f"[Link]({self.data['map-url']})"}) - webhook["embeds"][0]["fields"].append({"name": f"Unban cmd", "value": f"fail2ban-client unban {self.data['ip']}"}) - webhook["embeds"][0]["title"] = f"New ban on `{self.action.jail}`" - webhook["embeds"][0]["description"] = f"**{self.data['ip']}** got banned for `{self.action.time}` hours after `{self.action.fail}` tries" - webhook["embeds"][0]["color"] = 16194076 - elif self.action.action == "unban": - webhook["embeds"][0]["title"] = f"Revoked ban on `{self.action.jail}`" - webhook["embeds"][0]["description"] = f"**{self.data['ip']}** is now unbanned" - webhook["embeds"][0]["color"] = 845872 - elif self.action.action == "start": - webhook["content"] = "" - webhook["embeds"][0]["description"] = f"Started `{self.action.jail}`" - webhook["embeds"][0]["color"] = 845872 - elif self.action.action == "stopped": - webhook["content"] = "" - webhook["embeds"][0]["description"] = f"Stopped `{self.action.jail}`" - webhook["embeds"][0]["color"] = 16194076 - elif self.action.action == "test": - webhook["content"] = "" - webhook["embeds"][0]["description"] = f"I am working" - webhook["embeds"][0]["color"] = 845872 - else: - return None - return webhook +content = f"<@{you}> " +message = "The fail2ban to discord mod is deprecated. Please remove it from your container" - def send(self, payload): - r = requests.post(url=f"{self.base}{self.token}", json=payload) +webhook = { + "username":"Fail2Ban", + "content": content + message if you else message + } -class Helpers: - def __init__(self, ip): - self.data = {"ip": ip} - self.map_api = os.getenv('DISC_API', "") # If not setting enviroment variables, edit this - self.reader = geoip2.database.Reader('/config/geoip2db/GeoLite2-City.mmdb') - self.f2b() - self.map() +if token: + requests.post(url=f"{base}{token}", json=webhook) - def f2b(self): - r = self.reader.city(self.data['ip']) - self.data["iso"] = r.country.iso_code - self.data["name"] = r.country.name - self.data["city"] = r.city.name - self.data["lat"] = r.location.latitude - self.data["lon"] = r.location.longitude - - - def map(self): - img_params={"center":f"{self.data['lat']},{self.data['lon']}", "size":"500,300", "key": self.map_api} - img_r = requests.get('https://www.mapquestapi.com/staticmap/v5/map', params=img_params) - self.data["map-img"] = img_r.url - url_params={"center":f"{self.data['lat']},{self.data['lon']}", "size":"500,300"} - url_r = requests.get('https://mapquest.com/', params=url_params) - self.data["map-url"] = url_r.url - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description='Discord notifier for F2B') - parser.add_argument('-a', '--action', help="Which F2B action triggered the script", required=True) - parser.add_argument('-i', '--ip', help="ip which triggered the action", default="8.8.8.8") - parser.add_argument('-j', '--jail', help="jail which triggered the action") - parser.add_argument('-t', '--time', help="The time the action is valid") - parser.add_argument('-f', '--fail', help="Amount of attempts done") - - args = parser.parse_args() - - data = Helpers(args.ip).data if "ban" in args.action else {} - disc = Discord(data, args) - if (payload := disc.create_payload()): - disc.send(payload) diff --git a/root/etc/s6-overlay/s6-rc.d/init-mod-swag-f2bdiscord-add-package/run b/root/etc/s6-overlay/s6-rc.d/init-mod-swag-f2bdiscord-add-package/run index c6cbdf07..f922d8b5 100755 --- a/root/etc/s6-overlay/s6-rc.d/init-mod-swag-f2bdiscord-add-package/run +++ b/root/etc/s6-overlay/s6-rc.d/init-mod-swag-f2bdiscord-add-package/run @@ -1,13 +1,20 @@ #!/usr/bin/with-contenv bash +echo "_ _ _ " +echo "| | | |" +echo "| | | | The f2b to discord mod is deprecated" +echo "|_|_|_|" +echo "(_|_|_)" + -if ! pip list 2>&1 | grep -q "geoip2"; then +if ! pip list 2>&1 | grep -q "requests"; then echo '------------------------------------------------------------------------' - echo '| Adding required modules for swag-f2bdiscord to package install list' + echo '| Adding required modules for swag-f2bdiscord deprecation message' echo '------------------------------------------------------------------------' - echo "\ - requests \ - argparse \ - geoip2==3.0.0" >> /mod-pip-packages-to-install.list + echo "requests" >> /mod-pip-packages-to-install.list +fi + +if [ ! -f /config/fail2ban/Fail2Ban.py.orig ] && [ -f /config/fail2ban/Fail2Ban.py ]; then + mv /config/fail2ban/Fail2Ban.py /config/fail2ban/Fail2Ban.py.orig fi if [ ! -f /config/fail2ban/Fail2Ban.py ]; then @@ -15,6 +22,3 @@ if [ ! -f /config/fail2ban/Fail2Ban.py ]; then chmod +x /config/fail2ban/Fail2Ban.py fi -if [ ! -f /config/fail2ban/action.d/discordEmbed.conf ]; then - cp /AwesomeFolder/discordEmbed.conf /config/fail2ban/action.d -fi