[go: up one dir, main page]

File: hidden_echo.py

package info (click to toggle)
txtorcon 18.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,672 kB
  • sloc: python: 17,521; makefile: 227
file content (36 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (3)
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()