[Pkg-privacy-commits] [obfsproxy] 75/353: Improve code.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:42 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 45608b0a89dc0ea6a3407de0a0c914d85135d0ee
Author: George Kadianakis <desnacked at riseup.net>
Date:   Sat Oct 27 16:14:02 2012 +0300

    Improve code.
    
    - Add a log formatter to print timestamps in the logs.
    - Use buffers in SOCKS code.
    - Change some log severities.
---
 obfsproxy.py                 |  4 ++--
 obfsproxy/common/log.py      | 15 +++++++++++++--
 obfsproxy/managed/client.py  |  2 +-
 obfsproxy/managed/server.py  |  2 +-
 obfsproxy/network/network.py | 16 ++++++++--------
 obfsproxy/network/socks.py   | 40 ++++++++++++++++++++++++++++++++++++----
 6 files changed, 61 insertions(+), 18 deletions(-)

diff --git a/obfsproxy.py b/obfsproxy.py
index 10c2233..f007eb3 100755
--- a/obfsproxy.py
+++ b/obfsproxy.py
@@ -57,10 +57,10 @@ def do_managed_mode(): # XXX bad code
 
     # XXX original code caught exceptions here!!!
     if checkClientMode():
-        log.debug('Entering client managed-mode.')
+        log.info('Entering client managed-mode.')
         ManagedClient()
     else:
-        log.error('Entering server managed-mode.')
+        log.info('Entering server managed-mode.')
         ManagedServer()
 
 def do_external_mode(args):
diff --git a/obfsproxy/common/log.py b/obfsproxy/common/log.py
index 5b594a6..20f9907 100644
--- a/obfsproxy/common/log.py
+++ b/obfsproxy/common/log.py
@@ -1,12 +1,21 @@
-# obfsproxy logging code
+"""obfsproxy logging code"""
 
 import logging
 import sys
 
-# XXX Add Formatter!!!
+def set_formatter(handler):
+    """Given a log handler, plug our custom formatter to it."""
 
+    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)
+
 our_logger.addHandler(default_handler)
 our_logger.propagate = False
 
@@ -17,6 +26,8 @@ def set_log_file(filename):
     our_logger.removeHandler(default_handler)
 
     log_handler = logging.FileHandler(filename)
+    set_formatter(log_handler)
+
     our_logger.addHandler(log_handler)
 
 def set_log_severity(sev_string):
diff --git a/obfsproxy/managed/client.py b/obfsproxy/managed/client.py
index 183f0cf..6c49705 100644
--- a/obfsproxy/managed/client.py
+++ b/obfsproxy/managed/client.py
@@ -19,7 +19,7 @@ class ManagedClient:
         try:
             managedInfo = init(transports.transports.keys())
         except EnvError:
-            log.warn("Client managed-proxy protocol failed.")
+            log.warning("Client managed-proxy protocol failed.")
             return
 
         log.debug("pyptlib gave us the following data:\n'%s'", pprint.pformat(managedInfo))
diff --git a/obfsproxy/managed/server.py b/obfsproxy/managed/server.py
index 4781250..1dc0823 100644
--- a/obfsproxy/managed/server.py
+++ b/obfsproxy/managed/server.py
@@ -19,7 +19,7 @@ class ManagedServer:
         try:
             managedInfo = init(transports.transports.keys())
         except EnvError:
-            log.warn("Server managed-proxy protocol failed.")
+            log.warning("Server managed-proxy protocol failed.")
             return
 
         log.debug("pyptlib gave us the following data:\n'%s'", pprint.pformat(managedInfo))
diff --git a/obfsproxy/network/network.py b/obfsproxy/network/network.py
index e760913..a74abe2 100644
--- a/obfsproxy/network/network.py
+++ b/obfsproxy/network/network.py
@@ -200,23 +200,23 @@ class StaticDestinationProtocol(Protocol):
 
         # Find the connection's direction and register it in the circuit.
         if self.mode == 'client' and not self.circuit.upstream:
-            log.info("%s: connectionMade (client): " \
-                     "Setting it as upstream on our circuit." % self.name)
+            log.debug("%s: connectionMade (client): " \
+                      "Setting it as upstream on our circuit." % self.name)
 
             self.circuit.setUpstreamConnection(self)
         elif self.mode == 'client':
-            log.info("%s: connectionMade (client): " \
-                     "Setting it as downstream on our circuit." % self.name)
+            log.debug("%s: connectionMade (client): " \
+                      "Setting it as downstream on our circuit." % self.name)
 
             self.circuit.setDownstreamConnection(self)
         elif self.mode == 'server' and not self.circuit.downstream:
-            log.info("%s: connectionMade (server): " \
-                     "Setting it as downstream on our circuit." % self.name)
+            log.debug("%s: connectionMade (server): " \
+                      "Setting it as downstream on our circuit." % self.name)
 
             self.circuit.setDownstreamConnection(self)
         elif self.mode == 'server':
-            log.info("%s: connectionMade (server): " \
-                     "Setting it as upstream on our circuit." % self.name)
+            log.debug("%s: connectionMade (server): " \
+                      "Setting it as upstream on our circuit." % self.name)
 
             self.circuit.setUpstreamConnection(self)
 
diff --git a/obfsproxy/network/socks.py b/obfsproxy/network/socks.py
index 528cf13..dcc1e42 100644
--- a/obfsproxy/network/socks.py
+++ b/obfsproxy/network/socks.py
@@ -3,6 +3,7 @@ from twisted.internet.protocol import Protocol, Factory, ClientFactory
 
 import obfsproxy.common.log as log
 import obfsproxy.network.network as network
+import obfsproxy.network.buffer as buffer
 
 class MySOCKSv4Outgoing(socks.SOCKSv4Outgoing, object):
     """
@@ -29,7 +30,8 @@ class MySOCKSv4Outgoing(socks.SOCKSv4Outgoing, object):
         """
 
         self.circuit = socksProtocol.circuit
-        self.buffer = ''
+        self.buffer = buffer.Buffer()
+        self.closed = False # True if connection is closed.
 
         self.name = "socks_down_%s" % hex(id(self))
 
@@ -41,7 +43,19 @@ class MySOCKSv4Outgoing(socks.SOCKSv4Outgoing, object):
 
         assert(self.circuit.circuitIsReady()) # XXX Is this always true?
 
-        self.buffer = self.circuit.dataReceived(self.buffer + data, self)
+        self.buffer.write(data)
+        self.circuit.dataReceived(self.buffer, self)
+
+    def close(self): # XXX code duplication
+        """
+        Close the connection.
+        """
+        if self.closed: return # NOP if already closed
+
+        log.debug("%s: Closing connection." % self.name)
+
+        self.transport.loseConnection()
+        self.closed = True
 
 # Monkey patches socks.SOCKSv4Outgoing with our own class.
 socks.SOCKSv4Outgoing = MySOCKSv4Outgoing
@@ -57,7 +71,8 @@ class SOCKSv4Protocol(socks.SOCKSv4):
 
     def __init__(self, circuit):
         self.circuit = circuit
-        self.buffer = ''
+        self.buffer = buffer.Buffer()
+        self.closed = False # True if connection is closed.
 
         self.name = "socks_up_%s" % hex(id(self))
 
@@ -78,14 +93,20 @@ class SOCKSv4Protocol(socks.SOCKSv4):
 
         log.debug("%s: Received %d bytes:\n%s" \
                   % (self.name, len(data), str(data)))
+        self.buffer.write(data)
 
         assert(self.otherConn)
 
+        """
+        If we came here with an incomplete circuit, it means that we
+        finished the SOCKS handshake and connected downstream. Set up
+        our circuit and start proxying traffic.
+        """
         if not self.circuit.circuitIsReady():
             self.circuit.setDownstreamConnection(self.otherConn)
             self.circuit.setUpstreamConnection(self)
 
-        self.buffer = self.circuit.dataReceived(self.buffer + data, self)
+        self.circuit.dataReceived(self.buffer, self)
 
     def connectionLost(self, reason):
         log.info("%s: Connection was lost (%s)." % (self.name, reason.getErrorMessage()))
@@ -95,6 +116,17 @@ class SOCKSv4Protocol(socks.SOCKSv4):
         log.info("%s: Connection failed to connect (%s)." % (self.name, reason.getErrorMessage()))
         self.circuit.close()
 
+    def close(self): # XXX code duplication
+        """
+        Close the connection.
+        """
+        if self.closed: return # NOP if already closed
+
+        log.debug("%s: Closing connection." % self.name)
+
+        self.transport.loseConnection()
+        self.closed = True
+
 class SOCKSv4Factory(Factory):
     """
     A SOCKSv4 factory.

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