def get_circuit_path(self, circ_id): c = self.controller assert stem_utils.is_controller_okay(c) circ = c.get_circuit(circ_id, default=None) if circ is None: return None return [relay[0] for relay in circ.path]
def close_circuit(self, circ_id): c = self.controller if not stem_utils.is_controller_okay(c): return if c.get_circuit(circ_id, default=None): try: c.close_circuit(circ_id) except InvalidArguments: pass self.built_circuits.discard(circ_id)
def __del__(self): c = self.controller if not self.close_circuits_on_exit: return if not stem_utils.is_controller_okay(c): return for circ_id in self.built_circuits: if c.get_circuit(circ_id, default=None): try: c.close_circuit(circ_id) except InvalidArguments: pass self.built_circuits.clear()
def run_speedtest(args, conf): write_start_ts(conf) controller, _ = stem_utils.init_controller( path=conf['tor']['control_socket']) if not controller: controller = stem_utils.launch_tor(conf) else: log.warning( 'Is sbws already running? ' 'We found an existing Tor process at %s. We are not going to ' 'launch Tor, nor are we going to try to configure it to behave ' 'like we expect. This might work okay, but it also might not. ' 'If you experience problems, you should try letting sbws launch ' 'Tor for itself. The ability to use an already running Tor only ' 'exists for sbws developers. It is expected to be broken and may ' 'even lead to messed up results.', conf['tor']['control_socket']) time.sleep(15) assert stem_utils.is_controller_okay(controller) cb = CB(args, conf, controller) rl = RelayList(args, conf, controller) rd = ResultDump(args, conf, end_event) rp = RelayPrioritizer(args, conf, rl, rd) destinations, error_msg = DestinationList.from_config( conf, cb, rl, controller) if not destinations: fail_hard(error_msg) max_pending_results = conf.getint('scanner', 'measurement_threads') pool = Pool(max_pending_results) pending_results = [] while True: for target in rp.best_priority(): log.debug('Measuring %s %s', target.nickname, target.fingerprint[0:8]) callback = result_putter(rd) callback_err = result_putter_error(target) async_result = pool.apply_async( dispatch_worker_thread, [args, conf, destinations, cb, rl, target], {}, callback, callback_err) pending_results.append(async_result) while len(pending_results) >= max_pending_results: time.sleep(5) pending_results = [r for r in pending_results if not r.ready()]
def _build_circuit_impl(self, path): if not valid_circuit_length(path): raise PathLengthException() c = self.controller assert stem_utils.is_controller_okay(c) timeout = self.circuit_timeout fp_path = '[' + ' -> '.join([p[0:8] for p in path]) + ']' log.debug('Building %s', fp_path) for _ in range(0, 3): try: circ_id = c.new_circuit(path, await_build=True, timeout=timeout) except (InvalidRequest, CircuitExtensionFailed, ProtocolError, Timeout) as e: log.warning(e) continue else: return circ_id return None
def _init_relays(self): c = self._controller assert stem_utils.is_controller_okay(c) return [ns for ns in c.get_network_statuses()]