if SERVER_STARTSTOP_MODULE: SERVER_STARTSTOP_MODULE.at_server_cold_stop() # ------------------------------------------------------------ # # Start the Evennia game server and add all active services # # ------------------------------------------------------------ # Tell the system the server is starting up; some things are not available yet ServerConfig.objects.conf("server_starting_mode", True) # twistd requires us to define the variable 'application' so it knows # what to execute from. application = service.Application("Evennia") if "--nodaemon" not in sys.argv: # custom logging, but only if we are not running in interactive mode logfile = logger.WeeklyLogFile( os.path.basename(settings.SERVER_LOG_FILE), os.path.dirname(settings.SERVER_LOG_FILE), day_rotation=settings.SERVER_LOG_DAY_ROTATION, max_size=settings.SERVER_LOG_MAX_SIZE, ) application.setComponent(ILogObserver, logger.ServerLogObserver(logfile).emit) # The main evennia server program. This sets up the database # and is where we store all the other services. EVENNIA = Evennia(application)
print "$ redis-cli publish zz test" print "$ redis-cli publish foo.bar hello world" #self.auth("foobared") self.subscribe("zz") self.psubscribe("foo.*") # reactor.callLater(10, self.unsubscribe, "zz") # reactor.callLater(15, self.punsubscribe, "foo.*") # self.continueTrying = False # self.transport.loseConnection() def messageReceived(self, pattern, channel, message): print "pattern=%s, channel=%s message=%s" % (pattern, channel, message) def connectionLost(self, reason): print "lost connection:", reason class myFactory(redis.SubscriberFactory): # SubscriberFactory is a wapper for the ReconnectingClientFactory maxDelay = 120 continueTrying = True protocol = myProtocol application = service.Application("subscriber") srv = internet.TCPClient("127.0.0.1", 6379, myFactory()) srv.setServiceParent(application)
def buildProtocol(self, addr): ''' Creates Twister Protocol object. Args: addr (object): an object implementing twisted.internet.interfaces.IAddress . Returns: protocol (HotlineProtocol): HotlineProtocol object instance. ''' return HotlineProtocol(self) # Configuration parameters port = 5678 iface = 'localhost' n_operators = 2 timeout = True # Setup Hotline service top_service = service.MultiService() hotline_service = HotlineService(n_operators, timeout) hotline_service.setServiceParent(top_service) # Setup Hotline factory factory = HotlineFactory(hotline_service) tcp_service = internet.TCPServer(port, factory, interface=iface) tcp_service.setServiceParent(top_service) # Setup Callcenter Application application = service.Application('hotline') top_service.setServiceParent(application)
def testApplicationAsParent(self): s = service.Service() p = service.Application("") s.setServiceParent(p) self.assertEqual(list(service.IServiceCollection(p)), [s]) self.assertEqual(s.parent, service.IServiceCollection(p))
#if int(project.experimental) == 1: # for branch in project.branches(): # git.checkout_remote_branch(branch) # git.checkout_branch(branch) # git.pull() # if project.last_commit(branch) != git.last_commit(branch): # project.last_commit(branch, git.last_commit(branch)) # d = threads.deferToThread(self.send_job, project.name, branch, 'experimental', None) # # git.checkout_branch("master") except Exception, e: log.err(e) def startService(self): service.Service.startService(self) log.msg("scheduler: start %s" % self.sched_task) self.sched_task.start(10.0) @defer.inlineCallbacks def stopService(self): service.Service.stopService(self) yield self.sched_task.stop() brickService = BricklayerService() application = service.Application("Bricklayer") brickService.setServiceParent(application)
def testConstructor(self): service.Application("hello") service.Application("hello", 5) service.Application("hello", 5, 6)
def testPersistableComponent(self): a = service.Application("hello") p = sob.IPersistable(a) self.assertEqual(p.style, 'pickle') self.assertEqual(p.name, 'hello') self.assert_(p.original is a)
# configuration parameters port = 10000 iface = 'localhost' poetry_file = 'poetry/ecstasy.txt' # this will hold the services that combine to form the poetry server top_service = service.MultiService() # the poetry service holds the poem. it will load the poem when it is # started poetry_service = PoetryService(poetry_file) poetry_service.setServiceParent(top_service) # the tcp service connects the factory to a listening socket. it will # create the listening socket when it is started factory = PoetryFactory(poetry_service) tcp_service = internet.TCPServer(port, factory, interface=iface) tcp_service.setServiceParent(top_service) # this variable has to be named 'application' application = service.Application("fastpoetry") # this hooks the collection we made to the application top_service.setServiceParent(application) # at this point, the application is ready to go. when started by # twistd it will start the child services, thus starting up the # poetry server
def buildProtocol(self, addr): log.msg("connection received from %s" % addr) return CommandRx() class SerialService(service.Service): def __init__(self, factory, device): self.factory = factory self.device = device def startService(self): SerialPort(self.factory, self.device, reactor) logPath = "/var/log/serialtoethernetd/" if not os.path.exists(logPath): os.mkdir(logPath) logFile = DailyLogFile("messages.log", logPath, defaultMode=0644) multiService = service.MultiService() tcpfactory = CommandRxFactory() tcpService = internet.TCPServer(port, tcpfactory).setServiceParent(multiService) serialService = SerialService(USBClient(tcpfactory), device).setServiceParent(multiService) application = service.Application("serial port to ethernet") application.setComponent(ILogObserver, FileLogObserver(logFile).emit) multiService.setServiceParent(application) #SerialPort(USBClient(tcpfactory), device, reactor)
# f = manhole_ssh.ConchFactory(p) # f.publicKeys[b'ssh-rsa'] = keys.Key.fromFile("/home/deepen/Documents/deepen/key/deepen.pub") # f.privateKeys[b'ssh-rsa'] = keys.Key.fromFile("/home/deepen/Documents/deepen/key/deepen") # return f log = utils.get_logger("speech") observer = PythonLoggingObserver("speech") observer.start() manhole_service = internet.TCPServer(2024, getManholeFactory(globals(), admin='a&b')) mainService = service.MultiService() mainService.setName("speech") mainService.addService(unicast_manager.main()) application = service.Application("speech") mainService.setServiceParent(application) sys.argv.append("-y dummy") if os.name == 'posix': sys.argv.append("--logfile=/dev/null") sys.argv.append("--pidfile=run/dialer.pid") class ApplicationRunner(twistd._SomeApplicationRunner): def createOrGetApplication(self): return application def run(self): self.preApplication() self.application = self.createOrGetApplication()
import os import sys from twisted.application import internet, service from twisted.web import server import bnw.core.base import bnw.core.bnw_mongo from bnw.search.search_server import RPCSearch import config bnw.core.base.config.register(config) bnw.core.bnw_mongo.open_db() application = service.Application('BnW search service') r = RPCSearch(config.search_db, config.search_language) search_service = internet.TCPServer(config.search_port, server.Site(r), interface='127.0.0.1') search_service.setServiceParent(application) def runintwistd(): sys.argv.insert(1, __file__) sys.argv.insert(1, '-y') from twisted.scripts.twistd import run
def start_server(self, config, daemonize, pidfile, logfile, slaves_dir, mastercfgfile, buildbot_dir): from twisted.scripts._twistd_unix import UnixApplicationRunner, ServerOptions opts = ['--no_save'] if not daemonize: opts.append('--nodaemon') if pidfile: opts.extend(['--pidfile', pidfile]) if pidfile: opts.extend(['--logfile', logfile]) options = ServerOptions() options.parseOptions(opts) class JhBuildbotApplicationRunner(UnixApplicationRunner): application = None def createOrGetApplication(self): return self.application from twisted.application import service, strports from buildbot.master import BuildMaster application = service.Application('buildmaster') from buildbot.buildslave import BuildSlave from twisted.python import log from twisted.internet import defer from buildbot import interfaces from buildbot.process.properties import Properties class JhBuildSlave(BuildSlave): contact_name = None contact_email = None url = None distribution = None architecture = None version = None max_builds = 2 scheduler = None run_checks = True run_coverage_report = False run_clean_afterwards = False def load_extra_configuration(self, slaves_dir): from twisted.python import log slave_xml_file = os.path.join(slaves_dir, self.slavename + '.xml') if not os.path.exists(slave_xml_file): log.msg(_('No description for slave %s.') % self.slavename) return try: cfg = ET.parse(slave_xml_file) except: # parse error log.msg( _('Failed to parse slave config for %s.') % self.slavename) return for attribute in ( 'config/max_builds', 'config/missing_timeout', 'config/run_checks', 'config/run_coverage_report', 'config/run_clean_afterwards', 'config/scheduler', 'nightly_scheduler/minute', 'nightly_scheduler/hour', 'nightly_scheduler/dayOfMonth', 'nightly_scheduler/month', 'nightly_scheduler/dayOfWeek', 'info/contact_name', 'info/contact_email', 'info/url', 'info/distribution', 'info/architecture', 'info/version'): attr_name = attribute.split('/')[-1] try: value = cfg.find(attribute).text except AttributeError: continue if attr_name in ('max_builds', 'missing_timeout'): # int value try: value = int(value) except ValueError: continue if attr_name in ('run_checks', 'run_coverage_report', 'run_clean_afterwards'): value = (value == 'yes') if attr_name in ('minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'): try: value = int(value) except ValueError: value = '*' setattr(self, attr_name, value) if self.scheduler == 'nightly': self.nightly_kwargs = {} for attr_name in ('minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'): if hasattr(self, attr_name): self.nightly_kwargs[attr_name] = getattr( self, attr_name) class JhBuildMaster(BuildMaster): jhbuild_config = config def loadConfig(self, f): # modified from parent method to get slaves, projects, change # sources, schedulers, builders and web status ouf of # master.cfg [it would have been cleaner if jhbuild didn't # have to copy all that code.] localDict = {'basedir': os.path.expanduser(self.basedir)} try: exec f in localDict except: log.msg("error while parsing config file") raise jhbuild_config.load() try: config = localDict['BuildmasterConfig'] except KeyError: log.err("missing config dictionary") log.err("config file must define BuildmasterConfig") raise known_keys = ( "bots", "slaves", "sources", "change_source", "schedulers", "builders", "mergeRequests", "slavePortnum", "debugPassword", "logCompressionLimit", "manhole", "status", "projectName", "projectURL", "buildbotURL", "properties", "prioritizeBuilders", "eventHorizon", "buildCacheSize", "logHorizon", "buildHorizon", "changeHorizon", "logMaxSize", "logMaxTailSize", "logCompressionMethod", ) for k in config.keys(): if k not in known_keys: log.msg( "unknown key '%s' defined in config dictionary" % k) # the 'slaves' list is read from the 'slaves.csv' file in the # current directory (unless instructed different from command line) # it is a CSV file structured like this: # slavename,password config['slaves'] = [] slaves_csv_file = os.path.join(slaves_dir, 'slaves.csv') if os.path.exists(slaves_csv_file): for x in csv.reader(file(slaves_csv_file)): if not x or x[0].startswith('#'): continue kw = {} build_slave = JhBuildSlave(x[0], x[1]) build_slave.load_extra_configuration(slaves_dir) config['slaves'].append(build_slave) if len(config['slaves']) == 0: log.msg('you must fill slaves.csv with slaves') module_set = jhbuild.moduleset.load(self.jhbuild_config) module_list = module_set.get_module_list( self.jhbuild_config.modules, self.jhbuild_config.skip, include_afters=True) config['projects'] = [x.name for x in module_list \ if not x.name.startswith('meta-')] if self.jhbuild_config.jhbuildbot_svn_commits_box: # trigger builds from mails to svn-commit-list # (note Maildir must be correct, or everything will fail) from jhbuild.buildbot.changes import GnomeMaildirSource config['change_source'] = GnomeMaildirSource( self.jhbuild_config.jhbuildbot_svn_commits_box, modules=module_list, prefix=None) else: # support injection (use 'buildbot sendchange') from buildbot.changes.pb import PBChangeSource config['change_source'] = PBChangeSource() # Schedulers from jhbuild.buildbot.scheduler import SerialScheduler, NightlySerialScheduler, OnCommitScheduler config['schedulers'] = [] for slave in config['slaves']: s = None for project in config['projects']: buildername = str('%s-%s' % (project, slave.slavename)) scheduler_kwargs = {} if slave.scheduler == 'nightly': scheduler_class = NightlySerialScheduler scheduler_kwargs = slave.nightly_kwargs else: scheduler_class = SerialScheduler s = scheduler_class(buildername, project, upstream=s, builderNames=[buildername], **scheduler_kwargs) config['schedulers'].append(s) if self.jhbuild_config.jhbuildbot_svn_commits_box: # schedulers that will launch job when receiving # change notifications s2 = OnCommitScheduler('oc-' + buildername, project, builderNames=[buildername]) config['schedulers'].append(s2) # Builders from jhbuild.buildbot.factory import JHBuildFactory config['builders'] = [] for project in config['projects']: for slave in config['slaves']: f = JHBuildFactory(project, slave) config['builders'].append({ 'name': "%s-%s" % (project, slave.slavename), 'slavename': slave.slavename, 'builddir': 'builddir/%s.%s' % (project, slave.slavename), 'factory': f, 'category': project }) # Status targets if not config.has_key('status'): # let it be possible to define additional status in # master.cfg config['status'] = [] from jhbuild.buildbot.status.web import JHBuildWebStatus config['status'].append( JHBuildWebStatus(self.jhbuild_config.moduleset, config['projects'], [x.slavename for x in config['slaves']], http_port=8080, allowForce=True)) # remaining of the method is a straight copy from buildbot # ... try: # required schedulers = config['schedulers'] builders = config['builders'] slavePortnum = config['slavePortnum'] #slaves = config['slaves'] #change_source = config['change_source'] # optional debugPassword = config.get('debugPassword') manhole = config.get('manhole') status = config.get('status', []) projectName = config.get('projectName') projectURL = config.get('projectURL') buildbotURL = config.get('buildbotURL') properties = config.get('properties', {}) buildCacheSize = config.get('buildCacheSize', None) eventHorizon = config.get('eventHorizon', None) logHorizon = config.get('logHorizon', None) buildHorizon = config.get('buildHorizon', None) logCompressionLimit = config.get('logCompressionLimit', 4 * 1024) if logCompressionLimit is not None and not \ isinstance(logCompressionLimit, int): raise ValueError( "logCompressionLimit needs to be bool or int") logCompressionMethod = config.get('logCompressionMethod', "bz2") if logCompressionMethod not in ('bz2', 'gz'): raise ValueError( "logCompressionMethod needs to be 'bz2', or 'gz'") logMaxSize = config.get('logMaxSize') if logMaxSize is not None and not \ isinstance(logMaxSize, int): raise ValueError("logMaxSize needs to be None or int") logMaxTailSize = config.get('logMaxTailSize') if logMaxTailSize is not None and not \ isinstance(logMaxTailSize, int): raise ValueError( "logMaxTailSize needs to be None or int") mergeRequests = config.get('mergeRequests') if mergeRequests is not None and not callable( mergeRequests): raise ValueError("mergeRequests must be a callable") prioritizeBuilders = config.get('prioritizeBuilders') if prioritizeBuilders is not None and not callable( prioritizeBuilders): raise ValueError("prioritizeBuilders must be callable") changeHorizon = config.get("changeHorizon") if changeHorizon is not None and not isinstance( changeHorizon, int): raise ValueError("changeHorizon needs to be an int") except KeyError as e: log.msg( "config dictionary is missing a required parameter") log.msg("leaving old configuration in place") raise #if "bots" in config: # raise KeyError("c['bots'] is no longer accepted") slaves = config.get('slaves', []) if "bots" in config: m = ("c['bots'] is deprecated as of 0.7.6 and will be " "removed by 0.8.0 . Please use c['slaves'] instead.") log.msg(m) warnings.warn(m, DeprecationWarning) for name, passwd in config['bots']: slaves.append(JhBuildSlave(name, passwd)) if "bots" not in config and "slaves" not in config: log.msg( "config dictionary must have either 'bots' or 'slaves'" ) log.msg("leaving old configuration in place") raise KeyError("must have either 'bots' or 'slaves'") #if "sources" in config: # raise KeyError("c['sources'] is no longer accepted") if changeHorizon is not None: self.change_svc.changeHorizon = changeHorizon change_source = config.get('change_source', []) if isinstance(change_source, (list, tuple)): change_sources = change_source else: change_sources = [change_source] if "sources" in config: m = ( "c['sources'] is deprecated as of 0.7.6 and will be " "removed by 0.8.0 . Please use c['change_source'] instead." ) log.msg(m) warnings.warn(m, DeprecationWarning) for s in config['sources']: change_sources.append(s) # do some validation first for s in slaves: assert interfaces.IBuildSlave.providedBy(s) if s.slavename in ("debug", "change", "status"): raise KeyError("reserved name '%s' used for a bot" % s.slavename) if config.has_key('interlocks'): raise KeyError("c['interlocks'] is no longer accepted") assert isinstance(change_sources, (list, tuple)) for s in change_sources: assert interfaces.IChangeSource(s, None) # this assertion catches c['schedulers'] = Scheduler(), since # Schedulers are service.MultiServices and thus iterable. errmsg = "c['schedulers'] must be a list of Scheduler instances" assert isinstance(schedulers, (list, tuple)), errmsg for s in schedulers: assert interfaces.IScheduler(s, None), errmsg assert isinstance(status, (list, tuple)) for s in status: assert interfaces.IStatusReceiver(s, None) slavenames = [s.slavename for s in slaves] buildernames = [] dirnames = [] # convert builders from objects to config dictionaries builders_dicts = [] for b in builders: if isinstance(b, buildbot.config.BuilderConfig): builders_dicts.append(b.getConfigDict()) elif type(b) is dict: builders_dicts.append(b) else: raise ValueError( "builder %s is not a BuilderConfig object (or a dict)" % b) builders = builders_dicts for b in builders: if b.has_key( 'slavename') and b['slavename'] not in slavenames: raise ValueError("builder %s uses undefined slave %s" \ % (b['name'], b['slavename'])) for n in b.get('slavenames', []): if n not in slavenames: raise ValueError("builder %s uses undefined slave %s" \ % (b['name'], n)) if b['name'] in buildernames: raise ValueError("duplicate builder name %s" % b['name']) buildernames.append(b['name']) # sanity check name (BuilderConfig does this too) if b['name'].startswith("_"): errmsg = ("builder names must not start with an " "underscore: " + b['name']) log.err(errmsg) raise ValueError(errmsg) # Fix the dictionnary with default values, in case this wasn't # specified with a BuilderConfig object (which sets the same defaults) b.setdefault('builddir', buildbot.util.safeTranslate(b['name'])) b.setdefault('slavebuilddir', b['builddir']) if b['builddir'] in dirnames: raise ValueError("builder %s reuses builddir %s" % (b['name'], b['builddir'])) dirnames.append(b['builddir']) unscheduled_buildernames = buildernames[:] schedulernames = [] for s in schedulers: for b in s.listBuilderNames(): assert b in buildernames, \ "%s uses unknown builder %s" % (s, b) if b in unscheduled_buildernames: unscheduled_buildernames.remove(b) if s.name in schedulernames: # TODO: schedulers share a namespace with other Service # children of the BuildMaster node, like status plugins, the # Manhole, the ChangeMaster, and the BotMaster (although most # of these don't have names) msg = ("Schedulers must have unique names, but " "'%s' was a duplicate" % (s.name, )) raise ValueError(msg) schedulernames.append(s.name) if unscheduled_buildernames: log.msg( "Warning: some Builders have no Schedulers to drive them:" " %s" % (unscheduled_buildernames, )) # assert that all locks used by the Builds and their Steps are # uniquely named. lock_dict = {} for b in builders: for l in b.get('locks', []): if isinstance(l, locks.LockAccess ): # User specified access to the lock l = l.lockid if lock_dict.has_key(l.name): if lock_dict[l.name] is not l: raise ValueError( "Two different locks (%s and %s) " "share the name %s" % (l, lock_dict[l.name], l.name)) else: lock_dict[l.name] = l # TODO: this will break with any BuildFactory that doesn't use a # .steps list, but I think the verification step is more # important. for s in b['factory'].steps: for l in s[1].get('locks', []): if isinstance( l, locks.LockAccess ): # User specified access to the lock l = l.lockid if lock_dict.has_key(l.name): if lock_dict[l.name] is not l: raise ValueError( "Two different locks (%s and %s)" " share the name %s" % (l, lock_dict[l.name], l.name)) else: lock_dict[l.name] = l if not isinstance(properties, dict): raise ValueError("c['properties'] must be a dictionary") # slavePortnum supposed to be a strports specification if type(slavePortnum) is int: slavePortnum = "tcp:%d" % slavePortnum # now we're committed to implementing the new configuration, so do # it atomically # TODO: actually, this is spread across a couple of Deferreds, so it # really isn't atomic. d = defer.succeed(None) self.projectName = projectName self.projectURL = projectURL self.buildbotURL = buildbotURL self.properties = Properties() self.properties.update(properties, self.configFileName) self.status.logCompressionLimit = logCompressionLimit self.status.logCompressionMethod = logCompressionMethod self.status.logMaxSize = logMaxSize self.status.logMaxTailSize = logMaxTailSize # Update any of our existing builders with the current log parameters. # This is required so that the new value is picked up after a # reconfig. for builder in self.botmaster.builders.values(): builder.builder_status.setLogCompressionLimit( logCompressionLimit) builder.builder_status.setLogCompressionMethod( logCompressionMethod) builder.builder_status.setLogMaxSize(logMaxSize) builder.builder_status.setLogMaxTailSize(logMaxTailSize) if mergeRequests is not None: self.botmaster.mergeRequests = mergeRequests if prioritizeBuilders is not None: self.botmaster.prioritizeBuilders = prioritizeBuilders self.buildCacheSize = buildCacheSize self.eventHorizon = eventHorizon self.logHorizon = logHorizon self.buildHorizon = buildHorizon # self.slaves: Disconnect any that were attached and removed from the # list. Update self.checker with the new list of passwords, including # debug/change/status. d.addCallback(lambda res: self.loadConfig_Slaves(slaves)) # self.debugPassword if debugPassword: self.checker.addUser("debug", debugPassword) self.debugPassword = debugPassword # self.manhole if manhole != self.manhole: # changing if self.manhole: # disownServiceParent may return a Deferred d.addCallback( lambda res: self.manhole.disownServiceParent()) def _remove(res): self.manhole = None return res d.addCallback(_remove) if manhole: def _add(res): self.manhole = manhole manhole.setServiceParent(self) d.addCallback(_add) # add/remove self.botmaster.builders to match builders. The # botmaster will handle startup/shutdown issues. d.addCallback(lambda res: self.loadConfig_Builders(builders)) d.addCallback(lambda res: self.loadConfig_status(status)) # Schedulers are added after Builders in case they start right away d.addCallback( lambda res: self.loadConfig_Schedulers(schedulers)) # and Sources go after Schedulers for the same reason d.addCallback( lambda res: self.loadConfig_Sources(change_sources)) # self.slavePort if self.slavePortnum != slavePortnum: if self.slavePort: def closeSlavePort(res): d1 = self.slavePort.disownServiceParent() self.slavePort = None return d1 d.addCallback(closeSlavePort) if slavePortnum is not None: def openSlavePort(res): self.slavePort = strports.service( slavePortnum, self.slaveFactory) self.slavePort.setServiceParent(self) d.addCallback(openSlavePort) log.msg("BuildMaster listening on port %s" % slavePortnum) self.slavePortnum = slavePortnum log.msg("configuration update started") def _done(res): self.readConfig = True log.msg("configuration update complete") d.addCallback(_done) d.addCallback(lambda res: self.botmaster.maybeStartAllBuilds()) return d if buildbot_dir: basedir = buildbot_dir else: if PKGDATADIR: basedir = os.path.join(PKGDATADIR, 'buildbot') else: basedir = os.path.join(SRCDIR, 'buildbot') os.chdir(basedir) if not os.path.exists(os.path.join(basedir, 'builddir')): os.makedirs(os.path.join(basedir, 'builddir')) master_cfg_path = mastercfgfile JhBuildMaster(basedir, master_cfg_path).setServiceParent(application) JhBuildbotApplicationRunner.application = application JhBuildbotApplicationRunner(options).run()
def makeService(self, options): """ Construct a TCPServer from a factory defined in Cowrie. """ if os.name == 'posix' and os.getuid() == 0: print('ERROR: You must not run cowrie as root!') sys.exit(1) cfg = readConfigFile(options["config"]) topService = service.MultiService() application = service.Application('cowrie') topService.setServiceParent(application) factory = cowrie.ssh.factory.CowrieSSHFactory(cfg) factory.portal = portal.Portal(core.realm.HoneyPotRealm(cfg)) factory.portal.registerChecker( core.checkers.HoneypotPublicKeyChecker()) factory.portal.registerChecker( core.checkers.HoneypotPasswordChecker(cfg)) if cfg.has_option('honeypot', 'auth_none_enabled') and \ cfg.getboolean('honeypot', 'auth_none_enabled') == True: factory.portal.registerChecker(core.checkers.HoneypotNoneChecker()) # ssh is enabled by default if cfg.has_option('ssh', 'enabled') == False or \ (cfg.has_option('ssh', 'enabled') and \ cfg.getboolean('ssh', 'enabled') == True): enableSSH = True else: enableSSH = False # telnet is disabled by default if cfg.has_option('telnet', 'enabled') and \ cfg.getboolean('telnet', 'enabled') == True: enableTelnet = True else: enableTelnet = False if enableTelnet == False and enableSSH == False: print('ERROR: You must at least enable SSH or Telnet') sys.exit(1) if enableSSH: if cfg.has_option('ssh', 'listen_addr'): listen_ssh_addr = cfg.get('ssh', 'listen_addr') elif cfg.has_option('honeypot', 'listen_addr'): listen_ssh_addr = cfg.get('honeypot', 'listen_addr') else: listen_ssh_addr = '0.0.0.0' # Preference: 1, option, 2, config, 3, default of 2222 if options['port'] != 0: listen_ssh_port = int(options["port"]) elif cfg.has_option('ssh', 'listen_port'): listen_ssh_port = cfg.getint('ssh', 'listen_port') elif cfg.has_option('honeypot', 'listen_port'): listen_ssh_port = cfg.getint('honeypot', 'listen_port') else: listen_ssh_port = 2222 for i in listen_ssh_addr.split(): svc = internet.TCPServer(listen_ssh_port, factory, interface=i) # FIXME: Use addService on topService ? svc.setServiceParent(topService) if enableTelnet: if cfg.has_option('telnet', 'listen_addr'): listen_telnet_addr = cfg.get('telnet', 'listen_addr') else: listen_telnet_addr = '0.0.0.0' # Preference: 1, config, 2, default of 2223 if cfg.has_option('telnet', 'listen_port'): listen_telnet_port = cfg.getint('telnet', 'listen_port') else: listen_telnet_port = 2223 f = cowrie.telnet.transport.HoneyPotTelnetFactory(cfg) f.portal = portal.Portal(core.realm.HoneyPotRealm(cfg)) f.portal.registerChecker( core.checkers.HoneypotPasswordChecker(cfg)) for i in listen_telnet_addr.split(): tsvc = internet.TCPServer(listen_telnet_port, f, interface=i) # FIXME: Use addService on topService ? tsvc.setServiceParent(topService) if cfg.has_option('honeypot', 'interact_enabled') and \ cfg.getboolean('honeypot', 'interact_enabled') == True: iport = int(cfg.get('honeypot', 'interact_port')) # FIXME this doesn't support checking both Telnet and SSH sessions from cowrie.core import interact svc = internet.TCPServer(iport, interact.makeInteractFactory(factory), interface='127.0.0.1') # FIXME: Use addService on topService ? svc.setServiceParent(topService) return topService
import dns_server import config import web_server from twisted.application import internet, service application = service.Application("dyndns") dynamic_zone = dns_server.DynamicZone(config.ZONE, config.SOA_RECORD) dns_ = internet.UDPServer(53, dns_server.DNSServerFactory([dynamic_zone]), interface=config.DNS_INTERFACE) dns_.setServiceParent(application) web_ = internet.TCPServer(config.PORT, web_server.WebRegistrarFactory(dynamic_zone), interface=config.WEB_INTERFACE) web_.setServiceParent(application)
def remote_add(self): self.log("add") arg1, arg2 = self.stack.pop(), self.stack.pop() self.stack.append(arg1 + arg2) def remote_subtract(self): self.log("subtract") arg1, arg2 = self.stack.pop(), self.stack.pop() self.stack.append(arg2 - arg1) def remote_pop(self): self.log("pop") return self.stack.pop() tub = Tub(certFile="tub.pem") lp = "tcp:%d" % LOCALPORT if 0: lp += ":interface=127.0.0.1" tub.listenOn(lp) tub.setLocation("tor:%s:%d" % (ONION, ONIONPORT)) url = tub.registerReference(Calculator(), "calculator") print("the object is available at:", url) application = service.Application("check-connections-server") tub.setServiceParent(application) if __name__ == '__main__': raise RuntimeError( "please run this as 'twistd -noy check-connections-server.py'")
def main(args=None): """ The main routine. """ logger = cfg.configure_logger() if cfg.useTwisted: import logging logger = logging.getLogger('twisted') from twisted.internet import reactor from twisted.web.resource import Resource from twisted.web import static, server from twisted.web.wsgi import WSGIResource from twisted.application import service from twisted.internet import endpoints class ReactApp: """ Handles the react app (excluding the static dir). """ def __init__(self, path): # TODO allow this to load when in debug mode even if the files don't exist self.publicFiles = { f: static.File(os.path.join(path, f)) for f in os.listdir(path) if os.path.exists(os.path.join(path, f)) } self.indexHtml = ReactIndex(os.path.join(path, 'index.html')) def get_file(self, path): """ overrides getChild so it always just serves index.html unless the file does actually exist (i.e. is an icon or something like that) """ return self.publicFiles.get(path.decode('utf-8'), self.indexHtml) class ReactIndex(static.File): """ a twisted File which overrides getChild so it always just serves index.html (NB: this is a bit of a hack, there is probably a more correct way to do this but...) """ def getChild(self, path, request): return self class FlaskAppWrapper(Resource): """ wraps the flask app as a WSGI resource while allow the react index.html (and its associated static content) to be served as the default page. """ def __init__(self): super().__init__() self.wsgi = WSGIResource(reactor, reactor.getThreadPool(), app) import sys if getattr(sys, 'frozen', False): # pyinstaller lets you copy files to arbitrary locations under the _MEIPASS root dir uiRoot = os.path.join(sys._MEIPASS, 'ui') elif cfg.webappPath is not None: uiRoot = cfg.webappPath else: # release script moves the ui under the analyser package because setuptools doesn't seem to include # files from outside the package uiRoot = os.path.join(os.path.dirname(__file__), 'ui') logger.info('Serving ui from ' + str(uiRoot)) self.react = ReactApp(uiRoot) self.static = static.File(os.path.join(uiRoot, 'static')) self.icons = static.File(cfg.iconPath) ws_server.factory.startFactory() self.ws_resource = WebSocketResource(ws_server.factory) def getChild(self, path, request): """ Overrides getChild to allow the request to be routed to the wsgi app (i.e. flask for the rest api calls), the static dir (i.e. for the packaged css/js etc), the various concrete files (i.e. the public dir from react-app), the command icons or to index.html (i.e. the react app) for everything else. :param path: :param request: :return: """ # allow CORS (CROSS-ORIGIN RESOURCE SHARING) for debug purposes request.setHeader('Access-Control-Allow-Origin', '*') request.setHeader('Access-Control-Allow-Methods', 'GET, PUT') request.setHeader('Access-Control-Allow-Headers', 'x-prototype-version,x-requested-with') request.setHeader('Access-Control-Max-Age', '2520') # 42 hours logger.debug(f"Handling {path}") if path == b'ws': return self.ws_resource elif path == b'api': request.prepath.pop() request.postpath.insert(0, path) return self.wsgi elif path == b'static': return self.static elif path == b'icons': return self.icons else: return self.react.get_file(path) def render(self, request): return self.wsgi.render(request) application = service.Application('cmdserver') if cfg.is_access_logging is True: site = server.Site(FlaskAppWrapper(), logPath=path.join(cfg.config_path, 'access.log').encode()) else: site = server.Site(FlaskAppWrapper()) endpoint = endpoints.TCP4ServerEndpoint(reactor, cfg.port, interface='0.0.0.0') endpoint.listen(site) reactor.run() else: logger.error('Icons are not available in debug mode') # get config from a flask standard place not our config yml app.run(debug=cfg.run_in_debug, host='0.0.0.0', port=cfg.port, use_reloader=False)
if SERVER_STARTSTOP_MODULE: SERVER_STARTSTOP_MODULE.at_server_cold_stop() #------------------------------------------------------------ # # Start the Evennia game server and add all active services # #------------------------------------------------------------ # Tell the system the server is starting up; some things are not available yet ServerConfig.objects.conf("server_starting_mode", True) # twistd requires us to define the variable 'application' so it knows # what to execute from. application = service.Application('Evennia') # The main evennia server program. This sets up the database # and is where we store all the other services. EVENNIA = Evennia(application) print('-' * 50) print(' %(servername)s Server (%(version)s) started.' % { 'servername': SERVERNAME, 'version': VERSION }) if AMP_ENABLED: # The AMP protocol handles the communication between # the portal and the mud server. Only reason to ever deactivate
# just use that to control when the system should be # listening listeners = service.MultiService() for (ip, port) in validAddresses: listener = internet.TCPServer(port, proxySvc, interface=ip) listener.setServiceParent(listeners) # Set the proxy services listeners variable with the # new multiservice proxySvc.listeners = listeners proxySvc.activeAddresses = validAddresses return c # this is the core part of any tac file, the creation of the root-level # application object application = service.Application("Proxy65") try: configFromFile = readPlist(configPath) except: print("Failed to find configuration file: %s" % (configPath)) sys.exit(1) config.update(configFromFile) # attach the service to its parent application service = makeService(config) service.setServiceParent(application)
def testServiceComponent(self): a = service.Application("hello") self.assert_(service.IService(a) is service.IServiceCollection(a)) self.assertEqual(service.IService(a).name, "hello") self.assertEqual(service.IService(a).parent, None)
""" to run this script:: twistd -ny nevow_webserver.py """ from twisted.web import static from twisted.application import service, internet from nevow import appserver root = static.File('../www') #root = '../www' application = service.Application('LucsTwistedApp') site = appserver.NevowSite(root) webServer = internet.TCPServer(8080, site) webServer.setServiceParent(application)
def test_startApplication(self): appl = service.Application("lala") app.startApplication(appl, 0) self.assert_(service.IService(appl).running)
print 'The db is close... ok!' class Minitor_Factory(ServerFactory): # 还没想好要初始化什么 protocol = Mornitor_Protocol def __init__(self, service): self.service = service class Fish_Service(service.Service): def startService(self): service.Service.startService(self) def stopService(self): return self._port.stopListening() top_service = service.MultiService() fish_service = Fish_Service() factory = Minitor_Factory(Fish_Service) fish_service.setServiceParent(top_service) tcp_service = internet.TCPServer(port, factory, interface=iface) tcp_service.setServiceParent(top_service) application = service.Application("SmallFish--Monitor") # this hooks the collection we made to the application top_service.setServiceParent(application) # 使用 twisted -y main.py (脚本名称) 就可以以守护进程的方式运行了!
else: stack.append(int(tok)) result = str(stack.pop()) if stack: result += ' (warning: %d item(s) left on stack)' % len(stack) return result def command_moose(self,rest): return "Moose is Awesome" class AnnuskaIRCactory(protocol.ReconnectingClientFactory): protocol = AnnuskaIRC channels = irc_settings['channels'] if __name__ == '__main__': if irc_settings['ssl'] == True: contextFactory = ssl.ClientContextFactory() reactor.connectSSL(irc_settings['server'], irc_settings['port'], AnnuskaIRCactory(), contextFactory) else: reactor.connectTCP(irc_settings['server'], irc_settings['port'], AnnuskaIRCactory()) log.startLogging(sys.stdout) reactor.run() elif __name__ == '__builtin__': application = service.Application('AnnuskaIRCBot') ircService = internet.TCPClient(irc_settings['server'], irc_settings['port'], AnnuskaIRCactory()) ircService.setServiceParent(application)
def create_services(local_port, public_port, application): create_local_service(local_port, application) create_public_service(public_port, application) # # the application # def patch_noisy_factory(): from twisted.internet.protocol import Factory Factory.noisy = False def run(application): patch_noisy_factory() local_port = os.getenv('LOCAL_SERVICES_PORT', 2525) public_port = os.getenv('HTTPS_PORT', None) conf = get_config() check_env(local_port, public_port) check_conf(conf) d = check_schema_versions(conf['couch_url']) d.addCallback(lambda _: create_services(local_port, public_port, application)) d.addErrback(_log_and_exit) application = service.Application('soledad-server')
127.0.0.1 example.com Then visit http://example.com/ with a web browser and compare the results to visiting http://localhost/. """ from twisted.application import internet, service from twisted.web import script, server, static, vhost default = static.Data(b"", "text/html") # Setting up vhost resource. default.putChild(b"vhost", vhost.VHostMonsterResource()) resource = vhost.NameVirtualHost() resource.default = default # Here we use /var/www/html/ as our root diretory for the web server, you can # change it to whatever directory you want. root = static.File("/var/www/html/") root.processors = {".rpy": script.ResourceScript} # addHost binds domain name example.com to our root resource. resource.addHost("example.com", root) # Setup Twisted Application. site = server.Site(resource) application = service.Application("vhost") sc = service.IServiceCollection(application) # Only the processes owned by the root user can listen @ port 80, change the # port number here if you don't want to run it as root. i = internet.TCPServer(80, site) i.setServiceParent(sc)
class DetectorService(internet.TimerService): """Detect clients not sending heartbeats for too long""" def __init__(self): internet.TimerService.__init__(self, CHECK_PERIOD, self.detect) self.beats = {} def update(self, ip): self.beats[ip] = time.time() def detect(self): """Log a list of clients with heartbeat older than CHECK_TIMEOUT""" limit = time.time() - CHECK_TIMEOUT silent = [ip for (ip, ipTime) in self.beats.items() if ipTime < limit] log.msg('Silent clients: %s' % silent) application = service.Application('Heartbeat') # define and link the silent clients' detector service detectorSvc = DetectorService() detectorSvc.setServiceParent(application) # create an instance of the Receiver protocol, and give it the callback receiver = Receiver() receiver.callback = detectorSvc.update # define and link the UDP server service, passing the receiver in udpServer = internet.UDPServer(UDP_PORT, receiver) udpServer.setServiceParent(application) # each service is started automatically by Twisted at launch time log.msg('Asynchronous heartbeat server listening on port %d\n' 'press Ctrl-C to stop\n' % UDP_PORT)
from twisted.protocols import basic class MyChat(basic.LineReceiver): def connectionMade(self): print "Got new client!" self.factory.clients.append(self) def connectionLost(self, reason): print "Lost a client!" self.factory.clients.remove(self) def lineReceived(self, line): print "received", repr(line) for c in self.factory.clients: c.sendMessage(line) def sendMessage(self, message): self.transport.write(message + '\n') from twisted.internet import protocol from twisted.application import service, internet factory = protocol.ServerFactory() factory.protocol = MyChat factory.clients = [] application = service.Application("chatserver") internet.TCPServer(1025, factory).setServiceParent(application)
# encoding: utf-8 """ @Author: wanghuagang @Contact: [email protected] @Project: StudyPython @File: d4 @Date: 2019/8/26 下午6:12 @Description: """ from twisted.application import internet, service from twisted.internet import reactor from twisted.web import static, server root = static.File("/var/www/htdocs") application = service.Application('web') site = server.Site(root) sc = service.IServiceCollection(application) i = internet.TCPServer(8080, site) i.setServiceParent(sc) reactor.run()
def makeService(self, options): """ Construct a TCPServer from a factory defined in Cowrie. """ if options["help"] is True: print("""Usage: twistd [options] cowrie [-h] Options: -h, --help print this help message. Makes a Cowrie SSH/Telnet honeypot. """) sys.exit(1) if os.name == 'posix' and os.getuid() == 0: print('ERROR: You must not run cowrie as root!') sys.exit(1) tz = CowrieConfig().get('honeypot', 'timezone', fallback='UTC') # `system` means use the system time zone if tz != 'system': os.environ['TZ'] = tz log.msg("Python Version {}".format(str(sys.version).replace('\n', ''))) log.msg("Twisted Version {}.{}.{}".format(__version__.major, __version__.minor, __version__.micro)) # ssh is enabled by default enableSSH = CowrieConfig().getboolean('ssh', 'enabled', fallback=True) # telnet is disabled by default enableTelnet = CowrieConfig().getboolean('telnet', 'enabled', fallback=False) if enableTelnet is False and enableSSH is False: print('ERROR: You must at least enable SSH or Telnet') sys.exit(1) # Load output modules self.output_plugins = [] for x in CowrieConfig().sections(): if not x.startswith('output_'): continue if CowrieConfig().getboolean(x, 'enabled') is False: continue engine = x.split('_')[1] try: output = __import__('cowrie.output.{}'.format(engine), globals(), locals(), ['output']).Output() log.addObserver(output.emit) self.output_plugins.append(output) log.msg("Loaded output engine: {}".format(engine)) except ImportError as e: log.err( "Failed to load output engine: {} due to ImportError: {}". format(engine, e)) log.msg( "Please install the dependencies for {} listed in requirements-output.txt" .format(engine)) except Exception: log.err() log.msg("Failed to load output engine: {}".format(engine)) topService = service.MultiService() application = service.Application('cowrie') topService.setServiceParent(application) if enableSSH: factory = cowrie.ssh.factory.CowrieSSHFactory() factory.tac = self factory.portal = portal.Portal(core.realm.HoneyPotRealm()) factory.portal.registerChecker( core.checkers.HoneypotPublicKeyChecker()) factory.portal.registerChecker( core.checkers.HoneypotPasswordChecker()) if CowrieConfig().getboolean( 'ssh', 'auth_none_enabled', fallback=False) is True: factory.portal.registerChecker( core.checkers.HoneypotNoneChecker()) if CowrieConfig().has_section('ssh'): listen_endpoints = get_endpoints_from_section( CowrieConfig(), 'ssh', 2222) else: listen_endpoints = get_endpoints_from_section( CowrieConfig(), 'honeypot', 2222) create_endpoint_services(reactor, topService, listen_endpoints, factory) if enableTelnet: f = cowrie.telnet.factory.HoneyPotTelnetFactory() f.tac = self f.portal = portal.Portal(core.realm.HoneyPotRealm()) f.portal.registerChecker(core.checkers.HoneypotPasswordChecker()) listen_endpoints = get_endpoints_from_section( CowrieConfig(), 'telnet', 2223) create_endpoint_services(reactor, topService, listen_endpoints, f) return topService
from twisted.application import internet, service from nevow import appserver from nevow import loaders from nevow import rend from nevow import tags as T class Page(rend.Page): """Example of using stan to render a page. """ addSlash = True docFactory = loaders.stan(T.html[T.head[T.title['Hello'], ], T.body[T.p( id="body")['Welcome to the wonderful world of Nevow!'], ], ]) application = service.Application('hellostan') webServer = internet.TCPServer(8080, appserver.NevowSite(Page())) webServer.setServiceParent(application)