[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/master] Remove is_controller_ok and log exceptions



commit b5d3fb4fc8d1d2edb9c48a7a6d9996f7b37912b9
Author: juga0 <juga@xxxxxxxxxx>
Date:   Tue Jul 3 08:26:40 2018 +0000

    Remove is_controller_ok and log exceptions
---
 sbws/core/scanner.py                |  1 -
 sbws/lib/circuitbuilder.py          | 32 ++++++++++++---------
 sbws/lib/relaylist.py               | 21 +++++++++-----
 sbws/util/stem.py                   | 57 +++++++++++++++++++++----------------
 tests/integration/util/test_stem.py |  1 -
 5 files changed, 66 insertions(+), 46 deletions(-)

diff --git a/sbws/core/scanner.py b/sbws/core/scanner.py
index 249d8d4..5bcf276 100644
--- a/sbws/core/scanner.py
+++ b/sbws/core/scanner.py
@@ -347,7 +347,6 @@ def run_speedtest(args, conf):
             '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)
     rl = RelayList(args, conf, controller)
     cb = CB(args, conf, controller, rl)
     rd = ResultDump(args, conf, end_event)
diff --git a/sbws/lib/circuitbuilder.py b/sbws/lib/circuitbuilder.py
index d55b929..b8d4db7 100644
--- a/sbws/lib/circuitbuilder.py
+++ b/sbws/lib/circuitbuilder.py
@@ -1,7 +1,6 @@
 from stem import CircuitExtensionFailed, InvalidRequest, ProtocolError, Timeout
 from stem import InvalidArguments
 import random
-import sbws.util.stem as stem_utils
 from .relaylist import Relay
 import logging
 
@@ -59,28 +58,30 @@ class CircuitBuilder:
 
     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]
+        try:
+            circ = c.get_circuit(circ_id, default=None)
+        except Exception as e:
+            log.exception("Exception trying to get circuit: %s.", e)
+        else:
+            return [relay[0] for relay in circ.path]
+        return None
 
     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.get_circuit(circ_id, default=None)
             try:
                 c.close_circuit(circ_id)
             except InvalidArguments:
                 pass
             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):
         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)
@@ -92,6 +93,9 @@ class CircuitBuilder:
                     ProtocolError, Timeout) as e:
                 log.warning(e)
                 continue
+            except Exception as e:
+                log.exception("Exception trying to build circuit: %s.", e)
+                continue
             else:
                 return circ_id
         return None
@@ -100,14 +104,16 @@ class CircuitBuilder:
         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.get_circuit(circ_id, default=None)
                 try:
                     c.close_circuit(circ_id)
                 except InvalidArguments:
                     pass
+            except Exception as e:
+                log.exception("Exception trying to get circuit to delete: %s",
+                              e)
         self.built_circuits.clear()
 
 
diff --git a/sbws/lib/relaylist.py b/sbws/lib/relaylist.py
index 11390de..bafd7b7 100644
--- a/sbws/lib/relaylist.py
+++ b/sbws/lib/relaylist.py
@@ -1,6 +1,5 @@
 from stem.descriptor.router_status_entry import RouterStatusEntryV3
 from stem.descriptor.server_descriptor import ServerDescriptor
-import sbws.util.stem as stem_utils
 from stem import Flag
 from stem.util.connection import is_valid_ipv4_address
 from stem.util.connection import is_valid_ipv6_address
@@ -25,17 +24,22 @@ class Relay:
         '''
         assert isinstance(fp, str)
         assert len(fp) == 40
-        assert stem_utils.is_controller_okay(cont)
         if ns is not None:
             assert isinstance(ns, RouterStatusEntryV3)
             self._ns = ns
         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:
             assert isinstance(desc, ServerDescriptor)
             self._desc = desc
         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):
         if not self._desc:
@@ -190,9 +194,12 @@ class RelayList:
 
     def _init_relays(self):
         c = self._controller
-        assert stem_utils.is_controller_okay(c)
-        relays = [Relay(ns.fingerprint, c, ns=ns)
-                  for ns in c.get_network_statuses()]
+        try:
+            relays = [Relay(ns.fingerprint, c, ns=ns)
+                      for ns in c.get_network_statuses()]
+        except Exception as e:
+            log.exception("Exception trying to init relays %s", e)
+            return []
         return relays
 
     def _refresh(self):
diff --git a/sbws/util/stem.py b/sbws/util/stem.py
index 8a92673..54779fd 100644
--- a/sbws/util/stem.py
+++ b/sbws/util/stem.py
@@ -17,7 +17,6 @@ stream_building_lock = RLock()
 def attach_stream_to_circuit_listener(controller, circ_id):
     ''' 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 '''
-    assert is_controller_okay(controller)
 
     def closure_stream_event_listener(st):
         if st.status == 'NEW' and st.purpose == 'USER':
@@ -28,21 +27,25 @@ def attach_stream_to_circuit_listener(controller, circ_id):
             except (UnsatisfiableRequest, InvalidRequest) as e:
                 log.warning('Couldn\'t attach stream to circ %s: %s',
                             circ_id, e)
+            except Exception as e:
+                log.exception("Exception trying to get ns %s", e)
         else:
             pass
     return closure_stream_event_listener
 
 
 def add_event_listener(controller, func, event):
-    assert is_controller_okay(controller)
-    controller.add_event_listener(func, event)
+    try:
+        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):
-    if not is_controller_okay(controller):
-        log.warning('Controller not okay so not trying to remove event')
-        return
-    controller.remove_event_listener(func)
+    try:
+        controller.remove_event_listener(func)
+    except Exception as e:
+        log.exception("Exception trying to remove event %s", e)
 
 
 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):
-    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
-    line = c.get_info('status/bootstrap-phase')
     state, _, progress, *_ = line.split()
     progress = int(progress.split('=')[1])
     if state == 'NOTICE' and progress == 100:
@@ -83,12 +88,6 @@ def is_bootstrapped(c):
     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):
     assert isinstance(port, int)
     try:
@@ -106,6 +105,10 @@ def _init_controller_socket(socket):
         c = Controller.from_socket_file(path=socket)
         c.authenticate()
     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
     # TODO: Allow for auth via more than just CookieAuthentication
     return c
@@ -182,21 +185,26 @@ def launch_tor(conf):
         torrc, init_msg_handler=log.debug, take_ownership=True)
     # And return a controller to it
     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
     cont.set_conf('__DisablePredictedCircuits', '1')
     cont.set_conf('__LeaveStreamsUnattached', '1')
-    log.info('Started and connected to Tor %s via %s', cont.get_version(),
-             conf['tor']['control_socket'])
-    return cont
+    try:
+        log.info('Started and connected to Tor %s via %s', cont.get_version(),
+                 conf['tor']['control_socket'])
+        return cont
+    except Exception as e:
+        log.exception("Exception trying to launch tor %s", e)
 
 
 def get_socks_info(controller):
     ''' Returns the first SocksPort Tor is configured to listen on, in the form
     of an (address, port) tuple '''
-    assert is_controller_okay(controller)
-    socks_ports = controller.get_listeners(Listener.SOCKS)
-    return socks_ports[0]
+    try:
+        socks_ports = controller.get_listeners(Listener.SOCKS)
+        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):
@@ -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
     relays.
     '''
-    assert is_controller_okay(controller)
     assert min_bw is None or min_bw >= 0
     assert max_bw is None or max_bw >= 0
     ret = []
@@ -221,7 +228,6 @@ def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None):
 
 
 def circuit_str(controller, circ_id):
-    assert is_controller_okay(controller)
     assert isinstance(circ_id, str)
     int(circ_id)
     try:
@@ -230,6 +236,9 @@ def circuit_str(controller, circ_id):
         log.warning('Circuit %s no longer seems to exist so can\'t return '
                     'a valid circuit string for it: %s', circ_id, e)
         return None
+    except Exception as e:
+        log.exception("Exception trying to get circuit string %s", e)
+        return None
     return '[' +\
         ' -> '.join(['{} ({})'.format(n, fp[0:8]) for fp, n in circ.path]) +\
         ']'
diff --git a/tests/integration/util/test_stem.py b/tests/integration/util/test_stem.py
index 203cf5d..291f485 100644
--- a/tests/integration/util/test_stem.py
+++ b/tests/integration/util/test_stem.py
@@ -3,5 +3,4 @@ import sbws.util.stem as stem_utils
 
 def test_launch_and_okay(persistent_launch_tor):
     cont = persistent_launch_tor
-    assert stem_utils.is_controller_okay(cont)
     assert stem_utils.is_bootstrapped(cont)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits