Loading sbws/lib/destination.py +0 −75 Original line number Diff line number Diff line import logging import random import time import os from threading import RLock import requests from urllib.parse import urlparse from stem.control import EventType from sbws.globals import DESTINATION_VERIFY_CERTIFICATE import sbws.util.stem as stem_utils import sbws.util.requests as requests_utils from ..globals import MAXIMUM_NUMBER_DESTINATION_FAILURES Loading Loading @@ -153,17 +149,6 @@ class Destination: self.consecutive_failures += 1 self.failed = True def is_usable(self, circ_id, session, cont): ''' Use **connect_to_destination_over_circuit** to determine if this destination is usable and return what it returns. Just a small wrapper. ''' if not isinstance(self.verify, bool): if not os.path.isfile(self.verify): return False, '{} is believed to be a CA bundle file on disk '\ 'but it does not exist'.format(self.verify) return connect_to_destination_over_circuit( self, circ_id, session, cont, self._max_dl) @property def url(self): return self._url.geturl() Loading Loading @@ -208,71 +193,11 @@ class DestinationList: self._cb = circuit_builder self._rl = relay_list self._all_dests = dests # Inially all destionations are usable until proven otherwise. self._usable_dests = self._all_dests self._last_usability_test = 0 self._usability_test_interval = \ conf.getint('destinations', 'usability_test_interval') self._usability_test_timeout = \ conf.getfloat('general', 'http_timeout') self._usability_lock = RLock() @property def functional_destinations(self): return [d for d in self._all_dests if d.is_functional] def _should_perform_usability_test(self): # Until bigger refactor, do not perform usability test. # Every time a measurement is done, it already performs what usability # test does. return False def _perform_usability_test(self): self._usability_lock.acquire() log.debug('Perform usability tests') cont = self._cont timeout = self._usability_test_timeout session = requests_utils.make_session(cont, timeout) usable_dests = [] for dest in self._all_dests: possible_exits = self._rl.exits_not_bad_allowing_port(dest.port) # Keep the fastest 10% of exits, or 3, whichever is larger num_keep = int(max(3, len(possible_exits) * 0.1)) possible_exits = sorted( possible_exits, key=lambda e: e.consensus_bandwidth, reverse=True) exits = possible_exits[0:num_keep] if len(exits) < 1: log.warning("There are no exits to perform usability tests.") continue # Try three times to build a circuit to test this destination circ_id = None for _ in range(0, 3): # Pick a random exit exit = self._rng.choice(exits) circ_id, reason = \ self._cb.build_circuit([None, exit.fingerprint]) if circ_id: break if not circ_id: log.warning('Unable to build a circuit to test the usability ' 'of %s, %s', dest.url, reason) continue log.debug('Built circ %s %s to test usability of %s', circ_id, stem_utils.circuit_str(cont, circ_id), dest.url) is_usable, data = dest.is_usable(circ_id, session, cont) if not is_usable: log.warning(data) self._cb.close_circuit(circ_id) continue assert is_usable log.debug('%s seems usable so we will keep it', dest.url) usable_dests.append(dest) self._cb.close_circuit(circ_id) self._usable_dests = usable_dests self._last_usability_test = time.time() self._usability_lock.release() @staticmethod def from_config(conf, circuit_builder, relay_list, controller): assert 'destinations' in conf Loading tests/integration/lib/test_destination.py +0 −4 Original line number Diff line number Diff line Loading @@ -13,10 +13,6 @@ def test_destination_list_no_usability_test_success( destination_list, error_msg = DestinationList.from_config( conf, cb, rl, persistent_launch_tor ) # Initially all destinations should be "usable". assert destination_list._all_dests == destination_list._usable_dests # Because this is disabled. assert destination_list._should_perform_usability_test() is False # Because there's only 1 destination in conftest, random should return # the same one. assert destination_list.next() == \ Loading tests/integration/lib/test_relayprioritizer.py +0 −1 Original line number Diff line number Diff line Loading @@ -14,7 +14,6 @@ def static_time(value): def _build_result_for_relay(conf, rl, result_type, relay_nick, timestamp): relay = [r for r in rl.relays if r.nickname == relay_nick] print(rl.relays) assert len(relay) == 1 relay = relay[0] other = [r for r in rl.relays if r.nickname != relay_nick][0] Loading Loading
sbws/lib/destination.py +0 −75 Original line number Diff line number Diff line import logging import random import time import os from threading import RLock import requests from urllib.parse import urlparse from stem.control import EventType from sbws.globals import DESTINATION_VERIFY_CERTIFICATE import sbws.util.stem as stem_utils import sbws.util.requests as requests_utils from ..globals import MAXIMUM_NUMBER_DESTINATION_FAILURES Loading Loading @@ -153,17 +149,6 @@ class Destination: self.consecutive_failures += 1 self.failed = True def is_usable(self, circ_id, session, cont): ''' Use **connect_to_destination_over_circuit** to determine if this destination is usable and return what it returns. Just a small wrapper. ''' if not isinstance(self.verify, bool): if not os.path.isfile(self.verify): return False, '{} is believed to be a CA bundle file on disk '\ 'but it does not exist'.format(self.verify) return connect_to_destination_over_circuit( self, circ_id, session, cont, self._max_dl) @property def url(self): return self._url.geturl() Loading Loading @@ -208,71 +193,11 @@ class DestinationList: self._cb = circuit_builder self._rl = relay_list self._all_dests = dests # Inially all destionations are usable until proven otherwise. self._usable_dests = self._all_dests self._last_usability_test = 0 self._usability_test_interval = \ conf.getint('destinations', 'usability_test_interval') self._usability_test_timeout = \ conf.getfloat('general', 'http_timeout') self._usability_lock = RLock() @property def functional_destinations(self): return [d for d in self._all_dests if d.is_functional] def _should_perform_usability_test(self): # Until bigger refactor, do not perform usability test. # Every time a measurement is done, it already performs what usability # test does. return False def _perform_usability_test(self): self._usability_lock.acquire() log.debug('Perform usability tests') cont = self._cont timeout = self._usability_test_timeout session = requests_utils.make_session(cont, timeout) usable_dests = [] for dest in self._all_dests: possible_exits = self._rl.exits_not_bad_allowing_port(dest.port) # Keep the fastest 10% of exits, or 3, whichever is larger num_keep = int(max(3, len(possible_exits) * 0.1)) possible_exits = sorted( possible_exits, key=lambda e: e.consensus_bandwidth, reverse=True) exits = possible_exits[0:num_keep] if len(exits) < 1: log.warning("There are no exits to perform usability tests.") continue # Try three times to build a circuit to test this destination circ_id = None for _ in range(0, 3): # Pick a random exit exit = self._rng.choice(exits) circ_id, reason = \ self._cb.build_circuit([None, exit.fingerprint]) if circ_id: break if not circ_id: log.warning('Unable to build a circuit to test the usability ' 'of %s, %s', dest.url, reason) continue log.debug('Built circ %s %s to test usability of %s', circ_id, stem_utils.circuit_str(cont, circ_id), dest.url) is_usable, data = dest.is_usable(circ_id, session, cont) if not is_usable: log.warning(data) self._cb.close_circuit(circ_id) continue assert is_usable log.debug('%s seems usable so we will keep it', dest.url) usable_dests.append(dest) self._cb.close_circuit(circ_id) self._usable_dests = usable_dests self._last_usability_test = time.time() self._usability_lock.release() @staticmethod def from_config(conf, circuit_builder, relay_list, controller): assert 'destinations' in conf Loading
tests/integration/lib/test_destination.py +0 −4 Original line number Diff line number Diff line Loading @@ -13,10 +13,6 @@ def test_destination_list_no_usability_test_success( destination_list, error_msg = DestinationList.from_config( conf, cb, rl, persistent_launch_tor ) # Initially all destinations should be "usable". assert destination_list._all_dests == destination_list._usable_dests # Because this is disabled. assert destination_list._should_perform_usability_test() is False # Because there's only 1 destination in conftest, random should return # the same one. assert destination_list.next() == \ Loading
tests/integration/lib/test_relayprioritizer.py +0 −1 Original line number Diff line number Diff line Loading @@ -14,7 +14,6 @@ def static_time(value): def _build_result_for_relay(conf, rl, result_type, relay_nick, timestamp): relay = [r for r in rl.relays if r.nickname == relay_nick] print(rl.relays) assert len(relay) == 1 relay = relay[0] other = [r for r in rl.relays if r.nickname != relay_nick][0] Loading