diff --git a/.gitignore b/.gitignore index c631eb7..409bc69 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ config/*.ign config/*.iso # website -website/sendgrid.key +website/discord.txt # minecraft minecraft/server.properties diff --git a/config/server.example b/config/server.example index f067725..706f0bb 100644 --- a/config/server.example +++ b/config/server.example @@ -17,7 +17,7 @@ } ], "website": { - "sendgrid_key": "" + "discord_hook": "" }, "minecraft": { "world": "main" diff --git a/config/update.py b/config/update.py index cf83a99..803b017 100755 --- a/config/update.py +++ b/config/update.py @@ -27,8 +27,8 @@ IMAGES = { def generate(cfg): # website - with open("website/sendgrid.key", "w") as f: - f.write(cfg["website"]["sendgrid_key"]) + with open("website/discord.txt", "w") as f: + f.write(cfg["website"]["discord_hook"]) # minecraft shutil.copy("minecraft/server.default", "minecraft/server.properties") diff --git a/website/Dockerfile b/website/Dockerfile index c31a568..1289c28 100644 --- a/website/Dockerfile +++ b/website/Dockerfile @@ -6,13 +6,13 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update && apt-get -y upgrade RUN apt-get install -y nginx certbot python3-pip -RUN pip3 install sendgrid --break-system-packages +RUN pip3 install requests --break-system-packages WORKDIR /root # TODO make the website code not terrible ;-; COPY html /var/www/html -COPY sendgrid.key ip.py ./ +COPY discord.txt ip.py ./ COPY server.conf entry.sh ./ CMD ["/bin/bash", "/root/entry.sh"] diff --git a/website/ip.py b/website/ip.py index b28845e..48301da 100755 --- a/website/ip.py +++ b/website/ip.py @@ -1,45 +1,44 @@ #!/usr/bin/env python3 +import requests import time -import urllib.request from ipaddress import ip_network from pathlib import Path -from sendgrid import SendGridAPIClient -from sendgrid.helpers.mail import Mail if __name__ == "__main__": - sg = SendGridAPIClient(Path("sendgrid.key").read_text()) + link = Path("discord.txt").read_text() old_ipv4, old_ipv6 = None, None while True: + # get current ips try: - ipv4 = urllib.request.urlopen("https://v4.ident.me").read().decode("utf8") - ipv6 = urllib.request.urlopen("https://v6.ident.me").read().decode("utf8") + ipv4 = requests.get("https://v4.ident.me").text + ipv6 = requests.get("https://v6.ident.me").text ipv6 = str(ip_network(ipv6 + "/64", strict=False).network_address) # xfinity gives /64 except Exception as e: - print(e) + print("Error while getting IP", e) time.sleep(60) continue + # send message if either changed if ipv4 != old_ipv4 or ipv6 != old_ipv6: - msg = Mail( - from_email="mtran319@gmail.com", - to_emails="mtran319@gmail.com", - subject="pls update ip", - html_content=( - f"

old ipv4: {old_ipv4}

" - f"

old ipv6: {old_ipv6}

" - f"

new ipv4: {ipv4}

" - f"

new ipv6: {ipv6}

" - )) try: print(f"IP changed to {ipv4} and {ipv6}") - resp = sg.send(msg) + resp = requests.post(link, json={ + "content": "\n".join([ + f"old ipv4: {old_ipv4}", + f"old ipv6: {old_ipv6}", + f"new ipv4: {ipv4}", + f"new ipv6: {ipv6}", + ]) + }) + assert(resp.status_code == 204) except Exception as e: - print(e) - sg = SendGridAPIClient(Path("sendgrid.key").read_text()) + print("Error while sending update", e) time.sleep(60) continue + # retry every hour + print("Retrying in 1 hour...") old_ipv4, old_ipv6 = ipv4, ipv6 time.sleep(3600)