Skip to content

Commit

Permalink
Switch from ipgetter to AWS's IP address service
Browse files Browse the repository at this point in the history
For three reasons:

1) The upstream 'ipgetter' library is no longer available (¯\_(ツ)_/¯)

2) This results in more universal code, and uses a single, but reliable
servcice (AWS's 'checkip' service) instead of a oft-outdated and
manually-managed list of "what's my IP?" services (which all have
different responses that must be parsed). This also removes the
dependency on an obviously no-longer-maintained library.

3) Resolves #1
  • Loading branch information
zhimsel committed Nov 13, 2018
1 parent fb31a46 commit 959221e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions r53_dns_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import logging.handlers
import boto3
import sys
import ipgetter
import requests
import ipaddress

# Set up global logging object
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -141,33 +142,44 @@ def get_public_ip(self, max_tries=None):
Also check for consistency and "correctness" in the responses from the
public "IP Check" services, as these services may return bad data or no
data at all. This function checks two separate services and only
returns an IP if they both match.
data at all. This function checks the retrieved IP address for validity
and retries a few times until it gets a valid one.
Args:
max_tries (int): optional; override default max-attempts (5)
Returns:
str: public IP address
"""
candidate_ip = 'foo'
check_ip = 'bar'
tries = 0
if isinstance(max_tries, type(None)):
max_tries = 5 # default to 5 tries if no override is given
elif not isinstance(max_tries, int):
raise TypeError("get_public_ip(): 'max_tries' must be an int")

while candidate_ip != check_ip:
# Attempt to resolve public IP, up to a maximum number of retries
while True:
tries += 1
if tries > max_tries:
raise ValueError(
"get_public_ip(): could not determine public IP address "
"after {} attempts!".format(tries))
tries += 1
candidate_ip = ipgetter.myip()
check_ip = ipgetter.myip()

return candidate_ip
# Get our public IP from AWS's service
log.info('Getting public IP address from AWS: attempt %s', tries)
response = requests.get('http://checkip.amazonaws.com')
candidate_ip = response.text.strip('\n')
log.info("Found our actual public IP to be %s",
candidate_ip)

try: # Return the IP only if it's valid
if ipaddress.ip_address(candidate_ip).is_global:
return candidate_ip
else:
log.error("'%s' does not appear to be a public IP address",
candidate_ip)
except ValueError as e:
log.error(e)

def get_current_record(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
boto3>=1.4.4
docopt>=0.6.2
ipgetter>=0.6
requests>=2.20.1

0 comments on commit 959221e

Please # to comment.