okay, let's see what i broke

This commit is contained in:
brent s. 2020-05-14 13:21:40 -04:00
parent c23c803a20
commit c8c4957120
Signed by: bts
GPG Key ID: 8C004C2F93481F6B

View File

@ -120,13 +120,13 @@ class Assignment(object):
self.iface_addrs = [] self.iface_addrs = []
self.iface_blocks = [] self.iface_blocks = []
self.alloc = None # This must be set externally to a mapped Allocation instance 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.prefix = None
self.alloc_block = None self.alloc_block = None
self.parse() self.parse()


def _alloc(self): def _alloc(self):
self.alloc_name = self.xml.attrib['alloc'].strip() self.alloc_id = int(self.xml.attrib['alloc'].strip())
return(None) return(None)


def _iface(self): def _iface(self):
@ -156,53 +156,31 @@ class Assignment(object):




class Allocation(object): class Allocation(object):
def __init__(self, alloc_xml): def __init__(self, alloc_net):
self.xml = alloc_xml _ip, _prefix = alloc_net.split('/')
self.id = None self.id = int(_prefix.strip())
self.prefix = None self.prefix = self.id
self.ip = None self.ip = IP6(_ip.strip(), self.prefix)
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)




class Tunnel(object): class Tunnel(object):
def __init__(self, tun_xml, he_config): def __init__(self, tun_xml, he_tunnels):
self.xml = tun_xml self.xml = tun_xml
self.id = None self.id = None
self.client = None self.client = None
self.server = 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.creds_id = None
self.radvd = None self.radvd = None
self.enable_radvd = None self.enable_radvd = None
self.radvd_dns = None self.radvd_dns = None
self.allocations = {} # This is a dict of {}[alloc.id] = Allocation obj self.allocations = {} # This is a dict of {}[alloc.id] = Allocation obj
self.assignments = [] # This is a list of Assignment objs self.assignments = [] # This is a list of Assignment objs
self.heconf = he_config self.heconf = he_tunnels
self.parse() self.parse()


def _allocations(self): def _allocations(self):
_allocs_xml = self.xml.find('allocations') self.allocations = self.heconf[self.id].allocations
for _allocation_xml in _allocs_xml.findall('alloc'):
alloc = Allocation(_allocation_xml)
self.allocations[alloc.id] = alloc
return(None) return(None)


def _assignments(self): def _assignments(self):
@ -211,7 +189,7 @@ class Tunnel(object):
self.radvd_dns = xml2bool(_assigns_xml.attrib.get('radvdDns', 'false')) self.radvd_dns = xml2bool(_assigns_xml.attrib.get('radvdDns', 'false'))
for _assign_xml in _assigns_xml.findall('assign'): for _assign_xml in _assigns_xml.findall('assign'):
assign = Assignment(_assign_xml, radvd = self.enable_radvd, dns = self.radvd_dns) 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() assign.parse_alloc()
self.assignments.append(assign) self.assignments.append(assign)
return(None) return(None)
@ -403,11 +381,15 @@ class HEConfig(BaseConfig):
raise RuntimeError('Could not fetch remote tunnel information') raise RuntimeError('Could not fetch remote tunnel information')
raw_xml = self._add_ns(req.content) raw_xml = self._add_ns(req.content)
super().__init__(raw_xml, *args, **kwargs) super().__init__(raw_xml, *args, **kwargs)
# In the format of: {tun_id: HETunnel()}
self.tunnels = collections.OrderedDict() self.tunnels = collections.OrderedDict()
self.subparse() self.subparse()


def subparse(self): 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): def _add_ns(self, raw_xml):
# https://mailman-mail5.webfaction.com/pipermail/lxml/20100323/013260.html # https://mailman-mail5.webfaction.com/pipermail/lxml/20100323/013260.html
@ -422,3 +404,74 @@ class HEConfig(BaseConfig):
pretty_print = True, pretty_print = True,
with_tail = True, with_tail = True,
with_comments = 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)