Skip to content

Commit

Permalink
Merge pull request #34 from simonl169/develop/master_develop
Browse files Browse the repository at this point in the history
Add column to show time and date of last update
Several small improvements
Proper time zone and time format handling
  • Loading branch information
simonl169 authored Oct 18, 2024
2 parents 3c68e06 + 823862b commit aeb9c4f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 23 deletions.
6 changes: 5 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"TIMEZONE": "Europe/Berlin",
"PUBLIC_IP_CHECK": "CLOUDFLARE",
"ENABLE_WEBSERVER": true,
"WEBSERVER_PORT": 8000,
"NOTIFY_SERVER": "https://<YOUR_NTFY_SERVER>/<TOPIC>",
"ENABLE_NOTIFICATIONS": false,
"NOTIFICATIONS": {
"ENABLE_NOTIFICATIONS": true,
"ENABLE_NOTIFICATION_NO_CHANGE": false
},
"ZONE_ID": "your_zone_id",
"USER_EMAIL": "name.lastname@mail.com",
"API_KEY": "your_CF_API_key",
Expand Down
61 changes: 54 additions & 7 deletions owl/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 .config import load_config
from .webserver import write_to_template
from owl.notifications import Notifier
from owl.config import load_config
from owl.webserver import write_to_template
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

TIMEZONE = ZoneInfo(load_config('./config.json')['TIMEZONE'])

if load_config('./config.json')['ENABLE_NOTIFICATIONS']:
if load_config('./config.json')['NOTIFICATIONS']['ENABLE_NOTIFICATIONS']:
notification_service = Notifier()
else:
notification_service = None
Expand Down Expand Up @@ -92,11 +95,19 @@ def update_all_ip(current_ip):
check = compare_ip(current_ip, domain_ip)
domain['OLD_IP'] = domain_ip
if check:
print(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary!")
if notification_service:
notification_service.send_success(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary!")
if data['NOTIFICATIONS']['ENABLE_NOTIFICATION_NO_CHANGE']:
print(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary!")
notification_service.send_success(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary!")
else:
print(f"\tIP for domain: {domain['RECORD_NAME']} is {domain_ip} which is the current public IP {current_ip}. No Update necessary! No Notification send due to configuration!")
domain['NEW_IP'] = domain_ip
domain['RESULT'] = f"No change"
### Placeholder
### Needs some work to load time from last successful update
domain_info = get_current_dns_entry_from_cf(cf, domain)
last_update = datetime.strptime(domain_info['result']['modified_on'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
domain['LAST_UPDATE'] = last_update.astimezone(TIMEZONE).strftime("%d.%m.%Y at %H:%M:%S")
else:
print(f"\tUpdating DynDNS IP for domain: {domain['RECORD_NAME']}...")
response = set_ip(cf, domain, current_ip)
Expand All @@ -107,6 +118,9 @@ def update_all_ip(current_ip):
notification_service.send_success(f"IP for domain {domain['RECORD_NAME']} was successfully set to {current_ip}. Old IP was {domain_ip}")
domain['NEW_IP'] = current_ip
domain['RESULT'] = f"Update successfull"
domain_info = get_current_dns_entry_from_cf(cf, domain)
last_update = datetime.strptime(domain_info['result']['modified_on'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
domain['LAST_UPDATE'] = last_update.astimezone(TIMEZONE).strftime("%d.%m.%Y at %H:%M:%S")
else:
print(f"\tThere was an error, see below for more details")
print(f"\tResponse code was: {response.status_code}")
Expand All @@ -115,18 +129,50 @@ def update_all_ip(current_ip):
print(f"\tResponse json is: {response.json()}")
domain['NEW_IP'] = f"Not set"
domain['RESULT'] = f"Error"
### Placeholder
### Needs some work to load time from last successful update
domain_info = get_current_dns_entry_from_cf(cf, domain)
last_update = datetime.strptime(domain_info['result']['modified_on'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
domain['LAST_UPDATE'] = last_update.astimezone(TIMEZONE).strftime("%d.%m.%Y at %H:%M:%S")

print('\tDone!')
print(f"{'':#<40}")

# Updating index.html

write_to_template(data['domains'])

print('\tUpdating index.html...')
print('\tDone!')
print(f"{'':#<40}")

def get_current_dns_entry_from_cf(cloudflare, domain):
zone_id = cloudflare['ZONE_ID']
api_key = cloudflare['API_KEY']
user_email = cloudflare['USER_EMAIL']

record_id = domain['RECORD_ID']
record_name = domain['RECORD_NAME']

print(f"\tCloudflare Zone ID is: {zone_id}")
print(f"\tCloudflare API Key is: {api_key}")
print(f"\tRecord ID is: {record_id}")
print(f"\tRecord Name is: {record_name}")

url = (
"https://api.cloudflare.com/client/v4/zones/%(zone_id)s/dns_records/%(record_id)s"
% {"zone_id": zone_id, "record_id": record_id}
)

headers = {
"X-Auth-Email": user_email,
"X-Auth-Key": api_key,
"Content-Type": "application/json",
}

response = requests.get(url, headers=headers)
# print(response.status_code)
return response.json()


if __name__ == '__main__':

Expand All @@ -136,3 +182,4 @@ def update_all_ip(current_ip):
update_all_ip(ip)

print(f"\tDone updating, sleep until next CRON schedule...")

2 changes: 1 addition & 1 deletion owl/notifications.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import requests
from .config import load_config
from owl.config import load_config


class Notifier:
Expand Down
16 changes: 8 additions & 8 deletions owl/owl_logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
def print_owl():
print(r"""
# _____ _ _ _____ ____ __ __ _ ___ _ _
# | __ \ | \ | | / ____| / __ \\ \ / /| | / _ \ | || |
# | | | || \| || (___ | | | |\ \ /\ / / | | __ __| | | || || |_
# | | | || . ` | \___ \ | | | | \ \/ \/ / | | \ \ / /| | | ||__ _|
# | |__| || |\ | ____) | | |__| | \ /\ / | |____ \ V / | |_| |_ | |
# |_____/ |_| \_||_____/ \____/ \/ \/ |______| \_/ \___/(_) |_|
# _____ _ _ _____ ____ __ __ _ ___ _____
# | __ \ | \ | | / ____| / __ \\ \ / /| | / _ \ | ____|
# | | | || \| || (___ | | | |\ \ /\ / / | | __ __| | | | | |__
# | | | || . ` | \___ \ | | | | \ \/ \/ / | | \ \ / /| | | | |___ \
# | |__| || |\ | ____) | | |__| | \ /\ / | |____ \ V / | |_| |_ ___) |
# |_____/ |_| \_||_____/ \____/ \/ \/ |______| \_/ \___/(_)|____/
Expand Down Expand Up @@ -46,7 +46,7 @@ def starting_message():


def send_start_notification():
if load_config('./config.json')['ENABLE_NOTIFICATIONS']:
if load_config('./config.json')['NOTIFICATIONS']['ENABLE_NOTIFICATIONS']:
notification_service = Notifier()
notification_service.send_success(f"\tStarting DNS-Owl service!")

Expand Down
6 changes: 6 additions & 0 deletions owl/template.html.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<div class="cell">
Result
</div>
<div class="cell">
Last update on
</div>
</div>
{% for domain in domain_list %}
<div class="row">
Expand All @@ -40,6 +43,9 @@
<div class="cell" data-title="Result">
{{ domain['RESULT'] }}
</div>
<div class="cell" data-title="Updated on">
{{ domain['LAST_UPDATE'] }}
</div>
</div>
{% endfor %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "dns-owl",
"version": "0.4.1"
"version": "0.5.0"
}
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
certifi==2024.6.2
charset-normalizer==3.3.2
idna==3.7
certifi==2024.8.30
charset-normalizer==3.4.0
idna==3.10
Jinja2==3.1.4
MarkupSafe==2.1.5
MarkupSafe==3.0.2
pydig==0.4.0
requests==2.32.3
urllib3==2.2.1
urllib3==2.2.3

0 comments on commit aeb9c4f

Please sign in to comment.