Exemple #1
0
    def parseStreamServer(self, reactor, public_port, localPort=None,
                          controlPort=None, hiddenServiceDir=None):
        ''':api:`twisted.internet.interfaces.IStreamServerEndpointStringParser`'''

        public_port = int(public_port)

        if localPort is not None:
            localPort = int(localPort)

        hsd = hiddenServiceDir
        if hsd:
            orig = hsd
            hsd = os.path.expanduser(hsd)
            hsd = os.path.realpath(hsd)
            if orig != hsd:
                log.msg('Using "%s" for hsd' % hsd)

        if controlPort:
            try:
                ep = clientFromString(reactor, "tcp:host=127.0.0.1:port=%d" % int(controlPort))
            except ValueError:
                ep = clientFromString(reactor, "unix:path=%s" % controlPort)
            return TCPHiddenServiceEndpoint.system_tor(reactor, ep,
                                                       public_port,
                                                       hidden_service_dir=hsd,
                                                       local_port=localPort)

        return TCPHiddenServiceEndpoint.global_tor(reactor, public_port,
                                                   hidden_service_dir=hsd,
                                                   local_port=localPort,
                                                   control_port=controlPort)
Exemple #2
0
def run_command(config):
    c = dispatch_table[config.subCommand]()
    tub = Tub()
    try:
        from twisted.internet import reactor
        from twisted.internet.endpoints import clientFromString
        from foolscap.connections import tor
        CONTROL = os.environ.get("FOOLSCAP_TOR_CONTROL_PORT", "")
        SOCKS = os.environ.get("FOOLSCAP_TOR_SOCKS_PORT", "")
        if CONTROL:
            h = tor.control_endpoint(clientFromString(reactor, CONTROL))
            tub.addConnectionHintHandler("tor", h)
        elif SOCKS:
            h = tor.socks_endpoint(clientFromString(reactor, SOCKS))
            tub.addConnectionHintHandler("tor", h)
        #else:
        #    h = tor.default_socks()
        #    tub.addConnectionHintHandler("tor", h)
    except ImportError:
        pass
    d = defer.succeed(None)
    d.addCallback(lambda _ign: tub.startService())
    d.addCallback(lambda _ign: tub.getReference(config.furl))
    d.addCallback(c.run, config.subOptions) # might provide tub here
    d.addBoth(lambda res: tub.stopService().addCallback(lambda _ign: res))
    return d
Exemple #3
0
def get(host, path):
    f = protocol.ClientFactory()
    f.protocol = HTTPGETProtocol
    f.path = path
    f.host = host
    f.deferred = defer.Deferred()
    strport = "tcp:%s:80" % host
    endpoints.clientFromString(reactor, strport).connect(f)
    return f.deferred
def start():
    """ Client Entrypoint """
    endpoints.clientFromString(reactor, "tcp:localhost:1234").connect(PrintClientFactory())

    # gui = GameWindow()

    # gui_loop = task.LoopingCall(gui.update)
    # gui_loop.start(1.0)

    reactor.run()
Exemple #5
0
    async def _startup(reactor):
        if cfg.connect.startswith('tcp:') or cfg.connect.startswith('unix:'):
            ep = clientFromString(reactor, cfg.connect)
        else:
            if ':' in cfg.connect:
                ep = clientFromString(reactor, 'tcp:{}'.format(cfg.connect))
            else:
                ep = clientFromString(reactor, 'tcp:localhost:{}'.format(cfg.connect))
        tor = await txtorcon.connect(reactor, ep)

        if cfg.debug_protocol:

            click.echo("Low-level protocol debugging: ", nl=False)
            click.echo(click.style("data we write to Tor, ", fg='blue'), nl=False)
            click.echo(click.style("data from Tor", fg='yellow') + ".")

            def write_wrapper(data):
                tail = data
                while len(tail):
                    head, tail = tail.split('\r\n', 1)
                    click.echo(">>> " + click.style(head, fg='blue'), nl=False)
                click.echo()
                return orig_write(data)
            orig_write = tor.protocol.transport.write
            tor.protocol.transport.write = write_wrapper

            def read_wrapper(data):
                tail = data
                while '\r\n' in tail:
                    head, tail = tail.split('\r\n', 1)
                    if not read_wrapper.write_prefix:
                        click.echo(click.style(head, fg='yellow'))
                        read_wrapper.write_prefix = True
                    else:
                        click.echo("<<< " + click.style(head, fg='yellow'))
                if len(tail):
                    click.echo("<<< " + click.style(tail, fg='yellow'), nl=False)
                    read_wrapper.write_prefix = False
                else:
                    click.echo()
                return orig_read(data)
            read_wrapper.write_prefix = True
            orig_read = tor.protocol.dataReceived
            tor.protocol.dataReceived = read_wrapper


        if cfg.info:
            info = await tor.protocol.get_info('version', 'status/version/current', 'dormant')
            click.echo(
                'Connected to a Tor version "{version}" (status: '
                '{status/version/current}).\n'.format(**info)
            )
        await cmd(reactor, cfg, tor, *args, **kwargs)
    def test_parse_client_basic(self):
        from twisted.plugins import autobahn_endpoints

        self.assertTrue(hasattr(autobahn_endpoints, "AutobahnClientParser"))
        from twisted.internet.endpoints import clientFromString, quoteStringArgument
        from twisted.internet import reactor

        ep_string = "autobahn:{0}:url={1}".format(
            quoteStringArgument("tcp:localhost:9000"), quoteStringArgument("ws://localhost:9000")
        )
        # we're just testing that this doesn't fail entirely
        clientFromString(reactor, ep_string)
Exemple #7
0
def connect(hostname, worker=None, on_connect=None):
    """
    Connect to server using GrideaProtocol, automatically retry if it is
    not yet running.

    :param hostname: `hostname:port` to connect to
    :param worker: optional gridea.GrideaWorker() to make this process a worker
    :param on_connect: optional callback after connection
    """
    class ClientFactory(ReconnectingClientFactory):
        def buildProtocol(self, addr):
            return GrideaProtocol(worker, on_connect)
    clientFromString(reactor, 'tcp:' + hostname).connect(ClientFactory())
Exemple #8
0
 def _build_endpoint(cls, endpoint_string=None, encrypted=True, timeout=5, host=None, reactor=None):
     if not reactor:
         from twisted.internet import reactor
     host = host or cls.host
     if endpoint_string:
         endpoint = clientFromString(reactor, endpoint_string)
     else:
         if encrypted:
             port = 443
             proto = 'ssl'
         else:
             port = 80
             proto = 'tcp'
         endpoint = clientFromString(reactor, '{0}:host={1}:port={2}:timeout={3}'.format(proto, host, port, timeout))
     return endpoint
Exemple #9
0
def generateDestination(reactor, keyfile, api=None, apiEndpoint=None):
    """Generate a new I2P Destination.

    The function returns a :class:`twisted.internet.defer.Deferred`; register
    callbacks to receive the return value or errors.

    Args:
        reactor: The API endpoint will be constructed with this reactor.
        keyfile (str): Path to a local file where the keypair for the new
            Destination should be stored.
        api (str): The API to use.
        apiEndpoint (str): An endpoint string that will connect to the API.
            Alternatively, the caller can directly provide an
            :class:`twisted.internet.interfaces.IStreamClientEndpoint`, and the
            ``reactor`` will be ignored.

    Returns:
        txi2p.I2PAddress: The new Destination. Once this is received via the
        Deferred callback, the ``keyfile`` will have been written.

    Raises:
        ValueError: if the API doesn't support this method.
        ValueError: if the ``keyfile`` already exists.
        IOError: if the ``keyfile`` write fails.
    """
    api, apiEndpoint = getApi(api, apiEndpoint, _apiGenerators)
    if isinstance(apiEndpoint, str):
        apiEndpoint = clientFromString(reactor, apiEndpoint)
    return _apiGenerators[api](keyfile, apiEndpoint)
Exemple #10
0
    def test_basic(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())
        a2 = yield connectProtocol(ep, Accumulator())

        token1 = b"\x00"*32
        a1.transport.write(b"please relay " + hexlify(token1) + b"\n")
        a2.transport.write(b"please relay " + hexlify(token1) + b"\n")

        # a correct handshake yields an ack, after which we can send
        exp = b"ok\n"
        yield a1.waitForBytes(len(exp))
        self.assertEqual(a1.data, exp)
        s1 = b"data1"
        a1.transport.write(s1)

        exp = b"ok\n"
        yield a2.waitForBytes(len(exp))
        self.assertEqual(a2.data, exp)

        # all data they sent after the handshake should be given to us
        exp = b"ok\n"+s1
        yield a2.waitForBytes(len(exp))
        self.assertEqual(a2.data, exp)

        a1.transport.loseConnection()
        a2.transport.loseConnection()
Exemple #11
0
    def connect(self, terminal=None, on_error=None):
        connection_string = "tcp:host={host}:port={port}".format(
            host=self.options["host"],
            port=self.options["port"]
        )

        log.msg("Connecting with connection string %s" % connection_string)

        # TODO: add port forwarding here
        ssh_connection = SSHConnection(self, terminal)

        # TODO: verify host keys
        vhk = lambda *a: defer.succeed(1)

        # Build a basic auth client. There's a Twisted implementation
        # for this, but we need something simpler that does not access
        # keys that are stored on the disk
        uao = AuthClient(self.options, ssh_connection)

        d = defer.Deferred()
        factory = direct.SSHClientFactory(d, self.options, vhk, uao)
        if on_error:
            d.addErrback(on_error)

        endpoint = endpoints.clientFromString(reactor, connection_string)

        wp = None

        try:
            wp = yield endpoint.connect(factory)
        except Exception:
            terminal.leave()

        defer.returnValue(wp)
Exemple #12
0
def main(reactor):
    will_send_message = len(sys.argv) > 1
    ep = endpoints.clientFromString(reactor, "tcp:localhost:4002")
    f = WebSocketClientFactory("ws://127.0.0.1:4002/")
    f.reactor = reactor
    f.protocol = RelayEchoClient
    f.token = "a" * 64
    f.side = "0" * 16 if will_send_message else "1" * 16
    f.done = Deferred()
    f.ready = Deferred()

    proto = yield ep.connect(f)
    print("proto", proto)
    yield f.ready

    print("ready")
    if will_send_message:
        for _ in range(5):
            print("sending message")
            proto.sendMessage(b"it's a message", True)
            yield deferLater(reactor, 0.2)
        yield proto.sendClose()
        print("closing")
    yield f.done
    print("relayed {} bytes:".format(len(proto._received)))
    print(proto._received.decode("utf8"))
Exemple #13
0
def makeService(config):
    s = MultiService()


    # ZipkinTracer(
    #     scribe_client,
    #     category=None,
    #     end_annotations=None,
    #     max_traces=50,
    #     max_idle_time=10,
    #     _reactor=None)
    push_tracer(
        ZipkinTracer(
            ScribeClient(clientFromString(reactor, config['scribe'])), 'zipkin', None, 10, 10, None))

    root = RootResource()

    # if config['rproxy']:
    #     root = RProxyWrapper(root)

    site = server.Site(root)
    site.displayTracebacks = False

    api_service = strports.service(config['port'], site)
    api_service.setServiceParent(s)

    return s
    def test_parser_basic(self):
        ep = clientFromString(None, 'tor:host=timaq4ygg2iegci7.onion:port=80:socksPort=9050')

        self.assertEqual(ep.host, 'timaq4ygg2iegci7.onion')
        self.assertEqual(ep.port, 80)
        # XXX what's "the Twisted way" to get the port out here?
        self.assertEqual(ep._socks_endpoint._port, 9050)
    def test_system_tor(self, ftb):

        def boom():
            # why does the new_callable thing need a callable that
            # returns a callable? Feels like I must be doing something
            # wrong somewhere...
            def bam(*args, **kw):
                self.config.bootstrap()
                return defer.succeed(Tor(Mock(), self.config))
            return bam
        with patch('txtorcon.endpoints.launch_tor') as launch_mock:
            with patch('txtorcon.controller.connect', new_callable=boom):
                client = clientFromString(
                    self.reactor,
                    "tcp:host=localhost:port=9050"
                )
                ep = yield TCPHiddenServiceEndpoint.system_tor(self.reactor,
                                                               client, 80)
                port = yield ep.listen(NoOpProtocolFactory())
                toa = port.getHost()
                self.assertTrue(hasattr(toa, 'onion_uri'))
                self.assertTrue(hasattr(toa, 'onion_port'))
                port.startListening()
                str(port)
                port.tor_config
                # system_tor should be connecting to a running one,
                # *not* launching a new one.
                self.assertFalse(launch_mock.called)
Exemple #16
0
    def test_system_tor(self):
        from test_torconfig import FakeControlProtocol

        def boom(*args):
            # why does the new_callable thing need a callable that
            # returns a callable? Feels like I must be doing something
            # wrong somewhere...
            def bam(*args, **kw):
                return self.protocol

            return bam

        with patch('txtorcon.endpoints.launch_tor') as m:
            with patch('txtorcon.endpoints.build_tor_connection',
                       new_callable=boom) as btc:
                client = clientFromString(self.reactor,
                                          "tcp:host=localhost:port=9050")
                ep = yield TCPHiddenServiceEndpoint.system_tor(
                    self.reactor, client, 80)
                port = yield ep.listen(NoOpProtocolFactory())
                toa = port.getHost()
                self.assertTrue(hasattr(toa, 'onion_uri'))
                self.assertTrue(hasattr(toa, 'onion_port'))
                port.startListening()
                str(port)
                port.tor_config
                m.assert_called()
Exemple #17
0
    def connectSSH(self, strport, sshConnection=None):
        if sshConnection is None:
            sshConnection = SSHConnection(self, self.options)

        #vhk = default.verifyHostKey
        vhk = lambda *a: defer.succeed(1)
        uao = default.SSHUserAuthClient(self.options['user'], self.options,
                                        sshConnection)
        d = defer.Deferred()
        factory = direct.SSHClientFactory(d, self.options, vhk, uao)

        endpoint = endpoints.clientFromString(reactor, strport)
        try:
            wp = yield endpoint.connect(factory)
        except Exception:

            def _stop():
                try:
                    reactor.stop()
                except:
                    pass

            reactor.callLater(0.1, _stop)
            raise
        defer.returnValue(wp)
Exemple #18
0
    def _connect(self, reactor, update_status):
        # create a new Tor
        config = self.config = txtorcon.TorConfig()
        if self._data_directory:
            # The default is for launch_tor to create a tempdir itself, and
            # delete it when done. We only need to set a DataDirectory if we
            # want it to be persistent. This saves some startup time, because
            # we cache the descriptors from last time. On one of my hosts,
            # this reduces connect from 20s to 15s.
            if not os.path.exists(self._data_directory):
                # tor will mkdir this, but txtorcon wants to chdir to it
                # before spawning the tor process, so (for now) we need to
                # mkdir it ourselves. TODO: txtorcon should take
                # responsibility for this.
                os.mkdir(self._data_directory)
            config.DataDirectory = self._data_directory

        #config.ControlPort = allocate_tcp_port() # defaults to 9052
        config.SocksPort = allocate_tcp_port()
        socks_desc = "tcp:127.0.0.1:%d" % config.SocksPort
        self._socks_desc = socks_desc  # stash for tests
        socks_endpoint = clientFromString(reactor, socks_desc)

        with add_context(update_status, "launching Tor"):
            tpp = yield txtorcon.launch_tor(config,
                                            reactor,
                                            tor_binary=self._tor_binary)
        #print "launched"
        # gives a TorProcessProtocol with .tor_protocol
        self._tor_protocol = tpp.tor_protocol
        returnValue(socks_endpoint)
def main(reactor, *descriptions):
    log = Logger()
    globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)])
    endpointObjects = [
        endpoints.clientFromString(reactor, description)
        for description in descriptions
    ]
    hostPorts = [(endpoint._host, endpoint._port)
                 for endpoint in endpointObjects]

    pool = threadpool.ThreadPool(minthreads=1, maxthreads=1, name="persiter")
    persister = Persists(reactor, pool)
    reactor.addSystemEventTrigger("before", "shutdown", persister.stop)
    persister.start("log.sqlite", hostPorts)

    analyzer = AnalyzesText(persister)

    factory = EncodingCollectionFactory(reactor, random.SystemRandom(),
                                        analyzer)

    for (host, port), endpoint in zip(hostPorts, endpointObjects):
        try:
            protocol = yield endpoint.connect(factory)
        except Exception:
            log.failure("Could not connect to {host}:{port}",
                        host=host,
                        port=port)
            raise
        protocol.addr = (host, port)

    defer.returnValue(defer.Deferred())
 def test_unix_connect(self):
     """Demonstrate that the service connects to a Unix domain socket."""
     self.assertEqual(len(self.reactor.unixClients), 0)
     ep = clientFromString(self.reactor, "unix:/dev/null")
     service = makeSubscriberService(ep, "ivo://foo/bar", [], [], [])
     service.startService()
     self.assertEqual(len(self.reactor.unixClients), 1)
 def test_tcp_connect(self):
     """Demonstrate that the service connects to a TCP socket."""
     self.assertEqual(len(self.reactor.tcpClients), 0)
     ep = clientFromString(self.reactor, "tcp:foo:8099")
     service = makeSubscriberService(ep, "ivo://foo/bar", [], [], [])
     service.startService()
     self.assertEqual(len(self.reactor.tcpClients), 1)
Exemple #22
0
    def do_connect(self, domain: str, port: int):
        """
        If using CONNECT, the client is now in the established state.
        :param domain: 服务端地址
        :param port: 服务端端口
        :return: defer
        """
        self.log.info('do connect command')
        # Don't read anything from the connecting client until we have somewhere to send it to.
        self.transport.pauseProducing()

        point = clientFromString(self.factory.reactor, f"tcp:host={domain}:port={port}")
        d = connectProtocol(point, ProxyClient(self))

        def success(ignored):
            self.log.info("connected to {domain}, {port}", domain=domain, port=port)

            # We're connected, everybody can read to their hearts content.
            self.transport.resumeProducing()

            self.make_reply(constants.SOCKS5_GRANTED, address=self.client.host_address)
            self.set_state(self.STATE_ESTABLISHED)

        def error(failure):
            raise errors.HostUnreachable()

        d.addCallbacks(success, error)

        return d
Exemple #23
0
    def test_tls_socks_with_endpoint(self, ep_mock):
        """
        Same as above, except we provide an explicit endpoint
        """

        if not _HAVE_TLS:
            print ("no TLS support")
            return

        class FakeWrappedProto(object):
            wrappedProtocol = object()

        wrap = FakeWrappedProto()
        proto = defer.succeed(wrap)

        class FakeSocks5(object):
            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint(
            "torproject.org", 0, socks_endpoint=clientFromString(Mock(), "tcp:localhost:9050"), tls=True
        )
        p2 = yield endpoint.connect(None)
        self.assertTrue(wrap.wrappedProtocol is p2)
Exemple #24
0
 def __init__(self, request, config):
    self.request = request
    self.config = config
    factory = LDAPFactory()
    endpoint = clientFromString(reactor, self.config['client'])
    d = endpoint.connect(factory)
    d.addCallback(self.gotConnection)
Exemple #25
0
def _try_to_connect(reactor, endpoint_desc, stdout, txtorcon):
    # yields a TorState, or None
    ep = clientFromString(reactor, endpoint_desc)
    d = txtorcon.build_tor_connection(ep)
    def _failed(f):
        # depending upon what's listening at that endpoint, we might get
        # various errors. If this list is too short, we might expose an
        # exception to the user (causing "tahoe create-node" to fail messily)
        # when we're supposed to just try the next potential port instead.
        # But I don't want to catch everything, because that may hide actual
        # coding errrors.
        f.trap(ConnectionRefusedError, # nothing listening on TCP
               ConnectError, # missing unix socket, or permission denied
               #ValueError,
               # connecting to e.g. an HTTP server causes an
               # UnhandledException (around a ValueError) when the handshake
               # fails to parse, but that's not something we can catch. The
               # attempt hangs, so don't do that.
               RuntimeError, # authentication failure
               )
        if stdout:
            stdout.write("Unable to reach Tor at '%s': %s\n" %
                         (endpoint_desc, f.value))
        return None
    d.addErrback(_failed)
    return d
Exemple #26
0
    def _make_i2p_handler(self):
        enabled = self.get_config("i2p", "enabled", True, boolean=True)
        if not enabled:
            return None
        i2p = _import_i2p()
        if not i2p:
            return None

        samport = self.get_config("i2p", "sam.port", None)
        launch = self.get_config("i2p", "launch", False, boolean=True)
        configdir = self.get_config("i2p", "i2p.configdir", None)

        if samport:
            if launch:
                raise ValueError("tahoe.cfg [i2p] must not set both "
                                 "sam.port and launch")
            ep = endpoints.clientFromString(reactor, samport)
            return i2p.sam_endpoint(ep)

        if launch:
            executable = self.get_config("i2p", "i2p.executable", None)
            return i2p.launch(i2p_configdir=configdir, i2p_binary=executable)

        if configdir:
            return i2p.local_i2p(configdir)

        return i2p.default(reactor)
Exemple #27
0
 def _parseSAMServer(self, reactor, keyfile, port, samEndpoint,
                  nickname=None,
                  autoClose=False,
                  options=None):
     return SAMI2PStreamServerEndpoint.new(
         clientFromString(reactor, samEndpoint),
         keyfile, port, nickname, autoClose, options)
Exemple #28
0
    def _connect(self, reactor, update_status):
        # create a new Tor
        config = self.config = txtorcon.TorConfig()
        if self._data_directory:
            # The default is for launch_tor to create a tempdir itself, and
            # delete it when done. We only need to set a DataDirectory if we
            # want it to be persistent. This saves some startup time, because
            # we cache the descriptors from last time. On one of my hosts,
            # this reduces connect from 20s to 15s.
            if not os.path.exists(self._data_directory):
                # tor will mkdir this, but txtorcon wants to chdir to it
                # before spawning the tor process, so (for now) we need to
                # mkdir it ourselves. TODO: txtorcon should take
                # responsibility for this.
                os.mkdir(self._data_directory)
            config.DataDirectory = self._data_directory

        #config.ControlPort = allocate_tcp_port() # defaults to 9052
        config.SocksPort = allocate_tcp_port()
        socks_desc = "tcp:127.0.0.1:%d" % config.SocksPort
        self._socks_desc = socks_desc # stash for tests
        socks_endpoint = clientFromString(reactor, socks_desc)

        with add_context(update_status, "launching Tor"):
            tpp = yield txtorcon.launch_tor(config, reactor,
                                            tor_binary=self._tor_binary)
        #print "launched"
        # gives a TorProcessProtocol with .tor_protocol
        self._tor_protocol = tpp.tor_protocol
        returnValue(socks_endpoint)
Exemple #29
0
    def get_i2p_handler(self):
        enabled = self._get_i2p_config("enabled", True, boolean=True)
        if not enabled:
            return None
        if not self._i2p:
            return None

        sam_port = self._get_i2p_config("sam.port", None)
        launch = self._get_i2p_config("launch", False, boolean=True)
        configdir = self._get_i2p_config("i2p.configdir", None)
        keyfile = self._get_i2p_config("dest.private_key_file", None)

        if sam_port:
            if launch:
                raise ValueError("tahoe.cfg [i2p] must not set both "
                                 "sam.port and launch")
            ep = clientFromString(self._reactor, sam_port)
            return self._i2p.sam_endpoint(ep, keyfile=keyfile)

        if launch:
            executable = self._get_i2p_config("i2p.executable", None)
            return self._i2p.launch(i2p_configdir=configdir,
                                    i2p_binary=executable)

        if configdir:
            return self._i2p.local_i2p(configdir)

        return self._i2p.default(self._reactor, keyfile=keyfile)
    def test_tls_socks_with_endpoint(self, ep_mock):
        """
        Same as above, except we provide an explicit endpoint
        """
        the_proto = object()
        proto_d = defer.succeed(the_proto)

        class FakeSocks5(object):

            def __init__(self, *args, **kw):
                pass

            def connect(self, *args, **kw):
                return proto_d

            def _get_address(self):
                return defer.succeed(None)

        ep_mock.side_effect = FakeSocks5
        endpoint = TorClientEndpoint(
            u'torproject.org', 0,
            socks_endpoint=clientFromString(Mock(), "tcp:localhost:9050"),
            tls=True,
        )
        p2 = yield endpoint.connect(None)
        self.assertTrue(p2 is the_proto)
    def get_i2p_handler(self):
        enabled = self._get_i2p_config("enabled", True, boolean=True)
        if not enabled:
            return None
        if not self._i2p:
            return None

        sam_port = self._get_i2p_config("sam.port", None)
        launch = self._get_i2p_config("launch", False, boolean=True)
        configdir = self._get_i2p_config("i2p.configdir", None)
        keyfile = self._get_i2p_config("dest.private_key_file", None)

        if sam_port:
            if launch:
                raise ValueError("tahoe.cfg [i2p] must not set both "
                                 "sam.port and launch")
            ep = clientFromString(self._reactor, sam_port)
            return self._i2p.sam_endpoint(ep, keyfile=keyfile)

        if launch:
            executable = self._get_i2p_config("i2p.executable", None)
            return self._i2p.launch(i2p_configdir=configdir, i2p_binary=executable)

        if configdir:
            return self._i2p.local_i2p(configdir)

        return self._i2p.default(self._reactor, keyfile=keyfile)
def main():
    """
	Just start all 3 protos
	"""

    # WS listener
    ws_factory = WebSocketServerFactory(u"ws://52.34.209.113:8000")
    ws_factory.protocol = WSProto
    reactor.listenTCP(8000, ws_factory)

    # MQTT listener
    mqtt_factory = MQTTFactory(profile=MQTTFactory.SUBSCRIBER)
    mqtt_endpoint = clientFromString(reactor, BROKER)
    mqtt_serv = MQTTProto(mqtt_endpoint, mqtt_factory)
    mqtt_serv.startService()

    # CoAP listener
    coap_root = resource.CoAPResource()
    coap_ovh = CoAPProto()
    coap_root.putChild('test', coap_ovh)
    coap_endpoint = resource.Endpoint(coap_root)
    reactor.listenUDP(coap.COAP_PORT, coap.Coap(coap_endpoint))

    # run the program
    reactor.run()
Exemple #33
0
 def test_socks_endpoint_real(self):
     tor_socks_endpoint = clientFromString(reactor, "tcp:socks_host:100")
     h = tor.socks_endpoint(tor_socks_endpoint)
     res = yield h.hint_to_endpoint("tcp:example.com:1234", reactor)
     ep, host = res
     self.assertIsInstance(ep, txtorcon.endpoints.TorClientEndpoint)
     self.assertEqual(host, "example.com")
Exemple #34
0
    def __init__(self,
                 desc,
                 nickname='cassbot',
                 init_channels=(),
                 reactor=None,
                 statefile=None):
        service.MultiService.__init__(self)

        self.statefile = statefile or self.default_statefile
        self.state = {
            'nickname': nickname,
            'channels': init_channels,
            'cmd_prefix': None,
            'plugins': {},
        }
        self.auth = AuthMap()

        if reactor is None:
            from twisted.internet import reactor
        self.reactor = reactor

        self.endpoint_desc = desc
        self.endpoint = endpoints.clientFromString(reactor, desc)

        self.watcher_map = {}
        self.command_map = {}
        self.scanning_now = False

        # all 'enabled' or 'loaded' plugins have an entry in here, keyed by
        # the plugin name (as given by the .name() classmethod).
        self.pluginmap = {}

        self.pfactory = CassBotFactory()
Exemple #35
0
 def _connect(self, reactor, update_status):
     maker = self._tor_control_endpoint_maker
     with add_context(update_status, "making Tor control endpoint"):
         tor_control_endpoint = yield maker(reactor, update_status)
     assert IStreamClientEndpoint.providedBy(tor_control_endpoint)
     with add_context(update_status, "connecting to Tor"):
         tproto = yield txtorcon.build_tor_connection(tor_control_endpoint,
                                                      build_state=False)
     with add_context(update_status, "waiting for Tor bootstrap"):
         config = yield txtorcon.TorConfig.from_protocol(tproto)
     ports = list(config.SocksPort)
     # I've seen "9050", and "unix:/var/run/tor/socks WorldWritable"
     for port in ports:
         pieces = port.split()
         p = pieces[0]
         if p == txtorcon.DEFAULT_VALUE:
             p = "9050"
         try:
             portnum = int(p)
             socks_desc = "tcp:127.0.0.1:%d" % portnum
             self._socks_desc = socks_desc # stash for tests
             socks_endpoint = clientFromString(reactor, socks_desc)
             returnValue(socks_endpoint)
         except ValueError:
             pass
     raise ValueError("could not use config.SocksPort: %r" % (ports,))
Exemple #36
0
 def test_ssl(self):
     """
     When passed an SSL strports description, L{clientFromString} returns a
     L{SSL4ClientEndpoint} instance initialized with the values from the
     string.
     """
     reactor = object()
     client = endpoints.clientFromString(
         reactor, "ssl:host=example.net:port=4321:privateKey=%s:"
         "certKey=%s:bindAddress=10.0.0.3:timeout=3:caCertsDir=%s" %
         (escapedPEMPathName, escapedPEMPathName, escapedCAsPathName))
     self.assertIsInstance(client, endpoints.SSL4ClientEndpoint)
     self.assertIdentical(client._reactor, reactor)
     self.assertEqual(client._host, "example.net")
     self.assertEqual(client._port, 4321)
     self.assertEqual(client._timeout, 3)
     self.assertEqual(client._bindAddress, "10.0.0.3")
     certOptions = client._sslContextFactory
     self.assertIsInstance(certOptions, CertificateOptions)
     ctx = certOptions.getContext()
     self.assertIsInstance(ctx, ContextType)
     self.assertEqual(Certificate(certOptions.certificate), testCertificate)
     privateCert = PrivateCertificate(certOptions.certificate)
     privateCert._setPrivateKey(KeyPair(certOptions.privateKey))
     self.assertEqual(privateCert, testPrivateCertificate)
     expectedCerts = [
         Certificate.loadPEM(x.getContent()) for x in
         [casPath.child("thing1.pem"),
          casPath.child("thing2.pem")]
         if x.basename().lower().endswith('.pem')
     ]
     self.assertEqual(
         sorted((Certificate(x) for x in certOptions.caCerts),
                key=lambda cert: cert.digest()),
         sorted(expectedCerts, key=lambda cert: cert.digest()))
Exemple #37
0
def setup_connection(reactor, args):
    """
    Return Cassandra connection
    """
    return CQLClient(
        clientFromString(reactor, 'tcp:{}:{}'.format(args.host, args.port)),
        args.keyspace)
Exemple #38
0
    def connectionMade(self):
        logger.info('[%s] Connection received from VNC client', self.id)
        factory = protocol.ClientFactory()
        factory.protocol = VNCProxyClient
        factory.vnc_server = self
        factory.deferrable = defer.Deferred()
        endpoint = endpoints.clientFromString(reactor,
                                              self.factory.vnc_address)

        def _established_callback(client):
            if self._broken:
                client.close()
            self.vnc_client = client
            self.flush()

        def _established_errback(reason):
            logger.error(
                '[VNCProxyServer] Connection succeeded but could not establish session: %s',
                reason)
            self.close()

        factory.deferrable.addCallbacks(_established_callback,
                                        _established_errback)

        def _connect_errback(reason):
            logger.error('[VNCProxyServer] Connection failed: %s', reason)
            self.close()

        endpoint.connect(factory).addErrback(_connect_errback)

        self.send_ProtocolVersion_Handshake()
Exemple #39
0
 def __init__(self, live=False, retryPolicy=None,
              clock=None, prepareConnection=None):
     host = "ssl:" + (self.PROXY_LIVE if live else self.PROXY_DEMO)
     endpoint = clientFromString(reactor, host)
     factory = Client.Factory.forProtocol(Client.Protocol, client=self)
     super().__init__(endpoint, factory, retryPolicy=retryPolicy,
                      clock=clock, prepareConnection=prepareConnection)
Exemple #40
0
 def clean(self, value):
     from twisted.internet.endpoints import clientFromString
     from twisted.internet import reactor
     try:
         return clientFromString(reactor, value)
     except ValueError:
         self.raise_config_error('is not a valid client endpoint')
Exemple #41
0
 def _make_control_endpoint(self, reactor, update_status):
     # this will only be called when tahoe.cfg has "[tor] launch = true"
     update_status("launching Tor")
     with self._tor.add_context(update_status, "launching Tor"):
         (endpoint_desc, _) = yield self._get_launched_tor(reactor)
     tor_control_endpoint = clientFromString(reactor, endpoint_desc)
     returnValue(tor_control_endpoint)
Exemple #42
0
    def get_client_endpoint(self):
        """
        Get an ``IStreamClientEndpoint`` which will set up a connection to an I2P
        address.

        If I2P is not enabled or the dependencies are not available, return
        ``None`` instead.
        """
        enabled = self._get_i2p_config("enabled", True, boolean=True)
        if not enabled:
            return None
        if not self._i2p:
            return None

        sam_port = self._get_i2p_config("sam.port", None)
        launch = self._get_i2p_config("launch", False, boolean=True)
        configdir = self._get_i2p_config("i2p.configdir", None)
        keyfile = self._get_i2p_config("dest.private_key_file", None)

        if sam_port:
            if launch:
                raise ValueError("tahoe.cfg [i2p] must not set both "
                                 "sam.port and launch")
            ep = clientFromString(self._reactor, sam_port)
            return self._i2p.sam_endpoint(ep, keyfile=keyfile)

        if launch:
            executable = self._get_i2p_config("i2p.executable", None)
            return self._i2p.launch(i2p_configdir=configdir,
                                    i2p_binary=executable)

        if configdir:
            return self._i2p.local_i2p(configdir)

        return self._i2p.default(self._reactor, keyfile=keyfile)
Exemple #43
0
    def test_parser_basic(self):
        ep = clientFromString(None, "tor:host=timaq4ygg2iegci7.onion:port=80:socksPort=9050")

        self.assertEqual(ep.host, "timaq4ygg2iegci7.onion")
        self.assertEqual(ep.port, 80)
        # XXX what's "the Twisted way" to get the port out here?
        self.assertEqual(ep.socks_endpoint._port, 9050)
Exemple #44
0
def _try_to_connect(reactor, endpoint_desc, stdout, txi2p):
    # yields True or None
    ep = clientFromString(reactor, endpoint_desc)
    d = txi2p.testAPI(reactor, 'SAM', ep)

    def _failed(f):
        # depending upon what's listening at that endpoint, we might get
        # various errors. If this list is too short, we might expose an
        # exception to the user (causing "tahoe create-node" to fail messily)
        # when we're supposed to just try the next potential port instead.
        # But I don't want to catch everything, because that may hide actual
        # coding errors.
        f.trap(
            ConnectionRefusedError,  # nothing listening on TCP
            ConnectError,  # missing unix socket, or permission denied
            #ValueError,
            # connecting to e.g. an HTTP server causes an
            # UnhandledException (around a ValueError) when the handshake
            # fails to parse, but that's not something we can catch. The
            # attempt hangs, so don't do that.
            RuntimeError,  # authentication failure
        )
        if stdout:
            stdout.write("Unable to reach I2P SAM API at '%s': %s\n" %
                         (endpoint_desc, f.value))
        return None

    d.addErrback(_failed)
    return d
Exemple #45
0
 def _make_control_endpoint(self, reactor, update_status):
     # this will only be called when tahoe.cfg has "[tor] launch = true"
     update_status("launching Tor")
     with self._tor.add_context(update_status, "launching Tor"):
         (endpoint_desc, _) = yield self._get_launched_tor(reactor)
     tor_control_endpoint = clientFromString(reactor, endpoint_desc)
     returnValue(tor_control_endpoint)
Exemple #46
0
 def connect_to_exchange(self):
     """
     Connect to an AMQP exchange as a publisher.
     """
     exchange = self.pub_exchange
     vhost = self.pub_vhost
     user = self.pub_user
     passwd = self.pub_passwd
     endpoint_s = self.pub_endpoint_s
     spec = self.pub_spec
     e = clientFromString(self.reactor, endpoint_s)
     delegate = TwistedDelegate()
     amqp_protocol = AMQClient(
         delegate=delegate,
         vhost=vhost,
         spec=spec)
     try:
         conn = yield connectProtocol(e, amqp_protocol)
     except Exception:
         self.log.failure(
             "Failed to establish AMQP connection to endpoint '{0}'".format(
                 endpoint_s))
         raise
     yield conn.authenticate(user, passwd)
     self.pub_channel = yield conn.channel(1)
     yield self.pub_channel.channel_open()
 def connectionMade(self):
     # print("EndpointForwardingProtocol.connectionMade")
     self._destFactory = DestEndpointForwardingFactory(self)
     self._destEndpoint = clientFromString(
         self.factory.service._reactor, self.factory.service._destEndpointDescriptor
     )
     self._destEndpointPort = yield self._destEndpoint.connect(self._destFactory)
    def test_one_happy_one_jilted(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())
        a2 = yield connectProtocol(ep, Accumulator())

        token1 = b"\x00" * 32
        side1 = b"\x01" * 8
        side2 = b"\x02" * 8
        a1.transport.write(b"please relay " + hexlify(token1) + b" for side " +
                           hexlify(side1) + b"\n")
        while not self._transit_server._pending_requests:
            yield wait()  # make sure a1 connects first
        a2.transport.write(b"please relay " + hexlify(token1) + b" for side " +
                           hexlify(side2) + b"\n")
        while not self._transit_server._active_connections:
            yield wait()  # wait for the server to see the connection
        self.assertEqual(len(self._transit_server._pending_requests), 0)
        self.assertEqual(self._usage, [])  # no events yet
        a1.transport.write(b"\x00" * 13)
        yield a2.waitForBytes(13)
        a2.transport.write(b"\xff" * 7)
        yield a1.waitForBytes(7)

        a1.transport.loseConnection()
        yield a1._disconnect
        while self._transit_server._active_connections:
            yield wait()
        yield a2._disconnect
        self.assertEqual(len(self._usage), 1, self._usage)
        (started, result, total_bytes, total_time,
         waiting_time) = self._usage[0]
        self.assertEqual(result, "happy", self._usage)
        self.assertEqual(total_bytes, 20)
        self.assertNotIdentical(waiting_time, None)
Exemple #49
0
    def test_system_tor(self):
        from test_torconfig import FakeControlProtocol

        def boom(*args):
            # why does the new_callable thing need a callable that
            # returns a callable? Feels like I must be doing something
            # wrong somewhere...
            def bam(*args, **kw):
                return self.protocol
            return bam
        with patch('txtorcon.endpoints.launch_tor') as launch_mock:
            with patch('txtorcon.endpoints.build_tor_connection', new_callable=boom) as btc:
                client = clientFromString(
                    self.reactor,
                    "tcp:host=localhost:port=9050"
                )
                ep = yield TCPHiddenServiceEndpoint.system_tor(self.reactor,
                                                               client, 80)
                port = yield ep.listen(NoOpProtocolFactory())
                toa = port.getHost()
                self.assertTrue(hasattr(toa, 'onion_uri'))
                self.assertTrue(hasattr(toa, 'onion_port'))
                port.startListening()
                str(port)
                port.tor_config
                # system_tor should be connecting to a running one,
                # *not* launching a new one.
                self.assertFalse(launch_mock.called)
    def test_both_unsided(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())
        a2 = yield connectProtocol(ep, Accumulator())

        token1 = b"\x00" * 32
        a1.transport.write(b"please relay " + hexlify(token1) + b"\n")
        a2.transport.write(b"please relay " + hexlify(token1) + b"\n")

        # a correct handshake yields an ack, after which we can send
        exp = b"ok\n"
        yield a1.waitForBytes(len(exp))
        self.assertEqual(a1.data, exp)
        s1 = b"data1"
        a1.transport.write(s1)

        exp = b"ok\n"
        yield a2.waitForBytes(len(exp))
        self.assertEqual(a2.data, exp)

        # all data they sent after the handshake should be given to us
        exp = b"ok\n" + s1
        yield a2.waitForBytes(len(exp))
        self.assertEqual(a2.data, exp)

        a1.transport.loseConnection()
        a2.transport.loseConnection()
    def test_impatience_new_slow(self):
        ep = clientFromString(reactor, self.transit)
        a1 = yield connectProtocol(ep, Accumulator())
        # For full coverage, we need dataReceived to see a particular framing
        # of these two pieces of data, and ITCPTransport doesn't have flush()
        # (which probably wouldn't work anyways). For now, force a 100ms
        # stall between the two writes. I tried setTcpNoDelay(True) but it
        # didn't seem to help without the stall. The long-term fix is to
        # rewrite dataReceived() to remove the multiple "impatient"
        # codepaths, deleting the particular clause that this test exercises,
        # then remove this test.

        token1 = b"\x00" * 32
        side1 = b"\x01" * 8
        # sending too many bytes is impatience.
        a1.transport.write(b"please relay " + hexlify(token1) + b" for side " +
                           hexlify(side1) + b"\n")

        d = defer.Deferred()
        reactor.callLater(0.1, d.callback, None)
        yield d

        a1.transport.write(b"NOWNOWNOW")

        exp = b"impatient\n"
        yield a1.waitForBytes(len(exp))
        self.assertEqual(a1.data, exp)

        a1.transport.loseConnection()
def main(reactor, args):
    endpoint_str = args.endpoint
    e = clientFromString(reactor, endpoint_str)
    d = connectProtocol(e, LDAPClient())
    d.addCallback(onConnect, args)
    d.addErrback(onError)
    return d
def main(reactor, args):
    vhost = args.vhost
    user = args.user
    passwd_file = args.passwd_file
    if passwd_file is None:
        passwd = 'guest'
    else:
        passwd = passwd_file.read().rstrip("\n\r")
        passwd_file.close()
    spec_path = os.path.join(
        os.path.dirname(__file__),
        'spec/amqp0-9-1.stripped.xml')
    spec = txamqp.spec.load(spec_path)
    params = {
        'creds': (user, passwd),
        'exchange': args.exchange,
        'content': args.msg_file.read(),
        'route_key': args.route_key,
    }
    endpoint_s = args.endpoint 
    e = clientFromString(reactor, endpoint_s)
    delegate = TwistedDelegate()
    amqp_protocol = AMQClient(            
        delegate=delegate,
        vhost=vhost,      
        spec=spec)       
    d = connectProtocol(e, amqp_protocol)
    d.addCallback(on_amqp_connect, params)
    return d
Exemple #54
0
def main(reactor, procName, *args):
    clientEndpoints = {}
    for k, v in os.environ.iteritems():
        _, _, clientName = k.partition('client_endpoint_')
        if clientName:
            clientEndpoints[clientName] = clientFromString(reactor, v)
    if not clientEndpoints:
        raise ValueError("no client endpoints detected in the environment")

    plugins = [pluginClass(clientEndpoints)
               for pluginClass in sorted(pluginClasses, key=nameLength, reverse=True)]

    if args == ('suggest',):
        suggestions = []
        for plugin in plugins:
            suggestions.extend(plugin.name + arg for arg in plugin.suggest())
        print '\n'.join(suggestions)
        return defer.succeed(None)

    procName = os.path.basename(procName)
    for plugin in plugins:
        _, foundPluginName, arg = procName.partition(plugin.name)
        if not foundPluginName:
            continue
        command = 'fetch' if not args else args[0]
        method = getattr(plugin, 'command_' + command, None)
        if not method:
            raise ValueError("%r plugin can't handle the command %r" % (plugin.name, command))
        return defer.maybeDeferred(method, arg)

    raise ValueError("no plugin was found with the name %r" % (procName,))
Exemple #55
0
    def _connect(self):
        deferreds = []

        for i, remote in enumerate(self.remotes):
            d = defer.Deferred()
            deferreds.append(d)

            factory = vnc_client.client_factory(d, self.error_buffer)
            factory.rewarder_session = self
            factory.label = 'vnc:{}:{}'.format(i, remote)
            endpoint = endpoints.clientFromString(reactor, 'tcp:' + remote)

            def success(i):
                logger.info('[%s] VNC connection established', factory.label)

            def fail(reason):
                reason = error.Error('[{}] Connection failed: {}'.format(
                    factory.label, reason.value))
                try:
                    d.errback(utils.format_error(reason))
                except defer.AlreadyCalledError:
                    pass

            endpoint.connect(factory).addCallback(success).addErrback(fail)

        d = defer.DeferredList(deferreds, fireOnOneErrback=True)

        def success(results):
            # Store the _clients list when connected
            self._clients = [client for success, client in results]

        d.addCallback(success)
        return d
Exemple #56
0
 def main(self, reactor, description):
     endpoint = endpoints.clientFromString(reactor, description)
     factory = AkumaBotFactory(self.config)
     d = endpoint.connect(factory)
     d.addCallback(self.got_protocol)
     d.addCallback(lambda protocol: protocol.deferred)
     return d
Exemple #57
0
    def test_startServer(self):
        """
        Should call twisted.internet.endpoints.serverFromString and hook that
        up to the factory
        """
        h = Hub()
        h.remote_echo = lambda x: x
        h.startServer(h.getPBServerFactory(), 'tcp:10999')
        
        # connect to it
        self.clientPort = None
        client = clientFromString(reactor, 'tcp:host=127.0.0.1:port=10999')
        factory = pb.PBClientFactory()
        d = client.connect(factory)
        
        def saveClient(clientPort):
            self.clientPort = clientPort

        d.addCallback(saveClient)
        d.addCallback(lambda ign: factory.getRootObject())
        d.addCallback(lambda obj: obj.callRemote('echo', 'foo'))
        d.addCallback(lambda res: self.assertEqual(res, 'foo'))
        d.addCallback(lambda ign: self.clientPort.transport.loseConnection())
        d.addCallback(lambda ign: h.stopServer('tcp:10999'))
        return d
 def _connect_to_daemon(self):
     """Connect to the daemon so that we can receive events."""
     description = 'unix:path=%s' % DAEMON_SOCKET
     client = endpoints.clientFromString(reactor, description)
     self._protocol = yield client.connect(self._factory)
     # add the user with no paths
     yield self._protocol.add_user([])
Exemple #59
0
 def buildClientEndpoint(self, params):
     if not params.get('endpoint'):
         ep = "tcp:host=%s:port=%s" % (params['host'], params['port'])
     else:
         assert not params.get('host')
         assert not params.get('port')
         ep = params['endpoint']
     return endpoints.clientFromString(reactor, ep)
Exemple #60
0
def main(reactor):
    log.startLogging(sys.stdout)
    endpoint_str = "tcp:host=localhost:port=8080"
    e = clientFromString(reactor, endpoint_str)
    d = connectProtocol(e, LDAPClient())
    d.addCallback(onConnect)
    d.addErrback(onError, reactor)
    return d