use discord instead of sendgrid

This commit is contained in:
Matthew Tran 2025-05-06 03:31:31 -07:00
parent 8bd3def755
commit 516a02060c
5 changed files with 25 additions and 26 deletions

2
.gitignore vendored
View File

@ -10,7 +10,7 @@ config/*.ign
config/*.iso config/*.iso
# website # website
website/sendgrid.key website/discord.txt
# minecraft # minecraft
minecraft/server.properties minecraft/server.properties

View File

@ -17,7 +17,7 @@
} }
], ],
"website": { "website": {
"sendgrid_key": "<SendGrid API key from https://app.sendgrid.com/settings/api_keys>" "discord_hook": "<Secret Discord Webhook>"
}, },
"minecraft": { "minecraft": {
"world": "main" "world": "main"

View File

@ -27,8 +27,8 @@ IMAGES = {
def generate(cfg): def generate(cfg):
# website # website
with open("website/sendgrid.key", "w") as f: with open("website/discord.txt", "w") as f:
f.write(cfg["website"]["sendgrid_key"]) f.write(cfg["website"]["discord_hook"])
# minecraft # minecraft
shutil.copy("minecraft/server.default", "minecraft/server.properties") shutil.copy("minecraft/server.default", "minecraft/server.properties")

View File

@ -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 update && apt-get -y upgrade
RUN apt-get install -y nginx certbot python3-pip 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 WORKDIR /root
# TODO make the website code not terrible ;-; # TODO make the website code not terrible ;-;
COPY html /var/www/html COPY html /var/www/html
COPY sendgrid.key ip.py ./ COPY discord.txt ip.py ./
COPY server.conf entry.sh ./ COPY server.conf entry.sh ./
CMD ["/bin/bash", "/root/entry.sh"] CMD ["/bin/bash", "/root/entry.sh"]

View File

@ -1,45 +1,44 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import requests
import time import time
import urllib.request
from ipaddress import ip_network from ipaddress import ip_network
from pathlib import Path from pathlib import Path
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
if __name__ == "__main__": if __name__ == "__main__":
sg = SendGridAPIClient(Path("sendgrid.key").read_text()) link = Path("discord.txt").read_text()
old_ipv4, old_ipv6 = None, None old_ipv4, old_ipv6 = None, None
while True: while True:
# get current ips
try: try:
ipv4 = urllib.request.urlopen("https://v4.ident.me").read().decode("utf8") ipv4 = requests.get("https://v4.ident.me").text
ipv6 = urllib.request.urlopen("https://v6.ident.me").read().decode("utf8") ipv6 = requests.get("https://v6.ident.me").text
ipv6 = str(ip_network(ipv6 + "/64", strict=False).network_address) # xfinity gives /64 ipv6 = str(ip_network(ipv6 + "/64", strict=False).network_address) # xfinity gives /64
except Exception as e: except Exception as e:
print(e) print("Error while getting IP", e)
time.sleep(60) time.sleep(60)
continue continue
# send message if either changed
if ipv4 != old_ipv4 or ipv6 != old_ipv6: 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"<p>old ipv4: {old_ipv4}</p>"
f"<p>old ipv6: {old_ipv6}</p>"
f"<p>new ipv4: {ipv4}</p>"
f"<p>new ipv6: {ipv6}</p>"
))
try: try:
print(f"IP changed to {ipv4} and {ipv6}") 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: except Exception as e:
print(e) print("Error while sending update", e)
sg = SendGridAPIClient(Path("sendgrid.key").read_text())
time.sleep(60) time.sleep(60)
continue continue
# retry every hour
print("Retrying in 1 hour...")
old_ipv4, old_ipv6 = ipv4, ipv6 old_ipv4, old_ipv6 = ipv4, ipv6
time.sleep(3600) time.sleep(3600)