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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
import functools
from os.path import dirname
import sys
from tempfile import mkdtemp
import txtorcon
from twisted.application import service, internet
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.python import log
from twisted.web import static, server
from zope.interface import implements
class TorService(service.Service):
implements(service.IService)
directory = dirname(__file__)
port = 8080
def __init__(self):
self.torfactory = txtorcon.TorProtocolFactory()
self.connection = TCP4ClientEndpoint(reactor, 'localhost', 9052)
self.resource = server.Site(static.File(self.directory))
def startService(self):
service.Service.startService(self)
reactor.listenTCP(self.port, self.resource)
self._bootstrap().addCallback(self._complete)
def _bootstrap(self):
self.config = txtorcon.TorConfig()
self.config.HiddenServices = [
txtorcon.HiddenService(self.config, mkdtemp(),
['%d 127.0.0.1:%d' % (80, self.port)])
]
self.config.save()
return txtorcon.launch_tor(self.config, reactor,
progress_updates=self._updates,
tor_binary='tor')
def _updates(self, prog, tag, summary):
log.msg('%d%%: %s' % (prog, summary))
def _complete(self, proto):
log.msg(self.config.HiddenServices[0].hostname)
application = service.Application("Txtorcon Application")
torservice = TorService()
torservice.setServiceParent(application)
|