Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check wether Updating is neccessary (#19) #20

Merged
merged 1 commit into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions dns_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def resolve_current_server_ip(url, nameservers='8.8.8.8') -> [str, str]:
resolver = pydig.Resolver(nameservers=nameservers)
local_ip = pydig.query(url, 'A')
public_ip = resolver.query(url, 'A')
print(f'\tLocal IP address for {url} is: {local_ip[0]}')
print(f'\tPublic IP address for {url} is: {public_ip[0]}')
# print(f'\tLocal IP address for {url} is: {local_ip[0]}')
# print(f'\tPublic IP address for {url} is: {public_ip[0]}')
return local_ip[0], public_ip[0]


def compare_ip(ip1, ip2) -> bool:
print('\tComparing addresses:')
print(f'\tIP address 1: {ip1}')
print(f'\tIP address 2: {ip2}')
# print('\tComparing addresses:')
# print(f'\tIP address 1: {ip1}')
# print(f'\tIP address 2: {ip2}')
if ip1 == ip2:
return True
else:
Expand Down Expand Up @@ -79,19 +79,27 @@ def update_all_ip(current_ip):

for domain in data['domains']:
print(f"{'':#<40}")
print(f"\tUpdating DynDNS IP for domain: {domain['RECORD_NAME']}...")
response = set_ip(cf, domain, current_ip)

if response.json()['success']:
print(f"\tIP was set successfully!")
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)
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"IP for domain {domain} was successfully set to {current_ip}")
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"\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(f"\tUpdating DynDNS IP for domain: {domain['RECORD_NAME']}...")
response = set_ip(cf, domain, 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}")
Expand Down