-
Notifications
You must be signed in to change notification settings - Fork 101
/
stem_adv.py
66 lines (52 loc) · 2.32 KB
/
stem_adv.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
59
60
61
62
63
64
65
66
from argparse import ArgumentParser
from stem.control import Controller
from stem import CircStatus
from tbselenium.tbdriver import TorBrowserDriver
import tbselenium.common as cm
from tbselenium.utils import launch_tbb_tor_with_stem
from selenium.webdriver.common.utils import free_port
import tempfile
from os.path import join
def print_tor_circuits(controller):
"""Print built Tor circuits using Stem.
From https://stem.torproject.org/tutorials/examples/list_circuits.html
"""
for circ in sorted(controller.get_circuits()):
if circ.status != CircStatus.BUILT:
continue
print("\nCircuit %s (%s)" % (circ.id, circ.purpose))
for i, entry in enumerate(circ.path):
div = '+' if (i == len(circ.path) - 1) else '|'
fingerprint, nickname = entry
desc = controller.get_network_status(fingerprint, None)
address = desc.address if desc else 'unknown'
print(" %s- %s (%s, %s)" % (div, fingerprint, nickname, address))
def launch_tb_with_custom_stem(tbb_dir):
socks_port = free_port()
control_port = free_port()
tor_data_dir = tempfile.mkdtemp()
tor_binary = join(tbb_dir, cm.DEFAULT_TOR_BINARY_PATH)
print("SOCKS port: %s, Control port: %s" % (socks_port, control_port))
torrc = {'ControlPort': str(control_port),
'SOCKSPort': str(socks_port),
'DataDirectory': tor_data_dir}
tor_process = launch_tbb_tor_with_stem(tbb_path=tbb_dir, torrc=torrc,
tor_binary=tor_binary)
with Controller.from_port(port=control_port) as controller:
controller.authenticate()
with TorBrowserDriver(tbb_dir, socks_port=socks_port,
control_port=control_port,
tor_cfg=cm.USE_STEM) as driver:
driver.load_url("https://check.torproject.org", wait_on_page=3)
print(driver.find_element_by("h1.on").text)
print(driver.find_element_by(".content > p").text)
print_tor_circuits(controller)
tor_process.kill()
def main():
desc = "Use TorBrowserDriver with Stem"
parser = ArgumentParser(description=desc)
parser.add_argument('tbb_path')
args = parser.parse_args()
launch_tb_with_custom_stem(args.tbb_path)
if __name__ == '__main__':
main()