Skip to content

Commit

Permalink
Sort imports, remove commented out code, and remove whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mhum committed Sep 1, 2024
1 parent 9bc06b2 commit 18a9d89
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions nfsn-ddns.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from urllib.parse import urlencode
import requests
import os
from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import Union, NewType, Dict
import argparse
import hashlib
import random
import string
import os
from datetime import datetime, timezone
import hashlib
import argparse
from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import Union, NewType, Dict
from urllib.parse import urlencode

import requests
from dotenv import load_dotenv

IPAddress = NewType("IPAddress", Union[IPv4Address, IPv6Address])
Expand All @@ -31,10 +32,10 @@ def doIPsMatch(ip1:IPAddress, ip2:IPAddress) -> bool:

def output(msg, type_msg=None, timestamp=None):
if timestamp is None:
timestamp = datetime.now().strftime("%y-%m-%d %H:%M:%S")
timestamp = datetime.now().strftime("%y-%m-%d %H:%M:%S")
type_str = f"{type_msg}: " if type_msg is not None else ""
print(f"{timestamp}: {type_str}{msg}")


def validateNFSNResponse(response):
if response is None:
Expand Down Expand Up @@ -91,8 +92,8 @@ def fetchDomainIP(domain, subdomain, nfsn_username, nfsn_apikey, v6=False):

if len(response_data) == 0:
output("No IP address is currently set.")
return
return

return response_data[0].get("data")


Expand All @@ -117,7 +118,7 @@ def replaceDomain(domain, subdomain, current_ip, nfsn_username, nfsn_apikey, cre
output(f"Setting {record_type} record on {subdomain}.{domain} to {current_ip}...")

makeNFSNHTTPRequest(path, body, nfsn_username, nfsn_apikey)



def createNFSNAuthHeader(nfsn_username, nfsn_apikey, url_path, body) -> Dict[str,str]:
Expand Down Expand Up @@ -166,29 +167,27 @@ def check_ips(nfsn_domain, nfsn_subdomain, nfsn_username, nfsn_apikey, v6=False,

domain_ip = fetchDomainIP(nfsn_domain, nfsn_subdomain, nfsn_username, nfsn_apikey, v6=v6)
current_ip = fetchCurrentIP(v6=v6)

if domain_ip is not None and doIPsMatch(ip_address(domain_ip), ip_address(current_ip)):
output(f"IPs still match! Current IP: {current_ip} Domain IP: {domain_ip}")
return

updateIPs(nfsn_domain, nfsn_subdomain, domain_ip, current_ip, nfsn_username, nfsn_apikey, v6=v6)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='automate the updating of domain records to create Dynamic DNS for domains registered with NearlyFreeSpeech.net')
# parser.add_argument('integers', metavar='N', type=int, nargs='+',
# help='an integer for the accumulator')
parser.add_argument('--ipv6', '-6', action='store_true', help='also check and update the AAAA (IPv6) records')

#load variables set in .env file
load_dotenv()

args = parser.parse_args()
nfsn_username = os.getenv('USERNAME')
nfsn_apikey = os.getenv('API_KEY')
nfsn_domain = os.getenv('DOMAIN')
nfsn_subdomain = os.getenv('SUBDOMAIN')

ensure_present(nfsn_username, "USERNAME")
ensure_present(nfsn_apikey, "API_KEY")
ensure_present(nfsn_domain, "DOMAIN")
Expand Down

0 comments on commit 18a9d89

Please sign in to comment.