def nsBlockFilterTask(ns, block_path, name, _handler=None, _reject=None, **kw): task_path = "/tasks/filter/{}".format(name) if name in nsDir(ns, task_path): return True nsMkdir(ns, task_path) nsSet(ns, "{}/id".format(task_path), str(uuid.uuid4())) nsSet(ns, "{}/args".format(task_path), ()) nsSet(ns, "{}/kw".format(task_path), {}) nsSet(ns, "{}/blocking".format(task_path), False) nsSet(ns, "{}/in_q".format(task_path), Queue()) nsSet(ns, "{}/out_q".format(task_path), Queue()) nsSet(ns, "{}/in".format(task_path), partial(nsGet(ns, "/blocks/filter/in"), task_path)) nsSet(ns, "{}/inF".format(task_path), partial(nsGet(ns, "/blocks/filter/inF"), task_path)) nsSet(ns, "{}/out".format(task_path), partial(nsGet(ns, "/blocks/filter/out"), task_path)) nsSet(ns, "{}/outF".format(task_path), partial(nsGet(ns, "/blocks/filter/outF"), task_path)) nsSet(ns, "{}/empty".format(task_path), partial(nsGet(ns, "/blocks/filter/empty"), task_path)) nsSet(ns, "{}/server".format(task_path), partial(nsGet(ns, "/blocks/filter/server"), task_path)) if _handler is None: nsSet(ns, "{}/handler".format(task_path), partial(nsGet(ns, "/blocks/filter/handler"), task_path)) else: nsSet(ns, "{}/handler".format(task_path), partial(_handler, block_path, task_path)) if _reject is None: nsSet(ns, "{}/reject".format(task_path), partial(nsGet(ns, "/blocks/filter/reject"), task_path)) else: nsSet(ns, "{}/reject".format(task_path), partial(_reject, block_path, task_path)) nsSet(ns, "{}/call".format(task_path), partial(nsGet(ns, "/blocks/filter/call"), task_path)) for k in kw: nsSet(ns, "{}/{}".format(task_path, k), kw[k]) nsDaemon(ns, "TASK:filter:{}".format(name), nsGet(ns, "{}/server".format(task_path)), _raw=True) return True
def nsRPCBringupServer(ns, path, host, port, maxconn): name = os.path.basename(path) dev_path = '/dev/rpc/{}'.format(name) if nsRPCisServer(ns, path) is not True: nsError(ns, "RPC service {} misconfigured".format(name)) return False nsInfo(ns, "Configuring RPC server from {}".format(path)) nsMkdir(ns, dev_path) nsMkdir(ns, "{}/root".format(dev_path)) _to_root = nsGet(ns, "{}/jail".format(path), []) for j in _to_root: _n = os.path.basename(j) _dst = "{}/root/{}".format(dev_path, _n) nsInfo(ns, "RPC.JAIL({}): {}".format(name, j)) nsLn(ns, j, _dst) dispatcher = RPCDispatcher() nsSet(ns, "{}/dispatcher".format(dev_path), dispatcher) for h in nsLs(ns, "{}/handlers".format(path)): nsInfo(ns, "Registering {}->{} ".format(name, h)) _fun = nsGet(ns, "{}/handlers/{}".format(path, h)) dispatcher.add_method(partial(_fun, dev_path), h) transport = WsgiServerTransport(queue_class=gevent.queue.Queue) nsSet(ns, "{}/transport".format(dev_path), transport) nsSet(ns, "{}/listen".format(dev_path), host) nsSet(ns, "{}/port".format(dev_path), host) nsConsole( ns, "RPC server {} will be listening on tcp://{}:{}".format( name, host, port)) pool = gevent.pool.Pool(maxconn) nsSet(ns, "{}/pool".format(dev_path), pool) wsgi_server = gevent.pywsgi.WSGIServer((host, port), transport.handle, spawn=pool, log=None) nsSet(ns, "{}/wsgi".format(dev_path), wsgi_server) nsDaemon(ns, "{}:WSGI".format(name), wsgi_server.serve_forever, _raw=True) rpc_server = RPCServerGreenlets(transport, JSONRPCProtocol(), dispatcher) if nsGet(ns, "/config/RPCCatchCalls") is True or nsGet( ns, "/etc/flags/rpctrace", False) is True: _q = gevent.queue.Queue() nsSet(ns, "{}/trace".format(dev_path), _q) rpc_server.trace = partial(nsRPCCatcher, ns, dev_path, _q) nsSet(ns, "{}/rpc", rpc_server) nsDaemon(ns, "{}:RPC".format(name), rpc_server.serve_forever, _raw=True) nsInfo(ns, "RPC server {} is up".format(name)) return True
def nsBlockNullTask(ns, block_path, name): task_path = "/tasks/null/{}".format(name) if name in nsDir(ns, task_path): return True nsMkdir(ns, task_path) nsSet(ns, "{}/id".format(task_path), str(uuid.uuid4())) nsSet(ns, "{}/args".format(task_path), ()) nsSet(ns, "{}/kw".format(task_path), {}) nsSet(ns, "{}/in".format(task_path), partial(nsGet(ns, "/blocks/null/in"), task_path)) nsSet(ns, "{}/out".format(task_path), partial(nsGet(ns, "/blocks/null/out"), task_path)) nsSet(ns, "{}/server".format(task_path), partial(nsGet(ns, "/blocks/null/server"), task_path)) nsSet(ns, "{}/handler".format(task_path), partial(nsGet(ns, "/blocks/null/handler"), task_path)) nsSet(ns, "{}/call".format(task_path), partial(nsGet(ns, "/blocks/null/call"), task_path)) nsDaemon(ns, "TASK:null:{}".format(name), nsGet(ns, "{}/server".format(task_path))) return True
def nsBlockGateTask(ns, block_path, name, **kw): task_path = "/tasks/gate/{}".format(name) if name in nsDir(ns, task_path): return True nsMkdir(ns, task_path) nsSet(ns, "{}/id".format(task_path), str(uuid.uuid4())) nsSet(ns, "{}/blocking".format(task_path), False) nsSet(ns, "{}/gate".format(task_path), True) nsSet(ns, "{}/in_q".format(task_path), Queue()) nsSet(ns, "{}/out_q".format(task_path), Queue()) nsSet(ns, "{}/in".format(task_path), partial(nsGet(ns, "/blocks/gate/in"), task_path)) nsSet(ns, "{}/inF".format(task_path), partial(nsGet(ns, "/blocks/gate/inF"), task_path)) nsSet(ns, "{}/out".format(task_path), partial(nsGet(ns, "/blocks/gate/out"), task_path)) nsSet(ns, "{}/outF".format(task_path), partial(nsGet(ns, "/blocks/gate/outF"), task_path)) nsSet(ns, "{}/server".format(task_path), partial(nsGet(ns, "/blocks/gate/server"), task_path)) nsSet(ns, "{}/trigger".format(task_path), partial(nsGet(ns, "/blocks/gate/trigger"), task_path)) nsSet(ns, "{}/empty".format(task_path), partial(nsGet(ns, "/blocks/gate/empty"), task_path)) nsSet(ns, "{}/call".format(task_path), partial(nsGet(ns, "/blocks/gate/call"), task_path)) for k in kw: nsSet(ns, "{}/{}".format(task_path, k), kw[k]) nsDaemon(ns, "TASK:gate:{}".format(name), nsGet(ns, "{}/server".format(task_path)), _raw=True) return True
def nsBlockEchoTask(ns, block_path, name): task_path = "/tasks/echo/{}".format(name) if name in nsDir(ns, task_path): return True nsMkdir(ns, task_path) nsSet(ns, "{}/id".format(task_path), str(uuid.uuid4())) nsSet(ns, "{}/args".format(task_path), ()) nsSet(ns, "{}/kw".format(task_path), {}) nsSet(ns, "{}/data".format(task_path), None) nsSet(ns, "{}/in".format(task_path), partial(nsGet(ns, "/blocks/echo/in"), task_path)) nsSet(ns, "{}/out".format(task_path), partial(nsGet(ns, "/blocks/echo/out"), task_path)) nsSet(ns, "{}/server".format(task_path), partial(nsGet(ns, "/blocks/echo/server"), task_path)) nsSet(ns, "{}/handler".format(task_path), partial(nsGet(ns, "/blocks/echo/handler"), task_path)) nsSet(ns, "{}/call".format(task_path), partial(nsGet(ns, "/blocks/echo/call"), task_path)) nsDaemon(ns, "TASK:echo:{}".format(name), nsGet(ns, "{}/server".format(task_path))) return True