def _makeTFTPService(self, tftp_root, tftp_port, rpc_service): """Create the dynamic TFTP service.""" from provisioningserver.rackdservices.tftp import TFTPService tftp_service = TFTPService(resource_root=tftp_root, port=tftp_port, client_service=rpc_service) tftp_service.setName("tftp") # *** EXPERIMENTAL *** # https://code.launchpad.net/~allenap/maas/tftp-offload/+merge/312146 # If the TFTP port has been set to zero, use the experimental offload # service. Otherwise stick to the normal in-process TFTP service. if tftp_port == 0: from provisioningserver.path import get_data_path from provisioningserver.rackdservices import tftp_offload from twisted.internet.endpoints import UNIXServerEndpoint tftp_offload_socket = get_data_path( "/var/lib/maas/tftp-offload.sock") tftp_offload_endpoint = UNIXServerEndpoint(reactor, tftp_offload_socket, wantPID=False) tftp_offload_service = tftp_offload.TFTPOffloadService( reactor, tftp_offload_endpoint, tftp_service.backend) tftp_offload_service.setName("tftp-offload") return tftp_offload_service # *** /EXPERIMENTAL *** return tftp_service
def server(self, reactor): """ Construct a UNIX server endpoint. """ # self.mktemp() often returns a path which is too long to be used. path = mktemp(suffix='.sock', dir='.') return UNIXServerEndpoint(reactor, path)
def __init__(self, args=None): '''Args must be an object with the following attributes: foreground, logfile, mailbox, nClients, silent, socketpath, verbose Suitable defaults will be supplied.''' # Pass command line args to ProtocolIVSHMSG, then open logging. if args is None: args = argparse.Namespace() for arg, default in self._required_arg_defaults.items(): setattr(args, arg, getattr(args, arg, default)) # Mailbox may be sized above the requested number of clients to # satisfy QEMU IVSHMEM restrictions. args.server_id = args.nClients + 1 args.nEvents = args.nClients + 2 # It's a singleton so no reason to keep the instance, however it's # the way I wrote the Klein API server so... mb = MB(args=args) MailBoxReSTAPI(mb) shutdown_http_logging() if args.foreground: if args.verbose > 1: TPlog.startLogging(sys.stdout, setStdout=False) else: TPlog.startLogging(open('/dev/null', 'a'), setStdout=False) else: PRINT('Logging to %s' % args.logfile) TPlog.startLogging( DailyLogFile.fromFullPath(args.logfile), setStdout=True) # "Pass-through" explicit print() for debug args.logmsg = TPlog.msg args.logerr = TPlog.err # By Twisted version 18, "mode=" is deprecated and you should just # inherit the tacky bit from the parent directory. wantPID creates # <path>.lock as a symlink to "PID". E = UNIXServerEndpoint( TIreactor, args.socketpath, mode=0o666, # Deprecated at Twisted 18 wantPID=True) E.listen(self) args.logmsg( '%s server @%d ready for %d clients on %s' % (args.title, args.server_id, args.nClients, args.socketpath)) # https://stackoverflow.com/questions/1411281/twisted-listen-to-multiple-ports-for-multiple-processes-with-one-reactor # Voodoo kick to a) set up one-time SI and b)setup commander. # Docs mislead, have to explicitly pass something to get persistent # state across protocol/transport invocations. As there is only # one server object per process instantion, that's not necessary. protobj = ProtocolIVSHMSGServer(self, args) # With "args" Commander(protobj)
def __init__(self, reactor, workers=None, socket_path=None): super(IPCMasterService, self).__init__() self.reactor = reactor self.workers = workers self.socket_path = socket_path if self.socket_path is None: self.socket_path = get_ipc_socket_path() if os.path.exists(self.socket_path): os.remove(self.socket_path) self.endpoint = UNIXServerEndpoint(reactor, self.socket_path) self.port = None self.connections = {} self.factory = Factory.forProtocol(IPCMaster) self.factory.service = self
def __init__(self, args=None): '''Args must be an object with the following attributes: foreground, logfile, mailbox, nClients, silent, socketpath, verbose Suitable defaults will be supplied.''' # Pass command line args to ProtocolIVSHMSG, then open logging. if args is None: args = argparse.Namespace() for arg, default in self._required_arg_defaults.items(): setattr(args, arg, getattr(args, arg, default)) # Mailbox may be sized above the requested number of clients to # satisfy QEMU IVSHMEM restrictions. args.server_id = args.nClients + 1 args.nEvents = args.nClients + 2 FAMEZ_MailBox(args=args) # singleton class, no need to keep instance self.cmdlineargs = args if args.foreground: TPlog.startLogging(sys.stdout, setStdout=False) else: PRINT('Logging to %s' % args.logfile) TPlog.startLogging( DailyLogFile.fromFullPath(args.logfile), setStdout=True) # "Pass-through" explicit print() for debug args.logmsg = TPlog.msg args.logerr = TPlog.err # By Twisted version 18, "mode=" is deprecated and you should just # inherit the tacky bit from the parent directory. wantPID creates # <path>.lock as a symlink to "PID". E = UNIXServerEndpoint( TIreactor, args.socketpath, mode=0o666, # Deprecated at Twisted 18 wantPID=True) E.listen(self) args.logmsg('FAME-Z server @%d ready for %d clients on %s' % (args.server_id, args.nClients, args.socketpath))
def create_listening_endpoint_from_config(config, cbdir, reactor, log): """ Create a Twisted stream server endpoint from a Crossbar.io transport configuration. See: https://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IStreamServerEndpoint.html :param config: The transport configuration. :type config: dict :param cbdir: Crossbar.io node directory (we need this for TLS key/certificates). :type cbdir: str :param reactor: The reactor to use for endpoint creation. :type reactor: obj :returns obj -- An instance implementing IStreamServerEndpoint """ endpoint = None # a TCP endpoint # if config['type'] == 'tcp': # the TCP protocol version (v4 or v6) # version = int(config.get('version', 4)) # the listening port if isinstance(config['port'], str): # read port from environment variable .. try: port = int(environ[config['port'][1:]]) except Exception as e: log.warn("Could not read listening port from env var: {e}", e=e) raise else: port = config['port'] # the listening interface # interface = str(config.get('interface', '').strip()) # the TCP accept queue depth # backlog = int(config.get('backlog', 50)) if 'tls' in config: # create a TLS server endpoint # if _HAS_TLS: # TLS server context context = _create_tls_server_context(config['tls'], cbdir, log) if version == 4: endpoint = SSL4ServerEndpoint(reactor, port, context, backlog=backlog, interface=interface) elif version == 6: raise Exception("TLS on IPv6 not implemented") else: raise Exception( "invalid TCP protocol version {}".format(version)) else: raise Exception( "TLS transport requested, but TLS packages not available:\n{}" .format(_LACKS_TLS_MSG)) else: # create a non-TLS server endpoint # if version == 4: endpoint = TCP4ServerEndpoint(reactor, port, backlog=backlog, interface=interface) elif version == 6: endpoint = TCP6ServerEndpoint(reactor, port, backlog=backlog, interface=interface) else: raise Exception( "invalid TCP protocol version {}".format(version)) # a Unix Domain Socket endpoint # elif config['type'] == 'unix': # the accept queue depth # backlog = int(config.get('backlog', 50)) # the path # path = FilePath(join(cbdir, os.path.expandvars(config['path']))) # if there is already something there, delete it. # if path.exists(): log.info(("{path} exists, attempting to remove before using as a " "UNIX socket"), path=path) path.remove() # create the endpoint # endpoint = UNIXServerEndpoint(reactor, path.path, backlog=backlog) # twisted endpoint-string elif config['type'] == 'twisted': endpoint = serverFromString(reactor, config['server_string']) # tor endpoint elif config['type'] == 'onion': # or "tor"? r "tor_onion"? port = config['port'] private_key_fname = _ensure_absolute(config['private_key_file'], cbdir) tor_control_ep = create_connecting_endpoint_from_config( config['tor_control_endpoint'], cbdir, reactor, log) version = config.get('version', 3) # default to modern version 3 try: with open(private_key_fname, 'r') as f: private_key = f.read().strip() log.info( "Onion private key from '{private_key_fname}'", private_key_fname=private_key_fname, ) except (IOError, OSError): private_key = None @implementer(IStreamServerEndpoint) class _EphemeralOnion(object): @defer.inlineCallbacks def listen(self, proto_factory): # we don't care which local TCP port we listen on, but # we do need to know it local_ep = TCP4ServerEndpoint(reactor, 0, interface="127.0.0.1") target_port = yield local_ep.listen(proto_factory) tor = yield txtorcon.connect( reactor, tor_control_ep, ) log.info( "Creating onion service (descriptor upload can take 30s or more)" ) hs = yield tor.create_onion_service( ports=[ (port, target_port.getHost().port), ], private_key=private_key, version=version, ) # if it's new, store our private key # XXX better "if private_key is None"? if not exists(private_key_fname): with open(private_key_fname, 'w') as f: f.write(hs.private_key) log.info("Wrote private key to '{fname}'", fname=private_key_fname) log.info( "Listening on Tor onion service {hs.hostname} " " with ports: {ports}", hs=hs, ports=" ".join(hs.ports), ) defer.returnValue(target_port) endpoint = _EphemeralOnion() else: raise Exception("invalid endpoint type '{}'".format(config['type'])) return endpoint
handler_syslog = logging.handlers.SysLogHandler(address=('localhost', 514)) handler_syslog.setFormatter( logging.Formatter(fmt=master_config.get('syslog_format'))) logging.getLogger().addHandler(handler_syslog) loaded_plugins = {} plugins = Plugins() for (plugin, config) in master_config.plugins().items(): (modulename, classname) = plugin.rsplit('.', 1) module = importlib.import_module(modulename) constructor = getattr(module, classname) loaded_plugins[plugin] = constructor(plugins, config) logging.info('Loaded plugin %s from %s', loaded_plugins[plugin].name(), plugin) # Some configuration, to load the port from? endpoint = UNIXServerEndpoint(reactor, './collect-master.sock') socat = None class Socat(protocol.ProcessProtocol): def connectionMade(self): global socat socat = self.transport logging.info('Started proxy') def processEnded(self, status): global socat if socat: socat = None try:
def getDBusEndpoints(reactor, busAddress, client=True): """ Creates DBus endpoints. @param busAddress: 'session', 'system', or a valid bus address as defined by the DBus specification. If 'session' (the default) or 'system' is supplied, the contents of the DBUS_SESSION_BUS_ADDRESS or DBUS_SYSTEM_BUS_ADDRESS environment variables will be used for the bus address, respectively. If DBUS_SYSTEM_BUS_ADDRESS is not set, the well-known address unix:path=/var/run/dbus/system_bus_socket will be used. @type busAddress: C{string} @rtype: C{list} of L{twisted.internet.interfaces.IStreamServerEndpoint} @returns: A list of endpoint instances """ if busAddress == 'session': addrString = os.environ.get('DBUS_SESSION_BUS_ADDRESS', None) if addrString is None: raise Exception('DBus Session environment variable not set') elif busAddress == 'system': addrString = os.environ.get( 'DBUS_SYSTEM_BUS_ADDRESS', 'unix:path=/var/run/dbus/system_bus_socket') else: addrString = busAddress #XXX Add documentation about extra key=value parameters in address string # such as nonce-tcp vs tcp which use same endpoint class epl = list() for ep_addr in addrString.split(';'): d = dict() kind = None ep = None for c in ep_addr.split(','): if c.startswith('unix:'): kind = 'unix' c = c[5:] elif c.startswith('tcp:'): kind = 'tcp' c = c[4:] elif c.startswith('nonce-tcp:'): kind = 'tcp' c = c[10:] d['nonce-tcp'] = True elif c.startswith('launchd:'): kind = 'launchd' c = c[7:] if '=' in c: k, v = c.split('=') d[k] = v if kind == 'unix': if 'path' in d: path = d['path'] elif 'tmpdir' in d: path = d['tmpdir'] + '/dbus-' + str(os.getpid()) elif 'abstract' in d: path = '\0' + d['abstract'] if client: ep = UNIXClientEndpoint(reactor, path=path) else: ep = UNIXServerEndpoint(reactor, address=path) elif kind == 'tcp': if client: ep = TCP4ClientEndpoint(reactor, d['host'], int(d['port'])) else: ep = TCP4ServerEndpoint(reactor, int(d['port']), interface=d['host']) if ep: ep.dbus_args = d epl.append(ep) return epl
def create_listening_endpoint_from_config(config, cbdir, reactor, log): """ Create a Twisted stream server endpoint from a Crossbar.io transport configuration. See: https://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IStreamServerEndpoint.html :param config: The transport configuration. :type config: dict :param cbdir: Crossbar.io node directory (we need this for TLS key/certificates). :type cbdir: str :param reactor: The reactor to use for endpoint creation. :type reactor: obj :returns obj -- An instance implementing IStreamServerEndpoint """ endpoint = None # a TCP endpoint # if config['type'] == 'tcp': # the TCP protocol version (v4 or v6) # version = int(config.get('version', 4)) # the listening port if type(config['port']) is six.text_type: # read port from environment variable .. try: port = int(environ[config['port'][1:]]) except Exception as e: log.warn( "Could not read listening port from env var: {}".format(e)) raise e else: port = config['port'] # the listening interface # interface = str(config.get('interface', '').strip()) # the TCP accept queue depth # backlog = int(config.get('backlog', 50)) if 'tls' in config: # create a TLS server endpoint # if _HAS_TLS: # TLS server context context = _create_tls_server_context(config['tls'], cbdir, log) if version == 4: endpoint = SSL4ServerEndpoint(reactor, port, context, backlog=backlog, interface=interface) elif version == 6: raise Exception("TLS on IPv6 not implemented") else: raise Exception( "invalid TCP protocol version {}".format(version)) else: raise Exception( "TLS transport requested, but TLS packages not available:\n{}" .format(_LACKS_TLS_MSG)) else: # create a non-TLS server endpoint # if version == 4: endpoint = TCP4ServerEndpoint(reactor, port, backlog=backlog, interface=interface) elif version == 6: endpoint = TCP6ServerEndpoint(reactor, port, backlog=backlog, interface=interface) else: raise Exception( "invalid TCP protocol version {}".format(version)) # a Unix Domain Socket endpoint # elif config['type'] == 'unix': # the accept queue depth # backlog = int(config.get('backlog', 50)) # the path # path = FilePath(join(cbdir, config['path'])) # if there is already something there, delete it. # if path.exists(): log.info(("{path} exists, attempting to remove before using as a " "UNIX socket"), path=path) path.remove() # create the endpoint # endpoint = UNIXServerEndpoint(reactor, path.path, backlog=backlog) else: raise Exception("invalid endpoint type '{}'".format(config['type'])) return endpoint
# Give process it's own session under init os.setsid() # Final fork try: pid = os.fork() if pid > 0: pid_file = open(working_dir + '/mailrfd.pid', 'w') pid_file.write(str(pid)) pid_file.close() sys.exit() except OSError, e: print e sys.exit(1) if __debug__: log.startLogging(sys.stderr) else: syslog.startLogging(prefix='mailrfd', options=LOG_PID, facility=LOG_MAIL) if __debug__: endpoint = TCP4ServerEndpoint(reactor, 8027, interface='localhost') else: endpoint = UNIXServerEndpoint(reactor, working_dir + '/socket') endpoint.listen(MailRfFactory()) reactor.run()
index = 0 except ValueError: pass # Pas la bonne accolade (JSON imbriqué) index = self.current_buffer.find('}', index + 1) pass pass class GProxyFactory(Factory): protocol = GProxyProtocol def __init__(self, remote): self.remote = remote pass endpoint = TCP4ServerEndpoint(reactor, 1863) gfactory = GreenhubFactory() endpoint.listen(gfactory) localendpoint = UNIXServerEndpoint(reactor, "/tmp/greenhub.sock") localendpoint.listen(GProxyFactory(gfactory)) debug("INFO", "Serveur démarré.") reactor.run()
def create_listening_endpoint_from_config(config, cbdir, reactor): """ Create a Twisted stream server endpoint from a Crossbar.io transport configuration. See: https://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IStreamServerEndpoint.html :param config: The transport configuration. :type config: dict :param cbdir: Crossbar.io node directory (we need this for TLS key/certificates). :type cbdir: str :param reactor: The reactor to use for endpoint creation. :type reactor: obj :returns obj -- An instance implementing IStreamServerEndpoint """ endpoint = None ## a TCP endpoint ## if config['type'] == 'tcp': ## the TCP protocol version (v4 or v6) ## version = int(config.get('version', 4)) ## the listening port ## if type(config['port']) in (str, unicode): ## read port from environment variable .. try: port = int(os.environ[config['port'][1:]]) except Exception as e: print( "Could not read listening port from env var: {}".format(e)) raise e else: port = config['port'] ## the listening interface ## interface = str(config.get('interface', '').strip()) ## the TCP accept queue depth ## backlog = int(config.get('backlog', 50)) if 'tls' in config: if _HAS_TLS: key_filepath = os.path.abspath( os.path.join(cbdir, config['tls']['key'])) cert_filepath = os.path.abspath( os.path.join(cbdir, config['tls']['certificate'])) with open(key_filepath) as key_file: with open(cert_filepath) as cert_file: if 'dhparam' in config['tls']: dhparam_filepath = os.path.abspath( os.path.join(cbdir, config['tls']['dhparam'])) else: dhparam_filepath = None ## create a TLS context factory ## key = key_file.read() cert = cert_file.read() ciphers = config['tls'].get('ciphers', None) ctx = TlsServerContextFactory( key, cert, ciphers=ciphers, dhParamFilename=dhparam_filepath) ## create a TLS server endpoint ## if version == 4: endpoint = SSL4ServerEndpoint(reactor, port, ctx, backlog=backlog, interface=interface) elif version == 6: raise Exception("TLS on IPv6 not implemented") else: raise Exception( "invalid TCP protocol version {}".format(version)) else: raise Exception( "TLS transport requested, but TLS packages not available") else: ## create a non-TLS server endpoint ## if version == 4: endpoint = TCP4ServerEndpoint(reactor, port, backlog=backlog, interface=interface) elif version == 6: endpoint = TCP6ServerEndpoint(reactor, port, backlog=backlog, interface=interface) else: raise Exception( "invalid TCP protocol version {}".format(version)) ## a Unix Domain Socket endpoint ## elif config['type'] == 'unix': ## the accept queue depth ## backlog = int(config.get('backlog', 50)) ## the path ## path = os.path.abspath(os.path.join(cbdir, config['path'])) ## create the endpoint ## endpoint = UNIXServerEndpoint(reactor, path, backlog=backlog) else: raise Exception("invalid endpoint type '{}'".format(config['type'])) return endpoint
def create_listening_endpoint_from_config(config, cbdir, reactor): """ Create a Twisted stream server endpoint from a Crossbar.io transport configuration. See: https://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IStreamServerEndpoint.html :param config: The transport configuration. :type config: dict :param cbdir: Crossbar.io node directory (we need this for TLS key/certificates). :type cbdir: str :param reactor: The reactor to use for endpoint creation. :type reactor: obj :returns obj -- An instance implementing IStreamServerEndpoint """ log = make_logger() endpoint = None # a TCP endpoint # if config['type'] == 'tcp': # the TCP protocol version (v4 or v6) # version = int(config.get('version', 4)) # the listening port # if type(config['port']) is six.text_type: # read port from environment variable .. try: port = int(environ[config['port'][1:]]) except Exception as e: print( "Could not read listening port from env var: {}".format(e)) raise e else: port = config['port'] # the listening interface # interface = str(config.get('interface', '').strip()) # the TCP accept queue depth # backlog = int(config.get('backlog', 50)) if 'tls' in config: if _HAS_TLS: key_filepath = abspath(join(cbdir, config['tls']['key'])) cert_filepath = abspath( join(cbdir, config['tls']['certificate'])) with open(key_filepath) as key_file: with open(cert_filepath) as cert_file: if 'dhparam' in config['tls']: dhpath = FilePath( abspath(join(cbdir, config['tls']['dhparam']))) dh_params = DiffieHellmanParameters.fromFile( dhpath) else: # XXX won't be doing ANY EDH # curves... maybe make dhparam required? # or do "whatever tlxctx was doing" dh_params = None log.warn( "OpenSSL DH modes not active (no 'dhparam')") # create a TLS context factory # key = key_file.read() cert = cert_file.read() ca_certs = None if 'ca_certificates' in config['tls']: ca_certs = [] for fname in config['tls']['ca_certificates']: with open(fname, 'r') as f: ca_certs.append( Certificate.loadPEM(f.read()).original) crossbar_ciphers = AcceptableCiphers.fromOpenSSLCipherString( 'ECDHE-RSA-AES128-GCM-SHA256:' 'DHE-RSA-AES128-GCM-SHA256:' 'ECDHE-RSA-AES128-SHA256:' 'DHE-RSA-AES128-SHA256:' 'ECDHE-RSA-AES128-SHA:' 'DHE-RSA-AES128-SHA') ctx = CertificateOptions( privateKey=KeyPair.load( key, crypto.FILETYPE_PEM).original, certificate=Certificate.loadPEM(cert).original, verify=(ca_certs is not None), caCerts=ca_certs, dhParameters=dh_params, acceptableCiphers=crossbar_ciphers, ) if ctx._ecCurve is None: log.warn( "OpenSSL failed to set ECDH default curve") else: log.info( "Ok, OpenSSL is using ECDH elliptic curve {curve}", curve=ctx._ecCurve.snName, ) # create a TLS server endpoint # if version == 4: endpoint = SSL4ServerEndpoint(reactor, port, ctx, backlog=backlog, interface=interface) elif version == 6: raise Exception("TLS on IPv6 not implemented") else: raise Exception( "invalid TCP protocol version {}".format(version)) else: raise Exception( "TLS transport requested, but TLS packages not available:\n{}" .format(_LACKS_TLS_MSG)) else: # create a non-TLS server endpoint # if version == 4: endpoint = TCP4ServerEndpoint(reactor, port, backlog=backlog, interface=interface) elif version == 6: endpoint = TCP6ServerEndpoint(reactor, port, backlog=backlog, interface=interface) else: raise Exception( "invalid TCP protocol version {}".format(version)) # a Unix Domain Socket endpoint # elif config['type'] == 'unix': # the accept queue depth # backlog = int(config.get('backlog', 50)) # the path # path = FilePath(join(cbdir, config['path'])) # if there is already something there, delete it. # if path.exists(): log.info(("{path} exists, attempting to remove before using as a " "UNIX socket"), path=path) path.remove() # create the endpoint # endpoint = UNIXServerEndpoint(reactor, path.path, backlog=backlog) else: raise Exception("invalid endpoint type '{}'".format(config['type'])) return endpoint
def buildProtocol(self, addr): return TraphProtocol(self.traph) def close(self): self.traph.close() if __name__ == "__main__": sock = sys.argv[1] corpus = sys.argv[2] try: with open(sock + "-options.json") as f: options = json.load(f) except: options = {} traph = TraphServerFactory(corpus, **options) endpoint = UNIXServerEndpoint(reactor, sock) server_listening_deferred = endpoint.listen(traph) @server_listening_deferred.addErrback def server_listening_failed(failure): print failure.value reactor.stop() @server_listening_deferred.addCallback def server_listen_callback(twisted_port): traph.ready() reactor.run()