Utoljára aktív 1714397012

Monitors OVH servers for mitigation status

__main__.py Eredeti
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 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")
49
50
51def _conn_from_toml():
52 filepath = Path(__file__).parent.resolve() / "config.toml"
53 if not filepath.exists():
54 raise OSError(filepath)
55 with open(filepath, "rb") as f:
56 return tomllib.load(f)
57
58
59def main():
60 conn = _conn_from_toml()
61 client = ovh.Client(
62 **conn["ovh"],
63 )
64
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 )
71 logger.info(ips)
72
73 webhook_url = conn["discord"]["webhook"]
74 while True:
75 check_ips_in_mitigation(webhook_url, client, ips)
76 time.sleep(30)
77
78
79if __name__ == "__main__":
80 main()
81