Commit b5d3fb4f authored by juga's avatar juga Committed by Matt Traudt
Browse files

Remove is_controller_ok and log exceptions

parent 43856fdb
...@@ -347,7 +347,6 @@ def run_speedtest(args, conf): ...@@ -347,7 +347,6 @@ def run_speedtest(args, conf):
'exists for sbws developers. It is expected to be broken and may ' 'exists for sbws developers. It is expected to be broken and may '
'even lead to messed up results.', conf['tor']['control_socket']) 'even lead to messed up results.', conf['tor']['control_socket'])
time.sleep(15) time.sleep(15)
assert stem_utils.is_controller_okay(controller)
rl = RelayList(args, conf, controller) rl = RelayList(args, conf, controller)
cb = CB(args, conf, controller, rl) cb = CB(args, conf, controller, rl)
rd = ResultDump(args, conf, end_event) rd = ResultDump(args, conf, end_event)
......
from stem import CircuitExtensionFailed, InvalidRequest, ProtocolError, Timeout from stem import CircuitExtensionFailed, InvalidRequest, ProtocolError, Timeout
from stem import InvalidArguments from stem import InvalidArguments
import random import random
import sbws.util.stem as stem_utils
from .relaylist import Relay from .relaylist import Relay
import logging import logging
...@@ -59,28 +58,30 @@ class CircuitBuilder: ...@@ -59,28 +58,30 @@ class CircuitBuilder:
def get_circuit_path(self, circ_id): def get_circuit_path(self, circ_id):
c = self.controller c = self.controller
assert stem_utils.is_controller_okay(c) try:
circ = c.get_circuit(circ_id, default=None) circ = c.get_circuit(circ_id, default=None)
if circ is None: except Exception as e:
return None log.exception("Exception trying to get circuit: %s.", e)
return [relay[0] for relay in circ.path] else:
return [relay[0] for relay in circ.path]
return None
def close_circuit(self, circ_id): def close_circuit(self, circ_id):
c = self.controller c = self.controller
if not stem_utils.is_controller_okay(c): try:
return c.get_circuit(circ_id, default=None)
if c.get_circuit(circ_id, default=None):
try: try:
c.close_circuit(circ_id) c.close_circuit(circ_id)
except InvalidArguments: except InvalidArguments:
pass pass
self.built_circuits.discard(circ_id) self.built_circuits.discard(circ_id)
except Exception as e:
log.exception("Error trying to get circuit to close it: %s.", e)
def _build_circuit_impl(self, path): def _build_circuit_impl(self, path):
if not valid_circuit_length(path): if not valid_circuit_length(path):
raise PathLengthException() raise PathLengthException()
c = self.controller c = self.controller
assert stem_utils.is_controller_okay(c)
timeout = self.circuit_timeout timeout = self.circuit_timeout
fp_path = '[' + ' -> '.join([p[0:8] for p in path]) + ']' fp_path = '[' + ' -> '.join([p[0:8] for p in path]) + ']'
log.debug('Building %s', fp_path) log.debug('Building %s', fp_path)
...@@ -92,6 +93,9 @@ class CircuitBuilder: ...@@ -92,6 +93,9 @@ class CircuitBuilder:
ProtocolError, Timeout) as e: ProtocolError, Timeout) as e:
log.warning(e) log.warning(e)
continue continue
except Exception as e:
log.exception("Exception trying to build circuit: %s.", e)
continue
else: else:
return circ_id return circ_id
return None return None
...@@ -100,14 +104,16 @@ class CircuitBuilder: ...@@ -100,14 +104,16 @@ class CircuitBuilder:
c = self.controller c = self.controller
if not self.close_circuits_on_exit: if not self.close_circuits_on_exit:
return return
if not stem_utils.is_controller_okay(c):
return
for circ_id in self.built_circuits: for circ_id in self.built_circuits:
if c.get_circuit(circ_id, default=None): try:
c.get_circuit(circ_id, default=None)
try: try:
c.close_circuit(circ_id) c.close_circuit(circ_id)
except InvalidArguments: except InvalidArguments:
pass pass
except Exception as e:
log.exception("Exception trying to get circuit to delete: %s",
e)
self.built_circuits.clear() self.built_circuits.clear()
......
from stem.descriptor.router_status_entry import RouterStatusEntryV3 from stem.descriptor.router_status_entry import RouterStatusEntryV3
from stem.descriptor.server_descriptor import ServerDescriptor from stem.descriptor.server_descriptor import ServerDescriptor
import sbws.util.stem as stem_utils
from stem import Flag from stem import Flag
from stem.util.connection import is_valid_ipv4_address from stem.util.connection import is_valid_ipv4_address
from stem.util.connection import is_valid_ipv6_address from stem.util.connection import is_valid_ipv6_address
...@@ -25,17 +24,22 @@ class Relay: ...@@ -25,17 +24,22 @@ class Relay:
''' '''
assert isinstance(fp, str) assert isinstance(fp, str)
assert len(fp) == 40 assert len(fp) == 40
assert stem_utils.is_controller_okay(cont)
if ns is not None: if ns is not None:
assert isinstance(ns, RouterStatusEntryV3) assert isinstance(ns, RouterStatusEntryV3)
self._ns = ns self._ns = ns
else: else:
self._ns = cont.get_network_status(fp, default=None) try:
self._ns = cont.get_network_status(fp, default=None)
except Exception as e:
log.exception("Exception trying to get ns %s", e)
if desc is not None: if desc is not None:
assert isinstance(desc, ServerDescriptor) assert isinstance(desc, ServerDescriptor)
self._desc = desc self._desc = desc
else: else:
self._desc = cont.get_server_descriptor(fp, default=None) try:
self._desc = cont.get_server_descriptor(fp, default=None)
except Exception as e:
log.exception("Exception trying to get ns %s", e)
def _from_desc(self, attr): def _from_desc(self, attr):
if not self._desc: if not self._desc:
...@@ -190,9 +194,12 @@ class RelayList: ...@@ -190,9 +194,12 @@ class RelayList:
def _init_relays(self): def _init_relays(self):
c = self._controller c = self._controller
assert stem_utils.is_controller_okay(c) try:
relays = [Relay(ns.fingerprint, c, ns=ns) relays = [Relay(ns.fingerprint, c, ns=ns)
for ns in c.get_network_statuses()] for ns in c.get_network_statuses()]
except Exception as e:
log.exception("Exception trying to init relays %s", e)
return []
return relays return relays
def _refresh(self): def _refresh(self):
......
...@@ -17,7 +17,6 @@ stream_building_lock = RLock() ...@@ -17,7 +17,6 @@ stream_building_lock = RLock()
def attach_stream_to_circuit_listener(controller, circ_id): def attach_stream_to_circuit_listener(controller, circ_id):
''' Returns a function that should be given to add_event_listener(). It ''' Returns a function that should be given to add_event_listener(). It
looks for newly created streams and attaches them to the given circ_id ''' looks for newly created streams and attaches them to the given circ_id '''
assert is_controller_okay(controller)
def closure_stream_event_listener(st): def closure_stream_event_listener(st):
if st.status == 'NEW' and st.purpose == 'USER': if st.status == 'NEW' and st.purpose == 'USER':
...@@ -28,21 +27,25 @@ def attach_stream_to_circuit_listener(controller, circ_id): ...@@ -28,21 +27,25 @@ def attach_stream_to_circuit_listener(controller, circ_id):
except (UnsatisfiableRequest, InvalidRequest) as e: except (UnsatisfiableRequest, InvalidRequest) as e:
log.warning('Couldn\'t attach stream to circ %s: %s', log.warning('Couldn\'t attach stream to circ %s: %s',
circ_id, e) circ_id, e)
except Exception as e:
log.exception("Exception trying to get ns %s", e)
else: else:
pass pass
return closure_stream_event_listener return closure_stream_event_listener
def add_event_listener(controller, func, event): def add_event_listener(controller, func, event):
assert is_controller_okay(controller) try:
controller.add_event_listener(func, event) controller.add_event_listener(func, event)
except Exception as e:
log.exception("Exception trying to add event listener %s", e)
def remove_event_listener(controller, func): def remove_event_listener(controller, func):
if not is_controller_okay(controller): try:
log.warning('Controller not okay so not trying to remove event') controller.remove_event_listener(func)
return except Exception as e:
controller.remove_event_listener(func) log.exception("Exception trying to remove event %s", e)
def init_controller(port=None, path=None, set_custom_stream_settings=True): def init_controller(port=None, path=None, set_custom_stream_settings=True):
...@@ -72,9 +75,11 @@ def init_controller(port=None, path=None, set_custom_stream_settings=True): ...@@ -72,9 +75,11 @@ def init_controller(port=None, path=None, set_custom_stream_settings=True):
def is_bootstrapped(c): def is_bootstrapped(c):
if not is_controller_okay(c): try:
line = c.get_info('status/bootstrap-phase')
except Exception as e:
log.exception("Exception bootstrapping %s", e)
return False return False
line = c.get_info('status/bootstrap-phase')
state, _, progress, *_ = line.split() state, _, progress, *_ = line.split()
progress = int(progress.split('=')[1]) progress = int(progress.split('=')[1])
if state == 'NOTICE' and progress == 100: if state == 'NOTICE' and progress == 100:
...@@ -83,12 +88,6 @@ def is_bootstrapped(c): ...@@ -83,12 +88,6 @@ def is_bootstrapped(c):
return False return False
def is_controller_okay(c):
if not c:
return False
return c.is_alive() and c.is_authenticated()
def _init_controller_port(port): def _init_controller_port(port):
assert isinstance(port, int) assert isinstance(port, int)
try: try:
...@@ -106,6 +105,10 @@ def _init_controller_socket(socket): ...@@ -106,6 +105,10 @@ def _init_controller_socket(socket):
c = Controller.from_socket_file(path=socket) c = Controller.from_socket_file(path=socket)
c.authenticate() c.authenticate()
except (IncorrectSocketType, SocketError): except (IncorrectSocketType, SocketError):
log.debug("Error initting controller socket: socket error.")
return None
except Exception as e:
log.exception("Error initting controller socket: %s", e)
return None return None
# TODO: Allow for auth via more than just CookieAuthentication # TODO: Allow for auth via more than just CookieAuthentication
return c return c
...@@ -182,21 +185,26 @@ def launch_tor(conf): ...@@ -182,21 +185,26 @@ def launch_tor(conf):
torrc, init_msg_handler=log.debug, take_ownership=True) torrc, init_msg_handler=log.debug, take_ownership=True)
# And return a controller to it # And return a controller to it
cont = _init_controller_socket(conf['tor']['control_socket']) cont = _init_controller_socket(conf['tor']['control_socket'])
assert is_controller_okay(cont)
# Because we build things by hand and can't set these before Tor bootstraps # Because we build things by hand and can't set these before Tor bootstraps
cont.set_conf('__DisablePredictedCircuits', '1') cont.set_conf('__DisablePredictedCircuits', '1')
cont.set_conf('__LeaveStreamsUnattached', '1') cont.set_conf('__LeaveStreamsUnattached', '1')
log.info('Started and connected to Tor %s via %s', cont.get_version(), try:
conf['tor']['control_socket']) log.info('Started and connected to Tor %s via %s', cont.get_version(),
return cont conf['tor']['control_socket'])
return cont
except Exception as e:
log.exception("Exception trying to launch tor %s", e)
def get_socks_info(controller): def get_socks_info(controller):
''' Returns the first SocksPort Tor is configured to listen on, in the form ''' Returns the first SocksPort Tor is configured to listen on, in the form
of an (address, port) tuple ''' of an (address, port) tuple '''
assert is_controller_okay(controller) try:
socks_ports = controller.get_listeners(Listener.SOCKS) socks_ports = controller.get_listeners(Listener.SOCKS)
return socks_ports[0] return socks_ports[0]
except Exception as e:
log.exception("Exception trying to get socks info: %e.", e)
exit(1)
def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None): def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None):
...@@ -206,7 +214,6 @@ def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None): ...@@ -206,7 +214,6 @@ def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None):
min_bw nor max_bw are given, essentially just returns the input list of min_bw nor max_bw are given, essentially just returns the input list of
relays. relays.
''' '''
assert is_controller_okay(controller)
assert min_bw is None or min_bw >= 0 assert min_bw is None or min_bw >= 0
assert max_bw is None or max_bw >= 0 assert max_bw is None or max_bw >= 0
ret = [] ret = []
...@@ -221,7 +228,6 @@ def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None): ...@@ -221,7 +228,6 @@ def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None):
def circuit_str(controller, circ_id): def circuit_str(controller, circ_id):
assert is_controller_okay(controller)
assert isinstance(circ_id, str) assert isinstance(circ_id, str)
int(circ_id) int(circ_id)
try: try:
...@@ -230,6 +236,9 @@ def circuit_str(controller, circ_id): ...@@ -230,6 +236,9 @@ def circuit_str(controller, circ_id):
log.warning('Circuit %s no longer seems to exist so can\'t return ' log.warning('Circuit %s no longer seems to exist so can\'t return '
'a valid circuit string for it: %s', circ_id, e) 'a valid circuit string for it: %s', circ_id, e)
return None return None
except Exception as e:
log.exception("Exception trying to get circuit string %s", e)
return None
return '[' +\ return '[' +\
' -> '.join(['{} ({})'.format(n, fp[0:8]) for fp, n in circ.path]) +\ ' -> '.join(['{} ({})'.format(n, fp[0:8]) for fp, n in circ.path]) +\
']' ']'
...@@ -3,5 +3,4 @@ import sbws.util.stem as stem_utils ...@@ -3,5 +3,4 @@ import sbws.util.stem as stem_utils
def test_launch_and_okay(persistent_launch_tor): def test_launch_and_okay(persistent_launch_tor):
cont = persistent_launch_tor cont = persistent_launch_tor
assert stem_utils.is_controller_okay(cont)
assert stem_utils.is_bootstrapped(cont) assert stem_utils.is_bootstrapped(cont)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment