From 5f58109373e16ec72c770439447a6e0119de4f9f Mon Sep 17 00:00:00 2001 From: Disassembler Date: Thu, 1 Nov 2018 23:45:22 +0100 Subject: [PATCH] Simplify get_local/external_ip --- usr/lib/python3.6/vmmgr/__init__.py | 4 ++-- usr/lib/python3.6/vmmgr/tools.py | 27 +++++++-------------------- usr/lib/python3.6/vmmgr/wsgiapp.py | 12 ++++++------ 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/usr/lib/python3.6/vmmgr/__init__.py b/usr/lib/python3.6/vmmgr/__init__.py index 33a332b..2cd6449 100644 --- a/usr/lib/python3.6/vmmgr/__init__.py +++ b/usr/lib/python3.6/vmmgr/__init__.py @@ -279,9 +279,9 @@ class VMMgr: domain = self.domain # If the dummy host is used, take an IP address of a primary interface instead if domain == 'spotter.vm': - domain = tools.get_local_ipv4() + domain = tools.get_local_ip(4) if not domain: - domain = tools.get_local_ipv6() + domain = tools.get_local_ip(6) if not domain: domain = '127.0.0.1' # Rebuild the terminal banner diff --git a/usr/lib/python3.6/vmmgr/tools.py b/usr/lib/python3.6/vmmgr/tools.py index 1c0e4fd..c3853cc 100644 --- a/usr/lib/python3.6/vmmgr/tools.py +++ b/usr/lib/python3.6/vmmgr/tools.py @@ -17,22 +17,17 @@ def compile_url(domain, port, proto='https'): port = '' if (proto == 'https' and port == '443') or (proto == 'http' and port == '80') else ':{}'.format(port) return '{}://{}{}'.format(proto, domain, port) -def get_local_ipv4(): - # Return first routable IPv4 address of the VM (container host) +def get_local_ip(version): + # Return first routable IPv4/6 address of the VM (container host) try: - return subprocess.run(['/sbin/ip', 'route', 'get', '1'], check=True, stdout=subprocess.PIPE).stdout.decode().split()[-1] + output = subprocess.run(['/sbin/ip', 'route', 'get', '1' if version == 4 else '2003::'], check=True, stdout=subprocess.PIPE).stdout.decode().split() + return output[output.index('src')+1] except: return None -def get_local_ipv6(): - # Return first routable IPv6 address of the VM (container host) - try: - return subprocess.run(['/sbin/ip', 'route', 'get', '2003::'], check=True, stdout=subprocess.PIPE).stdout.decode().split()[-3] - except: - return None - -def get_external_ip(family): - # Return external IP address of given family via 3rd party service +def get_external_ip(version): + # Return external IPv4/6 address via remote service + family = socket.AF_INET if version == 4 else socket.AF_INET6 allowed_gai_family = requests.packages.urllib3.util.connection.allowed_gai_family try: requests.packages.urllib3.util.connection.allowed_gai_family = lambda: family @@ -42,14 +37,6 @@ def get_external_ip(family): finally: requests.packages.urllib3.util.connection.allowed_gai_family = allowed_gai_family -def get_external_ipv4(): - # Return external IPv4 address - return get_external_ip(socket.AF_INET) - -def get_external_ipv6(): - # Return external IPv6 address - return get_external_ip(socket.AF_INET6) - resolver = dns.resolver.Resolver() resolver.timeout = 3 resolver.lifetime = 3 diff --git a/usr/lib/python3.6/vmmgr/wsgiapp.py b/usr/lib/python3.6/vmmgr/wsgiapp.py index 032f7f8..79a684e 100644 --- a/usr/lib/python3.6/vmmgr/wsgiapp.py +++ b/usr/lib/python3.6/vmmgr/wsgiapp.py @@ -138,10 +138,10 @@ class WSGIApp(object): def setup_host_view(self, request): # Host setup view. - ex_ipv4 = tools.get_external_ipv4() - ex_ipv6 = tools.get_external_ipv6() - in_ipv4 = tools.get_local_ipv4() - in_ipv6 = tools.get_local_ipv6() + ex_ipv4 = tools.get_external_ip(4) + ex_ipv6 = tools.get_external_ip(6) + in_ipv4 = tools.get_local_ip(4) + in_ipv6 = tools.get_local_ip(6) cert_info = tools.get_cert_info(CERT_PUB_FILE) return self.render_template('setup-host.html', request, ex_ipv4=ex_ipv4, ex_ipv6=ex_ipv6, in_ipv4=in_ipv4, in_ipv6=in_ipv6, cert_info=cert_info) @@ -228,8 +228,8 @@ class WSGIApp(object): def verify_dns_action(self, request): # Check if all FQDNs for all applications are resolvable and point to current external IP domains = [self.vmmgr.domain]+['{}.{}'.format(self.conf['apps'][app]['host'], self.vmmgr.domain) for app in self.conf['apps']] - ipv4 = tools.get_external_ipv4() - ipv6 = tools.get_external_ipv6() + ipv4 = tools.get_external_ip(4) + ipv6 = tools.get_external_ip(6) for domain in domains: try: a = tools.resolve_ip(domain, 'A')