77from bwscanner .logger import log
88
99
10+ def is_valid_exit (relay ):
11+ """
12+ Check that has the correct flags and exit policy for exiting
13+
14+ TODO: Check the exit policy
15+ """
16+ is_exit = ('exit' in relay .flags and 'badexit' not in relay .flags )
17+ return is_exit and 'authority' not in relay .flags
18+
19+
20+ def random_path_to_exit (exit_relay , relays ):
21+ """
22+ Choose a random path to the specified exit relay
23+ """
24+ # Sample an extra relay in case one of the choices is the exit.
25+ candidate_relays = random .sample (relays , 3 )
26+ if exit_relay in candidate_relays :
27+ candidate_relays .remove (exit_relay )
28+ return candidate_relays [0 :2 ] + [exit_relay ]
29+
30+
1031class CircuitGenerator (object ):
1132 def __init__ (self , state ):
1233 self .state = state
1334 self .relays = list (set (state .routers .values ()))
14- self .exits = [relay for relay in self .relays if self . is_valid_exit (relay )]
35+ self .exits = [relay for relay in self .relays if is_valid_exit (relay )]
1536
1637 def __iter__ (self ):
1738 return self
1839
1940 def next (self ):
2041 raise NotImplementedError
2142
22- @staticmethod
23- def is_valid_exit (relay ):
24- """
25- Check that has the correct flags and exit policy for exiting
26-
27- TODO: Check the exit policy
28- """
29- is_exit = ('exit' in relay .flags and 'badexit' not in relay .flags )
30- return is_exit and 'authority' not in relay .flags
31-
3243
3344class ExitScan (CircuitGenerator ):
3445 """
@@ -48,11 +59,7 @@ def circuit_generator():
4859 to use the exit relay more than once.
4960 """
5061 for exit_relay in self .exits :
51- # Sample an extra relay in case one of the choices is the exit.
52- candidate_relays = random .sample (self .relays , 3 )
53- if exit_relay in candidate_relays :
54- candidate_relays .remove (exit_relay )
55- yield candidate_relays [0 :2 ] + [exit_relay ]
62+ yield random_path_to_exit (exit_relay , self .relays )
5663
5764 self ._circgen = circuit_generator ()
5865
0 commit comments