Skip to content
Open
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
66 changes: 39 additions & 27 deletions src/opnsense/service/modules/addons/template_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,45 +151,57 @@ def physical_interfaces(self, names):
result.append(self.getNodeByTag('interfaces.'+name+'.if'))
return list(filter(None, result))

def host_for_port(self, host_tag: str):
return self.host_with_port(
host_tag=host_tag,
port_tag='',
brackets_on_bare_ipv6=True
)

def host_with_port(self, host_tag: str, port_tag: str, brackets_on_bare_ipv6: bool = False):
""" returns a formatting host and port and bracketed if IPv6 from tags
:param host_tag: string
:param port_tag: setting this < 0 will disable it's output and just output the ip formatted for inclusion with a separate formatted port
:param no_brackets_on_bare_ip: setting this to True means that there will be brackets on IPv6 even if it just returns an address with no port
:return: string
def host_str_for_port(self, host: str) -> str:
"""
host = self.getNodeByTag(host_tag)
port = self.getNodeByTag(port_tag)

if port is None or port == "":
port = -1
else:
port = int(port)

:param host: network host
:return: formatted host, bracketed if IPv6
"""
if self.is_ipv6(host):
return "[" + host + "]"
return host

skip_port = port < 0
def host_str_with_port(self, host: str, port: str) -> str:
"""
:param host: network host
:param port: network port; setting this to none or "" will make it so no port is included
:return: formatted host, bracketed if IPv6 and there is a port
"""
skip_port = False

if host is None or host == "":
return ""

if skip_port and not brackets_on_bare_ipv6:
return host

if self.is_ipv6(host):
host = "[" + host + "]"
if port is None or port == "":
skip_port = True
else:
port = int(port)

if skip_port:
return host

host = self.host_str_for_port(host)

return '{}:{}'.format(host, port)

def host_for_port(self, host_tag: str) -> str:
"""
:param host_tag: tag of a network host
:return: formatted host and port, bracketed if IPv6
"""
host = self.getNodeByTag(host_tag)
return self.host_str_for_port(host)

def host_with_port(self, host_tag: str, port_tag: str) -> str:
"""
:param host_tag: tag of a network host
:param port_tag: tag of a network port; setting this to none or "" will make it so no port is included
:return: formatted host and port, bracketed if IPv6 and there is a port
"""
host = self.getNodeByTag(host_tag)
port = self.getNodeByTag(port_tag)

return self.host_str_with_port(host, port)

@staticmethod
def is_ipv6(ip: str) -> bool:
return ":" in ip
Expand Down