import os import sys import time from fluent import sender logger = sender.FluentSender('novnc-std', host=os.getenv("FLUENTD_URL"), port=os.getenv("FLUENTD_PORT")) class BasePlugin(object): def __init__(self, src): self.source = src self.local = False def lookup(self, token): return None class ReadOnlyTokenFile(BasePlugin): # source is a token file with lines like # token: host:port # or a directory of such files def __init__(self, *args, **kwargs): super(ReadOnlyTokenFile, self).__init__(*args, **kwargs) self._targets = None def _load_targets(self): if os.path.isdir(self.source): cfg_files = [os.path.join(self.source, f) for f in os.listdir(self.source)] else: cfg_files = [self.source] self._targets = {} index = 1 for f in cfg_files: for line in [l.strip() for l in open(f).readlines()]: if line and not line.startswith('#'): try: tok, target = line.split(': ') self._targets[tok] = target.strip().rsplit(':', 1) except ValueError: print >>sys.stderr, "Syntax error in %s on line %d" % (self.source, index) index += 1 def lookup(self, token): if self._targets is None: self._load_targets() if token in self._targets: return self._targets[token] else: return None # the above one is probably more efficient, but this one is # more backwards compatible (although in most cases # ReadOnlyTokenFile should suffice) class TokenFile(ReadOnlyTokenFile): # source is a token file with lines like # token: host:port # or a directory of such files def lookup(self, token): self._load_targets() return super(TokenFile, self).lookup(token) class BaseTokenAPI(BasePlugin): # source is a url with a '%s' in it where the token # should go # we import things on demand so that other plugins # in this file can be used w/o unecessary dependencies def process_result(self, resp): return resp.text.split(':') def lookup(self, token, vpn): import requests import configparser import os.path self.local = vpn if os.path.exists("/config/config.properties"): config = configparser.ConfigParser() config.read('/config/config.properties') header = {} # print(config.get('API','token')) header['Authorization'] = str(config.get('API','token')) # print(header) # print(self.source+token) resp = requests.get(self.source+token, headers=header) # print("IP HOST VNC : %s" % resp.json()['network']['ip_wan']) else: print("No properties File found") return None if resp.ok: return self.process_result(resp) else: return None class JSONTokenApi(BaseTokenAPI): # source is a url with a '%s' in it where the token # should go def process_result(self, resp): resp_json = resp.json() # print("IP HOST VNC : %s" % resp_json['network']['ip_wan']) # print("IP PORT VNC : %s" % resp_json['remote_control']['vncviewer']['port']) logger.emit_with_time('connection', time.time(), { 'from': resp_json['network']['ip_virtual'], 'port' : resp_json['remote_control']['vncviewer']['port']}) if self.local == 'true': print("VPN %s - IP HOST VNC : %s PORT VNC : %s" % (self.local,resp_json['network']['ip_virtual'],resp_json['remote_control']['vncviewer']['port'])) return (resp_json['network']['ip_virtual'], resp_json['remote_control']['vncviewer']['port']) else: print("VPN %s - IP HOST VNC : %s PORT VNC : %s" % (self.local,resp_json['network']['ip_wan'],resp_json['remote_control']['vncviewer']['port'])) return (resp_json['network']['ip_wan'], resp_json['remote_control']['vncviewer']['port'])