onyx-and-iris 已修改 . 還原成這個修訂版本
1 file changed, 24 insertions, 20 deletions
__main__.py
| @@ -28,25 +28,24 @@ def send_webhook_message(webhook_url, msg): | |||
| 28 | 28 | ||
| 29 | 29 | ||
| 30 | 30 | def check_ips_in_mitigation(webhook_url, client, ips): | |
| 31 | - | for ip in [ipaddress.ip_address(ip.split("/")[0]) for ip in ips]: | |
| 32 | - | if ip.version == 4: | |
| 33 | - | logger.debug(f"Checking {ip}") | |
| 34 | - | try: | |
| 35 | - | _ = client.get(f"/ip/{ip}/mitigation/{ip}") | |
| 36 | - | ||
| 37 | - | if ip not in mitigation_list: | |
| 38 | - | mitigation_list.append(ip) | |
| 39 | - | send_webhook_message( | |
| 40 | - | webhook_url, f"{ip} is in mitigation! DDOS detected." | |
| 41 | - | ) | |
| 42 | - | logger.info(f"{ip} is being ddosed") | |
| 43 | - | except ovh.exceptions.ResourceNotFoundError: | |
| 44 | - | if ip in mitigation_list: | |
| 45 | - | mitigation_list.remove(ip) | |
| 46 | - | send_webhook_message( | |
| 47 | - | webhook_url, f"{ip} is out of mitigation, DDOS over." | |
| 48 | - | ) | |
| 49 | - | logger.info(f"{ip} is no longer being ddosed") | |
| 31 | + | for ip in ips: | |
| 32 | + | logger.debug(f"Checking {ip}") | |
| 33 | + | try: | |
| 34 | + | _ = client.get(f"/ip/{ip}/mitigation/{ip}") | |
| 35 | + | ||
| 36 | + | if ip not in mitigation_list: | |
| 37 | + | mitigation_list.append(ip) | |
| 38 | + | send_webhook_message( | |
| 39 | + | webhook_url, f"{ip} is in mitigation! DDOS detected." | |
| 40 | + | ) | |
| 41 | + | logger.info(f"{ip} is being ddosed") | |
| 42 | + | except ovh.exceptions.ResourceNotFoundError: | |
| 43 | + | if ip in mitigation_list: | |
| 44 | + | mitigation_list.remove(ip) | |
| 45 | + | send_webhook_message( | |
| 46 | + | webhook_url, f"{ip} is out of mitigation, DDOS over." | |
| 47 | + | ) | |
| 48 | + | logger.info(f"{ip} is no longer being ddosed") | |
| 50 | 49 | ||
| 51 | 50 | ||
| 52 | 51 | def _conn_from_toml(): | |
| @@ -63,7 +62,12 @@ def main(): | |||
| 63 | 62 | **conn["ovh"], | |
| 64 | 63 | ) | |
| 65 | 64 | ||
| 66 | - | ips = client.get("/ip") | |
| 65 | + | ips = list( | |
| 66 | + | filter( | |
| 67 | + | lambda ip: ipaddress.ip_address(ip).version == 4, | |
| 68 | + | [ip.split("/")[0] for ip in client.get("/ip")], | |
| 69 | + | ) | |
| 70 | + | ) | |
| 67 | 71 | logger.info(ips) | |
| 68 | 72 | ||
| 69 | 73 | webhook_url = conn["discord"]["webhook"] | |
onyx_online 已修改 . 還原成這個修訂版本
1 file changed, 3 insertions, 5 deletions
__main__.py
| @@ -55,20 +55,18 @@ def _conn_from_toml(): | |||
| 55 | 55 | raise OSError(filepath) | |
| 56 | 56 | with open(filepath, "rb") as f: | |
| 57 | 57 | return tomllib.load(f) | |
| 58 | - | ||
| 59 | - | ||
| 60 | - | __conn = _conn_from_toml() | |
| 61 | 58 | ||
| 62 | 59 | ||
| 63 | 60 | def main(): | |
| 61 | + | conn = _conn_from_toml() | |
| 64 | 62 | client = ovh.Client( | |
| 65 | - | **__conn["ovh"], | |
| 63 | + | **conn["ovh"], | |
| 66 | 64 | ) | |
| 67 | 65 | ||
| 68 | 66 | ips = client.get("/ip") | |
| 69 | 67 | logger.info(ips) | |
| 70 | 68 | ||
| 71 | - | webhook_url = __conn["discord"]["webhook"] | |
| 69 | + | webhook_url = conn["discord"]["webhook"] | |
| 72 | 70 | while True: | |
| 73 | 71 | check_ips_in_mitigation(webhook_url, client, ips) | |
| 74 | 72 | time.sleep(30) | |
onyx_online 已修改 . 還原成這個修訂版本
1 file changed, 2 insertions, 1 deletion
__main__.py
| @@ -68,8 +68,9 @@ def main(): | |||
| 68 | 68 | ips = client.get("/ip") | |
| 69 | 69 | logger.info(ips) | |
| 70 | 70 | ||
| 71 | + | webhook_url = __conn["discord"]["webhook"] | |
| 71 | 72 | while True: | |
| 72 | - | check_ips_in_mitigation(__conn["discord"]["webhook"], client, ips) | |
| 73 | + | check_ips_in_mitigation(webhook_url, client, ips) | |
| 73 | 74 | time.sleep(30) | |
| 74 | 75 | ||
| 75 | 76 | ||
onyx_online 已修改 . 還原成這個修訂版本
1 file changed, 77 insertions
__main__.py(檔案已創建)
| @@ -0,0 +1,77 @@ | |||
| 1 | + | import ipaddress | |
| 2 | + | import logging | |
| 3 | + | import time | |
| 4 | + | ||
| 5 | + | import ovh | |
| 6 | + | ||
| 7 | + | try: | |
| 8 | + | import tomllib | |
| 9 | + | except ModuleNotFoundError: | |
| 10 | + | import tomli as tomllib | |
| 11 | + | ||
| 12 | + | from pathlib import Path | |
| 13 | + | ||
| 14 | + | from discord_webhook import DiscordWebhook | |
| 15 | + | ||
| 16 | + | logging.basicConfig(level=logging.INFO) | |
| 17 | + | logger = logging.getLogger("ddos-notifier") | |
| 18 | + | ||
| 19 | + | ||
| 20 | + | mitigation_list = [] | |
| 21 | + | ||
| 22 | + | ||
| 23 | + | def send_webhook_message(webhook_url, msg): | |
| 24 | + | webhook = DiscordWebhook( | |
| 25 | + | url=webhook_url, username="ovh-servers-mitigation", content=msg | |
| 26 | + | ) | |
| 27 | + | webhook.execute() | |
| 28 | + | ||
| 29 | + | ||
| 30 | + | def check_ips_in_mitigation(webhook_url, client, ips): | |
| 31 | + | for ip in [ipaddress.ip_address(ip.split("/")[0]) for ip in ips]: | |
| 32 | + | if ip.version == 4: | |
| 33 | + | logger.debug(f"Checking {ip}") | |
| 34 | + | try: | |
| 35 | + | _ = client.get(f"/ip/{ip}/mitigation/{ip}") | |
| 36 | + | ||
| 37 | + | if ip not in mitigation_list: | |
| 38 | + | mitigation_list.append(ip) | |
| 39 | + | send_webhook_message( | |
| 40 | + | webhook_url, f"{ip} is in mitigation! DDOS detected." | |
| 41 | + | ) | |
| 42 | + | logger.info(f"{ip} is being ddosed") | |
| 43 | + | except ovh.exceptions.ResourceNotFoundError: | |
| 44 | + | if ip in mitigation_list: | |
| 45 | + | mitigation_list.remove(ip) | |
| 46 | + | send_webhook_message( | |
| 47 | + | webhook_url, f"{ip} is out of mitigation, DDOS over." | |
| 48 | + | ) | |
| 49 | + | logger.info(f"{ip} is no longer being ddosed") | |
| 50 | + | ||
| 51 | + | ||
| 52 | + | def _conn_from_toml(): | |
| 53 | + | filepath = Path(__file__).parent.resolve() / "config.toml" | |
| 54 | + | if not filepath.exists(): | |
| 55 | + | raise OSError(filepath) | |
| 56 | + | with open(filepath, "rb") as f: | |
| 57 | + | return tomllib.load(f) | |
| 58 | + | ||
| 59 | + | ||
| 60 | + | __conn = _conn_from_toml() | |
| 61 | + | ||
| 62 | + | ||
| 63 | + | def main(): | |
| 64 | + | client = ovh.Client( | |
| 65 | + | **__conn["ovh"], | |
| 66 | + | ) | |
| 67 | + | ||
| 68 | + | ips = client.get("/ip") | |
| 69 | + | logger.info(ips) | |
| 70 | + | ||
| 71 | + | while True: | |
| 72 | + | check_ips_in_mitigation(__conn["discord"]["webhook"], client, ips) | |
| 73 | + | time.sleep(30) | |
| 74 | + | ||
| 75 | + | ||
| 76 | + | if __name__ == "__main__": | |
| 77 | + | main() | |