1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
from __future__ import print_function
from twisted.internet import protocol, reactor, endpoints
# like the echo-server example on the front page of
# https://twistedmatrix.com except this makes a Tor onion-service
# that's an echo server.
#
# Note the *only* difference is the string we give to "serverFromString"!
class Echo(protocol.Protocol):
def connectionMade(self):
print("Connection from {}".format(self.transport.getHost()))
def dataReceived(self, data):
print("echoing: '{}'".format(repr(data)))
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
print("Starting Tor, and onion service (can take a few minutes)")
d = endpoints.serverFromString(reactor, "onion:1234").listen(EchoFactory())
def listening(port):
# port is a Twisted IListeningPort
print("Listening on: {} port 1234".format(port.getHost()))
print("Try: torsocks telnet {} 1234".format(port.getHost().onion_uri))
d.addCallback(listening)
reactor.run()
|