[Pkg-privacy-commits] [obfsproxy] 88/353: Accept --no-safe-logging and add scrubbing func

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:44 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 e6668888863017c18612682a01f6601c07f227da
Author: Matthew Finkel <matthew.finkel at gmail.com>
Date:   Sun Nov 4 16:58:09 2012 -0500

    Accept --no-safe-logging and add scrubbing func
    
    The command line option --no-safe-logging is now accepted as a valid
    argument. If provided, IP addresses will no longer be replaced by
    the [scrubbed] placeholder and will be output as-is to the log.
    
    - Encapsulate logging functions in ObfsLogger class
    - ObfsLogger is instantiated when module is imported
    - Create ObfsLogger wrappers around logger.* log severity methods
---
 obfsproxy.py                  |  15 +++--
 obfsproxy/common/heartbeat.py |   4 +-
 obfsproxy/common/log.py       | 125 +++++++++++++++++++++++++++++++-----------
 obfsproxy/managed/client.py   |   6 +-
 obfsproxy/managed/server.py   |   6 +-
 obfsproxy/network/network.py  |   6 +-
 obfsproxy/network/socks.py    |   4 +-
 obfsproxy/transports/b64.py   |   4 +-
 obfsproxy/transports/base.py  |   5 +-
 obfsproxy/transports/obfs2.py |   4 +-
 10 files changed, 129 insertions(+), 50 deletions(-)

diff --git a/obfsproxy.py b/obfsproxy.py
index 3115662..6169429 100755
--- a/obfsproxy.py
+++ b/obfsproxy.py
@@ -13,7 +13,7 @@ import argparse
 
 import obfsproxy.network.launch_transport as launch_transport
 import obfsproxy.transports.transports as transports
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
 import obfsproxy.common.heartbeat as heartbeat
 import obfsproxy.managed.server as managed_server
 import obfsproxy.managed.client as managed_client
@@ -22,6 +22,8 @@ from pyptlib.util import checkClientMode
 
 from twisted.internet import task # for LoopingCall
 
+log = logging.get_obfslogger()
+
 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
@@ -37,10 +39,9 @@ def set_up_cli_parsing():
                         help='set minimum logging severity (default: %(default)s)')
     parser.add_argument('--no-log', action='store_true', default=False,
                         help='disable logging')
-# XXX
-#    parser.add_argument('--no-safe-logging', action='store_true',
-#                        default=False,
-#                        help='disable safe (scrubbed address) logging')
+    parser.add_argument('--no-safe-logging', action='store_true',
+                        default=False,
+                        help='disable safe (scrubbed address) logging')
 
     """Managed mode is a subparser for now because there are no
     optional subparsers: bugs.python.org/issue9253"""
@@ -77,7 +78,7 @@ def do_external_mode(args):
 
     addrport = launch_transport.launch_transport_listener(args.name, args.listen_addr, args.mode, args.dest)
     log.info("Launched '%s' listener at '%s:%s' for transport '%s'." % \
-                 (args.mode, args.listen_addr[0], args.listen_addr[1], args.name))
+                 (args.mode, log.safe_addr_str(args.listen_addr[0]), args.listen_addr[1], args.name))
     reactor.run()
 
 def consider_cli_args(args):
@@ -89,6 +90,8 @@ def consider_cli_args(args):
         log.set_log_severity(args.log_min_severity)
     if args.no_log:
         log.disable_logs()
+    if args.no_safe_logging:
+        log.set_no_safe_logging()
 
     # validate:
     if (args.name == 'managed') and (not args.log_file) and (args.log_min_severity):
diff --git a/obfsproxy/common/heartbeat.py b/obfsproxy/common/heartbeat.py
index bc36729..2e2243e 100644
--- a/obfsproxy/common/heartbeat.py
+++ b/obfsproxy/common/heartbeat.py
@@ -3,7 +3,9 @@
 import datetime
 import socket # for socket.inet_aton()
 
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
+
+log = logging.get_obfslogger()
 
 class Heartbeat(object):
     """
diff --git a/obfsproxy/common/log.py b/obfsproxy/common/log.py
index 20f9907..34d1f28 100644
--- a/obfsproxy/common/log.py
+++ b/obfsproxy/common/log.py
@@ -3,48 +3,107 @@
 import logging
 import sys
 
-def set_formatter(handler):
-    """Given a log handler, plug our custom formatter to it."""
+def get_obfslogger():
+    """ Return the current ObfsLogger instance """
+    return OBFSLOGGER
 
-    formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
-    handler.setFormatter(formatter)
 
-"""
-Create the default log handler that logs to stdout.
-"""
-our_logger = logging.getLogger('our_logger')
-default_handler = logging.StreamHandler(sys.stdout)
-set_formatter(default_handler)
+class ObfsLogger(object):
+    """
+    Maintain state of logging options specified with command line arguments
 
-our_logger.addHandler(default_handler)
-our_logger.propagate = False
+    Attributes:
+    safe_logging: Boolean value indicating if we should scrub addresses
+                  before logging
+    obfslogger: Our logging instance
+    """
 
-def set_log_file(filename):
-    """Set up our logger so that it starts logging to file in 'filename' instead."""
+    def __init__(self):
 
-    # remove the default handler, and add the FileHandler:
-    our_logger.removeHandler(default_handler)
+        self.safe_logging = True 
 
-    log_handler = logging.FileHandler(filename)
-    set_formatter(log_handler)
+        # Create the default log handler that logs to stdout.
+        self.obfslogger = logging.getLogger('obfslogger')
+        self.default_handler = logging.StreamHandler(sys.stdout)
+        self.set_formatter(self.default_handler)
+        self.obfslogger.addHandler(self.default_handler)
+        self.obfslogger.propagate = False
 
-    our_logger.addHandler(log_handler)
+    def set_formatter(self, handler):
+        """Given a log handler, plug our custom formatter to it."""
 
-def set_log_severity(sev_string):
-    """Update our minimum logging severity to 'sev_string'."""
+        formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
+        handler.setFormatter(formatter)
 
-    # Turn it into a numeric level that logging understands first.
-    numeric_level = getattr(logging, sev_string.upper(), None)
-    our_logger.setLevel(numeric_level)
+    def set_log_file(self, filename):
+        """Set up our logger so that it starts logging to file in 'filename' instead."""
 
-def disable_logs():
-    """Disable all logging."""
+        # remove the default handler, and add the FileHandler:
+        self.obfslogger.removeHandler(default_handler)
 
-    logging.disable(logging.CRITICAL)
+        log_handler = logging.FileHandler(filename)
+        set_formatter(log_handler)
 
-# Redirect logging functions to our custom logger.
-debug = our_logger.debug
-info = our_logger.info
-warning = our_logger.warning
-error = our_logger.error
-critical = our_logger.critical
+        self.obfslogger.addHandler(log_handler)
+
+
+    def set_log_severity(self, sev_string):
+        """Update our minimum logging severity to 'sev_string'."""
+
+        # Turn it into a numeric level that logging understands first.
+        numeric_level = getattr(logging, sev_string.upper(), None)
+        self.obfslogger.setLevel(numeric_level)
+
+
+    def disable_logs(self):
+        """Disable all logging."""
+
+        logging.disable(logging.CRITICAL)
+
+
+    def set_no_safe_logging(self):
+        """ Disable safe_logging """
+
+        self.safe_logging = False
+
+
+    def safe_addr_str(self, address):
+        """
+        Unless safe_logging is False, we return '[scrubbed]' instead
+        of the address parameter. If safe_logging is false, then we
+        return the address itself.
+        """
+
+        if self.safe_logging:
+            return '[scrubbed]'
+        else:
+            return address
+
+    def debug(self, msg, *args, **kwargs):
+        """ Class wrapper around debug logging method """
+
+        self.obfslogger.debug(msg, *args, **kwargs)
+
+    def warning(self, msg, *args, **kwargs):
+        """ Class wrapper around warning logging method """
+
+        self.obfslogger.warning(msg, *args, **kwargs)
+
+    def info(self, msg, *args, **kwargs):
+        """ Class wrapper around info logging method """
+
+        self.obfslogger.info(msg, *args, **kwargs)
+
+    def error(self, msg, *args, **kwargs):
+        """ Class wrapper around error logging method """
+
+        self.obfslogger.error(msg, *args, **kwargs)
+
+    def critical(self, msg, *args, **kwargs):
+        """ Class wrapper around critical logging method """
+
+        self.obfslogger.critical(msg, *args, **kwargs)
+
+
+""" Global variable that will track our Obfslogger instance """
+OBFSLOGGER = ObfsLogger()
diff --git a/obfsproxy/managed/client.py b/obfsproxy/managed/client.py
index ec18e02..bad67ce 100644
--- a/obfsproxy/managed/client.py
+++ b/obfsproxy/managed/client.py
@@ -6,13 +6,15 @@ from twisted.internet import reactor, error
 
 import obfsproxy.network.launch_transport as launch_transport
 import obfsproxy.transports.transports as transports
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
 
 from pyptlib.client import init, reportSuccess, reportFailure, reportEnd
 from pyptlib.config import EnvError
 
 import pprint
 
+log = logging.get_obfslogger()
+
 def do_managed_client():
     should_start_event_loop = False
 
@@ -37,7 +39,7 @@ def do_managed_client():
             continue
 
         should_start_event_loop = True
-        log.debug("Successfully launched '%s' at '%s'" % (transport, str(addrport)))
+        log.debug("Successfully launched '%s' at '%s'" % (transport, log.safe_addr_str(str(addrport))))
         reportSuccess(transport, 4, addrport, None, None) # XXX SOCKS v4 hardcoded
 
     reportEnd()
diff --git a/obfsproxy/managed/server.py b/obfsproxy/managed/server.py
index be8cc9d..268f38e 100644
--- a/obfsproxy/managed/server.py
+++ b/obfsproxy/managed/server.py
@@ -9,10 +9,12 @@ from pyptlib.config import EnvError
 import obfsproxy.network.network as network
 import obfsproxy.transports.transports as transports
 import obfsproxy.network.launch_transport as launch_transport
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
 
 import pprint
 
+log = logging.get_obfslogger()
+
 def do_managed_server():
     should_start_event_loop = False
 
@@ -40,7 +42,7 @@ def do_managed_server():
             continue
 
         should_start_event_loop = True
-        log.debug("Successfully launched '%s' at '%s'" % (transport, str(addrport)))
+        log.debug("Successfully launched '%s' at '%s'" % (transport, log.safe_addr_str(str(addrport))))
         reportSuccess(transport, addrport, None)
 
     reportEnd()
diff --git a/obfsproxy/network/network.py b/obfsproxy/network/network.py
index ec8dcbc..3db20af 100644
--- a/obfsproxy/network/network.py
+++ b/obfsproxy/network/network.py
@@ -2,12 +2,14 @@ from twisted.python import failure
 from twisted.internet import reactor, error, address, tcp
 from twisted.internet.protocol import Protocol, Factory, ClientFactory
 
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
 import obfsproxy.common.heartbeat as heartbeat
 
 import obfsproxy.network.buffer as buffer
 import obfsproxy.transports.base as base
 
+log = logging.get_obfslogger()
+
 """
 Networking subsystem:
 
@@ -322,7 +324,7 @@ class StaticDestinationServerFactory(Factory):
         log.debug("%s: Starting up static destination server factory." % self.name)
 
     def buildProtocol(self, addr):
-        log.debug("%s: New connection from %s:%d." % (self.name, addr.host, addr.port))
+        log.debug("%s: New connection from %s:%d." % (self.name, log.safe_addr_str(addr.host), addr.port))
 
         circuit = Circuit(self.transport_class())
 
diff --git a/obfsproxy/network/socks.py b/obfsproxy/network/socks.py
index 4ead850..bb98b57 100644
--- a/obfsproxy/network/socks.py
+++ b/obfsproxy/network/socks.py
@@ -1,10 +1,12 @@
 from twisted.protocols import socks
 from twisted.internet.protocol import Protocol, Factory, ClientFactory
 
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
 import obfsproxy.network.network as network
 import obfsproxy.network.buffer as buffer
 
+log = logging.get_obfslogger()
+
 class MySOCKSv4Outgoing(socks.SOCKSv4Outgoing, object):
     """
     Represents a downstream connection from the SOCKS server to the
diff --git a/obfsproxy/transports/b64.py b/obfsproxy/transports/b64.py
index 8e52324..6a34987 100644
--- a/obfsproxy/transports/b64.py
+++ b/obfsproxy/transports/b64.py
@@ -7,7 +7,9 @@ from obfsproxy.transports.base import BaseTransport
 
 import base64
 
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
+
+log = logging.get_obfslogger()
 
 def _get_b64_chunks_from_str(string):
     """
diff --git a/obfsproxy/transports/base.py b/obfsproxy/transports/base.py
index 131c91a..93f3ce6 100644
--- a/obfsproxy/transports/base.py
+++ b/obfsproxy/transports/base.py
@@ -1,7 +1,10 @@
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
+import argparse
+
+log = logging.get_obfslogger()
 
 """
 This module contains BaseTransport, a pluggable transport skeleton class.
diff --git a/obfsproxy/transports/obfs2.py b/obfsproxy/transports/obfs2.py
index f2838fb..31a12fd 100644
--- a/obfsproxy/transports/obfs2.py
+++ b/obfsproxy/transports/obfs2.py
@@ -13,7 +13,9 @@ import struct
 import obfsproxy.common.aes as aes
 import obfsproxy.transports.base as base
 
-import obfsproxy.common.log as log
+import obfsproxy.common.log as logging
+
+log = logging.get_obfslogger()
 
 MAGIC_VALUE = 0x2BF5CA7E
 SEED_LENGTH = 16

-- 
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