def ini(paths: List[str]) -> None: for s in paths: p = Path(s) if not p.is_dir(): raise Exception('Not a directory: %s' % (s)) for s in paths: p = Path(s) f = p.joinpath('regions.yaml') if f.exists(): regions = yaml.load(f.read_text(), Loader=yaml.SafeLoader) for regionid, o in regions.items(): regionid2region[regionid] = Region(o) logger.info('router_config.ini: loaded regions from %s.' % (f)) break for s in paths: p = Path(s) for f in p.glob('router-*.yaml'): name = f.name if name.endswith('.yaml'): try: routerid = Id6(name[:-5]) if routerid.cat == 'router': rc = RouterConfig( routerid, yaml.load(f.read_text(), Loader=yaml.SafeLoader)) routerid2config[routerid] = rc logger.info( 'router_config.ini: loaded router configuration from %s.' % (f)) else: logger.info('router_config.ini: ignore file %s.' % (f)) except: logger.info('router_config.ini: ignore file %s.' % (f), exc_info=True) pass
async def accept(self, websocket:WSSP, path:str) -> None: logger.info('%s: accept: path %s' % (self, path)) router = None # type:Optional[str] errmsg = None # type:Optional[str] try: s = json.loads(await websocket.recv()) logger.info('%s: read: %s' % (self, s)) if 'router' not in s: errmsg = 'Invalid request data' else: router = s['router'] routerid = Id6(router, 'router') if routerid not in routerid2router: errmsg = 'Router not provisioned' else: logger.info('%s: respond: %s' % (self, self.muxs_uri+'/'+str(routerid))) resp = { 'router': router, 'muxs': 'muxs-::0', 'uri': self.muxs_uri+'/'+str(routerid) } await websocket.send(json.dumps(resp)) return except asyncio.CancelledError: raise except Exception as exc: errmsg = 'Could not handle request' logger.error('%s: server socket failed: %s', self, exc, exc_info=True) await websocket_send_error(websocket, router, errmsg) resp = { 'error': errmsg } await websocket.send(json.dumps(resp))
async def main(args): global infos, muxs root = logging.getLogger() ll = logging.INFO if args.loglevel: ll = LOG_STR2LEVEL[args.loglevel] root.setLevel(ll) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') if args.logfile: fh = logging.FileHandler(args.logfile) fh.setLevel(logging.DEBUG) root.addHandler(fh) else: ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) ch.setFormatter(formatter) root.addHandler(ch) infosuri = urlparse(args.infosuri) infosport = infosuri.port infoshost = infosuri.hostname muxsuri = '' if args.muxsuri: muxsuri = urlparse(args.muxsuri) else: muxsuri = infosuri._replace( netloc="{}:{}".format(infoshost, infosport + 2)) muxsport = muxsuri.port muxshost = muxsuri.hostname pkfwduri = urlparse(args.pkfwduri) logger.info('Connection details: infosuri %s, muxsuri %s, pkfwduri %s' % (infosuri.geturl(), muxsuri.geturl(), pkfwduri.geturl())) infos = Infos(infoshost, infosport, muxsuri.geturl()) muxs = Muxs(muxshost, muxsport) await infos.start() logger.info('Infos started.') await muxs.start() logger.info('Muxs started.') router_config.ini([args.confdir]) routers = args.routerids if args.routerids else router_config.routerid2config.keys( ) for s in routers: routerid = Id6(s, 'router') logger.info("Instantiating %s" % (routerid)) rc = router_config.get_router_config(routerid) await add_router(routerid, rc, pkfwduri)
async def accept(self, websocket:WSSP, path:str) -> None: logger.info('%s: accept: %s' % (self, path)) try: s = path[1:] routerid = Id6(s, 'router') if routerid not in routerid2router: await websocket_send_error(websocket, s, 'Router not provisioned') return r = routerid2router[routerid] await r.on_ws_connect(websocket) except Exception as exc: logger.error('%s: server socket failed: %s', self, exc, exc_info=True) return
def normalizeId(self, id: Any) -> str: # For tests use a shorter representation # For production use str(Id6(id)) return str(Id6(id).id)
def ap_routerid(s: str) -> Id6: return Id6(s, 'router')