Skip to content

Commit

Permalink
Merge v0.4 changes (#26)
Browse files Browse the repository at this point in the history
* Refactor Python files into separate directory to increase project structure

* Updated Logo to v0.4

* Add small webserver to show current status and last updates

* Add small webserver to show current status and last updates

* Initial work for webserver

* Big changes to the webserver
Finished Layout
Finished Section that writes update information to html file

* Added .github to .dockerign

* Update Standard intervall to 10 mins and version to :latest

* Updated resolver for current public ip to use Cloudflare
Added config option for public ip determination, either "CLOUDFLARE" for cloudflare or any other string to use "https://ident.me"

* Added server_start command to init.sh
Needs testing and might be reverted

* Add query for port from config file and pass it to server run function

* Fix imports in Python owl module

* Fix file location relations

* Formatting echo messages
Modify init.sh for startup, still testing

* Update init.sh to run cron as daemon and not in foreground

* Update imports, resolve conflict of module folder name and module name

* Update gitignore

* Update bump_version.yml

* Version 0.4.1

---------

Co-authored-by: Simon_Bot <[email protected]>
  • Loading branch information
simonl169 and simonl169 authored Sep 1, 2024
1 parent 9d380e5 commit 4ae273e
Show file tree
Hide file tree
Showing 19 changed files with 271 additions and 31 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ venv
__pycache__
.idea
.gitignore
.github
.git
config.json
docker-compose.yml
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/bump_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
pip install -r requirements.txt
- name: Bump version
run: python test.py --server="${{ inputs.serverBump }}"
run: python bump_version.py --server="${{ inputs.serverBump }}"

- name: Commit and tag
id: push-tag
Expand All @@ -52,4 +52,4 @@ jobs:
author_name: Simon_Bot
message: 'Version ${{ env.NEXT_SERVER }}'
tag: ${{ env.NEXT_SERVER }}
push: true
push: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ docker-compose.yml
venv
__pycache__
.idea
index.html
mycron

Empty file added __init__.py
Empty file.
3 changes: 3 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"PUBLIC_IP_CHECK": "CLOUDFLARE",
"ENABLE_WEBSERVER": true,
"WEBSERVER_PORT": 8000,
"NOTIFY_SERVER": "https://<YOUR_NTFY_SERVER>/<TOPIC>",
"ENABLE_NOTIFICATIONS": false,
"ZONE_ID": "your_zone_id",
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.example.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version: '3.8'
services:
dns-owl:
image: ghcr.io/simonl169/dns-owl:v0.2
image: ghcr.io/simonl169/dns-owl:latest
environment:
- TZ=Europe/Berlin
- CRONVARS2=*/1 * * * *
- CRONVARS2=*/10 * * * *
volumes:
- ./config.json:/app/config.json

5 changes: 3 additions & 2 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
python -c 'from owl import *; starting_message()' # Run starting message and logo
python -c 'from owl.owl_logo 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
crond
python -m start_server
11 changes: 8 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import dns_functions as dns
import owl as owl
from owl import dns_functions as dns
from owl.config import load_config

if __name__ == "__main__":

# owl.print_owl()
print(f"{'':#<40}")
ip = dns.get_current_public_ip()

ip_provider = load_config('./config.json')['PUBLIC_IP_CHECK']

ip = dns.get_current_public_ip(ip_provider)

dns.update_all_ip(ip)

print(f"\tAdd some part to update the index.html")

print(f"\tDone updating, sleep until next CRON schedule...")
Empty file added owl/__init__.py
Empty file.
2 changes: 0 additions & 2 deletions config.py → owl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ def load_config(filename: str):
with open(filename, 'r') as f:
data = json.load(f)
return data


39 changes: 31 additions & 8 deletions dns_functions.py → owl/dns_functions.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import requests
import pydig
import json
from notifications import Notifier
from config import load_config
from .notifications import Notifier
from .config import load_config
from .webserver import write_to_template


if load_config('config.json')['ENABLE_NOTIFICATIONS']:
if load_config('./config.json')['ENABLE_NOTIFICATIONS']:
notification_service = Notifier()
else:
notification_service = None


def get_current_public_ip() -> str:
ip_address = requests.get('https://ident.me')
print(f'\tMy public IP address is: {ip_address.text}')
return ip_address.text
def get_current_public_ip(provider: str = 'CLOUDFLARE') -> str:
if provider == 'CLOUDFLARE':
print(f'\tChecking public IP via https://cloudflare.com/cdn-cgi/trace')
ip_address = requests.get('https://cloudflare.com/cdn-cgi/trace')
ip_address_text = ip_address.text.split('\n')[2].replace('ip=', '')
else:
print(f'\tChecking public IP via https://ident.me')
ip_address = requests.get('https://ident.me')
ip_address_text = ip_address.text
print(f'\tMy public IP address is: {ip_address_text}')
return ip_address_text


def resolve_current_server_ip(url, nameservers='8.8.8.8') -> [str, str]:
Expand Down Expand Up @@ -73,7 +81,7 @@ def set_ip(cloudflare, domain, current_ip: str):


def update_all_ip(current_ip):
data = load_config('config.json')
data = load_config('./config.json')

cf = data

Expand All @@ -82,10 +90,13 @@ def update_all_ip(current_ip):
print(f"\tPerform Check if Update is necessary...")
domain_ip = resolve_current_server_ip(domain['RECORD_NAME'])[1]
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!")
domain['NEW_IP'] = domain_ip
domain['RESULT'] = f"No change"
else:
print(f"\tUpdating DynDNS IP for domain: {domain['RECORD_NAME']}...")
response = set_ip(cf, domain, current_ip)
Expand All @@ -94,13 +105,25 @@ def update_all_ip(current_ip):
print(f"\tIP was set successfully!")
if notification_service:
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"
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 while setting IP for domain {domain['RECORD_NAME']}, status code {response.status_code}")
print(f"\tResponse json is: {response.json()}")
domain['NEW_IP'] = f"Not set"
domain['RESULT'] = f"Error"

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

# Updating index.html

write_to_template(data['domains'])

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

Expand Down
4 changes: 2 additions & 2 deletions notifications.py → owl/notifications.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import requests
from config import load_config
from .config import load_config


class Notifier:
def __init__(self):
self.data = load_config('config.json')
self.data = load_config('./config.json')
self.ntfy_server = self.data['NOTIFY_SERVER']

def send_success(self, message: str = "Generic success message"):
Expand Down
26 changes: 19 additions & 7 deletions owl.py → owl/owl_logo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from datetime import datetime
from owl.notifications import Notifier
from owl.config import load_config


def print_owl():
print(r"""
# _____ _ _ _____ ____ __ __ _ ___ ____
# | __ \ | \ | | / ____| / __ \\ \ / /| | / _ \ |___ \
# | | | || \| || (___ | | | |\ \ /\ / / | | __ __| | | | __) |
# | | | || . ` | \___ \ | | | | \ \/ \/ / | | \ \ / /| | | | |__ <
# | |__| || |\ | ____) | | |__| | \ /\ / | |____ \ V / | |_| |_ ___) |
# |_____/ |_| \_||_____/ \____/ \/ \/ |______| \_/ \___/(_)|____/
# _____ _ _ _____ ____ __ __ _ ___ _ _
# | __ \ | \ | | / ____| / __ \\ \ / /| | / _ \ | || |
# | | | || \| || (___ | | | |\ \ /\ / / | | __ __| | | || || |_
# | | | || . ` | \___ \ | | | | \ \/ \/ / | | \ \ / /| | | ||__ _|
# | |__| || |\ | ____) | | |__| | \ /\ / | |____ \ V / | |_| |_ | |
# |_____/ |_| \_||_____/ \____/ \/ \/ |______| \_/ \___/(_) |_|
Expand Down Expand Up @@ -38,11 +42,19 @@ def starting_message():
\t Time/Date: {current_time}
""")
print_owl()
send_start_notification()


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


if __name__ == "__main__":

starting_message()


# ASCII Art vreated with https://patorjk.com/software/taag
# ASCII Art created with https://patorjk.com/software/taag
# Settings: font big, width fitted
91 changes: 91 additions & 0 deletions owl/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
body {
font-family: "Helvetica Neue", Helvetica, Arial;
font-size: 14px;
line-height: 20px;
font-weight: 400;
color: #3b3b3b;
-webkit-font-smoothing: antialiased;
font-smoothing: antialiased;
background: #2b2b2b;
}
@media screen and (max-width: 580px) {
body {
font-size: 16px;
line-height: 22px;
}
}

.wrapper {
margin: 0 auto;
padding: 40px;
max-width: 800px;
}

.table {
margin: 0 0 40px 0;
width: 100%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
display: table;
}
@media screen and (max-width: 580px) {
.table {
display: block;
}
}

.row {
display: table-row;
background: #f6f6f6;
}
.row:nth-of-type(odd) {
background: #e9e9e9;
}
.row.header {
font-weight: 900;
color: #ffffff;
background: #ea6153;
}
.row.green {
background: #27ae60;
}
.row.blue {
background: #2980b9;
}
@media screen and (max-width: 580px) {
.row {
padding: 14px 0 7px;
display: block;
}
.row.header {
padding: 0;
height: 6px;
}
.row.header .cell {
display: none;
}
.row .cell {
margin-bottom: 10px;
}
.row .cell:before {
margin-bottom: 3px;
content: attr(data-title);
min-width: 98px;
font-size: 10px;
line-height: 10px;
font-weight: bold;
text-transform: uppercase;
color: #969696;
display: block;
}
}

.cell {
padding: 6px 12px;
display: table-cell;
}
@media screen and (max-width: 580px) {
.cell {
padding: 2px 16px;
display: block;
}
}
53 changes: 53 additions & 0 deletions owl/template.html.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!-- index.html -->

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>DNS OWL Status Server</title>
<link rel="stylesheet" href="./owl/style.css">
</head>
<body>

<div class="wrapper">
<div class="table">
<div class="row header green">
<div class="cell">
Domain
</div>
<div class="cell">
Old IP Address
</div>
<div class="cell">
New IP Address
</div>
<div class="cell">
Result
</div>
</div>
{% for domain in domain_list %}
<div class="row">
<div class="cell" data-title="Domain">
{{ domain['RECORD_NAME'] }}
</div>
<div class="cell" data-title="Old IP">
{{ domain['OLD_IP'] }}
</div>
<div class="cell" data-title="New IP">
{{ domain['NEW_IP'] }}
</div>
<div class="cell" data-title="Result">
{{ domain['RESULT'] }}
</div>
</div>
{% endfor %}
</div>
</div>
<h1>
Updated on : {{ updated_on }}
</h1>
</body>

</html>

Loading

0 comments on commit 4ae273e

Please sign in to comment.