From c8c4957120b6539f497e1c3502ec4d787c65d791 Mon Sep 17 00:00:00 2001 From: brent s Date: Thu, 14 May 2020 13:21:40 -0400 Subject: [PATCH] okay, let's see what i broke --- utils/he_ipv6/config.py | 123 ++++++++++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 35 deletions(-) diff --git a/utils/he_ipv6/config.py b/utils/he_ipv6/config.py index 1517e39..7dea709 100644 --- a/utils/he_ipv6/config.py +++ b/utils/he_ipv6/config.py @@ -120,13 +120,13 @@ class Assignment(object): self.iface_addrs = [] self.iface_blocks = [] self.alloc = None # This must be set externally to a mapped Allocation instance - self.alloc_name = None + self.alloc_id = None self.prefix = None self.alloc_block = None self.parse() def _alloc(self): - self.alloc_name = self.xml.attrib['alloc'].strip() + self.alloc_id = int(self.xml.attrib['alloc'].strip()) return(None) def _iface(self): @@ -156,53 +156,31 @@ class Assignment(object): class Allocation(object): - def __init__(self, alloc_xml): - self.xml = alloc_xml - self.id = None - self.prefix = None - self.ip = None - self.iface = None - self.iface_idx = None - self.parse() - - def _id(self): - self.id = self.xml.attrib['id'].strip() - return(None) - - def _ip(self): - _ip_txt = self.xml.text.strip() - _prefix_txt = self.xml.attrib['prefix'].strip() - self.ip = IP6(_ip_txt, _prefix_txt) - self.prefix = self.ip.prefix - return(None) - - def parse(self): - self._id() - self._ip() - return(None) + def __init__(self, alloc_net): + _ip, _prefix = alloc_net.split('/') + self.id = int(_prefix.strip()) + self.prefix = self.id + self.ip = IP6(_ip.strip(), self.prefix) class Tunnel(object): - def __init__(self, tun_xml, he_config): + def __init__(self, tun_xml, he_tunnels): self.xml = tun_xml self.id = None self.client = None self.server = None - self.creds = None # This should be handled externally and map to a Cred obj + self.creds = None self.creds_id = None self.radvd = None self.enable_radvd = None self.radvd_dns = None self.allocations = {} # This is a dict of {}[alloc.id] = Allocation obj self.assignments = [] # This is a list of Assignment objs - self.heconf = he_config + self.heconf = he_tunnels self.parse() def _allocations(self): - _allocs_xml = self.xml.find('allocations') - for _allocation_xml in _allocs_xml.findall('alloc'): - alloc = Allocation(_allocation_xml) - self.allocations[alloc.id] = alloc + self.allocations = self.heconf[self.id].allocations return(None) def _assignments(self): @@ -211,7 +189,7 @@ class Tunnel(object): self.radvd_dns = xml2bool(_assigns_xml.attrib.get('radvdDns', 'false')) for _assign_xml in _assigns_xml.findall('assign'): assign = Assignment(_assign_xml, radvd = self.enable_radvd, dns = self.radvd_dns) - assign.alloc = self.allocations[assign.alloc_name] + assign.alloc = self.allocations[assign.alloc_id] assign.parse_alloc() self.assignments.append(assign) return(None) @@ -403,11 +381,15 @@ class HEConfig(BaseConfig): raise RuntimeError('Could not fetch remote tunnel information') raw_xml = self._add_ns(req.content) super().__init__(raw_xml, *args, **kwargs) + # In the format of: {tun_id: HETunnel()} self.tunnels = collections.OrderedDict() self.subparse() def subparse(self): - pass # TODO. nativize blocks, etc. Move out of local config. + for tun_xml in self.xml.findall('tunnel'): + tun = HETunnel(tun_xml) + self.tunnels[tun.id] = tun + return(None) def _add_ns(self, raw_xml): # https://mailman-mail5.webfaction.com/pipermail/lxml/20100323/013260.html @@ -422,3 +404,74 @@ class HEConfig(BaseConfig): pretty_print = True, with_tail = True, with_comments = True)) + + +class HETunnel(object): + def __init__(self, tun_xml): + self.xml = tun_xml + self.id = None + self.description = None + self.client = None # Client IPv6 + self.server = None # Server IPv6 + self.endpoint = None # Server IPv4 + self.my_ip = None # Client IPv4 (not necessary; we locally cache Tunnel.my_ip) + self.allocations = {} # keys are 64 and 48 + self.rdns = [] # Also not necessary, but it's in the XML so why not. + self.parse() + + def _alloc(self): + for a in ('64', '48'): + _alloc = self.xml.find('routed{0}'.format(a)) + if _alloc is not None and _alloc.text.strip() != '': + self.allocations[int(a)] = Allocation(_alloc.text.strip()) + return(None) + + def _client(self): + _client = self.xml.find('clientv4').text + if _client is not None and _client.strip() != '': + self.client = IP4(_client.strip(), 32) + return(None) + + def _desc(self): + _desc = self.xml.find('description').text + if _desc is not None and _desc.strip() != '': + self.description = _desc.strip() + return(None) + + def _endpoint(self): + self.endpoint = IP4(self.xml.find('serverv4').text.strip(), 32) + return(None) + + def _id(self): + self.id = int(self.xml.attrib['id']) + return(None) + + def _my_ip(self): + _ip = self.xml.find('clientv4').text + if _ip is not None and _ip.strip() != '': + self.my_ip = IP4(_ip.strip(), 32) + return(None) + + def _rdns(self): + self.rdns = [] + for r in range(1, 6): + _rdns = self.xml.find('rdns{0}'.format(r)) + if _rdns is not None and _rdns.text.strip() != '': + self.rdns.append(_rdns.text.strip()) + self.rdns = tuple(self.rdns) + return(None) + + def _server(self): + self.server = IP6(self.xml.find('serverv6'), 128) + return(None) + + def parse(self): + self._id() + self._client() + self._desc() + self._endpoint() + self._server() + self._my_ip() + self._alloc() + self._rdns() + return(None)