How to use the txtorcon.connect function in txtorcon

To help you get started, we’ve selected a few txtorcon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github meejah / txtorcon / test / test_controller.py View on Github external
def test_connect_multiple_endpoints_error(self, fake_cfg):
        transport = Mock()
        reactor = FakeReactor(self, transport, lambda: None)
        ep0 = Mock()

        def boom(*args, **kw):
            raise RuntimeError("the bad thing")
        ep0.connect = boom
        directlyProvides(ep0, IStreamClientEndpoint)
        with self.assertRaises(RuntimeError) as ctx:
            yield connect(reactor, ep0)
        self.assertEqual("the bad thing", str(ctx.exception))
github meejah / txtorcon / test / test_controller.py View on Github external
def test_connect_success(self, fake_cfg):
        transport = Mock()
        reactor = FakeReactor(self, transport, lambda: None)
        torcfg = Mock()
        fake_cfg.from_protocol = Mock(return_value=torcfg)
        ep0 = Mock()
        proto = object()
        torcfg.protocol = proto
        ep0.connect = Mock(return_value=proto)
        directlyProvides(ep0, IStreamClientEndpoint)

        ans = yield connect(reactor, [ep0])
        cfg = yield ans.get_config()
        self.assertEqual(cfg, torcfg)
        self.assertEqual(ans.protocol, proto)
github meejah / txtorcon / examples / stem_relay_descriptor.py View on Github external
def main(reactor):
    tor = yield txtorcon.connect(reactor)

    or_nickname = "moria1"
    print("Trying to get decriptor information about '{}'".format(or_nickname))
    # If the fingerprint is used in place of nickname then, desc/id/
    # should be used.
    try:
        descriptor_info = yield tor.protocol.get_info('desc/name/' + or_nickname)
    except txtorcon.TorProtocolError:
        print("No information found. Enable descriptor downloading by setting:")
        print("  UseMicrodescritors 0")
        print("In your torrc")
        raise SystemExit(1)

    descriptor_info = descriptor_info.values()[0]
    relay_info = RelayDescriptor(descriptor_info)
    print("The relay's fingerprint is: {}".format(relay_info.fingerprint))
github meejah / txtorcon / examples / resolve.py View on Github external
def main(reactor):
    ep = TCP4ClientEndpoint(reactor, "localhost", 9051)
    print("Connecting via '{}'".format(ep))
    tor = yield txtorcon.connect(reactor, ep)
    print("Connected to tor:", tor)

    for uri in ['torproject.org']:
        print("RESOLVE '{}'...".format(uri))
        answer = yield tor.dns_resolve(uri)
        print("'{}' resolves to '{}'".format(uri, answer))

        print("RESOLVE_PTR '{}'".format(answer))
        rev = yield tor.dns_resolve_ptr(answer)
        print("'{}' reverses to '{}'".format(answer, rev))
github meejah / txtorcon / examples / web_onion_service_prop224_endpoints_file.py View on Github external
def main(reactor):
    tor = yield txtorcon.connect(
        reactor,
        endpoints.TCP4ClientEndpoint(reactor, "localhost", 9251),
    )
    print(default_control_port())
    ep = tor.create_filesystem_onion_endpoint(80, "./test_prop224_service", version=3)

    def on_progress(percent, tag, msg):
        print('%03d: %s' % (percent, msg))
    txtorcon.IProgressProvider(ep).add_progress_listener(on_progress)
    print("Note: descriptor upload can take several minutes")

    port = yield ep.listen(server.Site(Simple()))
    print("Site listening: {}".format(port.getHost()))
    print("Private key:\n{}".format(port.getHost().onion_key))
    yield defer.Deferred()  # wait forever
github meejah / txtorcon / examples / web_client.py View on Github external
def main(reactor):
    # use port 9051 for system tor instances, or:
    # ep = UNIXClientEndpoint(reactor, '/var/run/tor/control')
    # ep = UNIXClientEndpoint(reactor, '/var/run/tor/control')
    ep = TCP4ClientEndpoint(reactor, '127.0.0.1', default_control_port())
    tor = yield txtorcon.connect(reactor, ep)
    print("Connected to {tor} via localhost:{port}".format(
        tor=tor,
        port=default_control_port(),
    ))

    # create a web.Agent that will talk via Tor. If the socks port
    # given isn't yet configured, this will do so. It may also be
    # None, which means "the first configured SOCKSPort"
    # agent = tor.web_agent(u'9999')
    agent = tor.web_agent()
    uri = b'http://surely-this-has-not-been-registered-and-is-invalid.com'
    uri = b'https://www.torproject.org'
    uri = b'http://timaq4ygg2iegci7.onion/'  # txtorcon documentation
    print("Downloading {}".format(uri))
    resp = yield agent.request(b'GET', uri)
github AnemoneLabs / unmessage / unmessage / peer.py View on Github external
def display_bootstrap_lines(prog, tag, summary):
                self._notify_bootstrap('{}%: {}'.format(prog, summary))

            self._tor = yield txtorcon.launch(
                self._reactor,
                progress_updates=display_bootstrap_lines,
                data_directory=self._paths.tor_data_dir,
                socks_port=self._port_tor_socks)
        else:
            self._notify_bootstrap('Connecting to existing Tor')

            endpoint = TCP4ClientEndpoint(self._reactor,
                                          HOST,
                                          self._port_tor_control)
            self._tor = yield txtorcon.connect(self._reactor, endpoint)

        self._notify_bootstrap('Controlling Tor process')

        onion_service_string = '{} {}:{}'.format(self._port_local_server,
                                                 self._ip_local_server,
                                                 self._port_local_server)

        if self.onion_service_key:
            args = ([onion_service_string], self.onion_service_key)
            save_key = False
        else:
            args = ([onion_service_string],)
            save_key = True

        self._onion_service = txtorcon.EphemeralHiddenService(*args)
github meejah / txtorcon / examples / web_onion_service_ephemeral_auth.py View on Github external
def main(reactor):
    tor = yield txtorcon.connect(
        reactor,
        endpoints.TCP4ClientEndpoint(reactor, "localhost", 9251),
    )
    ep = tor.create_authenticated_onion_endpoint(
        80,
        auth=AuthBasic([
            ("alice", "0GaFhnbunp0TxZuBhejhxg"),
            "bob",
        ]),
    )

    def on_progress(percent, tag, msg):
        print('%03d: %s' % (percent, msg))
    txtorcon.IProgressProvider(ep).add_progress_listener(on_progress)
    print("Note: descriptor upload can take several minutes")
github meejah / txtorcon / examples / web_onion_service_prop224.py View on Github external
def main(reactor):
    tor = yield txtorcon.connect(
        reactor,
        endpoints.TCP4ClientEndpoint(reactor, "localhost", 9251),
    )
    print("{}".format(tor))
    hs = yield tor.create_filesystem_onion_service(
        [(80, 8787)],
        "./prop224_hs",
        version=3,
    )
    print("{}".format(hs))
    print(dir(hs))

    ep = endpoints.TCP4ServerEndpoint(reactor, 8787, interface="localhost")
    port = yield ep.listen(server.Site(Simple()))
    print("Site listening: {}".format(hs.hostname))
    print("Private key:\n{}".format(hs.private_key))
github crossbario / crossbar / crossbar / common / twisted / endpoint.py View on Github external
def listen(self, proto_factory):
                # we don't care which local TCP port we listen on, but
                # we do need to know it
                local_ep = TCP4ServerEndpoint(reactor, 0, interface="127.0.0.1")
                target_port = yield local_ep.listen(proto_factory)
                tor = yield txtorcon.connect(
                    reactor,
                    tor_control_ep,
                )

                log.info("Creating onion service (descriptor upload can take 30s or more)")
                hs = yield tor.create_onion_service(
                    ports=[
                        (port, target_port.getHost().port),
                    ],
                    private_key=private_key,
                    version=version,
                )

                # if it's new, store our private key
                # XXX better "if private_key is None"?
                if not exists(private_key_fname):