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) print("Response has {} bytes".format(resp.length)) body = yield readBody(resp) print("received body ({} bytes)".format(len(body))) print("{}\n[...]\n{}\n".format(body[:200], body[-200:]))
def main(reactor): # "onion:" is for Tor Onion Services, and the only required # argument is the public port we advertise. You can pass # "controlPort=9051" for example, to connect to a system Tor # (accepts paths, too, e.g. "controlPort=/var/run/tor/control") # ep = endpoints.serverFromString(reactor, "onion:80:controlPort=9151") # to re-create a previous hidden-service, pass the private key # blob you retrieved earler via port.getHost().onion_key like so: # ep = endpoints.serverFromString(reactor, "onion:80:privateKey=<keyblob>") # Beware that you have to escape the ":" after RSA1024 because of # reasons (the endpoint syntax uses them). For this reason, you # can omit the "RSA1024:" part if you wish. # ep = endpoints.serverFromString(reactor, "onion:80") # this one should be "nyfnesplt3f5nvn3.onion" print(default_control_port()) ep = endpoints.serverFromString(reactor, "onion:80:controlPort={port}:privateKey=RSA1024\\:MIICWwIBAAKBgQDml0L1Btxe1QIs88mvKvcgAEd19bUorzMndfXXBbPt2y1lTjm+vGldJRCXb/RArfCb9F2q7IWL4ScuJBiUCqpKVG2aGK8yOxw4c5WKvnLW8MRf5+jAPlR3h7idBdrVGCY/9gXf9JzWfpIhMfFidM4Xq6VpzMvignss6FB6i9zhOwIDAQABAoGAXuWjVamUKabp9UwDFYbOGypiPmZ3Pp4TpErEeNBNAzdvUEDIPPnXNtEZKemWEMREwDnqDny2XSG0+SU7xDk7aQGTFxipo+NAl18QMW2XcBjWrIG5P0L9E+j58k5Nq6EEaMQ8G8X3hsnX7EwRqnJYOwUWUQ4emi6TvNScSMS251kCQQD6KJXltkSfwU3d5hOh37x3pOp4ZcpI6eKwwfgqP+1pVfOwjvXfLqLgLRf9+NtmG+cU5HRDwmf9rbJNCOE++11HAkEA6/mz/L+54NRk9tPN4vYfn969v7fz9CQndQUsTTrtArqtjg7baKts3ndagj+/itJfY6qV/OonN9XdntQXTWWGbQJAY244TmrJEfqieZ2WlhO49JFPRPWolpyoJvuiKSDpu6GXT8ky/zepM5OY4rDEe+yBR/OaJsihztn4cdgit4bvxwJAFsnZiOoXEFBSo8eWlXmBWlYPawlfxM8NBG8IdTjglKfkhNiIddZAQEe0dOmlHMnuLljV/UO7n9fGfEUtLutEDQJAC3c9gSe2of41TaZhQ+aHzQ8E9cs7fg3gXXUgWlocQK6fYq+tC0CF7dDmydShF8vI8oEcgJGhgtUAXQDwH9eD0A==".format(port=default_control_port())) 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
def main(reactor): # "onion:" is for Tor Onion Services, and the only required # argument is the public port we advertise. You can pass # "controlPort=9051" for example, to connect to a system Tor # (accepts paths, too, e.g. "controlPort=/var/run/tor/control") # ep = endpoints.serverFromString(reactor, "onion:80:controlPort=9151") # to re-create a previous hidden-service, pass the private key # blob you retrieved earler via port.getHost().onion_key like so: # ep = endpoints.serverFromString(reactor, "onion:80:privateKey=<keyblob>") # Beware that you have to escape the ":" after RSA1024 because of # reasons (the endpoint syntax uses them). For this reason, you # can omit the "RSA1024:" part if you wish. # ep = endpoints.serverFromString(reactor, "onion:80") # this one should be "nyfnesplt3f5nvn3.onion" ep = endpoints.serverFromString(reactor, "onion:80:controlPort={port}:privateKey=RSA1024\\:MIICWwIBAAKBgQDml0L1Btxe1QIs88mvKvcgAEd19bUorzMndfXXBbPt2y1lTjm+vGldJRCXb/RArfCb9F2q7IWL4ScuJBiUCqpKVG2aGK8yOxw4c5WKvnLW8MRf5+jAPlR3h7idBdrVGCY/9gXf9JzWfpIhMfFidM4Xq6VpzMvignss6FB6i9zhOwIDAQABAoGAXuWjVamUKabp9UwDFYbOGypiPmZ3Pp4TpErEeNBNAzdvUEDIPPnXNtEZKemWEMREwDnqDny2XSG0+SU7xDk7aQGTFxipo+NAl18QMW2XcBjWrIG5P0L9E+j58k5Nq6EEaMQ8G8X3hsnX7EwRqnJYOwUWUQ4emi6TvNScSMS251kCQQD6KJXltkSfwU3d5hOh37x3pOp4ZcpI6eKwwfgqP+1pVfOwjvXfLqLgLRf9+NtmG+cU5HRDwmf9rbJNCOE++11HAkEA6/mz/L+54NRk9tPN4vYfn969v7fz9CQndQUsTTrtArqtjg7baKts3ndagj+/itJfY6qV/OonN9XdntQXTWWGbQJAY244TmrJEfqieZ2WlhO49JFPRPWolpyoJvuiKSDpu6GXT8ky/zepM5OY4rDEe+yBR/OaJsihztn4cdgit4bvxwJAFsnZiOoXEFBSo8eWlXmBWlYPawlfxM8NBG8IdTjglKfkhNiIddZAQEe0dOmlHMnuLljV/UO7n9fGfEUtLutEDQJAC3c9gSe2of41TaZhQ+aHzQ8E9cs7fg3gXXUgWlocQK6fYq+tC0CF7dDmydShF8vI8oEcgJGhgtUAXQDwH9eD0A==".format(port=default_control_port())) def on_progress(percent, tag, msg): print('%03d: %s' % (percent, msg)) txtorcon.IProgressProvider(ep).add_progress_listener(on_progress) try: port = yield ep.listen(server.Site(Simple())) except error.ConnectionRefusedError: print("Couldn't connect; is Tor listening on localhost:{}?".format(default_control_port())) defer.returnValue(1) print("Site listening: {}".format(port.getHost())) print("Private key:\n{}".format(port.getHost().onion_key)) yield defer.Deferred() # wait forever
def main(reactor): # use port 9051 for system tor instances, or: # 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:", tor) state = yield tor.create_state() socks = tor.config.socks_endpoint(reactor) # create a custom circuit; in this case we're just letting Tor # decide the path but you *can* select a path (again: for advanced # use cases that will probably de-anonymize you) circ = yield state.build_circuit() print("Building a circuit:", circ) # at this point, the circuit will be "under way" but may not yet # be in BUILT state -- and hence usable. So, we wait. yield circ.when_built() print("Circuit is ready:", circ) if True: # create a web.Agent that will use this circuit (or fail) agent = circ.web_agent(reactor, socks) uri = 'https://www.torproject.org' print("Downloading {}".format(uri)) resp = yield agent.request('GET', uri) print("Response has {} bytes".format(resp.length)) body = yield readBody(resp) print("received body ({} bytes)".format(len(body))) print("{}\n[...]\n{}\n".format(body[:200], body[-200:])) if True: # make a plain TCP connection to a thing ep = circ.stream_via(reactor, 'torproject.org', 80) d = Deferred() class ToyWebRequestProtocol(Protocol): def connectionMade(self): print("Connected via {}".format(self.transport.getHost())) self.transport.write('GET http://torproject.org/ HTTP/1.1\r\n' 'Host: torproject.org\r\n' '\r\n') def dataReceived(self, d): print(" received {} bytes".format(len(d))) def connectionLost(self, reason): print("disconnected") d.callback(reason) proto = yield ep.connect(Factory.forProtocol(ToyWebRequestProtocol)) yield d print("All done, closing the circuit") yield circ.close()
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(), )) # add our client-side authentication tokens for the service. # You can create these by running the # web_onion_service_ephemeral_auth.py in a separate shell and # using either the "alice" or "bob" token in this client. token = u"0GaFhnbunp0TxZuBhejhxg" # alice's token onion_uri = b"FIXME.onion" if b'FIXME' in onion_uri: print("Please edit to the correct .onion URI") return yield tor.add_onion_authentication(onion_uri, token) try: # do the Web request as with any other agent = tor.web_agent() uri = b'http://{}/'.format(onion_uri) print("Downloading {}".format(uri)) resp = yield agent.request(b'GET', uri) print("Response has {} bytes".format(resp.length)) body = yield readBody(resp) print(body) finally: # if you're using python3, see the example that uses async # context-managers to do this more cleanly. yield tor.remove_onion_authentication(onion_uri)
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
def main(reactor): ep = TCP4ClientEndpoint(reactor, '127.0.0.1', default_control_port()) # ep = UNIXClientEndpoint(reactor, '/var/run/tor/control') tor = yield txtorcon.connect(reactor, ep) print("Connected:", tor) resp = yield treq.get( 'https://www.torproject.org:443', agent=tor.web_agent(), ) print("Retrieving {} bytes".format(resp.length)) data = yield resp.text() print("Got {} bytes:\n{}\n[...]{}".format( len(data), data[:120], data[-120:], ))
def test_env_var(self, fake_os): fake_os.environ = dict(TX_CONTROL_PORT=1234) p = default_control_port() self.assertEqual(p, 1234)
def test_no_env_var(self): p = default_control_port() self.assertEqual(p, 9151)
def main(reactor): # use port 9051 for system tor instances, or: # 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:", tor) config = yield tor.get_config() state = yield tor.create_state() socks = config.socks_endpoint(reactor) # create a custom circuit; in this case we're just letting Tor # decide the path but you *can* select a path (again: for advanced # use cases that will probably de-anonymize you) circ = yield state.build_circuit() print("Building a circuit:", circ) # at this point, the circuit will be "under way" but may not yet # be in BUILT state -- and hence usable. So, we wait. (Just for # demo purposes: the underlying connect will wait too) yield circ.when_built() print("Circuit is ready:", circ) if True: # create a web.Agent that will use this circuit (or fail) agent = circ.web_agent(reactor, socks) uri = 'https://www.torproject.org' print("Downloading {}".format(uri)) resp = yield agent.request('GET', uri) print("Response has {} bytes".format(resp.length)) body = yield readBody(resp) print("received body ({} bytes)".format(len(body))) print("{}\n[...]\n{}\n".format(body[:200], body[-200:])) if True: # make a plain TCP connection to a thing ep = circ.stream_via(reactor, 'torproject.org', 80, config.socks_endpoint(reactor)) d = Deferred() class ToyWebRequestProtocol(Protocol): def connectionMade(self): print("Connected via {}".format(self.transport.getHost())) self.transport.write( 'GET http://torproject.org/ HTTP/1.1\r\n' 'Host: torproject.org\r\n' '\r\n' ) def dataReceived(self, d): print(" received {} bytes".format(len(d))) def connectionLost(self, reason): print("disconnected: {}".format(reason.value)) d.callback(None) proto = yield ep.connect(Factory.forProtocol(ToyWebRequestProtocol)) yield d print("All done, closing the circuit") yield circ.close()