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.xml = cred_xml
self.id = None self.id = None
self.user = None self.user = None
self.key = None self.password = None
self.parse() self.parse()


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


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


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




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


class BaseConfig(object): class BaseConfig(object):
default_xsd = None default_xsd = None


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




# class HEConfig(BaseConfig): class HEBaseConfig(BaseConfig):
# # This is unused. Kept mostly for reference. default_xsd = ''
# nsmap = {None: '',
# default_xsd = 'http://schema.xml.r00t2.io/projects/tunnelbroker.xsd' 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
# nsmap = {None: 'https://tunelbroker.net/tunnelInfo.php', attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
# 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'} schema_loc = ' {0}'.format(default_xsd)
# attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation') url = ''
# schema_loc = 'https://tunnelbroker.net/tunnelInfo.php {0}'.format(default_xsd)
# def __init__(self, creds, *args, **kwargs):
# def __init__(self, creds, xml_url = 'https://tunnelbroker.net/tunnelInfo.php', *args, **kwargs): self.creds = creds
# self.creds = creds super().__init__(self._fetch(), *args, **kwargs)
# self.url = xml_url
# req = requests.get(self.url, def _add_ns(self, raw_xml):
# auth = requests.auth.HTTPBasicAuth(self.creds.user, self.creds.password)) # https://mailman-mail5.webfaction.com/pipermail/lxml/20100323/013260.html
# if not req.ok: _xml = etree.fromstring(raw_xml)
# raise RuntimeError('Could not fetch remote tunnel information') _nsmap = copy.deepcopy(_xml.nsmap)
# raw_xml = self._add_ns(req.content) _nsmap.update(self.nsmap)
# super().__init__(raw_xml, *args, **kwargs) mod_xml = etree.Element(_xml.tag,
# # In the format of: {tun_id: HETunnel()} {self.attr_qname: self.schema_loc},
# self.tunnels = collections.OrderedDict() nsmap = _nsmap)
# self.subparse() mod_xml[:] = _xml[:]
# return(etree.tostring(mod_xml,
# def subparse(self): encoding = 'UTF-8',
# for tun_xml in self.xml.findall('tunnel'): xml_declaration = True,
# tun = HETunnel(tun_xml) pretty_print = True,
# self.tunnels[tun.id] = tun with_tail = True,
# return(None) with_comments = True))
#
# def _add_ns(self, raw_xml): def _fetch(self):
# # https://mailman-mail5.webfaction.com/pipermail/lxml/20100323/013260.html req = requests.get(self.url,
# _xml = etree.fromstring(raw_xml) auth = requests.auth.HTTPBasicAuth(self.creds.user,
# _nsmap = copy.deepcopy(_xml.nsmap) self.creds.password))
# _nsmap.update(self.nsmap) if not req.ok:
# mod_xml = etree.Element(_xml.tag, {self.attr_qname: self.schema_loc}, nsmap = _nsmap) raise RuntimeError('Could not fetch remote tunnel information')
# mod_xml[:] = _xml[:] raw_xml = self._add_ns(req.content)
# return(etree.tostring(mod_xml, return(raw_xml)
# encoding = 'UTF-8',
# xml_declaration = True,
# pretty_print = True,
# with_tail = True,
# with_comments = True))




class HETunnelConfig(BaseConfig): # This isn't really used anymore.
# TODO: RESTRUCTURE THIS and create an HETunnel() object class HEConfig(HEBaseConfig):
default_xsd = 'http://schema.xml.r00t2.io/projects/tunnelbroker.tun.xsd' default_xsd = 'http://schema.xml.r00t2.io/projects/tunnelbroker.tun.xsd'
nsmap = {None: 'https://tunelbroker.net/tunnelInfo.php?tid', nsmap = {None: 'https://tunelbroker.net/tunnelInfo.php?tid',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance'} 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation') attr_qname = etree.QName('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
schema_loc = 'https://tunnelbroker.net/tunnelInfo.php?tid {0}'.format(default_xsd) 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): def __init__(self, creds, *args, **kwargs):
self.xml = tun_xml super().__init__(creds, *args, **kwargs)
self.creds = creds 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.id = None
self.description = None self.description = None
self.client = None # Client IPv6 self.client = None # Client IPv6

View File

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



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