i think i got it

This commit is contained in:
2020-05-13 21:10:09 -04:00
parent fc8eda8198
commit cb1a877ddc
3 changed files with 240 additions and 192 deletions

View File

@@ -16,9 +16,9 @@ def xml2bool(xml_str):
if xml_str is None:
return(None)
xml_str = xml_str.lower()[0]
if xml_str == ('t', '1'):
if xml_str in ('t', '1'):
return(True)
elif xml_str == ('f', '0'):
elif xml_str in ('f', '0'):
return(False)
else:
raise ValueError('Not a boolean value')
@@ -67,140 +67,6 @@ class IP6(IP):
self.alloc_block = netaddr.SubnetSplitter(self.net_net)
class Assignment(object):
def __init__(self, assign_xml, radvd = False, dns = False):
self.xml = assign_xml
self.do_radvd = radvd
self.radvd_dns = dns
self.iface = None
self.alloc = None # This must be set externally to a mapped Allocation instance
self.alloc_name = None
self.prefix = None
self.iface_ip = None
self.alloc_block = None
self.parse()
def _alloc(self):
self.alloc_name = self.xml.attrib['alloc'].strip()
return(None)
def _iface(self):
self.iface = self.xml.attrib['iface'].strip()
return(None)
def _prefix(self):
self.prefix = int(self.xml.attrib.get('prefix', 64).strip())
return(None)
def parse(self):
self._iface()
self._alloc()
self._prefix()
return(None)
def parse_alloc(self):
self.iface_ip = IP6(str(next(self.alloc.ip.net.hosts())), 128)
self.alloc_block = self.alloc.ip.alloc_block
return(None)
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 _iface(self):
_iface_txt = self.xml.attrib['iface']
self.iface = _iface_txt.strip()
ipr = IPRoute()
self.iface_idx = ipr.link_lookup(ifname = self.iface)[0]
ipr.close()
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._iface()
self._ip()
return(None)
class Tunnel(object):
def __init__(self, tun_xml):
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_id = None
self.allocations = {}
self.assignments = []
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
return(None)
def _assignments(self):
_assigns_xml = self.xml.find('assignments')
radvd = xml2bool(_assigns_xml.attrib.get('radvd', 'false'))
radvd_dns = xml2bool(_assigns_xml.attrib.get('radvdDns', 'false'))
for _assign_xml in _assigns_xml.findall('assign'):
assign = Assignment(_assign_xml, radvd = radvd, dns = radvd_dns)
assign.alloc = self.allocations[assign.alloc_name]
assign.parse_alloc()
self.assignments.append(assign)
return(None)
def _client(self):
_client_xml = self.xml.find('client')
_ip_txt = _client_xml.text.strip()
_prefix_txt = _client_xml.attrib['prefix'].strip()
self.client = IP6(_ip_txt, _prefix_txt)
return(None)
def _creds(self):
self.creds_id = self.xml.attrib['creds'].strip()
return(None)
def _id(self):
self.id = int(self.xml.attrib['id'].strip())
return(None)
def _server(self):
_server_xml = self.xml.find('server')
_ip_text = _server_xml.text.strip()
self.server = IP4(_ip_text, 32)
return(None)
def parse(self):
self._id()
self._creds()
self._client()
self._server()
self._allocations()
self._assignments()
return(None)
class Credential(object):
def __init__(self, cred_xml):
self.xml = cred_xml
@@ -243,6 +109,148 @@ class Credential(object):
return(None)
class Assignment(object):
def __init__(self, assign_xml, radvd = False, dns = False):
self.xml = assign_xml
self.do_radvd = radvd
self.radvd_dns = dns
self.iface = None
self.iface_idx = None
self.iface_addrs = []
self.iface_blocks = []
self.alloc = None # This must be set externally to a mapped Allocation instance
self.alloc_name = None
self.prefix = None
self.alloc_block = None
self.parse()
def _alloc(self):
self.alloc_name = self.xml.attrib['alloc'].strip()
return(None)
def _iface(self):
_iface_txt = self.xml.attrib['iface'].strip()
self.iface = _iface_txt.strip()
ipr = IPRoute()
self.iface_idx = ipr.link_lookup(ifname = self.iface)[0]
ipr.close()
return(None)
def _prefix(self):
self.prefix = int(self.xml.attrib.get('prefix', 64).strip())
return(None)
def parse(self):
self._iface()
self._alloc()
self._prefix()
return(None)
def parse_alloc(self):
self.alloc_block = self.alloc.ip.alloc_block
self.iface_blocks = self.alloc_block.extract_subnet(self.prefix, count = 1)
for i in self.iface_blocks:
self.iface_addrs.append(IP6(str(next(i.iter_hosts())), 128))
return(None)
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)
class Tunnel(object):
def __init__(self, tun_xml):
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_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.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
return(None)
def _assignments(self):
_assigns_xml = self.xml.find('assignments')
self.enable_radvd = xml2bool(_assigns_xml.attrib.get('radvd', 'false'))
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.parse_alloc()
self.assignments.append(assign)
return(None)
def _client(self):
_client_xml = self.xml.find('client')
_ip_txt = _client_xml.text.strip()
_prefix_txt = _client_xml.attrib['prefix'].strip()
self.client = IP6(_ip_txt, _prefix_txt)
return(None)
def _creds(self):
self.creds_id = self.xml.attrib['creds'].strip()
return(None)
def _id(self):
self.id = int(self.xml.attrib['id'].strip())
return(None)
def _radvd(self):
self.radvd = radvd.RADVD()
self.radvd.conf.generate(self.assignments)
return(None)
def _server(self):
_server_xml = self.xml.find('server')
_ip_text = _server_xml.text.strip()
self.server = IP4(_ip_text, 32)
return(None)
def parse(self):
self._id()
self._creds()
self._client()
self._server()
self._allocations()
self._assignments()
self._radvd()
return(None)
class Config(object):
default_xsd = 'http://schema.xml.r00t2.io/projects/he_ipv6.xsd'