[Pkg-privacy-commits] [obfsproxy] 214/353: fix #10342: make circuit an attribute of transport, rather than passing it in as method params - also rm redundant "pass" statements, python doesn't need it if you have a docstring

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:02:01 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 f6fd1c198e7322c13b61faa50bd2e8c2aa305437
Author: Ximin Luo <infinity0 at gmx.com>
Date:   Tue Dec 10 20:37:50 2013 +0000

    fix #10342: make circuit an attribute of transport, rather than passing it in as method params
    - also rm redundant "pass" statements, python doesn't need it if you have a docstring
---
 obfsproxy/network/network.py  | 10 ++++++----
 obfsproxy/transports/b64.py   | 11 ++++-------
 obfsproxy/transports/base.py  | 30 +++++++++++++++---------------
 obfsproxy/transports/dummy.py | 14 +++++++++-----
 obfsproxy/transports/obfs2.py | 15 ++++++++-------
 obfsproxy/transports/obfs3.py | 19 ++++++++++---------
 6 files changed, 52 insertions(+), 47 deletions(-)

diff --git a/obfsproxy/network/network.py b/obfsproxy/network/network.py
index 0f12083..d1a4f6a 100644
--- a/obfsproxy/network/network.py
+++ b/obfsproxy/network/network.py
@@ -125,7 +125,8 @@ class Circuit(Protocol):
 
         # Call the transport-specific handshake method since this is a
         # good time to perform a handshake.
-        self.transport.handshake(self)
+        self.transport.circuit = self
+        self.transport.handshake()
 
         # Do a dummy dataReceived on the initiating connection in case
         # it has any buffered data that must be flushed to the network.
@@ -148,10 +149,10 @@ class Circuit(Protocol):
         try:
             if conn is self.downstream:
                 log.debug("%s: downstream: Received %d bytes." % (self.name, len(data)))
-                self.transport.receivedDownstream(data, self)
+                self.transport.receivedDownstream(data)
             else:
                 log.debug("%s: upstream: Received %d bytes." % (self.name, len(data)))
-                self.transport.receivedUpstream(data, self)
+                self.transport.receivedUpstream(data)
         except base.PluggableTransportError, err: # Our transport didn't like that data.
             log.info("%s: %s: Closing circuit." % (self.name, str(err)))
             self.close()
@@ -174,7 +175,8 @@ class Circuit(Protocol):
         if self.upstream:
             self.upstream.close()
 
-        self.transport.circuitDestroyed(self, reason, side)
+        self.transport.circuitDestroyed(reason, side)
+        self.transport.circuit = None
 
 class GenericProtocol(Protocol, object):
     """
diff --git a/obfsproxy/transports/b64.py b/obfsproxy/transports/b64.py
index eb63c4d..c50d5eb 100644
--- a/obfsproxy/transports/b64.py
+++ b/obfsproxy/transports/b64.py
@@ -45,10 +45,7 @@ class B64Transport(BaseTransport):
     base64 before pushing them to the network.
     """
 
-    def __init__(self, transport_config):
-        pass
-
-    def receivedDownstream(self, data, circuit):
+    def receivedDownstream(self, data):
         """
         Got data from downstream; relay them upstream.
         """
@@ -70,14 +67,14 @@ class B64Transport(BaseTransport):
                 return
 
         data.drain()
-        circuit.upstream.write(decoded_data)
+        self.circuit.upstream.write(decoded_data)
 
-    def receivedUpstream(self, data, circuit):
+    def receivedUpstream(self, data):
         """
         Got data from upstream; relay them downstream.
         """
 
-        circuit.downstream.write(base64.b64encode(data.read()))
+        self.circuit.downstream.write(base64.b64encode(data.read()))
         return
 
 
diff --git a/obfsproxy/transports/base.py b/obfsproxy/transports/base.py
index f3f65e5..2a05170 100644
--- a/obfsproxy/transports/base.py
+++ b/obfsproxy/transports/base.py
@@ -28,13 +28,19 @@ class BaseTransport(object):
     The BaseTransport class is a skeleton class for pluggable transports.
     It contains callbacks that your pluggable transports should
     override and customize.
+
+    Attributes:
+    circuit: Circuit object. This is set just before handshake is called.
     """
 
     def __init__(self):
         """
         Initialize transport. This is called right after TCP connect.
+
+        Subclass overrides should still call this via super().
         """
-        pass
+        self.name = "tran_%s" % hex(id(self))
+        self.circuit = None
 
     @classmethod
     def setup(cls, pt_config):
@@ -43,7 +49,6 @@ class BaseTransport(object):
         and save state in class attributes.
         Called at obfsproxy startup.
         """
-        pass
 
     @classmethod
     def get_public_server_options(cls, transport_options):
@@ -74,40 +79,35 @@ class BaseTransport(object):
         """
         return None
 
-    def handshake(self, circuit):
+    def handshake(self):
         """
-        The Circuit 'circuit' was completed, and this is a good time
+        The Circuit 'self.circuit' was completed, and this is a good time
         to do your transport-specific handshake on its downstream side.
         """
-        pass
 
-    def circuitDestroyed(self, circuit, reason, side):
+    def circuitDestroyed(self, reason, side):
         """
-        Circuit 'circuit' was tore down.
+        Circuit 'self.circuit' was tore down.
         Both connections of the circuit are closed when this callback triggers.
         """
-        pass
 
-    def receivedDownstream(self, data, circuit):
+    def receivedDownstream(self, data):
         """
-        Received 'data' in the downstream side of 'circuit'.
+        Received 'data' in the downstream side of 'self.circuit'.
         'data' is an obfsproxy.network.buffer.Buffer.
         """
-        pass
 
-    def receivedUpstream(self, data, circuit):
+    def receivedUpstream(self, data):
         """
-        Received 'data' in the upstream side of 'circuit'.
+        Received 'data' in the upstream side of 'self.circuit'.
         'data' is an obfsproxy.network.buffer.Buffer.
         """
-        pass
 
     def handle_socks_args(self, args):
         """
         'args' is a list of k=v strings that serve as configuration
         parameters to the pluggable transport.
         """
-        pass
 
     @classmethod
     def register_external_mode_cli(cls, subparser):
diff --git a/obfsproxy/transports/dummy.py b/obfsproxy/transports/dummy.py
index 9e46f8e..8755282 100644
--- a/obfsproxy/transports/dummy.py
+++ b/obfsproxy/transports/dummy.py
@@ -13,21 +13,25 @@ class DummyTransport(BaseTransport):
     """
 
     def __init__(self, transport_config):
-        pass
+        """
+        If you override __init__, you ought to call the super method too.
+        """
+
+        super(DummyTransport, self).__init__(transport_config)
 
-    def receivedDownstream(self, data, circuit):
+    def receivedDownstream(self, data):
         """
         Got data from downstream; relay them upstream.
         """
 
-        circuit.upstream.write(data.read())
+        self.circuit.upstream.write(data.read())
 
-    def receivedUpstream(self, data, circuit):
+    def receivedUpstream(self, data):
         """
         Got data from upstream; relay them downstream.
         """
 
-        circuit.downstream.write(data.read())
+        self.circuit.downstream.write(data.read())
 
 class DummyClient(DummyTransport):
 
diff --git a/obfsproxy/transports/obfs2.py b/obfsproxy/transports/obfs2.py
index 3b687a5..f0f1064 100644
--- a/obfsproxy/transports/obfs2.py
+++ b/obfsproxy/transports/obfs2.py
@@ -53,6 +53,7 @@ class Obfs2Transport(base.BaseTransport):
 
     def __init__(self, transport_config):
         """Initialize the obfs2 pluggable transport."""
+        super(Obfs2Transport, self).__init__(transport_config)
 
         # Check if the shared_secret class attribute was already
         # instantiated. If not, instantiate it now.
@@ -149,7 +150,7 @@ class Obfs2Transport(base.BaseTransport):
 
         self.shared_secret = args[0][14:]
 
-    def handshake(self, circuit):
+    def handshake(self):
         """
         Do the obfs2 handshake:
         SEED | E_PAD_KEY( UINT32(MAGIC_VALUE) | UINT32(PADLEN) | WR(PADLEN) )
@@ -170,9 +171,9 @@ class Obfs2Transport(base.BaseTransport):
                   "initiator" if self.we_are_initiator else "responder",
                   len(handshake_message), padding_length)
 
-        circuit.downstream.write(handshake_message)
+        self.circuit.downstream.write(handshake_message)
 
-    def receivedUpstream(self, data, circuit):
+    def receivedUpstream(self, data):
         """
         Got data from upstream. We need to obfuscated and proxy them downstream.
         """
@@ -183,9 +184,9 @@ class Obfs2Transport(base.BaseTransport):
 
         log.debug("obfs2 receivedUpstream: Transmitting %d bytes.", len(data))
         # Encrypt and proxy them.
-        circuit.downstream.write(self.send_crypto.crypt(data.read()))
+        self.circuit.downstream.write(self.send_crypto.crypt(data.read()))
 
-    def receivedDownstream(self, data, circuit):
+    def receivedDownstream(self, data):
         """
         Got data from downstream. We need to de-obfuscate them and
         proxy them upstream.
@@ -243,10 +244,10 @@ class Obfs2Transport(base.BaseTransport):
 
         if self.pending_data_to_send:
             log.debug("%s: We got pending data to send and our crypto is ready. Pushing!" % log_prefix)
-            self.receivedUpstream(circuit.upstream.buffer, circuit) # XXX touching guts of network.py
+            self.receivedUpstream(self.circuit.upstream.buffer) # XXX touching guts of network.py
             self.pending_data_to_send = False
 
-        circuit.upstream.write(self.recv_crypto.crypt(data.read()))
+        self.circuit.upstream.write(self.recv_crypto.crypt(data.read()))
 
     def _derive_crypto(self, pad_string): # XXX consider secret_seed
         """
diff --git a/obfsproxy/transports/obfs3.py b/obfsproxy/transports/obfs3.py
index 8f95229..32d03f9 100644
--- a/obfsproxy/transports/obfs3.py
+++ b/obfsproxy/transports/obfs3.py
@@ -33,6 +33,7 @@ class Obfs3Transport(base.BaseTransport):
 
     def __init__(self, transport_config):
         """Initialize the obfs3 pluggable transport."""
+        super(Obfs3Transport, self).__init__(transport_config)
 
         # Our state.
         self.state = ST_WAIT_FOR_KEY
@@ -67,7 +68,7 @@ class Obfs3Transport(base.BaseTransport):
         self.recv_magic_const = None
         self.we_are_initiator = None
 
-    def handshake(self, circuit):
+    def handshake(self):
         """
         Do the obfs3 handshake:
         PUBKEY | WR(PADLEN)
@@ -80,9 +81,9 @@ class Obfs3Transport(base.BaseTransport):
                   "initiator" if self.we_are_initiator else "responder",
                   len(handshake_message), padding_length, repr(self.dh.get_public()))
 
-        circuit.downstream.write(handshake_message)
+        self.circuit.downstream.write(handshake_message)
 
-    def receivedUpstream(self, data, circuit):
+    def receivedUpstream(self, data):
         """
         Got data from upstream. We need to obfuscated and proxy them downstream.
         """
@@ -95,16 +96,16 @@ class Obfs3Transport(base.BaseTransport):
         log.debug("obfs3 receivedUpstream: Transmitting %d bytes.", len(message))
 
         # Proxy encrypted message.
-        circuit.downstream.write(message)
+        self.circuit.downstream.write(message)
 
-    def receivedDownstream(self, data, circuit):
+    def receivedDownstream(self, data):
         """
         Got data from downstream. We need to de-obfuscate them and
         proxy them upstream.
         """
 
         if self.state == ST_WAIT_FOR_KEY: # Looking for the other peer's pubkey
-            self._read_handshake(data, circuit)
+            self._read_handshake(data)
 
         if self.state == ST_SEARCHING_MAGIC: # Looking for the magic string
             self._scan_for_magic(data)
@@ -112,9 +113,9 @@ class Obfs3Transport(base.BaseTransport):
         if self.state == ST_OPEN: # Handshake is done. Just decrypt and read application data.
             log.debug("obfs3 receivedDownstream: Processing %d bytes of application data." %
                       len(data))
-            circuit.upstream.write(self.recv_crypto.crypt(data.read()))
+            self.circuit.upstream.write(self.recv_crypto.crypt(data.read()))
 
-    def _read_handshake(self, data, circuit):
+    def _read_handshake(self, data):
         """
         Read handshake message, parse the other peer's public key and
         set up our crypto.
@@ -152,7 +153,7 @@ class Obfs3Transport(base.BaseTransport):
         self.queued_data = ''
 
         log.debug("%s: Transmitting %d bytes (with magic)." % (log_prefix, len(message)))
-        circuit.downstream.write(message)
+        self.circuit.downstream.write(message)
 
         self.state = ST_SEARCHING_MAGIC
 

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