[Pkg-privacy-commits] [obfsproxy] 57/353: Add basic skeleton for external-mode support.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:39 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository obfsproxy.

commit 48b85ddbb9e805b87fa9ecb518b0f7cf69fa2be9
Author: George Kadianakis <desnacked at riseup.net>
Date:   Tue Sep 4 20:03:51 2012 +0300

    Add basic skeleton for external-mode support.
---
 src/cli.py                       | 120 +++++++++++++++++++++++++--------------
 src/obfsproxy/transports/base.py |  41 ++++++++++++-
 2 files changed, 116 insertions(+), 45 deletions(-)

diff --git a/src/cli.py b/src/cli.py
index 5b57270..ebad961 100644
--- a/src/cli.py
+++ b/src/cli.py
@@ -10,13 +10,14 @@ Currently, not all of the obfsproxy command line options have been implemented.
 import os
 import sys
 import logging
+import argparse
+import obfsproxy.transports.base as base
 
-logging.basicConfig(filename='pyobfslog.txt', loglevel=logging.DEBUG)
+logging.basicConfig(filename='pyobfslog.txt', loglevel=logging.DEBUG) # XXX hardcoded
 logging.error('py-obfsproxy CLI loaded')
 logging.error('argv: ' + str(sys.argv))
 
-import argparse
-
+# XXX kill these
 sys.path.insert(0,
                 os.path.realpath(os.path.join(os.path.dirname(__file__),
                 '../../Dust/py')))
@@ -32,53 +33,84 @@ try:
 except Exception, e:
     logging.error('Error loading framework: ' + str(e))
 
-protocols = ['dummy', 'rot13']
+def set_up_cli_parsing():
+    """Set up our CLI parser. Register our arguments and options and
+    query individual transports to register their own external-mode
+    arguments."""
+
+    parser = argparse.ArgumentParser(
+        description='py-obfsproxy: A pluggable transports proxy written in Python')
+    subparsers = parser.add_subparsers(title='supported transports', dest='name')
 
-if __name__ == '__main__':
-    parser = \
-        argparse.ArgumentParser(description='* Available protocols: '
-                                + ' '.join(protocols))
     parser.add_argument('--log-file', nargs=1, help='set logfile')
     parser.add_argument('--log-min-severity', nargs=1, default='notice'
                         , choices=['warn', 'notice', 'info', 'debug'],
-                        help='set minimum logging severity (default: %(default)s)'
-                        )
+                        help='set minimum logging severity (default: %(default)s)')
     parser.add_argument('--no-log', action='store_false', default=True,
                         help='disable logging')
     parser.add_argument('--no-safe-logging', action='store_false',
                         default=True,
                         help='disable safe (scrubbed address) logging')
-    parser.add_argument('--managed', action='store_true',
-                        default=False,
-                        help='enabled managed mode, for use when called by tor'
-                        )
-
-    try:
-        args = parser.parse_args()
-    except Exception, e:
-        logging.error('Exception parsing')
-        logging.error(str(e))
-
-    if args.log_file and len(args.log_file) > 0:
-        logging.error('file logging: ' + str(args.log_file[0]) + ' '
-                      + str(os.path.exists(args.log_file[0])))
-        logging.config.fileConfig(str(args.log_file[0]),
-                                  disable_existing_loggers=False)
-        logging.error('new logging in place')
-
-    logging.error('py-obfsproxy CLI loaded')
-
-    try:
-        daemon = None
-        if args.managed:
-            if checkClientMode():
-                logging.error('client')
-                daemon = ManagedClient()
-            else:
-                logging.error('server')
-                daemon = ManagedServer()
-        else:
-            logging.error('Unsupported mode. Only managed mode is available at the moment.'
-                          )
-    except:
-        logging.exception('Exception launching daemon')
+
+    """Managed mode is a subparser for now because there are no
+    optional subparsers: bugs.python.org/issue9253"""
+    sp = subparsers.add_parser("managed", help="managed mode")
+
+    """Add a subparser for each transport."""
+    for transport, transport_class in base.transports.items():
+        subparser = subparsers.add_parser(transport, help='%s help' % transport)
+        transport_class['client'].register_external_mode_cli(subparser) # XXX
+
+    return parser
+
+def do_managed_mode(): # XXX bad code
+    """This function starts obfsproxy's managed-mode functionality."""
+
+    # XXX original code caught exceptions here!!!
+    if checkClientMode():
+        logging.error('client')
+        ManagedClient()
+    else:
+        logging.error('server')
+        ManagedServer()
+
+def do_external_mode(args):
+    """This function starts obfsproxy's external-mode functionality."""
+
+    assert(args)
+    assert(args.name)
+    assert(args.name in base.transports)
+
+    our_class = base.get_transport_class_from_name_and_mode(args.name, args.mode)
+
+    if (our_class is None):
+        logging.error("Transport class was not found for '%s' in mode '%s'" % (args.name, args.mode))
+        sys.exit(1)
+
+
+    # XXX ugly code:
+    from obfsproxy.framework.proxy import ProxyHandler
+    from monocle.stack import eventloop
+    from monocle.stack.network import add_service, Service
+
+    handler = ProxyHandler(args.dest[0], int(args.dest[1]))
+    handler.setTransport(our_class)
+    add_service(Service(handler.handle, bindaddr=args.listen_addr[0], port=int(args.listen_addr[1])))
+    eventloop.run()
+
+def main(argv):
+    parser = set_up_cli_parsing()
+
+    args = parser.parse_args()
+
+    logging.error("Parsed %s" % str(args)) # XXX
+
+    # XXX do sanity checks. like in case managed is set along with external options etc.
+
+    if (args.name == 'managed'):
+        do_managed_mode()
+    else:
+        do_external_mode(args)
+
+if __name__ == '__main__':
+    main(sys.argv[1:])
diff --git a/src/obfsproxy/transports/base.py b/src/obfsproxy/transports/base.py
index 44f10be..f4d8325 100644
--- a/src/obfsproxy/transports/base.py
+++ b/src/obfsproxy/transports/base.py
@@ -7,6 +7,17 @@ It is not necessary to subclass BaseDaemon in order to implement pluggable trans
 However, BaseDaemon provides utility methods that are useful for a variety of common transports.
 """
 
+def addrport(string):
+  """Receive '<addr>:<port>' and return [<addr>,<port>]. Used during
+  argparse CLI parsing."""
+
+  addrport = string.split(':')
+
+  if (len(addrport) != 2):
+    msg = "'%s' is not in <addr>:<port> format" % string
+    raise argparse.ArgumentTypeError(msg)
+
+  return addrport
 
 class BaseDaemon:
 
@@ -20,6 +31,16 @@ class BaseDaemon:
         self.downstreamConnection = circuit.downstream
         self.upstreamConnection = circuit.upstream
 
+    @staticmethod
+    def register_external_mode_cli(subparser):
+        """ Given an argparse ArgumentParser in 'subparser', register
+        some default external-mode CLI arguments. Transports with more
+        complex CLI are expected to override this function."""
+
+        subparser.add_argument('mode', choices=['server','client','socks'])
+        subparser.add_argument('listen_addr', type=addrport)
+        subparser.add_argument('--dest', type=addrport, help='Destination address')
+
     def read(
         self,
         socket,
@@ -90,4 +111,22 @@ class BaseDaemon:
 
         pass
 
-
+# XXX modulify transports and move this to a single import
+import obfsproxy.transports.dummy as dummy
+import obfsproxy.transports.rot13 as rot13
+import obfsproxy.transports.dust_transport as dust
+# XXX obfs2 is broken...
+#import obfsproxy.transports.obfs2 as obfs2
+import obfsproxy.transports.obfs3 as obfs3
+
+transports = { 'dummy' : {'client' : dummy.DummyClient, 'server' : dummy.DummyServer },
+               'rot13' : {'client' : rot13.Rot13Client, 'server' : rot13.Rot13Server },
+               'dust' :  {'client' : dust.DustClient, 'server' : dust.DustServer },
+#               'obfs2' : {'client' : obfs2.Obfs2Client, 'server' : obfs2.Obfs2Server },
+               'obfs3' : {'client' : obfs3.Obfs3Client, 'server' : obfs3.Obfs3Server } }
+
+def get_transport_class_from_name_and_mode(name, mode):
+    if (name in transports) and (mode in transports[name]):
+        return transports[name][mode]
+    else:
+        return None

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/obfsproxy.git



More information about the Pkg-privacy-commits mailing list