Última atividade 1714397012

Monitors OVH servers for mitigation status

Revisão 34c38bf91c8363c5c6cee9800455052e1dd68687

__main__.py Bruto
1import ipaddress
2import logging
3import time
4
5import ovh
6
7try:
8 import tomllib
9except ModuleNotFoundError:
10 import tomli as tomllib
11
12from pathlib import Path
13
14from discord_webhook import DiscordWebhook
15
16logging.basicConfig(level=logging.INFO)
17logger = logging.getLogger("ddos-notifier")
18
19
20mitigation_list = []
21
22
23def 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
30def 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
52def _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
63def 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
76if __name__ == "__main__":
77 main()
78