Skip to content

Commit

Permalink
Develop/feature notifications (#13)
Browse files Browse the repository at this point in the history
* Changed DynDNS registrar to Cloudflare
Updated some functions
Rewrite to better add multiple domains
New Logo

* Updated deps

* Updated Readme.md

* Update README.md

Added section on how to obtain Cloudflare credentials and IDs

* Added new feature for push notifications
Added start-up sequence
Refactor code into more files

* Change image to Python 3.12
  • Loading branch information
simonl169 authored Apr 20, 2024
1 parent 774299c commit 965feff
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11-alpine
FROM python:3.12-alpine

RUN mkdir app
RUN apk add bind-tools
Expand Down
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{

"NOTIFY_SERVER": "https://<YOUR_NTFY_SERVER>/<TOPIC>",
"ENABLE_NOTIFICATIONS": false,
"ZONE_ID": "your_zone_id",
"USER_EMAIL": "[email protected]",
"API_KEY": "your_CF_API_key",
Expand Down
9 changes: 9 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json


def load_config(filename: str):
with open(filename, 'r') as f:
data = json.load(f)
return data


49 changes: 16 additions & 33 deletions dns_functions.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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')

Expand All @@ -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)
update_all_ip(ip)

print(f"\tDone updating, sleep until next CRON schedule...")
2 changes: 2 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)
dns.update_all_ip(ip)

print(f"\tDone updating, sleep until next CRON schedule...")
32 changes: 32 additions & 0 deletions notifications.py
Original file line number Diff line number Diff line change
@@ -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')
48 changes: 48 additions & 0 deletions owl.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 965feff

Please sign in to comment.