Exemplo n.º 1
0
 def getFeedback(self, deferred):
     logger.info("Connecting to Feedback Server, App: %s:%s" %
                 (self.app_mode, self.app_id))
     cc = ClientCreator(self.reactor, FeedbackProtocol, deferred)
     # SRI: not sure what the client_context_factory is for.. is it ok to reuse like this?
     cc.connectSSL(self.feedback_host, self.feedback_port,
                   self.client_context_factory)
Exemplo n.º 2
0
def run():
    # Create the client
    FTPClient.debug = 1
    creator = ClientCreator(reactor, FTPClient, "admin", '1', passive=1)
    creator.connectSSL("localhost", 2121,
            ssl.ClientContextFactory()).addCallback(connectionMade).addErrback(connectionFailed)
    reactor.run(installSignalHandlers=0)
Exemplo n.º 3
0
    def dataReceived(self, data):
        # NB: this only receives whole frames;  so we will just decode
        #     data as-is.
        # NB: its cometsession.py:TCPConnectionResource that makes sure
        #     we receive whole frames here.
        self.logger.debug('dataReceived: data=%r' % data)
        self.logger.debug('self.outgoingConn is', self.outgoingConn)

        if self.outgoingConn:
            # NB: outgoingConn is-a ProxyOutgoingProtocol
            self.logger.debug("write (out): %r" % data)
            return self.outgoingConn.transport.write(data)
        if self.state == "handshake":
            try:
                data = data.strip()
                urlparse.uses_netloc.append("ssl")
                url = urlparse.urlparse(data)
                if not url.netloc: 
                    ssl = False
                else:
                    ssl = url.scheme == "ssl"
                host, port = url.netloc.split(':')
                port = int(port)
                self.completedHandshake = True
            except:
                self.logger.error("failed to connect on handshake", tb=True)
                self.transport.write("0" + str(ERRORS['InvalidHandshake']))
                self.transport.loseConnection()
                return
            peer = self.transport.getPeer()
            self.fromHost = peer.host
            self.fromPort = peer.port
            self.toHost = host
            self.toPort = port
            allowed = False
            for source in config.map['[access]'].get((host, port), []):
                if source == self.transport.hostHeader or source == '*':
                    allowed = True
                    break
            if not allowed:
                self.logger.warn('Unauthorized connect from %r:%d to %r:%d' % (self.fromHost, self.fromPort, self.toHost, self.toPort))
                self.transport.write("0" + str(ERRORS['Unauthorized']))
                self.transport.loseConnection()
                return
            self.logger.access('new connection from %s:%s to %s:%d' % (self.fromHost, self.fromPort, self.toHost, self.toPort))
            self.state = 'connecting'
            client = ClientCreator(reactor, ProxyOutgoingProtocol, self)
            
            if ssl:
                from twisted.internet import ssl
                client.connectSSL(host, port, ssl.ClientContextFactory()).addErrback(self.errorConnection) 
            else:
                client.connectTCP(host, port).addErrback(self.errorConnection) 
                # TODO: connect timeout or onConnectFailed handling...
        else:
            self.transport.write("0" + str(ERRORS['InvalidHandshake']))            
            self.state = 'closed'
            self.transport.loseConnection()
Exemplo n.º 4
0
    def _connect(self, scheme, host, port):
        """
        Connect to the given host and port, using a transport selected based on
        scheme.

        @param scheme: A string like C{'http'} or C{'https'} (the only two
            supported values) to use to determine how to establish the
            connection.

        @param host: A C{str} giving the hostname which will be connected to in
            order to issue a request.

        @param port: An C{int} giving the port number the connection will be on.

        @return: A L{Deferred} which fires with a connected instance of
            C{self._protocol}.
        """
        cc = ClientCreator(self._reactor, self._protocol)
        if scheme == 'http':
            d = cc.connectTCP(host, port)
        elif scheme == 'https':
            d = cc.connectSSL(host, port, self._wrapContextFactory(host, port))
        else:
            d = defer.fail(
                SchemeNotSupported("Unsupported scheme: %r" % (scheme, )))
        return d
Exemplo n.º 5
0
    def _connect(self, scheme, host, port):
        """
        Connect to the given host and port, using a transport selected based on
        scheme.

        @param scheme: A string like C{'http'} or C{'https'} (the only two
            supported values) to use to determine how to establish the
            connection.

        @param host: A C{str} giving the hostname which will be connected to in
            order to issue a request.

        @param port: An C{int} giving the port number the connection will be on.

        @return: A L{Deferred} which fires with a connected instance of
            C{self._protocol}.
        """
        cc = ClientCreator(self._reactor, self._protocol)
        if scheme == "http":
            d = cc.connectTCP(host, port)
        elif scheme == "https":
            d = cc.connectSSL(host, port, self._wrapContextFactory(host, port))
        else:
            d = defer.fail(SchemeNotSupported("Unsupported scheme: %r" % (scheme,)))
        return d
Exemplo n.º 6
0
def writeMetric(metric_path, value, timestamp, host, port, username, password,
                vhost, exchange, spec=None, channel_number=1, ssl=False):

    if not spec:
        spec = txamqp.spec.load(os.path.normpath(
            os.path.join(os.path.dirname(__file__), 'amqp0-8.xml')))

    delegate = TwistedDelegate()

    connector = ClientCreator(reactor, AMQClient, delegate=delegate,
                              vhost=vhost, spec=spec)
    if ssl:
        from twisted.internet.ssl import ClientContextFactory
        conn = yield connector.connectSSL(host, port, ClientContextFactory())
    else:
        conn = yield connector.connectTCP(host, port)

    yield conn.authenticate(username, password)
    channel = yield conn.channel(channel_number)
    yield channel.channel_open()

    yield channel.exchange_declare(exchange=exchange, type="topic",
                                   durable=True, auto_delete=False)

    message = Content( "%f %d" % (value, timestamp) )
    message["delivery mode"] = 2

    channel.basic_publish(exchange=exchange, content=message, routing_key=metric_path)
    yield channel.channel_close()
Exemplo n.º 7
0
    def connect(self, user, targetserver):
        if targetserver not in self.ircd.servconfig["serverlinks"]:
            if user:
                user.sendMessage(irc.ERR_NOSUCHSERVER, targetserver, ":No link block exists")
            return

        def sendServerHandshake(protocol, password):
            protocol.callRemote(
                IntroduceServer,
                name=self.ircd.name,
                password=password,
                description=self.ircd.servconfig["server_description"],
                version=protocol_version,
                commonmodules=self.ircd.common_modules,
            )
            protocol.sentDataBurst = False

        servinfo = self.ircd.servconfig["serverlinks"][targetserver]
        if "ip" not in servinfo or "port" not in servinfo:
            return
        if "bindaddress" in servinfo and "bindport" in servinfo:
            bind = (servinfo["bindaddress"], servinfo["bindport"])
        else:
            bind = None
        creator = ClientCreator(reactor, ServerProtocol, self.ircd)
        if "ssl" in servinfo and servinfo["ssl"]:
            d = creator.connectSSL(servinfo["ip"], servinfo["port"], self.ircd.ssl_cert, bindAddress=bind)
        else:
            d = creator.connectTCP(servinfo["ip"], servinfo["port"], bindAddress=bind)
        d.addCallback(sendServerHandshake, servinfo["outgoing_password"])
Exemplo n.º 8
0
 def connect_to_rez(rez_info):
     dyno_id = rez_info.get('dyno_id')
     cc = ClientCreator(reactor, ProcLiteProtocol)
     (cc.connectSSL(rez_info.get('host'),
                    self.settings['dynohost_rendezvous_port'],
                    ssl.ClientContextFactory()).
      addCallback(buildProtoCallback(dyno_id)))
Exemplo n.º 9
0
 def connect_to_rez(rez_info):
     dyno_id = rez_info.get('dyno_id')
     cc = ClientCreator(reactor, ProcLiteProtocol)
     (cc.connectSSL(rez_info.get('host'),
                    self.settings['dynohost_rendezvous_port'],
                    ssl.ClientContextFactory()).addCallback(
                        buildProtoCallback(dyno_id)))
Exemplo n.º 10
0
    def createConnection(self, addr, keyId):
        if addr is None:
            return False

        host, port = addr
        cc = ClientCreator(reactor, TintProtocol, self)
        d = cc.connectSSL(host, port, self.contextFactory)
        return d.addCallback(self.saveConnection, keyId)
Exemplo n.º 11
0
 def connect(self, host, port):
     self._stack_conn = _Connection()
     self._stack_conn.attach(self)
     c = ClientCreator(reactor, lambda: self._stack_conn)
     if self.ssl_options is not None:
         yield c.connectSSL(host, port, SSLContextFactory(self.ssl_options))
     else:
         yield c.connectTCP(host, port)
Exemplo n.º 12
0
 def connect(self, host, port, scheme='http'):
     self.host = host
     self.port = port
     c = ClientCreator(reactor, _HttpClient)
     if scheme == 'http':
         self._proto = yield c.connectTCP(self.host, self.port)
     elif scheme == 'https':
         self._proto = yield c.connectSSL(self.host, self.port,
                                          ssl.ClientContextFactory())
     else:
         raise HttpException('unsupported url scheme %s' % scheme)
     yield self._proto.connect_cb
Exemplo n.º 13
0
    def createConnection(self, addrs, keyId):
        if len(addrs) == 0:
            raise HostUnreachableError("Cannot connect to %s" % keyId)

        host, port = addrs.pop()
        self.log.debug("Attempting to create connection to %s:%i" % (host, port))
        cc = ClientCreator(reactor, TintProtocol, self)
        d = cc.connectSSL(host, port, self.contextFactory, timeout=5)
        d.addCallback(self.saveConnection, keyId)
        if len(addrs) > 0:
            d.addErrback(lambda _: self.createConnection(addrs, keyId))
        return d
Exemplo n.º 14
0
def writeMetric(metric_path,
                value,
                timestamp,
                host,
                port,
                username,
                password,
                vhost,
                exchange,
                spec=None,
                channel_number=1,
                ssl=False):

    if not spec:
        spec = txamqp.spec.load(
            os.path.normpath(
                os.path.join(os.path.dirname(__file__), 'amqp0-8.xml')))

    delegate = TwistedDelegate()

    connector = ClientCreator(reactor,
                              AMQClient,
                              delegate=delegate,
                              vhost=vhost,
                              spec=spec)
    if ssl:
        from twisted.internet.ssl import ClientContextFactory
        conn = yield connector.connectSSL(host, port, ClientContextFactory())
    else:
        conn = yield connector.connectTCP(host, port)

    yield conn.authenticate(username, password)
    channel = yield conn.channel(channel_number)
    yield channel.channel_open()

    yield channel.exchange_declare(exchange=exchange,
                                   type="topic",
                                   durable=True,
                                   auto_delete=False)

    message = Content("%f %d" % (value, timestamp))
    message["delivery mode"] = 2

    channel.basic_publish(exchange=exchange,
                          content=message,
                          routing_key=metric_path)
    yield channel.channel_close()
def writeMetric(metricList, host, port, username, password,
                vhost, exchange, queue, spec=None, channel_number=1, ssl=False):
    
    global conn
    if not spec:
        spec = txamqp.spec.load(os.path.normpath(
            os.path.join(os.path.dirname(__file__), 'amqp0-8.xml')))

    delegate = TwistedDelegate()
   
    connector = ClientCreator(reactor, AMQClient, delegate=delegate,
                              vhost=vhost, spec=spec)
    if ssl:
        from twisted.internet.ssl import ClientContextFactory
        conn = yield connector.connectSSL(host, port, ClientContextFactory())
    else:
        conn = yield connector.connectTCP(host, port, timeout=130)
            
    yield conn.authenticate(username, password)
       
    channel = yield conn.channel(channel_number)
    yield channel.channel_open()
  
    yield channel.exchange_declare(exchange=exchange, type="topic", durable=True, auto_delete=False)
    
    #reply = yield channel.queue_declare(queue = queue, durable = True)
    #my_queue = reply.queue

    #Pickup settings.BIND_PATTERNS somewhere else
    #for bind_pattern in settings.BIND_PATTERNS:
    #  yield channel.queue_bind(exchange=exchange, queue=my_queue, routing_key=bind_pattern)
    
    for (metric, datapoints) in metricList:
      body = ""
      for point in datapoints:
        temp = "%f %d\n"%(point[1], point[0])
        body = body + temp 
      message = Content(body)
      message["delivery mode"] = 2
      channel.basic_publish(exchange=exchange, content=message, routing_key=metric)       
      
    yield channel.channel_close()
    yield conn.close("Publish Done. Closing Connection.")
Exemplo n.º 16
0
 def createClientChannel(self, request):
     """
     Creates the actual channel for the request.
     
     @param request: The request
     @type request: L{ClientRequest}
     """
     c = ClientCreator(reactor, self.clientChannel, self)
     if request.uri.scheme == 'http':
         d = c.connectTCP(host=request.uri.netloc, port=request.uri.port)
     else:
         d = c.connectSSL(host=request.uri.netloc, port=request.uri.port,
             contextFactory=ssl.ClientContextFactory())
     pending = PendingChannel()
     pending.host = request.uri.getHost()
     if request.closeAfter:
         pending.readPersistent = False
     self.pendingChannels.add(pending)
     d.addCallback(self.__request, request, pending)
     d.addErrback(self.__handleConnErrback, request, pending)
Exemplo n.º 17
0
 def server_autoconnect(self):
     def sendServerHandshake(protocol, password):
         protocol.callRemote(IntroduceServer, name=self.name, password=password, description=self.servconfig["server_description"], version=protocol_version, commonmodules=self.common_modules)
         protocol.sentDataBurst = False
     for server in self.servconfig["serverlink_autoconnect"]:
         if server not in self.servers and server in self.servconfig["serverlinks"]:
             log.msg("Initiating autoconnect to server {}".format(server))
             servinfo = self.servconfig["serverlinks"][server]
             if "ip" not in servinfo or "port" not in servinfo:
                 continue
             if "bindaddress" in servinfo and "bindport" in servinfo:
                 bind = (servinfo["bindaddress"], servinfo["bindport"])
             else:
                 bind = None
             creator = ClientCreator(reactor, ServerProtocol, self)
             if "ssl" in servinfo and servinfo["ssl"]:
                 d = creator.connectSSL(servinfo["ip"], servinfo["port"], self.ssl_cert, bindAddress=bind)
             else:
                 d = creator.connectTCP(servinfo["ip"], servinfo["port"], bindAddress=bind)
             d.addCallback(sendServerHandshake, servinfo["outgoing_password"])
Exemplo n.º 18
0
def do_tcp_check(host, port, tls=False, tls_verify=True, timeout=None):
    """Generic connection check function."""
    if not isIPAddress(host):
        try:
            ip = yield reactor.resolve(host, timeout=(1, timeout))
        except DNSLookupError:
            raise ValueError("dns resolution failed")
    else:
        ip = host
    creator = ClientCreator(reactor, TCPCheckProtocol)
    try:
        if tls:
            context = VerifyingContextFactory(tls_verify, CA_CERTS)
            yield creator.connectSSL(ip, port, context, timeout=timeout)
        else:
            yield creator.connectTCP(ip, port, timeout=timeout)
    except TimeoutError:
        if ip == host:
            raise ValueError("timed out")
        else:
            raise ValueError("timed out connecting to {}".format(ip))
Exemplo n.º 19
0
def do_tcp_check(host, port, tls=False, tls_verify=True,
                 timeout=None):
    """Generic connection check function."""
    if not isIPAddress(host):
        try:
            ip = yield reactor.resolve(host, timeout=(1, timeout))
        except DNSLookupError:
            raise ValueError("dns resolution failed")
    else:
        ip = host
    creator = ClientCreator(reactor, TCPCheckProtocol)
    try:
        if tls:
            context = VerifyingContextFactory(tls_verify, CA_CERTS)
            yield creator.connectSSL(ip, port, context,
                                     timeout=timeout)
        else:
            yield creator.connectTCP(ip, port, timeout=timeout)
    except TimeoutError:
        if ip == host:
            raise ValueError("timed out")
        else:
            raise ValueError("timed out connecting to {}".format(ip))
def writeMetric(metricList, host, port, username, password,
                vhost, exchange, spec=None, channel_number=1, ssl=False):
    
    t1 = time.time()  
   
    if not spec:
        spec = txamqp.spec.load(os.path.normpath(
            os.path.join(os.path.dirname(__file__), 'amqp0-8.xml')))

    delegate = TwistedDelegate()
   
    #log.msg("Creating AMQClient")
    connector = ClientCreator(reactor, AMQClient, delegate=delegate,
                              vhost=vhost, spec=spec)
    #log.msg("Created AMQClient")
    try: 
      if ssl:
          from twisted.internet.ssl import ClientContextFactory
          conn = yield connector.connectSSL(host, port, ClientContextFactory())
      else:
          #log.msg("Requesting TCP connection to host %s, and port %s" % (host, port))
          #try:
          conn = yield connector.connectTCP(host, port, timeout=130)
          #except:
            #log.msg("Not able to connect to RabitMQ server!!")
            #Push metrics back into the cache
            #backIntoCache(metricList)
            #return
            #raise Exception("Not able to connect to RabitMQ server!!") 
          #log.msg("TCP connection established")
    
     
      #log.msg("Authenticating")    
      yield conn.authenticate(username, password)
      #log.msg("Authentication done")
    except:
      log.msg("Not able to connect to RabitMQ server!!")
      backIntoCache(metricList)
      return
     
    channel = yield conn.channel(channel_number)
    yield channel.channel_open()
  
    #log.msg("Channel open")  
       
    yield channel.exchange_declare(exchange=exchange, type="topic", durable=True, auto_delete=False)
    
    reply = yield channel.queue_declare(queue = settings.AMQP_QUEUE, durable = True)
    my_queue = reply.queue

    for bind_pattern in settings.BIND_PATTERNS:
      #log.listener("binding exchange '%s' to queue '%s' with pattern %s" % (exchange, my_queue, bind_pattern))
      yield channel.queue_bind(exchange=exchange, queue=my_queue, routing_key=bind_pattern)
    
    pointCount = 0
    for (metric, datapoints) in metricList:
      body = ""
      pointCount += len(datapoints)
      for point in datapoints:
        temp = "%f %d\n"%(point[1], point[0])
        body = body + temp 
      message = Content(body)
      message["delivery mode"] = 2
      channel.basic_publish(exchange=exchange, content=message, routing_key=metric)
    
    yield channel.channel_close()
    yield conn.close("The job is done")
    
    t2 = time.time()
    publishTime = t2 - t1
    if settings.LOG_UPDATES:
      log.updates("Published %d metrics in %.5f seconds" % (len(metricList), publishTime))
    instrumentation.append('updateTimes', publishTime)
    instrumentation.increment('committedPoints', pointCount) 
Exemplo n.º 21
0
 def getFeedback(self, deferred):
     logger.info("Connecting to Feedback Server, App: %s:%s" % (self.app_mode, self.app_id))
     cc = ClientCreator(self.reactor, FeedbackProtocol, deferred)
     # SRI: not sure what the client_context_factory is for.. is it ok to reuse like this?
     cc.connectSSL(self.feedback_host, self.feedback_port, self.client_context_factory)