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

feature: IPv6 support #162

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion noipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

__title__ = "noipy"
__version_info__ = ('1', '5', '3')
__version_info__ = ('1', '5', '4')
__version__ = ".".join(__version_info__)
__author__ = "Pablo O Vieira"
__email__ = "[email protected]"
Expand Down
68 changes: 59 additions & 9 deletions noipy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
# Copyright (c) 2013 Pablo O Vieira (povieira)
# See README.rst and LICENSE for details.

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

import socket

import dns.resolver
import requests

HTTPBIN_URL = "https://httpbin.org/ip"

IP4ONLY_URL = "http://ip4only.me/api"
IP6ONLY_URL = "http://ip6only.me/api"

COMMON_DNS = "8.8.8.8"

try:
input = raw_input
except NameError:
Expand All @@ -21,20 +32,59 @@ def read_input(message):
return input(message)


def get_ip():
"""Return machine's origin IP address.
"""
def _try_request_get_and_store(url, callback):
try:
r = requests.get(HTTPBIN_URL)
return r.json()['origin'] if r.status_code == 200 else None
r = requests.get(url)
if r.status_code == 200:
callback(r)
except requests.exceptions.ConnectionError:
pass


def get_ip():
"""Return machine's origin IP address(es).
"""

lst = []
for url in (IP4ONLY_URL, IP6ONLY_URL):
_try_request_get_and_store(
url,
lambda r: lst.append(r.text.split(',')[1])
)
if not lst:
_try_request_get_and_store(
HTTPBIN_URL,
lambda r: lst.append(r.json()['origin'])
)
if not lst:
return None
return ','.join(lst)


def _safe_resolve(dnsname, dnstype):
resolver = dns.resolver.Resolver(StringIO("nameserver %s" % COMMON_DNS))

try:
resolve = resolver.resolve
except AttributeError:
resolve = resolver.query

try:
return list(resolve(dnsname, dnstype))
except dns.exception.DNSException:
return []


def get_dns_ip(dnsname):
"""Return machine's current IP address in DNS.
"""Return machine's current IP address(es) in DNS.
"""
try:
return socket.gethostbyname(dnsname)
except socket.error:

lst = [a.address for a in _safe_resolve(dnsname, 'A') + _safe_resolve(dnsname, 'AAAA')]
if not lst:
try:
lst.append(socket.gethostbyname(dnsname))
except socket.error:
pass
if not lst:
return None
return ','.join(lst)
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
flake8==3.8.4
tox==3.23.0
pytest==4.6.9
coverage
IPy
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dnspython
requests==2.25.1
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

install_requires = [
"requests>=2.0",
"dnspython",
]

if sys.version_info[:2] < (2, 7):
Expand Down
52 changes: 47 additions & 5 deletions test/test_noipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# Copyright (c) 2013 Pablo O Vieira (povieira)
# See README.rst and LICENSE for details.

from IPy import IP

import getpass
import os
import re
import shutil
import unittest

Expand All @@ -32,9 +33,44 @@ def tearDown(self):
shutil.rmtree(self.test_dir)

def test_get_ip(self):
ips = utils.get_ip()

for ip in ips.split(','):
try:
IP(ip)
except Exception as ex:
self.assertIsNone(ex, "get_ip() failed.")

# monkey patch for testing (forcing HTTP 404)
utils.IP6ONLY_URL = "http://ip6only.me/bad"
ip = utils.get_ip()
self.assertIsNotNone(ip)
self.assertNotIn(',', ip)

self.assertTrue(re.match(VALID_IP_REGEX, ip), "get_ip() failed.")
# monkey patch for testing (forcing ConnectionError)
utils.IP6ONLY_URL = "http://example.nothing"
ip = utils.get_ip()
self.assertIsNotNone(ip)
self.assertNotIn(',', ip)

# monkey patch for testing (forcing HTTP 404)
utils.IP4ONLY_URL = "http://ip4only.me/bad"
ip = utils.get_ip()
self.assertIsNotNone(ip)
self.assertNotIn(',', ip)

# monkey patch for testing (forcing ConnectionError)
utils.IP4ONLY_URL = "http://example.nothing"

ip = utils.get_ip()
self.assertIsNotNone(ip)
self.assertNotIn(',', ip)

# monkey patch for testing (forcing HTTP 404)
utils.HTTPBIN_URL = "https://httpbin.org/bad"

ip = utils.get_ip()
self.assertTrue(ip is None, "get_ip() should return None. IP=%s" % ip)

# monkey patch for testing (forcing ConnectionError)
utils.HTTPBIN_URL = "http://example.nothing"
Expand All @@ -47,7 +83,13 @@ def test_get_dns_ip(self):

self.assertEqual(ip, "127.0.0.1", "get_dns_ip() failed.")

ip = utils.get_dns_ip("http://example.nothing")
ip = utils.get_dns_ip("ip4only.me")
self.assertIsNotNone(ip, "get_dns_ip() should resolve IPv4")

ip = utils.get_dns_ip("ip6only.me")
self.assertIsNotNone(ip, "get_dns_ip() should resolve IPv6")

ip = utils.get_dns_ip("example.nothing")
self.assertTrue(ip is None, "get_dns_ip() should return None. IP=%s"
% ip)

Expand All @@ -56,7 +98,7 @@ class PluginsTest(unittest.TestCase):

def setUp(self):
self.parser = main.create_parser()
self.test_ip = "10.1.2.3"
self.test_ip = "10.1.2.3,2004::1:2:3:4"

def tearDown(self):
pass
Expand Down Expand Up @@ -168,7 +210,7 @@ class AuthInfoTest(unittest.TestCase):

def setUp(self):
self.parser = main.create_parser()
self.test_ip = "10.1.2.3"
self.test_ip = "10.1.2.3,2004::1:2:3:4"
self.test_dir = os.path.join(os.path.expanduser("~"), "noipy_test")

def tearDown(self):
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ passenv = CI TRAVIS_BUILD_ID TRAVIS TRAVIS_BRANCH TRAVIS_JOB_NUMBER TRAVIS_PULL_
deps =
-rrequirements-dev.txt
py{26,27,35,36,37},pypy,pypy3: coverage
py{26,27,35,36,37},pypy,pypy3: IPy

commands =
python --version
Expand Down