Simplify get_local/external_ip
This commit is contained in:
parent
48ef45e487
commit
5f58109373
@ -279,9 +279,9 @@ class VMMgr:
|
|||||||
domain = self.domain
|
domain = self.domain
|
||||||
# If the dummy host is used, take an IP address of a primary interface instead
|
# If the dummy host is used, take an IP address of a primary interface instead
|
||||||
if domain == 'spotter.vm':
|
if domain == 'spotter.vm':
|
||||||
domain = tools.get_local_ipv4()
|
domain = tools.get_local_ip(4)
|
||||||
if not domain:
|
if not domain:
|
||||||
domain = tools.get_local_ipv6()
|
domain = tools.get_local_ip(6)
|
||||||
if not domain:
|
if not domain:
|
||||||
domain = '127.0.0.1'
|
domain = '127.0.0.1'
|
||||||
# Rebuild the terminal banner
|
# Rebuild the terminal banner
|
||||||
|
@ -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)
|
port = '' if (proto == 'https' and port == '443') or (proto == 'http' and port == '80') else ':{}'.format(port)
|
||||||
return '{}://{}{}'.format(proto, domain, port)
|
return '{}://{}{}'.format(proto, domain, port)
|
||||||
|
|
||||||
def get_local_ipv4():
|
def get_local_ip(version):
|
||||||
# Return first routable IPv4 address of the VM (container host)
|
# Return first routable IPv4/6 address of the VM (container host)
|
||||||
try:
|
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:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_local_ipv6():
|
def get_external_ip(version):
|
||||||
# Return first routable IPv6 address of the VM (container host)
|
# Return external IPv4/6 address via remote service
|
||||||
try:
|
family = socket.AF_INET if version == 4 else socket.AF_INET6
|
||||||
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
|
|
||||||
allowed_gai_family = requests.packages.urllib3.util.connection.allowed_gai_family
|
allowed_gai_family = requests.packages.urllib3.util.connection.allowed_gai_family
|
||||||
try:
|
try:
|
||||||
requests.packages.urllib3.util.connection.allowed_gai_family = lambda: family
|
requests.packages.urllib3.util.connection.allowed_gai_family = lambda: family
|
||||||
@ -42,14 +37,6 @@ def get_external_ip(family):
|
|||||||
finally:
|
finally:
|
||||||
requests.packages.urllib3.util.connection.allowed_gai_family = allowed_gai_family
|
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 = dns.resolver.Resolver()
|
||||||
resolver.timeout = 3
|
resolver.timeout = 3
|
||||||
resolver.lifetime = 3
|
resolver.lifetime = 3
|
||||||
|
@ -138,10 +138,10 @@ class WSGIApp(object):
|
|||||||
|
|
||||||
def setup_host_view(self, request):
|
def setup_host_view(self, request):
|
||||||
# Host setup view.
|
# Host setup view.
|
||||||
ex_ipv4 = tools.get_external_ipv4()
|
ex_ipv4 = tools.get_external_ip(4)
|
||||||
ex_ipv6 = tools.get_external_ipv6()
|
ex_ipv6 = tools.get_external_ip(6)
|
||||||
in_ipv4 = tools.get_local_ipv4()
|
in_ipv4 = tools.get_local_ip(4)
|
||||||
in_ipv6 = tools.get_local_ipv6()
|
in_ipv6 = tools.get_local_ip(6)
|
||||||
cert_info = tools.get_cert_info(CERT_PUB_FILE)
|
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)
|
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):
|
def verify_dns_action(self, request):
|
||||||
# Check if all FQDNs for all applications are resolvable and point to current external IP
|
# 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']]
|
domains = [self.vmmgr.domain]+['{}.{}'.format(self.conf['apps'][app]['host'], self.vmmgr.domain) for app in self.conf['apps']]
|
||||||
ipv4 = tools.get_external_ipv4()
|
ipv4 = tools.get_external_ip(4)
|
||||||
ipv6 = tools.get_external_ipv6()
|
ipv6 = tools.get_external_ip(6)
|
||||||
for domain in domains:
|
for domain in domains:
|
||||||
try:
|
try:
|
||||||
a = tools.resolve_ip(domain, 'A')
|
a = tools.resolve_ip(domain, 'A')
|
||||||
|
Loading…
Reference in New Issue
Block a user