__main__.py
· 1.9 KiB · Python
Eredeti
import ipaddress
import logging
import time
import ovh
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
from pathlib import Path
from discord_webhook import DiscordWebhook
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("ddos-notifier")
mitigation_list = []
def send_webhook_message(webhook_url, msg):
webhook = DiscordWebhook(
url=webhook_url, username="ovh-servers-mitigation", content=msg
)
webhook.execute()
def check_ips_in_mitigation(webhook_url, client, ips):
for ip in [ipaddress.ip_address(ip.split("/")[0]) for ip in ips]:
if ip.version == 4:
logger.debug(f"Checking {ip}")
try:
_ = client.get(f"/ip/{ip}/mitigation/{ip}")
if ip not in mitigation_list:
mitigation_list.append(ip)
send_webhook_message(
webhook_url, f"{ip} is in mitigation! DDOS detected."
)
logger.info(f"{ip} is being ddosed")
except ovh.exceptions.ResourceNotFoundError:
if ip in mitigation_list:
mitigation_list.remove(ip)
send_webhook_message(
webhook_url, f"{ip} is out of mitigation, DDOS over."
)
logger.info(f"{ip} is no longer being ddosed")
def _conn_from_toml():
filepath = Path(__file__).parent.resolve() / "config.toml"
if not filepath.exists():
raise OSError(filepath)
with open(filepath, "rb") as f:
return tomllib.load(f)
def main():
conn = _conn_from_toml()
client = ovh.Client(
**conn["ovh"],
)
ips = client.get("/ip")
logger.info(ips)
webhook_url = conn["discord"]["webhook"]
while True:
check_ips_in_mitigation(webhook_url, client, ips)
time.sleep(30)
if __name__ == "__main__":
main()
| 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 | def main(): |
| 61 | conn = _conn_from_toml() |
| 62 | client = ovh.Client( |
| 63 | **conn["ovh"], |
| 64 | ) |
| 65 | |
| 66 | ips = client.get("/ip") |
| 67 | logger.info(ips) |
| 68 | |
| 69 | webhook_url = conn["discord"]["webhook"] |
| 70 | while True: |
| 71 | check_ips_in_mitigation(webhook_url, client, ips) |
| 72 | time.sleep(30) |
| 73 | |
| 74 | |
| 75 | if __name__ == "__main__": |
| 76 | main() |
| 77 |