|
6 | 6 | from bwscanner.logger import log |
7 | 7 |
|
8 | 8 |
|
9 | | -@implementer(IStreamAttacher) |
10 | | -class SOCKSClientStreamAttacher(CircuitListenerMixin, StreamListenerMixin): |
11 | | - """ |
12 | | - An attacher that builds a chosen path for a client identified by |
13 | | - its source port and ip address. |
14 | | - """ |
15 | | - |
16 | | - def __init__(self, state): |
17 | | - """ |
18 | | - Instantiates a SOCKSClientStreamAttacher with a |
19 | | - txtorcon.torstate.TorState instance. |
20 | | - """ |
21 | | - self.state = state |
22 | | - self.waiting_circuits = {} |
23 | | - self.expected_streams = {} |
24 | | - self.state.add_stream_listener(self) |
25 | | - self.state.add_circuit_listener(self) |
26 | | - |
27 | | - def create_circuit(self, host, port, path, using_guards=False): |
28 | | - """ |
29 | | - Specify the path for streams created on a specific client |
30 | | - SOCKS connection. |
31 | | -
|
32 | | - Returns a deferred that calls back with the constructed circuit |
33 | | - or errs back with a failure instance. |
34 | | - """ |
35 | | - circ_deferred = defer.Deferred() |
36 | | - key = (str(host), int(port)) |
37 | | - self.expected_streams[key] = circ_deferred |
38 | | - |
39 | | - def add_to_waiting(circ): |
40 | | - self.waiting_circuits[circ.id] = (circ, circ_deferred) |
41 | | - return circ |
42 | | - |
43 | | - circuit_build = self.state.build_circuit( |
44 | | - path, using_guards=using_guards) |
45 | | - circuit_build.addCallback(add_to_waiting) |
46 | | - return circ_deferred |
47 | | - |
48 | | - def attach_stream(self, stream, _): |
49 | | - """ |
50 | | - Attaches a NEW stream to the circuit created for it by matching the |
51 | | - source address and source port of the SOCKS client connection to the |
52 | | - corresponding circuit in the expected_streams dictionary. |
53 | | -
|
54 | | - Returns a deferred that calls back with the appropriate circuit, |
55 | | - or None if there is no matching entry. |
56 | | -
|
57 | | - Note, Tor can be configured to leave streams unattached by setting |
58 | | - the "__LeaveStreamsUnattached" torrc option to "1". |
59 | | - """ |
60 | | - try: |
61 | | - key = (str(stream.source_addr), int(stream.source_port)) |
62 | | - return self.expected_streams.pop(key) |
63 | | - except KeyError: |
64 | | - # We didn't expect this stream, so let Tor handle it |
65 | | - return None |
66 | | - |
67 | | - def circuit_built(self, circuit): |
68 | | - """ |
69 | | - Calls back the deferred awaiting the circuit build with the |
70 | | - circuit object. |
71 | | - """ |
72 | | - if circuit.purpose != "GENERAL": |
73 | | - return |
74 | | - try: |
75 | | - (_, circ_deferred) = self.waiting_circuits.pop(circuit.id) |
76 | | - circ_deferred.callback(circuit) |
77 | | - except KeyError: |
78 | | - pass |
79 | | - |
80 | | - def circuit_failed(self, circuit, **kw): |
81 | | - """ |
82 | | - Calls the errback of the deferred waiting the circuit build if the |
83 | | - circuit build failed. The failure reason is contained in the circuit |
84 | | - object. The corresponding state in waiting_circuits is removed. |
85 | | -
|
86 | | - If the circuit failure did not correspond to a circuit requested |
87 | | - by create_circuit, it is ignored. |
88 | | - """ |
89 | | - try: |
90 | | - (circ, circ_deferred) = self.waiting_circuits.pop(circuit.id) |
91 | | - circ_deferred.errback(circ) |
92 | | - except KeyError: |
93 | | - pass |
94 | | - |
95 | | - |
96 | 9 | class StreamClosedListener(StreamListenerMixin): |
97 | 10 | """ |
98 | 11 | Closes the contained circuit if the listened stream closes. |
|
0 commit comments