This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathexamples.py
executable file
·127 lines (116 loc) · 4.09 KB
/
examples.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/env python
import torpylle
import sys
import signal
class AlarmException(Exception):
pass
def alarmhandler(signum, frame):
raise AlarmException()
signal.signal(signal.SIGALRM, alarmhandler)
BASE_TIMEOUT = 10
# to run with -i flag on python interpreter
import atexit
import os
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.torpylle_history")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
readline.parse_and_bind('tab: complete')
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
# directory manipulation
print "Adding default directory servers...",
torpylle.add_default_directory_authorities()
print "[OK]"
while True:
try:
signal.alarm(BASE_TIMEOUT)
d = torpylle.random.choice(torpylle.DIRECTORY_SERVERS)
print "Getting consensus from a random server [%s:%d]..." % d.address,
sys.stdout.flush()
d.parse_consensus()
signal.alarm(0)
print "[DONE]"
signal.alarm(BASE_TIMEOUT)
print "Selecting an entry node and getting infos...",
sys.stdout.flush()
# our entry node should be a Guard, Fast and Running node with
# a version compatible with protocol 3.
n1 = torpylle.random.choice(torpylle.search_node(
flags=['Fast', 'Running', 'Guard'],
minversion=torpylle.torminversionproto3))
d.get_node_info(n1)
print "[OK]"
signal.alarm(BASE_TIMEOUT)
print "Connecting to the entry node [%s:%d]..." % torpylle.KNOWN_NODES[n1].address,
sys.stdout.flush()
s = torpylle.TorSocket(torpylle.KNOWN_NODES[n1])
print "[OK]"
print "Initiating connection...",
sys.stdout.flush()
s.init_connection()
print "[OK]"
signal.alarm(0)
print "Entry node certificates:"
for c in s.node.certificates:
print " ", c, s.node.certificates[c].subject[0].value.val, 'issued by', s.node.certificates[c].issuer[0].value.val
print
print "IP Addresses:"
print " ", "I am %s" % s.public_address.Address
print " ", "Peer is %s" % ', '.join([x.Address for x in s.node.addresses])
print
signal.alarm(BASE_TIMEOUT)
print "Creating circuit...",
sys.stdout.flush()
c = s.create(fast=False)
print "[OK]"
signal.alarm(BASE_TIMEOUT)
print "Selecting an exit node and getting infos...",
sys.stdout.flush()
# our exit node shoub be an Exit, Fast and Running node.
n2 = torpylle.random.choice(torpylle.search_node(flags=['Fast', 'Exit', 'Running']))
d.get_node_info(n2)
print "[OK]"
signal.alarm(4 * BASE_TIMEOUT)
print "Extending circuit [%s:%d]..." % torpylle.KNOWN_NODES[n2].address,
sys.stdout.flush()
c.extend(torpylle.KNOWN_NODES[n2])
print "[OK]"
signal.alarm(BASE_TIMEOUT)
print "Resolving www.google.com",
sys.stdout.flush()
rslv = c.resolve("www.google.com")
print "[OK]",
print rslv[0].Address
signal.alarm(2 * BASE_TIMEOUT)
print "Connecting to www.google.com and GETting /",
sys.stdout.flush()
strm = c.connect((rslv[0].Address, 80))
c.send(strm[0], 'GET / HTTP/1.0\r\n\r\n')
data = ''
da = c.recv()[1]
while da:
data += da
da = c.recv()[1]
print "[OK]"
signal.alarm(0)
print data[:80]
if len(data) > 80:
print '[...]'
break
except AlarmException:
print "[TIMEOUT]"
print
except Exception as e:
print "[ERROR] %s" % e.message
if hasattr(torpylle, "errorcell"):
print "Cell:", repr(torpylle.errorcell)
del(torpylle.errorcell)
if hasattr(torpylle, "errorcellclear"):
print "Clear Cell:", repr(torpylle.errorcellclear)
del(torpylle.errorcellclear)
print