more config stuff

This commit is contained in:
brent s 2020-05-14 21:21:11 -04:00
parent 315af935ac
commit efb53be81b
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
2 changed files with 73 additions and 60 deletions

View File

@ -20,7 +20,7 @@ class Credential(object):
self.xml = cred_xml
self.id = None
self.user = None
self.key = None
self.password = None
self.parse()

def _id(self):
@ -30,14 +30,14 @@ class Credential(object):
self.id = _id.strip()
return(None)

def _update_key(self):
_key_xml = self.xml.find('updateKey')
def _password(self):
_key_xml = self.xml.find('password')
if _key_xml is None:
raise ValueError('Missing required updateKey element')
raise ValueError('Missing required password element')
_key_txt = _key_xml.text
if not _key_txt:
raise ValueError('updateKey element is empty')
self.key = _key_txt.strip()
raise ValueError('password element is empty')
self.password = _key_txt.strip()
return(None)

def _user(self):
@ -53,15 +53,10 @@ class Credential(object):
def parse(self):
self._id()
self._user()
self._update_key()
self._password()
return(None)


class HETunnel(object):
def __init__(self, tun_xml):
pass


class BaseConfig(object):
default_xsd = None

@ -190,60 +185,76 @@ class Config(BaseConfig):
return(None)


# class HEConfig(BaseConfig):
# # This is unused. Kept mostly for reference.
#
# default_xsd = 'http://schema.xml.r00t2.io/projects/tunnelbroker.xsd'
# nsmap = {None: 'https://tunelbroker.net/tunnelInfo.php',
# 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
# attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
# schema_loc = 'https://tunnelbroker.net/tunnelInfo.php {0}'.format(default_xsd)
#
# def __init__(self, creds, xml_url = 'https://tunnelbroker.net/tunnelInfo.php', *args, **kwargs):
# self.creds = creds
# self.url = xml_url
# req = requests.get(self.url,
# auth = requests.auth.HTTPBasicAuth(self.creds.user, self.creds.password))
# if not req.ok:
# 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):
# 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
# _xml = etree.fromstring(raw_xml)
# _nsmap = copy.deepcopy(_xml.nsmap)
# _nsmap.update(self.nsmap)
# mod_xml = etree.Element(_xml.tag, {self.attr_qname: self.schema_loc}, nsmap = _nsmap)
# mod_xml[:] = _xml[:]
# return(etree.tostring(mod_xml,
# encoding = 'UTF-8',
# xml_declaration = True,
# pretty_print = True,
# with_tail = True,
# with_comments = True))
class HEBaseConfig(BaseConfig):
default_xsd = ''
nsmap = {None: '',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
schema_loc = ' {0}'.format(default_xsd)
url = ''

def __init__(self, creds, *args, **kwargs):
self.creds = creds
super().__init__(self._fetch(), *args, **kwargs)

def _add_ns(self, raw_xml):
# https://mailman-mail5.webfaction.com/pipermail/lxml/20100323/013260.html
_xml = etree.fromstring(raw_xml)
_nsmap = copy.deepcopy(_xml.nsmap)
_nsmap.update(self.nsmap)
mod_xml = etree.Element(_xml.tag,
{self.attr_qname: self.schema_loc},
nsmap = _nsmap)
mod_xml[:] = _xml[:]
return(etree.tostring(mod_xml,
encoding = 'UTF-8',
xml_declaration = True,
pretty_print = True,
with_tail = True,
with_comments = True))

def _fetch(self):
req = requests.get(self.url,
auth = requests.auth.HTTPBasicAuth(self.creds.user,
self.creds.password))
if not req.ok:
raise RuntimeError('Could not fetch remote tunnel information')
raw_xml = self._add_ns(req.content)
return(raw_xml)


class HETunnelConfig(BaseConfig):
# TODO: RESTRUCTURE THIS and create an HETunnel() object
# This isn't really used anymore.
class HEConfig(HEBaseConfig):
default_xsd = 'http://schema.xml.r00t2.io/projects/tunnelbroker.tun.xsd'
nsmap = {None: 'https://tunelbroker.net/tunnelInfo.php?tid',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
schema_loc = 'https://tunnelbroker.net/tunnelInfo.php?tid {0}'.format(default_xsd)
url = 'https://tunnelbroker.net/tunnelInfo.php?tid={0}'

def __init__(self, tun_xml, creds):
self.xml = tun_xml
self.creds = creds
def __init__(self, creds, *args, **kwargs):
super().__init__(creds, *args, **kwargs)
self.tunnels = {}

def add_tunnel(self, tun_id, update_key):
self.tunnels[tun_id] = HETunnelConfig(tun_id, self.creds, update_key)
return(None)


class HETunnelConfig(HEBaseConfig):
default_xsd = 'http://schema.xml.r00t2.io/projects/tunnelbroker.tun.xsd'
nsmap = {None: 'https://tunelbroker.net/tunnelInfo.php?tid',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
schema_loc = 'https://tunnelbroker.net/tunnelInfo.php?tid {0}'.format(default_xsd)
url = 'https://tunnelbroker.net/tunnelInfo.php?tid={0}'

def __init__(self, tun_id, creds, update_key, *args, **kwargs):
self.tun_id = int(tun_id)
self.url = self.url.format(self.tun_id)
self.creds = copy.deepcopy(creds)
self.creds.password = update_key
super().__init__(self.creds, *args, **kwargs)
self.id = None
self.description = None
self.client = None # Client IPv6

View File

@ -8,8 +8,10 @@ try:
except ImportError:
_has_journald = False


if os.geteuid() == 0:
logfile = '/var/log/tunnelbroker_manager.log'
else:
logfile = '~/.cache/tunnelbroker_manager.log'
# Prep the log file.
logfile = os.path.abspath(os.path.expanduser(logfile))
os.makedirs(os.path.dirname(logfile), exist_ok = True, mode = 0o0700)