Private GIT

Skip to content
Snippets Groups Projects
Commit 09495c37 authored by DarkGigaByte's avatar DarkGigaByte
Browse files

make ipify api configurable & codestyle

now its possible to use a selfhosted ipify api instance instead of disclosing your ip to ipify.org
parent f3b2eca3
Branches
No related tags found
No related merge requests found
...@@ -13,8 +13,9 @@ a_name = raspbian ...@@ -13,8 +13,9 @@ a_name = raspbian
ttl = 900 ttl = 900
# Production API # Production API
api = https://dns.api.gandi.net/api/v5/ gandi_api = https://dns.api.gandi.net/api/v5/
# Number of retries for looking up our own IP address, since # Number of retries for looking up our own IP address, since
# the service seems to be flaky sometimes. # the service seems to be flaky sometimes.
retries = 3 retries = 3
ipify_api = https://api.ipify.org
...@@ -11,17 +11,18 @@ config_file = "config.txt" ...@@ -11,17 +11,18 @@ config_file = "config.txt"
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
DEFAULT_RETRIES = 3 DEFAULT_RETRIES = 3
DEFAULT_IPIFY_API = "https://api.ipify.org"
class GandiDdnsError(Exception): class GandiDdnsError(Exception):
pass pass
def get_ip_inner(): def get_ip_inner(ipify_api):
# Get external IP # Get external IP
try: try:
# Could be any service that just gives us a simple raw ASCII IP address (not HTML etc) # Could be any service that just gives us a simple raw ASCII IP address (not HTML etc)
r = requests.get('https://api.ipify.org', timeout=3) r = requests.get(ipify_api, timeout=3)
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
raise GandiDdnsError('Failed to retrieve external IP.') raise GandiDdnsError('Failed to retrieve external IP.')
if r.status_code != 200: if r.status_code != 200:
...@@ -37,14 +38,14 @@ def get_ip_inner(): ...@@ -37,14 +38,14 @@ def get_ip_inner():
return ip return ip
def get_ip(retries): def get_ip(ipify_api, retries):
# Get external IP with retries # Get external IP with retries
# Start at 5 seconds, double on every retry. # Start at 5 seconds, double on every retry.
retry_delay_time = 5 retry_delay_time = 5
for attempt in range(retries): for attempt in range(retries):
try: try:
return get_ip_inner() return get_ip_inner(ipify_api)
except GandiDdnsError as e: except GandiDdnsError as e:
print('Getting external IP failed: %s' % e) print('Getting external IP failed: %s' % e)
print('Waiting for %d seconds before trying again' % retry_delay_time) print('Waiting for %d seconds before trying again' % retry_delay_time)
...@@ -54,6 +55,7 @@ def get_ip(retries): ...@@ -54,6 +55,7 @@ def get_ip(retries):
print('Exhausted retry attempts') print('Exhausted retry attempts')
sys.exit(2) sys.exit(2)
def read_config(config_path): def read_config(config_path):
# Read configuration file # Read configuration file
cfg = configparser.ConfigParser() cfg = configparser.ConfigParser()
...@@ -61,12 +63,14 @@ def read_config(config_path): ...@@ -61,12 +63,14 @@ def read_config(config_path):
return cfg return cfg
def get_record(url, headers): def get_record(url, headers):
# Get existing record # Get existing record
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
return r return r
def update_record(url, headers, payload): def update_record(url, headers, payload):
# Add record # Add record
r = requests.put(url, headers=headers, json=payload) r = requests.put(url, headers=headers, json=payload)
...@@ -94,14 +98,16 @@ def main(): ...@@ -94,14 +98,16 @@ def main():
apikey = config.get(section, 'apikey') apikey = config.get(section, 'apikey')
# Set headers # Set headers
headers = { 'Content-Type': 'application/json', 'X-Api-Key': '%s' % config.get(section, 'apikey')} headers = {'Content-Type': 'application/json', 'X-Api-Key': '%s' % apikey}
# Set URL # Set URL
url = '%sdomains/%s/records/%s/A' % (config.get(section, 'api'), config.get(section, 'domain'), config.get(section, 'a_name')) url = '%sdomains/%s/records/%s/A' % (config.get(section, 'gandi_api'),
config.get(section, 'domain'), config.get(section, 'a_name'))
print(url) print(url)
# Discover External IP # Discover External IP
ipify_api = config.get(section, 'ipify_api', fallback=DEFAULT_IPIFY_API)
retries = int(config.get(section, 'retries', fallback=DEFAULT_RETRIES)) retries = int(config.get(section, 'retries', fallback=DEFAULT_RETRIES))
external_ip = get_ip(retries) external_ip = get_ip(ipify_api, retries)
print(('External IP is: %s' % external_ip)) print(('External IP is: %s' % external_ip))
# Prepare record # Prepare record
...@@ -120,5 +126,6 @@ def main(): ...@@ -120,5 +126,6 @@ def main():
update_record(url, headers, payload) update_record(url, headers, payload)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment