diff --git a/Dockerfile b/Dockerfile index 5bd5458..3e2f727 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.11-alpine +FROM python:3.12-alpine RUN mkdir app RUN apk add bind-tools diff --git a/config.example.json b/config.example.json index e51c7f1..5a46d03 100644 --- a/config.example.json +++ b/config.example.json @@ -1,5 +1,6 @@ { - + "NOTIFY_SERVER": "https:///", + "ENABLE_NOTIFICATIONS": false, "ZONE_ID": "your_zone_id", "USER_EMAIL": "name.lastname@mail.com", "API_KEY": "your_CF_API_key", diff --git a/config.py b/config.py new file mode 100644 index 0000000..d15dee2 --- /dev/null +++ b/config.py @@ -0,0 +1,9 @@ +import json + + +def load_config(filename: str): + with open(filename, 'r') as f: + data = json.load(f) + return data + + diff --git a/dns_functions.py b/dns_functions.py index e7d3d2f..2655cd9 100644 --- a/dns_functions.py +++ b/dns_functions.py @@ -1,12 +1,15 @@ import requests import pydig import json +from notifications import Notifier +from owl import print_owl +from config import load_config -def load_config(filename: str): - with open(filename, 'r') as f: - data = json.load(f) - return data +if load_config('config.json')['ENABLE_NOTIFICATIONS']: + notification_service = Notifier() +else: + notification_service = None def get_current_public_ip() -> str: @@ -70,33 +73,6 @@ def set_ip(cloudflare, domain, current_ip: str): return response -def print_owl(): - print(r""" - # _____ _ _ _____ ____ __ __ _ ___ ___ - # | __ \ | \ | | / ____| / __ \\ \ / /| | / _ \ |__ \ - # | | | || \| || (___ | | | |\ \ /\ / / | | __ __| | | | ) | - # | | | || . ` | \___ \ | | | | \ \/ \/ / | | \ \ / /| | | | / / - # | |__| || |\ | ____) | | |__| | \ /\ / | |____ \ V / | |_| |_ / /_ - # |_____/ |_| \_||_____/ \____/ \/ \/ |______| \_/ \___/(_)|____| - - - __________-------____ ____-------__________ - \------____-------___--__---------__--___-------____------/ - \//////// / / / / / \ _-------_ / \ \ \ \ \ \\\\\\\\/ - \////-/-/------/_/_| /___ ___\ |_\_\------\-\-\\\\/ - --//// / / / //|| (O)\ /(O) ||\\ \ \ \ \\\\-- - ---__/ // /| \_ /V\ _/ |\ \\ \__--- - -// / /\_ ------- _/\ \ \\- - \_/_/ /\---------/\ \_\_/ - ----\ | /---- - | -|- | - / | \ - ---- \___| - - # by Simon169 - """) - - def update_all_ip(current_ip): data = load_config('config.json') @@ -109,17 +85,24 @@ def update_all_ip(current_ip): if response.json()['success']: print(f"\tIP was set successfully!") + if notification_service: + notification_service.send_success(f"IP for domain {domain} was successfully set to {current_ip}") else: print(f"\tThere was an error, see below for more details") print(f"\tResponse code was: {response.status_code}") + if notification_service: + notification_service.send_error(f"An error occurred, status code {response.status_code}") + print(f"\tResponse json is: {response.json()}") print('\tDone!') print(f"{'':#<40}") if __name__ == '__main__': - print_owl() + print(f"{'':#<40}") ip = get_current_public_ip() - update_all_ip(ip) \ No newline at end of file + update_all_ip(ip) + + print(f"\tDone updating, sleep until next CRON schedule...") diff --git a/init.sh b/init.sh index 6d75739..65914ed 100644 --- a/init.sh +++ b/init.sh @@ -1,3 +1,5 @@ +python -c 'from owl import *; starting_message()' # Run starting message and logo +python main.py # Run Update once directly at the start echo "$CRONVARS2" "cd /app && python main.py" >> mycron crontab mycron crond -f \ No newline at end of file diff --git a/main.py b/main.py index 7cf33fd..5b74ee4 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,12 @@ import dns_functions as dns +import owl as owl if __name__ == "__main__": - dns.print_owl() + # owl.print_owl() print(f"{'':#<40}") ip = dns.get_current_public_ip() - dns.update_all_ip(ip) \ No newline at end of file + dns.update_all_ip(ip) + + print(f"\tDone updating, sleep until next CRON schedule...") diff --git a/notifications.py b/notifications.py new file mode 100644 index 0000000..0b96aaf --- /dev/null +++ b/notifications.py @@ -0,0 +1,32 @@ +import requests +from config import load_config + + +class Notifier: + def __init__(self): + self.data = load_config('config.json') + self.ntfy_server = self.data['NOTIFY_SERVER'] + + def send_success(self, message: str = "Generic success message"): + message = "🟢 Success: " + message + requests.post(self.ntfy_server, data=message.encode(encoding='utf-8')) + + def send_warning(self, message: str = "Generic warning message"): + message = "⚠️ Warning: " + message + requests.post(self.ntfy_server, data=message.encode(encoding='utf-8')) + + def send_error(self, message: str = "Generic error message"): + message = "🚩 Error: " + message + requests.post(self.ntfy_server, data=message.encode(encoding='utf-8')) + + +if __name__ == "__main__": + + test_notifier = Notifier() + test_notifier.send_success() + test_notifier.send_warning() + test_notifier.send_error() + + # test_notifier = None + # if test_notifier: + # print('ladida') diff --git a/owl.py b/owl.py new file mode 100644 index 0000000..9fed72d --- /dev/null +++ b/owl.py @@ -0,0 +1,48 @@ +from datetime import datetime + + +def print_owl(): + print(r""" + # _____ _ _ _____ ____ __ __ _ ___ ____ + # | __ \ | \ | | / ____| / __ \\ \ / /| | / _ \ |___ \ + # | | | || \| || (___ | | | |\ \ /\ / / | | __ __| | | | __) | + # | | | || . ` | \___ \ | | | | \ \/ \/ / | | \ \ / /| | | | |__ < + # | |__| || |\ | ____) | | |__| | \ /\ / | |____ \ V / | |_| |_ ___) | + # |_____/ |_| \_||_____/ \____/ \/ \/ |______| \_/ \___/(_)|____/ + + + + __________-------____ ____-------__________ + \------____-------___--__---------__--___-------____------/ + \//////// / / / / / \ _-------_ / \ \ \ \ \ \\\\\\\\/ + \////-/-/------/_/_| /___ ___\ |_\_\------\-\-\\\\/ + --//// / / / //|| (O)\ /(O) ||\\ \ \ \ \\\\-- + ---__/ // /| \_ /V\ _/ |\ \\ \__--- + -// / /\_ ------- _/\ \ \\- + \_/_/ /\---------/\ \_\_/ + ----\ | /---- + | -|- | + / | \ + ---- \___| + + # by Simon169 + """) + + +def starting_message(): + now = datetime.now() + current_time = now.strftime("%d/%m/%Y, %H:%M:%S") + + print(f""" + \t Starting DNS-Owl... + \t Time/Date: {current_time} + """) + print_owl() + + +if __name__ == "__main__": + + starting_message() + + +# ASCII Art vreated with https://patorjk.com/software/taag