-
Notifications
You must be signed in to change notification settings - Fork 71
/
launch_tor.py
executable file
·58 lines (49 loc) · 1.84 KB
/
launch_tor.py
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
52
53
54
55
56
57
58
from __future__ import print_function
"""
Launch a private Tor instance.
"""
import sys
import txtorcon
from twisted.web.client import readBody
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks
@react
@inlineCallbacks
def main(reactor):
# note that you can pass a few options as kwargs
# (e.g. data_directory=, or socks_port= ). For other torrc
# changes, see below.
tor = yield txtorcon.launch(
reactor,
data_directory="./tordata",
stdout=sys.stdout,
socks_port='unix:/tmp/tor2/socks',
)
# tor = yield txtorcon.connect(
# reactor,
# clientFromString(reactor, "unix:/var/run/tor/control"),
# )
print("Connected to Tor version '{}'".format(tor.protocol.version))
state = yield tor.create_state()
# or state = yield txtorcon.TorState.from_protocol(tor.protocol)
print("This Tor has PID {}".format(state.tor_pid))
print("This Tor has the following {} Circuits:".format(len(state.circuits)))
for c in state.circuits.values():
print(" {}".format(c))
agent = tor.web_agent(u'unix:/tmp/tor2/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:]))
# SOCKSPort is 'really' a list of SOCKS ports in Tor now, so we
# have to set it to a list ... :/
print("Changing our config (SOCKSPort=9876)")
#tor.config.SOCKSPort = ['unix:/tmp/foo/bar']
tor.config.SOCKSPort = ['9876']
yield tor.config.save()
print("Querying to see it changed:")
socksport = yield tor.protocol.get_conf("SOCKSPort")
print("SOCKSPort", socksport)