[tor-commits] [obfsproxy/master] Added exception handing to function validate_external_mode_cli, in accordance with ticket #9823.

asn at torproject.org asn at torproject.org
Wed Dec 31 15:30:08 UTC 2014


commit 401a4823826dbc288565b11bc582c1f2d976099f
Author: Colin Teberg <me at cteberg.net>
Date:   Tue Dec 30 17:11:47 2014 -0500

    Added exception handing to function validate_external_mode_cli, in accordance with ticket #9823.
    
     Note: I had to adjust the sleep time to .1 from .05 on line 121 in
     test/tester.py. My machine was taking longer to setup the client and
     servers during testing (my machine is an older core2duo).
---
 obfsproxy/pyobfsproxy.py      |   12 +++++++++---
 obfsproxy/transports/base.py  |   18 +++++++++---------
 obfsproxy/transports/obfs2.py |    8 ++++++--
 3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/obfsproxy/pyobfsproxy.py b/obfsproxy/pyobfsproxy.py
index 90e5729..4a3ca83 100755
--- a/obfsproxy/pyobfsproxy.py
+++ b/obfsproxy/pyobfsproxy.py
@@ -66,7 +66,11 @@ def set_up_cli_parsing():
     for transport, transport_class in transports.transports.items():
         subparser = subparsers.add_parser(transport, help='%s help' % transport)
         transport_class['base'].register_external_mode_cli(subparser)
-        subparser.set_defaults(validation_function=transport_class['base'].validate_external_mode_cli)
+        try:
+            subparser.set_defaults(validation_function=transport_class['base'].validate_external_mode_cli)
+        except ValueError, err:
+            log.error(err)
+            sys.exit(1)
 
     return parser
 
@@ -178,8 +182,10 @@ def pyobfsproxy():
         # they can initialize and setup themselves. Exit if the
         # provided arguments were corrupted.
 
-        # XXX use exceptions
-        if (args.validation_function(args) == False):
+        try: 
+            args.validation_function(args)
+        except Exception, err:
+            log.error(err)
             sys.exit(1)
 
         do_external_mode(args)
diff --git a/obfsproxy/transports/base.py b/obfsproxy/transports/base.py
index a2b78f1..ae1d13b 100644
--- a/obfsproxy/transports/base.py
+++ b/obfsproxy/transports/base.py
@@ -140,18 +140,18 @@ class BaseTransport(object):
         # If we are not 'socks', we need to have a static destination
         # to send our data to.
         if (args.mode != 'socks') and (not args.dest):
-            log.error("'client' and 'server' modes need a destination address.")
-            return False
+            err = "'client' and 'server' modes need a destination address."
 
-        if (args.mode != 'ext_server') and args.ext_cookie_file:
-            log.error("No need for --ext-cookie-file if not an ext_server.")
-            return False
+        elif (args.mode != 'ext_server') and args.ext_cookie_file:
+            err = "No need for --ext-cookie-file if not an ext_server."
 
-        if (args.mode == 'ext_server') and (not args.ext_cookie_file):
-            log.error("You need to specify --ext-cookie-file as an ext_server.")
-            return False
+        elif (args.mode == 'ext_server') and (not args.ext_cookie_file):
+            err = "You need to specify --ext-cookie-file as an ext_server."
 
-        return True
+        try:
+            raise argparse.ArgumentTypeError(err)
+        except NameError:
+            return True
 
 class PluggableTransportError(Exception): pass
 class SOCKSArgsError(Exception): pass
diff --git a/obfsproxy/transports/obfs2.py b/obfsproxy/transports/obfs2.py
index 23a60d4..c77c5cd 100644
--- a/obfsproxy/transports/obfs2.py
+++ b/obfsproxy/transports/obfs2.py
@@ -126,8 +126,12 @@ class Obfs2Transport(base.BaseTransport):
         if args.ss_hash_iterations:
             cls.ss_hash_iterations = args.ss_hash_iterations
 
-        super(Obfs2Transport, cls).validate_external_mode_cli(args)
-
+        try: 
+            super(Obfs2Transport, cls).validate_external_mode_cli(args)
+        except ValueError, err:
+            log.error(err)
+            sys.exit(1)
+            
     def handle_socks_args(self, args):
         log.debug("obfs2: Got '%s' as SOCKS arguments." % args)
 





More information about the tor-commits mailing list