i'm an idiot. i spent like 15 minutes debugging this.

This commit is contained in:
2020-05-14 03:46:55 -04:00
parent 9a8bae0ba8
commit fb89feb046
4 changed files with 122 additions and 48 deletions

View File

@@ -1,3 +1,5 @@
import datetime
import json
import logging
import os
import socket
@@ -14,6 +16,7 @@ class TunnelBroker(object):
url_ip = 'https://ipv4.clientinfo.square-r00t.net/'
params_ip = {'raw': '1'}
url_api = 'https://ipv4.tunnelbroker.net/nic/update'
ip_cache = '~/.cache/he_tunnelbroker.my_ip.json'
def __init__(self, conf_xml, tun_id = None, wan_ip = True, update = True, *args, **kwargs):
self.conf_file = os.path.abspath(os.path.expanduser(conf_xml))
@@ -26,11 +29,18 @@ class TunnelBroker(object):
self.tun = self._conf.tunnels[tun_id]
self.iface_name = 'he-{0}'.format(self.tun.id)
self.wan = wan_ip
self.needs_update = False
self.force_update = update
self.ip_cache = os.path.abspath(os.path.expanduser(self.ip_cache))
self.cached_ips = []
self.my_ip = None
self.iface_idx = None
def _get_my_ip(self):
if os.path.isfile(self.ip_cache):
with open(self.ip_cache, 'r') as fh:
self.cached_ips = [(datetime.datetime.fromtimestamp(i[0]),
config.IP4(i[1], 32)) for i in json.loads(fh.read())]
if self.wan:
logger.debug('WAN IP tunneling enabled; fetching WAN IP.')
req = requests.get(self.url_ip, params = self.params_ip)
@@ -49,12 +59,19 @@ class TunnelBroker(object):
self.my_ip = config.IP4(_defrt[0]['attrs']['RTA_PREFSRC'], 32)
ipr.close()
logger.debug('Set my_ip to {0}.'.format(self.my_ip.str))
chk_tuple = (datetime.datetime.utcnow(), self.my_ip)
if self.my_ip.str != self.cached_ips[-1][1].str:
self.needs_update = True
if self.needs_update:
self.cached_ips.append(chk_tuple)
with open(self.ip_cache, 'w') as fh:
fh.write(json.dumps([(i[0].timestamp(), i[1].str) for i in self.cached_ips], indent = 4))
return(None)
def start(self):
if self.force_update:
logger.debug('IP update forced; updating.')
self._get_my_ip()
self._get_my_ip()
if any((self.force_update, self.needs_update)):
logger.debug('IP update forced or needed; updating.')
self.update()
logger.debug('Attempting to clean up any pre-existing config')
try:
@@ -184,8 +201,9 @@ class TunnelBroker(object):
self.tun.radvd.svc.stop()
return(None)
def update(self, oneshot = False):
self._get_my_ip()
def update(self):
if not self.my_ip:
self._get_my_ip()
auth_handler = requests.auth.HTTPBasicAuth(self.tun.creds.user, self.tun.creds.key)
logger.debug('Set auth handler.')
logger.debug('Requesting IP update at provider.')