def requestAvatar(self, avatar_id, mind, *interfaces):
        if ALLVERBOSE or VERBOSE: print "ImapProxyUserRealm.requestAvatar"
        #Only one interface/class exists, but this is the way Twisted does this...
        for requestedInterface in interfaces:
            if self.avatarInterfaces.has_key(requestedInterface):
                #return account class from cache if exists or create new
                avatar_class = self.avatarInterfaces[requestedInterface]
                #set up new proxy
                account = avatar_class(avatar_id, self.proxy)
                #parse uid, @server, and pwd from passed avatarid in which this info was encoded 
                address, pwd = avatar_id.split(':')
                uid, imap_server_address = address.split('@')
                if ALLVERBOSE or VERBOSE: print uid, imap_server_address, pwd           
                #Set up account connection to server
                #Reuses existing connection or creates a new one and caches it for reuse
                if self.proxy.connected.has_key(avatar_id):
                    account.server = self.proxy.connected[avatar_id]
                else:
                    account.server = ImapProxyClient(uid,pwd,imap_server_address)
                    self.proxy.connected[avatar_id] = account.server
                    account.server.factory = ImapProxyClientProtocolFactory(uid, pwd)
                    account.server.factory.deferred.addCallback(account.firstConnect).addErrback(account.connectError)
                    reactor.connectSSL(imap_server_address, 993, account.server.factory, ssl.ClientContextFactory())
                #return account.factory.deferred
                return account.server.factory.deferred.addCallback(self.__requestedAvatar,requestedInterface, account, lambda: None) #lambda is used to create a null logout function

        #If no interface supported
        raise KeyError("None of the requested avatar interfaces is supported")
Exemple #2
0
 def testFactoryInfo(self):
     url = self.getURL('file')
     scheme, host, port, path = client._parse(url)
     factory = client.HTTPClientFactory(url)
     reactor.connectSSL(host, port, factory, ssl.ClientContextFactory())
     # The base class defines _cbFactoryInfo correctly for this
     return factory.deferred.addCallback(self._cbFactoryInfo, factory)
Exemple #3
0
def main():
    from optparse import OptionParser
    parser = OptionParser(usage="usage: %prog [options] channels")

    # IRC connection options
    parser.add_option("-s", "--server",
                      action="store", dest="server",
                      default="irc.freenode.net",
                      help="IRC server to connect to")
    parser.add_option("-p", "--port",
                      action="store", type="int", dest="port", default=None,
                      help="IRC server to connect to")
    parser.add_option("--ssl",
                      action="store_true", dest="ssl", default=False,
                      help="use SSL")
    parser.add_option("--password",
                      action="store", dest="password", default=None,
                      help="server password")
    parser.add_option("-n", "--nick",
                      action="store", dest="nick", default="karmabot",
                      help="nickname to use")
    # Bot options
    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose", default=False,
                      help="enable verbose output")
    parser.add_option("-d", "--data",
                      action="store", dest="filename", default="karma.json",
                      help="karma data file name")
    parser.add_option("-t", "--trust",
                      action="append", dest="trusted", default=[],
                      help="trusted hostmasks")
    parser.add_option("-f", "--facets",
                      action="append", dest="facets", default=[],
                      help="additional facets to load")

    (options, channels) = parser.parse_args()

    if not channels:
        parser.error("You must supply some channels to join.")
    else:
        log.msg("Channels to join: %s" % channels)

    if options.verbose:
        log.startLogging(sys.stdout)

    if not options.port:
        options.port = 6667 if not options.ssl else 9999
    
    # FIXME: this needs to be replaced with a real facet manager
    for facet_path in options.facets:
        execfile(facet_path, globals())

    factory = KarmaBotFactory(options.filename, options.nick,
                              channels, options.trusted, options.password)
    if not options.ssl:
        reactor.connectTCP(options.server, options.port, factory)
    else:
        reactor.connectSSL(options.server, options.port,
                           factory, ssl.ClientContextFactory())
    reactor.run()
    def _request (self, method, url, *a, **kw) :
        if not url or (not url.startswith("http://") and not url.startswith("https://")) :
            if not self._base_url :
                return defer.maybeDeferred(lambda x : Response(url, ), )

            url = urllib.basejoin(self._base_url, (url and url or ""), )

        _scheme, _host, _port, _path = client_http._parse(url, )

        kw["method"] = method
        _factory = self._client_factory(url, *a, **kw)

        if _scheme == "https" :
            from twisted.internet import ssl
            #_contextFactory = kw.get("contextFactory")
            #if _contextFactory is None :
            #    _contextFactory = ssl.ClientContextFactory()
            _contextFactory = ssl.ClientContextFactory()

            reactor.connectSSL(_host, _port, _factory, _contextFactory, )
        else:
            reactor.connectTCP(_host, _port, _factory)

        return _factory.deferred.addCallback(
            self._cb_request, _factory, url,
        ).addCallback(
            self._cb_request_debug,
        )
Exemple #5
0
 def write(self, notifications):
   "Connect to the APNS service and send notifications"
   if not self.factory:
     log.msg('APNSService write (connecting)')
     server, port = ((APNS_SERVER_SANDBOX_HOSTNAME 
                     if self.environment == 'sandbox'
                     else APNS_SERVER_HOSTNAME), APNS_SERVER_PORT)
     self.factory = self.clientProtocolFactory()
     context = self.getContextFactory()
     reactor.connectSSL(server, port, self.factory, context)
   
   client = self.factory.clientProtocol
   if client:
     return client.sendMessage(notifications)
   else:      
     d = self.factory.deferred
     timeout = reactor.callLater(self.timeout, 
       lambda: d.called or d.errback(
         Exception('Notification timed out after %i seconds' % self.timeout)))
     def cancel_timeout(r):
       try: timeout.cancel()
       except: pass
       return r
     
     d.addCallback(lambda p: p.sendMessage(notifications))
     d.addErrback(log_errback('apns-service-write'))
     d.addBoth(cancel_timeout)
     return d
Exemple #6
0
def _makeDeferredRequest(url, contextFactory=None, proxy=None,
                         progress_tracker=None,
                         clientFactoryClass=None,
                         *args, **kwargs):
    """Download a web page as a string.

    Download a page. Return a deferred, which will callback with a
    page (as a string) or errback with a description of the error.

    See HTTPClientFactory to see what extra args can be passed.
    """
    if proxy:
        scheme, host, port, path = _parse(proxy)
        kwargs['proxy'] = proxy
    else:
        scheme, host, port, path = _parse(url)

    if not clientFactoryClass:
        clientFactoryClass = HTTPClientFactory
    factory = clientFactoryClass(url, *args, **kwargs)

    if progress_tracker is not None and hasattr(factory, 'set_progress_tracker'):
        factory.set_progress_tracker(progress_tracker)

    if scheme == 'https':
        from twisted.internet import ssl
        if contextFactory is None:
            contextFactory = ssl.ClientContextFactory()
        reactor.connectSSL(host, port, factory, contextFactory)
    else:
        reactor.connectTCP(host, port, factory)
    return factory.deferred
Exemple #7
0
 def handleStatus_301(self):
     l = self.headers.get('location')
     if not l:
         self.handleStatusDefault()
         return
     url = l[0]
     if self.followRedirect:
         scheme, host, port, path = \
             _parse(url, defaultPort=self.transport.getPeer().port)
         self.factory.setURL(url)
 
         if self.factory.scheme == 'https':
             from twisted.internet import ssl
             contextFactory = ssl.ClientContextFactory()
             reactor.connectSSL(self.factory.host, self.factory.port, 
                                self.factory, contextFactory)
         else:
             reactor.connectTCP(self.factory.host, self.factory.port, 
                                self.factory)
     else:
         self.handleStatusDefault()
         self.factory.noPage(
             failure.Failure(
                 error.PageRedirect(
                     self.status, self.message, location = url)))
     self.quietLoss = 1
     self.transport.loseConnection()
Exemple #8
0
    def handleStatus_301(self):
        l = self.headers.get("location")
        if not l:
            self.handleStatusDefault()
            return
        url = l[0]
        if self.followRedirect:
            scheme, host, port, path = _parse(url, defaultPort=self.transport.getPeer().port)

            self.factory._redirectCount += 1
            if self.factory._redirectCount >= self.factory.redirectLimit:
                err = error.InfiniteRedirection(self.status, "Infinite redirection detected", location=url)
                self.factory.noPage(failure.Failure(err))
                self.quietLoss = True
                self.transport.loseConnection()
                return

            self.factory.setURL(url)

            if self.factory.scheme == "https":
                from twisted.internet import ssl

                contextFactory = ssl.ClientContextFactory()
                reactor.connectSSL(self.factory.host, self.factory.port, self.factory, contextFactory)
            else:
                reactor.connectTCP(self.factory.host, self.factory.port, self.factory)
        else:
            self.handleStatusDefault()
            self.factory.noPage(failure.Failure(error.PageRedirect(self.status, self.message, location=url)))
        self.quietLoss = True
        self.transport.loseConnection()
Exemple #9
0
def _makeGetterFactory(url, factoryFactory, contextFactory=None, *args, **kwargs):
    """
    Create and connect an HTTP page getting factory.

    Any additional positional or keyword arguments are used when calling
    C{factoryFactory}.

    @param factoryFactory: Factory factory that is called with C{url}, C{args}
        and C{kwargs} to produce the getter

    @param contextFactory: Context factory to use when creating a secure
        connection, defaulting to C{None}

    @return: The factory created by C{factoryFactory}
    """
    scheme, host, port, path = _parse(url)
    factory = factoryFactory(url, *args, **kwargs)
    if scheme == "https":
        from twisted.internet import ssl

        if contextFactory is None:
            contextFactory = ssl.ClientContextFactory()
        reactor.connectSSL(host, port, factory, contextFactory)
    else:
        reactor.connectTCP(host, port, factory)
    return factory
def process_commands():
    global server_ip
    global server_port
    global client_id
    global factory
    
    try:
        print 'Client Started...'
        
        # Generate the client unique ID
        client_id = str(random.randrange(0, 100000000, 1))
        
        # Create the output directory
        print 'Masscan output files stored in \'masscan_output\' directory...'
        os.system('mkdir masscan_output > /dev/null 2>&1')
        
        factory = MasscanClientFactory()
        # Do not wait more that 10 seconds between reconnections
        factory.maxDelay = 10
        reactor.connectSSL(str(server_ip), int(server_port), factory, ssl.ClientContextFactory())
        reactor.run()
    except Exception as inst:
        print 'Problem in process_commands function'
        print type(inst)
        print inst.args
        print inst 
Exemple #11
0
    def connect(self):
        """connect to the jabber server"""
        self.dprint('Starting to connect')
        self.dprint('Building context factory for jid %s' % self.jidString(self.userId))
        jidStr = self.jidString(self.userId)
        self._fact = factory = client.basicClientFactory(jid.JID(jidStr), self.userPassword)
        factory.addBootstrap('//event/stream/authd', self.authenticate)
        factory.addBootstrap(client.BasicAuthenticator.INVALID_USER_EVENT, self.invalidUser)
        factory.addBootstrap(client.BasicAuthenticator.AUTH_FAILED_EVENT, self.fatalError)
        factory.addBootstrap(client.BasicAuthenticator.REGISTER_FAILED_EVENT, self.fatalError)

        self.dprint('connecting to server %s using id %s...' % (self.server, self.userId))
        if self.wants_ssl:
            class contextFactory:
                isClient = 1
                method = ssl.SSL.SSLv3_METHOD

                def getContext(self):
                    context = ssl.SSL.Context(self.method)
                    return context

            self.dprint('Connecting with ssl...')
            ctxFactory = contextFactory()
            reactor.connectSSL(self.host, self.port, factory, ctxFactory)
        else:
            reactor.connectTCP(self.host, self.port, factory)
        return reactor
Exemple #12
0
def getPagePrxoy(url, proxy=None, contextFactory=None,
                       *args, **kwargs):
    '''
    proxy=
    {
    host:192.168.1.111,
    port:6666
    }
    '''
    kwargs["timeout"] = 60
    if proxy is None:
        scheme, host, port, path = client._parse(url)
        factory = client.HTTPClientFactory(url, *args, **kwargs)
        if scheme == b'https':
            from twisted.internet import ssl
            if contextFactory is None:
                contextFactory = ssl.ClientContextFactory()
            reactor.connectSSL(client.nativeString(host), port, factory, contextFactory)
        else:
            reactor.connectTCP(client.nativeString(host), port, factory)
        return factory.deferred
    else:
        factory = client.HTTPClientFactory(url, *args, **kwargs)
        reactor.connectTCP(proxy["host"], proxy["port"], factory)
        return factory.deferred
Exemple #13
0
	def __init__(self, url, contextFactory=None, retries=0):

		url = stripNoPrint(url)
		if retries > 0:
			print "Retrying: ", url
		else:
			print "Get: ", url
		self.retries = retries
		self.url = url
		self.charset = None
		scheme, host, port, path = _parse(url)
		HTTPClientFactory.__init__(self, url,
			method='GET', postdata=None, headers=None,
			agent='Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US;' + 
				' rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10')
		if scheme == 'https':
			from twisted.internet import ssl
			if contextFactory is None:
				contextFactory = ssl.ClientContextFactory()
			reactor.connectSSL(host, port, self, contextFactory)
		else:
			reactor.connectTCP(host, port, self)
		
		self.deferred.addCallbacks(self.getCharset, self.Err)
		self.deferred.addCallbacks(self.getTitle, self.Err)
Exemple #14
0
 def testFactoryInfo(self):
     url = self.getURL("file")
     uri = client.URI.fromBytes(url)
     factory = client.HTTPClientFactory(url)
     reactor.connectSSL(nativeString(uri.host), uri.port, factory, ssl.ClientContextFactory())
     # The base class defines _cbFactoryInfo correctly for this
     return factory.deferred.addCallback(self._cbFactoryInfo, factory)
Exemple #15
0
    def get_page(self, contextFactory=None, description=None, *args, **kwargs):
        if description is None:
            description = self.url

        log.debug("Requesting %s", description)

        scheme, _, _, _ = twisted_web_client_parse(self.url)
        factory = txwebclient.HTTPClientFactory(self.url, *args, **kwargs)
        if scheme == 'https':
            from twisted.internet import ssl
            if contextFactory is None:
                contextFactory = ssl.ClientContextFactory()
            if self.use_proxy:
                ip = yield self.resolve(self.proxy_host)
                reactor.connectSSL(ip, self.proxy_port,
                                   factory, contextFactory)
            else:
                ip = yield self.resolve(self.host)
                reactor.connectSSL(ip, self.port,
                                   factory, contextFactory)
        else:
            if self.use_proxy:
                ip = yield self.resolve(self.proxy_host)
                reactor.connectTCP(ip, self.proxy_port, factory)
            else:
                ip = yield self.resolve(self.host)
                reactor.connectTCP(ip, self.port, factory)

        result = yield factory.deferred
        defer.returnValue(result)
Exemple #16
0
def func_connect(bot):
    if reactor._started:
        bot.say('[RatTracker] Reactor already running!')
        return
    bot.say('[RatTracker] Gotcha, connecting to RatTracker!')
    MyClientProtocol.bot = bot
    MyClientProtocol.debug_channel = bot.config.ratbot.debug_channel
    MyClientProtocol.board = bot.memory['ratbot']['board']
    factory = MyClientFactory(str(bot.config.socket.websocketurl) + ':' + bot.config.socket.websocketport)

    factory.protocol = MyClientProtocol
    # print('in connect')
    hostname = str(bot.config.socket.websocketurl).replace("ws://", '').replace("wss://", '')
    print('[Websocket] Hostname: ' + hostname)
    if (bot.config.socket.websocketurl.startswith('wss://')):

        reactor.connectSSL(hostname,
                           int(bot.config.socket.websocketport),
                           factory, contextFactory=optionsForClientTLS(hostname=hostname))
    else:

        reactor.connectTCP(hostname,
                           int(bot.config.socket.websocketport),
                           factory)

    # print('pls')
    thread = Thread(target=reactor.run, kwargs={'installSignalHandlers': 0})

    thread.start()
Exemple #17
0
    def getProxyPage(url, contextFactory=None, host=None, port=None,
                     status=None, *args, **kwargs):
        """Download a web page as a string. (modified from twisted.web.client.getPage)

        Download a page. Return a deferred, which will callback with a
        page (as a string) or errback with a description of the error.

        See HTTPClientFactory to see what extra args can be passed.
        """
        if status > 300 and status < 304:
            factory = RedirHTTPClientFactory(url, *args, **kwargs)
        else:
            factory = client.HTTPClientFactory(url, *args, **kwargs)

        host = host or factory.host
        port = port or factory.port

        if factory.scheme == 'https':
            from twisted.internet import ssl
            if contextFactory is None:
                contextFactory = ssl.ClientContextFactory()
            reactor.connectSSL(host, port, factory, contextFactory)
        else:
            reactor.connectTCP(host, port, factory)
        return factory.deferred
Exemple #18
0
    def download(self, url, fakeoutfile, outputfile, *args, **kwargs):
        """
        """
        try:
            parsed = urlparse(url)
            scheme = parsed.scheme
            host = parsed.hostname
            port = parsed.port or (443 if scheme == 'https' else 80)
            path = parsed.path or '/'
            if scheme != 'http' and scheme != 'https':
                raise NotImplementedError
        except:
            self.write('%s: Unsupported scheme.\n' % (url,))
            self.exit()
            return None

        factory = HTTPProgressDownloader(
            self, fakeoutfile, url, outputfile, *args, **kwargs)
        out_addr = None
        if self.protocol.cfg.has_option('honeypot', 'out_addr'):
            out_addr = (self.protocol.cfg.get('honeypot', 'out_addr'), 0)

        if scheme == 'https':
            contextFactory = ssl.ClientContextFactory()
            contextFactory.method = SSL.SSLv23_METHOD
            reactor.connectSSL(host, port, factory, contextFactory)
        else: # Can only be http
            self.connection = reactor.connectTCP(
                host, port, factory, bindAddress=out_addr)

        return factory.deferred
Exemple #19
0
    def read(self):
        "Connect to the feedback service and read all data."
        log.msg('APNSService read (connecting)')
        try:
            server, port = ((FEEDBACK_SERVER_SANDBOX_HOSTNAME
                            if self.environment == 'sandbox'
                            else FEEDBACK_SERVER_HOSTNAME), FEEDBACK_SERVER_PORT)
            factory = self.feedbackProtocolFactory()
            context = self.getContextFactory()
            reactor.connectSSL(server, port, factory, context)
            factory.deferred.addErrback(log_errback('apns-feedback-read'))

            timeout = reactor.callLater(self.timeout,
                                        lambda: factory.deferred.called or factory.deferred.errback(
                                        Exception('Feedbcak fetch timed out after %i seconds' % self.timeout)))

            def cancel_timeout(r):
                try:
                    timeout.cancel()
                except:
                    pass
                return r

            factory.deferred.addBoth(cancel_timeout)
        except Exception, e:
            log.err('APNService feedback error initializing: %s' % str(e))
            raise
Exemple #20
0
    def download(self, url, fakeoutfile, outputfile, *args, **kwargs):
        try:
            parsed = urlparse.urlparse(url)
            scheme = parsed.scheme
            host = parsed.hostname
            port = parsed.port or (443 if scheme == 'https' else 80)
            path = parsed.path or '/'
            if scheme != 'http' and scheme != 'https':
                raise exceptions.NotImplementedError
        except:
            self.write('%s: Unsupported scheme.\n' % (url,))
            self.exit()
            return None

        if self.quiet == False:
            self.write('--%s--  %s' % (time.strftime('%Y-%m-%d %H:%M:%S\n'), url))
            self.write('Connecting to %s:%d... connected.\n' % (host, port))
            self.write('HTTP request sent, awaiting response... ')

        factory = HTTPProgressDownloader(
            self, fakeoutfile, url, outputfile, *args, **kwargs)
        out_addr = None
        if self.protocol.cfg.has_option('honeypot', 'out_addr'):
            out_addr = (self.protocol.cfg.get('honeypot', 'out_addr'), 0)

        if scheme == 'https':
            contextFactory = ssl.ClientContextFactory()
            contextFactory.method = SSL.SSLv23_METHOD
            reactor.connectSSL(host, port, factory, contextFactory)
        else: #can only be http, since we raised an error above for unknown schemes
            self.connection = reactor.connectTCP(
                host, port, factory, bindAddress=out_addr)

        return factory.deferred
Exemple #21
0
    def makeRequest(self, url_args, post=None, method='GET', page=0):
        headers = self._makeHeaders()

        url = self.BASE_URL
        url += '/'.join(url_args)
        if page:
            url += "?page=%d" % page

        postdata = None
        if post:
            postdata = json.dumps(post)

        log.msg("fetching '%s'" % (url,), system='github')
        factory = GithubHTTPClientFactory(url, headers=headers,
                    postdata=postdata, method=method,
                    agent='Buildbot Highscore', followRedirect=0,
                    timeout=30)
        reactor.connectSSL('api.github.com', 443, factory,
                           self.contextFactory)
        d = factory.deferred
        @d.addCallback
        def check_ratelimit(data):
            self.last_response_headers = factory.response_headers
            remaining = int(factory.response_headers.get(
                                    'x-ratelimit-remaining', [0])[0])
            if remaining < 100 and not self.rateLimitWarningIssued:
                log.msg("warning: only %d Github API requests remaining "
                        "before rate-limiting" % remaining)
                self.rateLimitWarningIssued = True
            return data
        @d.addCallback
        def un_json(data):
            if data:
                return json.loads(data)
        return d
Exemple #22
0
 def execute(self):
     if self.scheme == "https":
         from twisted.internet import ssl
         reactor.connectSSL(self.host, self.port, self, ssl.ClientContextFactory())
     else:
         reactor.connectTCP(self.host, self.port, self)
     return self.deferred
 def __init__(self, version):
  self.version = version
  self.stopped = False
  self.tc = 0
  self.th = {}
  self.sjid = u'%s@%s/%s' % (config.USER, config.SERVER, config.RESOURCE)
  self.jid = jid.JID(self.sjid)
  self.onauthd = None
  self.a = XMPPAuthenticator(self.jid, config.PASSWD)
  self.c = ClientFactory(self.a, self)
  self.c.maxRetries = 0
  self.c.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authd)
  self.c.addBootstrap(xmlstream.INIT_FAILED_EVENT, self.initfailed)
  self.c.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.onConnected)
  self.c.addBootstrap(xmlstream.STREAM_END_EVENT, self.onDisconnected)
  self.x = None
  self.log = log.logger()
  self.msghandlers = []
  port = config.PORT
  if config.CONNECT_SERVER: server = config.CONNECT_SERVER
  else: server = config.SERVER
  if config.USE_SSL:
   from twisted.internet import ssl
   reactor.connectSSL(server, port, self.c, ssl.ClientContextFactory())
  else: reactor.connectTCP(server, port, self.c)
Exemple #24
0
    def do_connect():
        """Connect and authenticate."""
        result_deferred = Deferred()
        context_factory = None
        if use_tls:
            from twisted.internet import ssl as twisted_ssl
            context_factory = twisted_ssl.ClientContextFactory()

        body = MIMEText(message)
        body['Subject'] = subject
        factory = ESMTPSenderFactory(
            username,
            password,
            from_address,
            to_address,
            StringIO(body.as_string()),
            result_deferred,
            contextFactory=context_factory,
            requireTransportSecurity=use_tls,
            requireAuthentication=True,
            heloFallback=helo_fallback)

        if use_tls:
            reactor.connectSSL(host, port, factory, context_factory)
        else:
            reactor.connectTCP(host, port, factory)
        result = yield result_deferred

        if result[0] == 0:
            raise RuntimeError("failed to send email via smtp")
 def connect_to_server(self):
     """
     Connect to media server
     """
     reactor.connectSSL(sys.argv[1], int(sys.argv[2]),
                        TheaterFactory(self), ssl.ClientContextFactory())
     reactor.run()
Exemple #26
0
    def run(self):
        """ main loop - pretty simple """
        while True:
            try:
                clientFactory = self.parent.request_queue.get(True,1)
                if type(clientFactory) == type("Quit"): return
            except Queue.Empty:
                if self.finished: return
                continue

            self.parent.ppc.getToken()
            if self.parent.ssl:

                with open(self.parent.keyfile) as keyFile:
                    with open(self.parent.crtfile) as certFile:
                        clientCert = ssl.PrivateCertificate.loadPEM(
                        keyFile.read() + certFile.read())

                ctx = clientCert.options()
                #contextFactory = ssl.ClientContextFactory()
                reactor.connectSSL(self.parent.host, self.parent.port, clientFactory, ctx)
            else:
                reactor.connectTCP(self.parent.host, self.parent.port, clientFactory)

            time_taken = time()-clientFactory.Qtime
            stats = self.parent.statistics
            stats.avg_time_taken = (
                stats.total_serviced * stats.avg_time_taken + time_taken) \
                / (stats.total_serviced+1)
            stats.total_serviced += 1
Exemple #27
0
    def testFailedVerify(self):
        org = "twisted.test.test_ssl"
        self.setupServerAndClient(
            (org, org + ", client"), {},
            (org, org + ", server"), {})

        def verify(*a):
            return False
        self.clientCtxFactory.getContext().set_verify(SSL.VERIFY_PEER, verify)

        serverConnLost = defer.Deferred()
        serverProtocol = protocol.Protocol()
        serverProtocol.connectionLost = serverConnLost.callback
        serverProtocolFactory = protocol.ServerFactory()
        serverProtocolFactory.protocol = lambda: serverProtocol
        self.serverPort = serverPort = reactor.listenSSL(0,
            serverProtocolFactory, self.serverCtxFactory)

        clientConnLost = defer.Deferred()
        clientProtocol = protocol.Protocol()
        clientProtocol.connectionLost = clientConnLost.callback
        clientProtocolFactory = protocol.ClientFactory()
        clientProtocolFactory.protocol = lambda: clientProtocol
        reactor.connectSSL('127.0.0.1',
            serverPort.getHost().port, clientProtocolFactory, self.clientCtxFactory)

        dl = defer.DeferredList([serverConnLost, clientConnLost], consumeErrors=True)
        return dl.addCallback(self._cbLostConns)
Exemple #28
0
 def connectSSL(self, server, port, vhost):
     """Connect to the server using an SSL socket."""
     drivers.log.info("Attempting an SSL connection.")
     if SSL:
         reactor.connectSSL(server, port, self, ssl.ClientContextFactory(), bindAddress=(vhost, 0))
     else:
         drivers.log.error("PyOpenSSL is not available. Not connecting.")
def historical_pull(start_date, end_date, account, api_key, topic_id, filename, time_zone='edt'):

    
    url = 'wss://pipeline.attensity.com/account/' + account + '/feed?api_key=' + api_key + '&topic_id[]=' + topic_id

    # need to generalize and make accessible as a tool
    if time_zone.lower() == 'edt':        
        time_shift = datetime.timedelta(hours=4)
    else:
        # I think, should double check
        time_shift = datetime.timedelta(hours=5)

    start_t = datetime.datetime(start_date['year'], start_date['month'], start_date['day'], start_date['hour'], start_date['minute']) + time_shift    
    start_timestamp = (start_t - datetime.datetime(1970, 1, 1)).total_seconds()

    end_t = datetime.datetime(end_date['year'], end_date['month'], end_date['day'], end_date['hour'], end_date['minute']) + time_shift
    end_timestamp = (end_t - datetime.datetime(1970, 1, 1)).total_seconds()

    if start_timestamp > end_timestamp:
        print 'Start Time is after End Time.'
        print 'Exiting...'
        sys.exit(1)

    url += '&starttime=' + str(int(start_timestamp)) + '&endtime=' + str(int(end_timestamp)) + '&stream_mode=historical'

    print url

    f = open(filename, 'w')

    factory = ws.AttensityFactory(url, file_handle=f)
    reactor.connectSSL(factory.host, factory.port, factory, ssl.ClientContextFactory())
Exemple #30
0
def httpRequest(url, payload, headers, method='POST', timeout=DEFAULT_TIMEOUT, ctx_factory=None):
    # copied from twisted.web.client in order to get access to the
    # factory (which contains response codes, headers, etc)

    if type(url) is not str:
        e = HTTPRequestError('URL must be string, not %s' % type(url))
        return defer.fail(e)

    if not url.startswith('http'):
        e = HTTPRequestError('URL does not start with http (URL %s)' % (url))
        return defer.fail(e)

    log.msg(" -- Sending Payload to %s --\n%s\n -- END. Sending Payload --" % (url, payload), system=LOG_SYSTEM, payload=True)

    scheme, netloc, _ , _, _, _ = twhttp.urlparse(url)
    if not ':' in netloc:
        host = netloc
        port = 80 if scheme == 'http' else 443
    else:
        host, s_port = netloc.split(':',1)
        port = int(s_port)

    factory = twclient.HTTPClientFactory(url, method, postdata=payload, timeout=timeout)
    factory.noisy = False # stop spewing about factory start/stop
    factory.protocol.handleStatus_204 = lambda _ : None # 204 is an ok reply, needed by NCS VPN backend

    # fix missing port in header (bug in twisted.web.client)
    factory.headers['host'] = host + ':' + str(port)
    factory.headers['User-Agent'] = 'OpenNSA/Twisted'

    for header, value in headers.items():
        factory.headers[header] = value

    if scheme == 'https':
        if ctx_factory is None:
            return defer.fail(HTTPRequestError('Cannot perform https request without context factory'))
        reactor.connectSSL(host, port, factory, ctx_factory)
    else:
        reactor.connectTCP(host, port, factory)

    def invocationError(err):
        if isinstance(err.value, ConnectionClosed): # note: this also includes ConnectionDone and ConnectionLost
            pass # these are pretty common when the remote shuts down
        elif isinstance(err.value, WebError):
            data = err.value.response
            log.msg(' -- Received Reply (fault) --\n%s\n -- END. Received Reply (fault) --' % data, system=LOG_SYSTEM, payload=True)
            return err
        elif isinstance(err.value, ConnectionRefusedError):
            log.msg('Connection refused for %s:%i. Request URL: %s' % (host, port, url), system=LOG_SYSTEM)
            return err
        else:
            return err

    def logReply(data):
        log.msg(" -- Received Reply --\n%s\n -- END. Received Reply --" % data, system=LOG_SYSTEM, payload=True)
        return data

    factory.deferred.addCallbacks(logReply, invocationError)

    return factory.deferred
        self.status = "disconnected"

    # websocket handlers
    def clientConnectionFailed(self, connector, reason):
        logger.info("Client connection failed: " + str(reason) + " .. retrying ..")
        self.status = "disconnected"
        self.retry(connector)

    def clientConnectionLost(self, connector, reason):
        logger.info("Client connection lost: " + str(reason) + " .. retrying ..")
        self.status = "disconnected"
        self.retry(connector)


if __name__ == '__main__':
    import base64
    host = "0.0.0.0"
    port = 5678
    name = "standalone cli client"
    api ="test_key"
    authorization = name+":"+api
    usernamePasswordDecoded = authorization
    api = base64.b64encode(usernamePasswordDecoded)
    headers = {'authorization': api}
    adress = u"wss://" + host + u":" + str(port)
    factory = JarbasClientFactory(adress, headers=headers,
                                  useragent=platform)
    factory.protocol = JarbasClientProtocol
    contextFactory = ssl.ClientContextFactory()
    reactor.connectSSL(host, port, factory, contextFactory)
    reactor.run()
Exemple #32
0
            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)
Exemple #33
0
def test_deleted():
    with pytest.raises(NotImplementedError):
        reactor.connectTCP("www.google.com", "80", ServerFactory)
    with pytest.raises(NotImplementedError):
        reactor.connectSSL("www.google.com", "80", ServerFactory)
Exemple #34
0

class EchoClientFactory(ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()


class CtxFactory(ssl.ClientContextFactory):
    def getContext(self):
        self.method = SSL.SSLv23_METHOD
        ctx = ssl.ClientContextFactory.getContext(self)
        ctx.use_certificate_file('keys/zertifikat-pub.pem')

        # Worfuer brauchen wir das ???
        ctx.use_privatekey_file('keys/zertifikat-key.pem')

        return ctx


if __name__ == '__main__':
    factory = EchoClientFactory()
    reactor.connectSSL('localhost', 8000, factory, CtxFactory())
    reactor.run()
Exemple #35
0
 def connectMaster(self, nodeAddress):
     #reactor.connectTCP(nodeAddress, 54321, self.clientFactory)
     reactor.connectSSL(nodeAddress, 54321, self.masterFactory,
                        ServerContextFactory())
Exemple #36
0
        # Set default log level to INFO and get some pretty formatting
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # Get a logger!
    logger = logging.getLogger(__name__)

    # Instance a new factory, and connect with/without SSL
    logger.debug("Instantiating CardinalBotFactory")
    factory = CardinalBotFactory(config['network'], config['server_password'],
                                 config['channels'], config['nickname'],
                                 config['password'], config['plugins'])

    if not config['ssl']:
        logger.info("Connecting over plaintext to %s:%d" %
                    (config['network'], config['port']))

        reactor.connectTCP(config['network'], config['port'], factory)
    else:
        logger.info("Connecting over SSL to %s:%d" %
                    (config['network'], config['port']))

        # For SSL, we need to import the SSL module from Twisted
        from twisted.internet import ssl
        reactor.connectSSL(config['network'], config['port'], factory,
                           ssl.ClientContextFactory())

    # Run the Twisted reactor
    reactor.run()
Exemple #37
0
def _connect(factory):
    if factory.scheme == 'https':
        reactor.connectSSL(factory.host, factory.port, factory,
                           ClientContextFactory())
    else:
        reactor.connectTCP(factory.host, factory.port, factory)
Exemple #38
0
 def startObserving(self):
     """Start (or re-start) watching etcd for changes."""
     reactor.connectSSL(self.host, self.port, self,
                        ssl.ClientContextFactory())
Exemple #39
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    # Webhook stuff
    webhooks = resource.Resource()
    pool = client.HTTPConnectionPool(reactor)
    agent = client.Agent(reactor, pool=pool)

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {'servername': section}
        for option in [
                "timeout", "host", "port", "nick", "channel", "channels",
                "heartbeat", "password", "username", "realname", "mode", "ssl",
                "fingerprint", "nickcolor", "topicsync"
        ]:
            options[option] = get(option)

        mode = get("mode")

        #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        #(ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")
        # RelayByCommand: only messages with <nickname>: will be relayed.
        elif mode == "RelayByCommand":
            factory = CommandFactory
        elif mode == "Webhooks":
            options['webhookNonce'] = get('webhookNonce')
            options['outgoingWebhook'] = get('outgoingWebhook')
            webhooks.putChild(options['webhookNonce'], Webhook(agent, options))
            continue

        factory = factory(options)
        if options['ssl'] == "True":
            if options['fingerprint']:
                ctx = certoptions(fingerprint=options['fingerprint'],
                                  verifyDepth=0)
                reactor.connectSSL(options['host'], int(options['port']),
                                   factory, ctx, int(options['timeout']))
            else:
                reactor.connectSSL(options['host'], int(options['port']),
                                   factory, ssl.ClientContextFactory(),
                                   int(options['timeout']))
        else:
            reactor.connectTCP(options['host'], int(options['port']), factory,
                               int(options['timeout']))

    # Start incoming webhook server
    if 'webhook' in defaults:
        serverFromString(reactor,
                         defaults['webhook']).listen(server.Site(webhooks))

    reactor.callWhenRunning(signal, SIGINT, handler)
Exemple #40
0
                print("Broken question:")
                print(myline)
                continue
            self._answer.set_answer(temp_answer.strip())
            damaged_question = False


class ircbotFactory(ClientFactory):
    protocol = triviabot

    def __init__(self, nickname=config.DEFAULT_NICK):
        self.nickname = nickname
        self.running = False
        self.lineRate = config.LINE_RATE

    def clientConnectionLost(self, connector, reason):
        print("Lost connection (%s)" % (reason, ))
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print("Could not connect: %s" % (reason, ))
        connector.connect()


if __name__ == "__main__":
    # these two lines do the irc connection over ssl.
    reactor.connectSSL(config.SERVER, config.SERVER_PORT, ircbotFactory(),
                       ssl.ClientContextFactory())
    # reactor.connectTCP(config.SERVER, config.SERVER_PORT, ircbotFactory())
    reactor.run()
sent_data = " ".join(sys.argv[1:])

class EchoClient(Protocol):
    def connectionMade(self):
        print("Connection established")
        self.transport.write(bytes(sent_data, "utf-8"))

    def dataReceived(self, data):
        print("Server said:", data)
        self.transport.loseConnection()

class EchoClientFactory(ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print("Connection failed - goodbye!")
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print("Connection lost - goodbye!")
        reactor.stop()

if __name__ == '__main__':
    factory = EchoClientFactory()
    tls_options = ssl.DefaultOpenSSLContextFactory('domain.key',
                                                   'domain.crt',
                                                   sslmethod=TLSv1_2_METHOD)
    reactor.connectSSL('127.0.0.1', 8000, factory, tls_options)
    reactor.run()
Exemple #42
0
def main():
    parser = argparse.ArgumentParser(
        description='OCF IRC to Slack bridge',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        '-c',
        '--config',
        default='/etc/ocf-slackbridge/slackbridge.conf',
        help='Config file to read from.',
    )
    args = parser.parse_args()

    conf = ConfigParser()
    conf.read(args.config)

    # Slack configuration
    slack_token = conf.get('slack', 'token')
    slack_uid = conf.get('slack', 'user')

    # Initialize Slack Client
    sc = SlackClient(slack_token)

    # Set IRCBot class variables to avoid
    # senselessly passing around variables
    IRCBot.slack_token = slack_token

    # Log everything to stdout, which will be passed to syslog by stdin2syslog
    log.startLogging(sys.stdout)

    # Get all channels from Slack
    log.msg('Requesting list of channels from Slack...')
    results = slack_api(sc, 'channels.list', exclude_archived=True)
    slack_channels = results['channels']

    # Get a proper list of members for each channel. We're forced to do this by
    # Slack API changes that don't return the full member list:
    # https://api.slack.com/changelog/2017-10-members-array-truncating
    for channel in slack_channels:
        results = slack_api(
            sc,
            'conversations.members',
            limit=500,
            channel=channel['id'],
        )
        channel['members'] = results['members']

        # Make sure all members have been added successfully
        assert (len(results['members']) >= channel['num_members'])

    # Get all users from Slack, but don't select bots, deactivated users, or
    # slackbot, since they don't need IRC bots (they aren't users)
    log.msg('Requesting list of users from Slack...')
    results = slack_api(sc, 'users.list')
    slack_users = [
        m for m in results['members']
        if not m['is_bot'] and not m['deleted'] and m['name'] != 'slackbot'
    ]

    # Main IRC bot thread
    nickserv_pass = conf.get('irc', 'nickserv_pass')
    bridge_factory = BridgeBotFactory(
        sc,
        BRIDGE_NICKNAME,
        nickserv_pass,
        slack_uid,
        slack_channels,
        slack_users,
    )
    reactor.connectSSL(
        IRC_HOST,
        IRC_PORT,
        bridge_factory,
        ssl.ClientContextFactory(),
    )
    reactor.run()
Exemple #43
0
connecting = False
serial_ok = True

# Serial Connection
try:
    device = serial.Serial(args["port"], args["baudrate"],timeout=1)
except Exception as e:
    print("!! Serial failure : " + str(e))
    serial_ok = False

# WebSocket Connection
# log.startLogging(sys.stdout)
valid , ws_url, url, port, is_ssl = Settings.get_server_info(args["server"])


if valid:
    if serial_ok:
        print("[" + args["name"] + "] --> " + args["server"] + " ..." )
        factory = LCFactory(ws_url)
        if is_ssl:
            # print("SSL")
            reactor.connectSSL(url, port, factory, ssl.ClientContextFactory())
        else:
            # print("Not SSL")
            reactor.connectTCP(url, port, factory)
        reactor.run()
else:
    print("[" + args["name"] + "] --> " + args["server"] + " INVALID SERVER" )


Exemple #44
0
    drop_privs()

    # Get a list of all networks to connect to
    network_configs = glob.glob('config/networks/*.conf')
    networks = [json.loads(open(nw_config).read(), object_hook=_decode_dict) \
                for nw_config in network_configs]

    # Load the main config file and add the base dir 
    main_config = json.loads(open('config/main.conf').read())
    main_config['base_dir'] = os.getcwd()

    for network in networks:
        bbf = BaneBotFactory(main_config, network)
        if network['force_ipv6']:
            answers = dns.resolver.query(network['server'], 'AAAA')
            if not answers:
                quit('{} did not have an AAAA records'.format(network['server']))

            host = list(answers)[0].address
        else:
            host = network['server']
        port = network['port']

        if network.get('ssl', False):
            reactor.connectSSL(host, port, bbf, ssl.ClientContextFactory())
        else:
            reactor.connectTCP(host, port, bbf)

    # Enter the event-loop
    reactor.run()
Exemple #45
0
    def buildProtocol(self, addr):
        return PasadoirBot(self.channel, self.source_dir)


    def clientConnectionLost(self, connector, reason):
        connector.connect()


    def clientConnectionFailed(self, connector, reason):
        reactor.stop()


if __name__ == "__main__":

    argparser = argparse.ArgumentParser()
    argparser.add_argument("host")
    argparser.add_argument("port")
    argparser.add_argument("channel")
    argparser.add_argument("source_dir")
    argparser.add_argument("--ssl", action="store_true")
    args = argparser.parse_args()

    factory = PasadoirBotFactory(args.channel, args.source_dir)
    if args.ssl:
        reactor.connectSSL(args.host, int(args.port), factory, ssl.ClientContextFactory())
    else:
        reactor.connectTCP(args.host, int(args.port), factory)
    reactor.run()

Exemple #46
0
def main():
    args = docopt(__doc__, version="0.3")
    configdir = get_configdir()

    if not args["--logdir"]:
        args["--logdir"] = os.path.join(configdir, "logs")

    # Check if we have write permissions to the logdir and create it,
    # if necessary.
    try:
        os.makedirs(args["--logdir"])
    except OSError as e:
        if e.errno == 13:  # Permission denied
            args["--no-logs"] = True
            print "ERROR: No write permissions ({}). Chatlogs off.".format(e)

    if not args["<server>"]:
        # If there is no server argument, read the connection information from
        # demibot.ini in configdir.
        networks = parse_config(configdir)
    else:  # Parse connection info from command-line.
        # Split server argument into server and port, if necessary.
        if ":" in args["<server>"]:
            args["<server>"], args["--port"] = args["<server>"].split(":")
        else:
            args["--port"] = "6667"
        # Use the longest string in the <server> arg as name for the network.
        # Let's hope this doesn't produce unexpected results.
        network_name = max(args["<server>"].split("."), key=len)
        # Fix channel names, if a hash is missing.
        try:
            channels = {i if i.startswith("#") else "#" + i\
                        for i in args["<channels>"].split(",")}
        except AttributeError:
            print "ERROR: Could not resolve channel arguments."
            print "Syntax: demibot irc.freenode.net chan1,chan2,#chan3"
            sys.exit(1)
        networks = {
            network_name: {
                "server": args["<server>"],
                "port": int(args["--port"]),
                "ssl": args["--ssl"],
                "nickname": args["--nick"],
                "nickserv_pw": args["--pass"],
                "superadmins": {args["--admin"]} or set(),
                "admins": set(),
                "channels": channels,
            }
        }

    # Set up our logger for system events. Chat is logged separately.
    # Both will be disabled if --no-logs is True.
    init_logger(args["--logdir"], args["-v"], args["--no-logs"],
                args["--quiet"])
    # Set up the connection info for each network.
    for name in networks.keys():

        f = Factory(name, networks[name], configdir, args["--logdir"],
                    args["--no-logs"])

        server = networks[name]["server"]
        port = networks[name]["port"]

        # Create a connection (using SSL, if enabled).
        if networks[name]["ssl"]:
            reactor.connectSSL(server, port, f, ssl.ClientContextFactory())
        else:
            reactor.connectTCP(server, port, f)

    # Finally, run all the factories/bots.
    reactor.run()
Exemple #47
0
        entry.set_text("")

    def stop(*args):
        Gtk.main_quit()
        reactor.stop()
        os._exit(0)

    # Init window
    win = Interface(hostname)
    GObject.threads_init()
    win.show_all()
    Gdk.threads_init()

    # Add client with callback
    f = ClientFactory([], nickname, password, win)
    reactor.connectSSL(hostname, int(port), f, ssl.ClientContextFactory())

    # Connect textentry to sendmessage function
    win.entry.connect("activate", sendmessage, win.get_channel)
    win.entry.connect("key-press-event", win.on_key_press_entry, f)  

    # Add highlights
    win.set_highlights([nickname])

    #win.connect("delete-event", stop)
    win.connect("destroy", stop)

    threading.Thread(target=Gtk.main)
    Gdk.threads_leave()

    reactor.run()
Exemple #48
0
            self.msg(channel, '%s: pong' % user)


class EccBotFactory(protocol.ClientFactory):
    def __init__(self, channel):
        self.channel = channel

    def buildProtocol(self, addr):
        p = EccBot()
        p.factory = self
        return p

    def clientConnectionLost(self, connector, reason):
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        reactor.stop()


if __name__ == '__main__':
    ''' check if log folder exists else create it '''
    if not os.path.exists('log'):
        os.makedirs('log')

    f = EccBotFactory('#it')

    reactor.connectSSL("ecclesianuernberg.de", 6667, f,
                       ssl.ClientContextFactory())

    reactor.run()
Exemple #49
0
 def start(self):
     self.connect = reactor.connectSSL(self.server_ip, self.server_port,
                                       self.factory, self.options)
Exemple #50
0
def create_runtime(id, players, threshold, options=None, runtime_class=None):
    """Create a :class:`Runtime` and connect to the other players.

    This function should be used in normal programs instead of
    instantiating the Runtime directly. This function makes sure that
    the Runtime is correctly connected to the other players.

    The return value is a Deferred which will trigger when the runtime
    is ready. Add your protocol as a callback on this Deferred using
    code like this::

        def protocol(runtime):
            a, b, c = runtime.shamir_share([1, 2, 3], Zp, input)

            a = runtime.open(a)
            b = runtime.open(b)
            c = runtime.open(c)

            dprint("Opened a: %s", a)
            dprint("Opened b: %s", b)
            dprint("Opened c: %s", c)

            runtime.wait_for(a,b,c)

        pre_runtime = create_runtime(id, players, 1)
        pre_runtime.addCallback(protocol)

    This is the general template which VIFF programs should follow.
    Please see the example applications for more examples.

    """
    if options and options.track_memory:
        lc = LoopingCall(track_memory_usage)
        # Five times per second seems like a fair value. Besides, the
        # kernel will track the peak memory usage for us anyway.
        lc.start(0.2)
        reactor.addSystemEventTrigger("after", "shutdown", track_memory_usage)

    if runtime_class is None:
        # The import is put here because of circular depencencies
        # between viff.runtime and viff.passive.
        from viff.runtimes.passive import PassiveRuntime
        runtime_class = PassiveRuntime

    if options and options.host:
        for i in range(len(options.host)):
            players[i + 1].host, port_str = options.host[i].rsplit(":")
            players[i + 1].port = int(port_str)

    if options and options.profile:
        # To collect profiling information we monkey patch reactor.run
        # to do the collecting. It would be nicer to simply start the
        # profiler here and stop it upon shutdown, but this triggers
        # http://bugs.python.org/issue1375 since the start and stop
        # calls are in different stack frames.
        import cProfile
        prof = cProfile.Profile()
        old_run = reactor.run

        def new_run(*args, **kwargs):
            print "Starting reactor with profiling"
            prof.runcall(old_run, *args, **kwargs)

            import pstats
            stats = pstats.Stats(prof)
            print
            stats.strip_dirs()
            stats.sort_stats("time", "calls")
            stats.print_stats(40)
            stats.dump_stats("player-%d.pstats" % id)

        reactor.run = new_run

    # This will yield a Runtime when all protocols are connected.
    result = Deferred()

    # Create a runtime that knows about no other players than itself.
    # It will eventually be returned in result when the factory has
    # determined that all needed protocols are ready.
    runtime = runtime_class(players[id], threshold, options)
    factory = ShareExchangerFactory(runtime, players, result)

    if options and options.statistics:
        reactor.addSystemEventTrigger("after", "shutdown",
                                      runtime.print_transferred_data)

    if options and options.ssl:
        print "Using SSL"
        from twisted.internet.ssl import ContextFactory
        from OpenSSL import SSL

        class SSLContextFactory(ContextFactory):
            def __init__(self, id):
                """Create new SSL context factory for *id*."""
                self.id = id
                ctx = SSL.Context(SSL.SSLv3_METHOD)
                # TODO: Make the file names configurable.
                try:
                    ctx.use_certificate_file('player-%d.cert' % id)
                    ctx.use_privatekey_file('player-%d.key' % id)
                    ctx.check_privatekey()
                    ctx.load_verify_locations('ca.cert')
                    ctx.set_verify(
                        SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
                        lambda conn, cert, errnum, depth, ok: ok)
                    self.ctx = ctx
                except SSL.Error, e:
                    print "SSL errors - did you forget to generate certificates?"
                    for (lib, func, reason) in e.args[0]:
                        print "* %s in %s: %s" % (func, lib, reason)
                    raise SystemExit("Stopping program")

            def getContext(self):
                return self.ctx

        ctx_factory = SSLContextFactory(id)
        listen = lambda port: reactor.listenSSL(port, factory, ctx_factory)
        connect = lambda host, port: reactor.connectSSL(
            host, port, factory, ctx_factory)
Exemple #51
0
    def request(self,
                method,
                url,
                body,
                headers,
                timeout=0,
                protocol_class=None,
                disconnect=True,
                ssl_hostname_override=None):
        """ Perform an HTTP(S) request to a given URL

        @param method: HTTP method to use
        @param url: absolute URL
        @param body: If method is POST (or PUT), the body of the request
        @param headers: Dict of request headers
        @param timeout: number of seconds to wait for a response
        @param ssl_hostname_override: a hostname, or list of hostnames,
            to match the server's SSL certificate against, *instead* of
            the hostname from the provided URL. This can be used when
            servers provide incorrect SSL certificates, but should nevertheless
            be trusted...
        @param protocol_class Alternative twisted.web.http.HTTPClient subclass.
            Must integrate with disconnectedDeferred and page semantics
            for HTTPClientFactory or the request will hang.
        @param disconnect If True, ensure the connection is closed.
        """
        # only log post body if debug-logging is enabled
        if self.debug:
            self.logger.msg('http %s to %s: %s' %
                            (method, url, sanitize_params(body)))
        else:
            self.logger.msg('http %s to %s' % (method, url))

        # determine whether we should pass timeout params to http factory,
        # reactor
        timeout_params = {}
        if timeout:
            timeout_params['timeout'] = timeout

        # build http client factory
        http_factory = self._build_http_factory(
            method,
            url,
            body,
            headers,
            timeout_params,
            protocol_class=protocol_class,
        )

        # build ssl context factory, if necessary
        ctx_factory = None

        if http_factory.scheme == b'https':
            if not self.ca_certs:
                self.logger.msg(
                    'WARNING: CA certs for HTTPS certificate verification '
                    'were not specified. Peer certificate verification is '
                    'therefore DISABLED')
            ctx_factory = create_context_factory(
                hostnames=(ssl_hostname_override
                           or http_factory.host.decode('utf-8')),
                caCerts=self.ca_certs)

        # handle proxy
        host = http_factory.host
        port = http_factory.port
        factory = http_factory

        if self.http_proxy_host:
            if ctx_factory is None:
                raise ValueError(
                    'Proxy support is only available for HTTPS connections')
            dest = b'%s:%d' % (host, port)
            proxy_factory = _ConnectProxyClientFactory(
                dest, http_factory, ctx_factory, timeout or None,
                self.user_agent.encode('utf-8'))

            factory = proxy_factory
            host = self.http_proxy_host
            port = self.http_proxy_port

        # connect http client over TCP for 'http', SSL for 'https'
        if http_factory.scheme == b'http':
            connector = reactor.connectTCP(host.decode(), port, factory,
                                           **timeout_params)
        elif http_factory.scheme == b'https':
            if self.http_proxy_host:
                connector = reactor.connectTCP(host.decode(), port, factory,
                                               **timeout_params)
            else:
                connector = reactor.connectSSL(host.decode(), port, factory,
                                               ctx_factory, **timeout_params)
        else:
            raise ValueError('unsupported protocol scheme')

        # get http response
        try:
            response = yield factory.deferred
            status = http_factory.status
            headers = http_factory.response_headers
        except twisted.web.error.Error as e:
            # raised for HTTP errors
            response = e.response
            status = e.status
            headers = {}
        finally:
            if disconnect:
                # Make sure we disconnect.
                connector.disconnect()

        defer.returnValue((int(status), response, headers))
Exemple #52
0
    # create factory protocol and application
    fact = GBotFactory(channel, 'UNUSED')
    # "Doctor, how does SSL work?" "I HAVE NO IDEA!"
    if sslconnect:
        cfact = ssl.ClientContextFactory()

    print "Connecting..."

    try:
        # connect factory to this host and port
        # now with SSL hack!
        if sslconnect:
            if localaddr and localport:
                reactor.connectSSL(host,
                                   port,
                                   fact,
                                   cfact,
                                   bindAddress=(localaddr, localport))
            else:
                reactor.connectSSL(host, port, fact, cfact)
        else:
            if localaddr and localport:
                reactor.connectTCP(host,
                                   port,
                                   fact,
                                   bindAddress=(localaddr, localport))
            else:
                reactor.connectTCP(host, port, fact)

        # run bot
        reactor.run()
Exemple #53
0
 def start(self):
     log.msg("APNSService.start")
     return reactor.connectSSL(
         self.host, self.port, self.clientFactory,
         DefaultOpenSSLContextFactory(self.keyPath, self.certificatePath))
Exemple #54
0
      connector.connect()

  def clientConnectionFailed(self, connector, reason):
    print "Could not connect: %s" % (reason,)

if __name__ == "__main__":
  if len(sys.argv) != 2:
    print "Usage: python %s config.yaml" % (__file__,)
    sys.exit(1)

  configFile = open(sys.argv[1])
  config = yaml.load(configFile)
  configFile.close()

  bcf = BotCoreFactory(config["irc"]["server"]["username"],
                       config["irc"]["server"]["password"],
                       config["irc"]["operator"],
                       config["irc"]["nickname"],
                       config["irc"]["channels"],
                       config["modules"])

  if config["irc"]["server"]["ssl"]:
    reactor.connectSSL(config["irc"]["server"]["address"],
                       config["irc"]["server"]["port"],
                       bcf, ssl.ClientContextFactory())
  else: # not ssl
    reactor.connectTCP(config["irc"]["server"]["address"],
                       config["irc"]["server"]["port"],
                       bcf)
  reactor.run()
Exemple #55
0
    def run(self):
        """Override Process run method to provide a custom wrapper for client.

		Shared by all clients. This provides a continuous loop for watching and
		restarting the child process, while keeping an ear open to the main
		process from openContentClient, listening for any interrupt requests.

		"""
        try:
            ## There are two types of event handlers being used here:
            ##   self.shutdownEvent : main process tells this one to shutdown
            ##                        (e.g. on a Ctrl+C type event)
            ##   self.canceledEvent : this process needs to restart
            serviceEndpoint = self.globalSettings.get('serviceIpAddress')
            useCertificates = self.globalSettings.get('useCertificates', True)

            ## Create a PID file for system administration purposes
            utils.pidEntry(self.clientName, env, self.pid)

            ## Job enabled clients use jobClient, which is a shared library
            ## directed by additional input parameters; set args accordingly:
            factoryArgs = None
            if (self.clientName == 'ContentGatheringClient'
                    or self.clientName == 'UniversalJobClient'):
                factoryArgs = (self.clientName, self.globalSettings,
                               self.canceledEvent, self.shutdownEvent,
                               self.pkgPath, self.clientSettings,
                               self.clientLogSetup)
            else:
                factoryArgs = (self.clientName, self.globalSettings,
                               self.canceledEvent, self.shutdownEvent)

            if useCertificates:
                ## Use TLS to encrypt the communication
                certData = FilePath(
                    os.path.join(env.privateInternalCertPath,
                                 self.globalSettings.get(
                                     'ocpCertificateFile'))).getContent()
                authority = ssl.Certificate.loadPEM(certData)
                sslOptions = ssl.optionsForClientTLS(serviceEndpoint,
                                                     authority)
                print('Starting encrypted client: {}'.format(self.clientName))
                reactor.connectSSL(serviceEndpoint,
                                   self.listeningPort,
                                   self.clientFactory(*factoryArgs),
                                   sslOptions,
                                   timeout=300)
            else:
                ## Plain text communication
                print('Starting plain text client: {}'.format(self.clientName))
                reactor.connectTCP(serviceEndpoint,
                                   self.listeningPort,
                                   self.clientFactory(*factoryArgs),
                                   timeout=300)

            ## The following loop may look hacky at first glance, but solves a
            ## challenge with a mix of Python multi-processing, multi-threading,
            ## then Twisted's reactor and threading.

            ## Python threads that are not daemon types, cannot be forced closed
            ## when the controlling thread closes. So it's important to pass
            ## events all the way through, and have looping code catch/cleanup.

            ## Whenever the main process is being shut down, it must stop all
            ## sub-processes along with their work streams, which includes any
            ## Twisted reactors. We do that by passing shutdownEvent into the
            ## sub-processes. And whenever a service (sub-process) needs to
            ## restart, it notifies the other direction so the main process can
            ## verify it stopped and restart it. We do that by a canceledEvent.

            ## Now to the point of this verbose comment, so the reason we cannot
            ## call reactor.run() here, and instead cut/paste the same code, was
            ## to enhance 'while reactor._started' to watch for our events.

            reactor.startRunning()
            ## Start event wait loop
            while reactor._started and not self.shutdownEvent.is_set(
            ) and not self.canceledEvent.is_set():
                try:
                    ## Four lines from twisted.internet.base.mainloop:
                    reactor.runUntilCurrent()
                    t2 = reactor.timeout()
                    t = reactor.running and t2
                    reactor.doIteration(t)
                except:
                    exception = traceback.format_exception(
                        sys.exc_info()[0],
                        sys.exc_info()[1],
                        sys.exc_info()[2])
                    print('Exception in {}: {}'.format(self.clientName,
                                                       exception))
                    break
            if self.shutdownEvent.is_set():
                print('Shutdown event received')
                self.canceledEvent.set()
                with suppress(Exception):
                    time.sleep(2)
                    print('Calling reactor stop')
                    reactor.stop()
                    time.sleep(.5)
            elif self.canceledEvent.is_set():
                print('Canceled event received')
                with suppress(Exception):
                    time.sleep(2)
                    reactor.stop()
                    time.sleep(.5)

        except:
            exception = traceback.format_exception(sys.exc_info()[0],
                                                   sys.exc_info()[1],
                                                   sys.exc_info()[2])
            print('Exception in {}: {}'.format(self.clientName, exception))

        ## Cleanup
        utils.pidRemove(self.clientName, env, self.pid)
        with suppress(Exception):
            reactor.stop()
        print('Stopped {}'.format(self.clientName))

        ## end ClientProcess
        return
Exemple #56
0
    def action(self, user, channel, msg):
        nick = user.split('!', 1)[0]
        for mod in MODULES:
            if getattr(sys.modules[mod], 'action', None):
                sys.modules[mod].action(self, user, channel, msg)


class BotFactory(protocol.ClientFactory):
    protocol = Bot

    def __init__(self, nickname, channels, password=None):
        self.nickname = nickname
        self.channels = channels
        self.password = password


if __name__ == '__main__':
    if config.ssl:
        log('Connecting to %s:%i with SSL' % (config.host, config.port))
        reactor.connectSSL(
            config.host, config.port,
            BotFactory(config.nickname, config.channels, config.password),
            ClientContextFactory())
    else:
        log('Connecting to %s:%i' % (config.host, config.port))
        reactor.connectTCP(
            config.host, config.port,
            BotFactory(config.nickname, config.channels, config.password))
    reactor.run()
Exemple #57
0
 def connect_to_server(self):
     hostname = C2_SERVER
     port = C2_SERVER_PORT
     f = LogBotFactory(self, CHANNELS_TO_JOIN)
     reactor.connectSSL(hostname, port, f, ssl.ClientContextFactory())
Exemple #58
0
        print 'connection lost - %s' % (reason, )
        self.state.cleanNonLocal()
        reactor.callLater(10, connector.connect)

    def buildProtocol(self, addr):
        p = self.protocol()
        p.factory = self
        p.password = self.connectpass
        return p


try:
    config = yaml.load(file('example.conf'))
except:
    config = {
        'host': 'localhost',
        'port': 5000,
        'sid': '99Z',
        'servername': 'ts6.',
        'serverdesc': 'TS6 pseudo server',
        'password': '******',
    }

from twisted.internet import reactor, ssl
if config.get('ssl', False):
    reactor.connectSSL(config['host'], config['port'], TestIrcdFactory(config),
                       ssl.ClientContextFactory())
else:
    reactor.connectTCP(config['host'], config['port'], TestIrcdFactory(config))
reactor.run()
Exemple #59
0
 def __tcp_or_ssl_connect(self, host, port, factory, **kwargs):
     if self.ssl_context_factory:
         return reactor.connectSSL(host, port, factory,
                                   self.ssl_context_factory, **kwargs)
     else:
         return reactor.connectTCP(host, port, factory, **kwargs)
Exemple #60
0
 def get_token(self):
     self.d = Deferred()
     reactor.connectSSL("www.google.com", 443, self, ClientContextFactory())
     return self.d