13 pendingEvents
= [] #XXX This tric. should become standard
14 TorControl
._event
_handler
= pendingEvents
.append
15 TorControl
.set_events(s
,
16 [TorControl
.EVENT_TYPE
.CIRCSTATUS
,
17 TorControl
.EVENT_TYPE
.STREAMSTATUS
])
18 TorControl
.set_option(s
,"__LeaveStreamsUnattached 1")
24 _
, tp
, body
= TorControl
.receive_message(s
)
25 if tp
== TorControl
.MSG_TYPE
.EVENT
:
30 assert name
.endswith(".path")
31 items
= name
.split(".")
36 path
= items
[-(2+n
):-2]
39 return path
,".".join(host
)
41 def handleEvent(s
,body
):
42 event
, args
= TorControl
.unpack_event(body
)
43 if event
== TorControl
.EVENT_TYPE
.STREAMSTATUS
:
44 status
, ident
, target
= args
45 print "Got stream event:",TorControl
.STREAM_STATUS
.nameOf
[status
],\
47 if status
in (TorControl
.STREAM_STATUS
.NEW_CONNECT
,
48 TorControl
.STREAM_STATUS
.NEW_RESOLVE
):
49 target
,port
=target
.split(":")
50 if not target
.endswith(".path"):
51 TorControl
.attach_stream(s
, ident
, 0)
53 path
,host
= parsePath(target
)
54 #XXXX Don't launch so many circuits!
55 streams
[ident
] = path
,host
56 circid
= TorControl
.extend_circuit(s
, 0, path
)
57 circuits
[circid
] = path
58 elif status
== TorControl
.STREAM_STATUS
.DETACHED
:
59 if not streams
.has_key(ident
):
60 TorControl
.attach_stream(s
, ident
, 0)
62 TorControl
.close_stream(s
, ident
, 1)
63 elif event
== TorControl
.EVENT_TYPE
.CIRCSTATUS
:
64 status
, ident
, path
= args
65 print "Got circuit event",TorControl
.CIRC_STATUS
.nameOf
[status
],\
67 if not circuits
.has_key(ident
):
69 if status
in (TorControl
.CIRC_STATUS
.CLOSED
,
70 TorControl
.CIRC_STATUS
.FAILED
):
72 elif status
== TorControl
.CIRC_STATUS
.BUILT
:
77 ids
= [ streamID
for (streamID
, (path
,host
)) in streams
.items()
78 if path
== circuits
[ident
] ]
82 _
,host
= streams
[streamID
]
83 TorControl
.redirect_stream(s
, streamID
, host
)
84 TorControl
.attach_stream(s
, streamID
, ident
)
85 #XXXX Don't do this twice.
87 TorControl
.close_stream(s
, streamID
, 1)
93 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
94 s
.connect(("127.0.0.1", 9051))
95 TorControl
.authenticate(s
)
98 if __name__
== '__main__':