[Pkg-privacy-commits] [obfsproxy] 87/353: Use a base class for Twisted protocols.
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:43 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 eeef3e828b998bbca90280f4906fabf326c9e1a3
Author: George Kadianakis <desnacked at riseup.net>
Date: Wed Nov 21 01:15:52 2012 +0200
Use a base class for Twisted protocols.
We currently have duplicate code in StaticDestinationProtocol and
SOCKSv4Protocol, because they both do generic connection housekeeping
stuff. To avoid having even more duplicate code when more types of
Protocols come around (like an Extended ORPort protocol), let's create
a generic Protocol that can be inherrited by the current protocols.
---
obfsproxy/network/network.py | 79 ++++++++++++++++++++++++++------------------
obfsproxy/network/socks.py | 28 ++--------------
2 files changed, 49 insertions(+), 58 deletions(-)
diff --git a/obfsproxy/network/network.py b/obfsproxy/network/network.py
index ec8dcbc..032a685 100644
--- a/obfsproxy/network/network.py
+++ b/obfsproxy/network/network.py
@@ -161,7 +161,50 @@ class Circuit(Protocol):
self.transport.circuitDestroyed(self)
-class StaticDestinationProtocol(Protocol):
+class GenericProtocol(Protocol):
+ """
+ Generic obfsproxy connection. Contains useful methods and attributes.
+
+ Attributes:
+ circuit: The circuit this connection belongs to.
+ buffer: Buffer that holds data that can't be proxied right
+ away. This can happen because the circuit is not yet
+ complete, or because the pluggable transport needs more
+ data before deciding what to do.
+ """
+ def __init__(self, circuit):
+ self.circuit = circuit
+ self.buffer = buffer.Buffer()
+ self.closed = False # True if connection is closed.
+
+ def connectionLost(self, reason):
+ log.debug("%s: Connection was lost (%s)." % (self.name, reason.getErrorMessage()))
+ self.circuit.close()
+
+ def connectionFailed(self, reason):
+ log.debug("%s: Connection failed to connect (%s)." % (self.name, reason.getErrorMessage()))
+ self.circuit.close()
+
+ def write(self, buf):
+ """
+ Write 'buf' to the underlying transport.
+ """
+ log.debug("%s: Writing %d bytes." % (self.name, len(buf)))
+
+ self.transport.write(buf)
+
+ def close(self):
+ """
+ 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 StaticDestinationProtocol(GenericProtocol):
"""
Represents a connection to a static destination (as opposed to a
SOCKS connection).
@@ -178,14 +221,11 @@ class StaticDestinationProtocol(Protocol):
def __init__(self, circuit, mode, peer_addr):
self.mode = mode
- self.circuit = circuit
- self.buffer = buffer.Buffer()
self.peer_addr = peer_addr
-
- self.closed = False # True if connection is closed.
-
self.name = "conn_%s" % hex(id(self))
+ GenericProtocol.__init__(self, circuit)
+
def connectionMade(self):
"""
Callback for when a connection is successfully established.
@@ -219,14 +259,6 @@ class StaticDestinationProtocol(Protocol):
self.circuit.setUpstreamConnection(self)
- def connectionLost(self, reason):
- log.debug("%s: Connection was lost (%s)." % (self.name, reason.getErrorMessage()))
- self.circuit.close()
-
- def connectionFailed(self, reason):
- log.debug("%s: Connection failed to connect (%s)." % (self.name, reason.getErrorMessage()))
- self.circuit.close()
-
def dataReceived(self, data):
"""
We received some data from the network. See if we have a
@@ -249,25 +281,6 @@ class StaticDestinationProtocol(Protocol):
self.circuit.dataReceived(self.buffer, self)
- def write(self, buf):
- """
- Write 'buf' to the underlying transport.
- """
- log.debug("%s: Writing %d bytes." % (self.name, len(buf)))
-
- self.transport.write(buf)
-
- def close(self):
- """
- 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 StaticDestinationClientFactory(Factory):
"""
Created when our listener receives a client connection. Makes the
diff --git a/obfsproxy/network/socks.py b/obfsproxy/network/socks.py
index 4ead850..ee708d8 100644
--- a/obfsproxy/network/socks.py
+++ b/obfsproxy/network/socks.py
@@ -60,7 +60,7 @@ class MySOCKSv4Outgoing(socks.SOCKSv4Outgoing, object):
# Monkey patches socks.SOCKSv4Outgoing with our own class.
socks.SOCKSv4Outgoing = MySOCKSv4Outgoing
-class SOCKSv4Protocol(socks.SOCKSv4):
+class SOCKSv4Protocol(socks.SOCKSv4, network.GenericProtocol):
"""
Represents an upstream connection from a SOCKS client to our SOCKS
server.
@@ -70,13 +70,10 @@ class SOCKSv4Protocol(socks.SOCKSv4):
"""
def __init__(self, circuit):
- self.circuit = circuit
- self.buffer = buffer.Buffer()
- self.closed = False # True if connection is closed.
-
self.name = "socks_up_%s" % hex(id(self))
- return socks.SOCKSv4.__init__(self)
+ network.GenericProtocol.__init__(self, circuit)
+ socks.SOCKSv4.__init__(self)
def dataReceived(self, data):
"""
@@ -108,25 +105,6 @@ class SOCKSv4Protocol(socks.SOCKSv4):
self.circuit.dataReceived(self.buffer, self)
- def connectionLost(self, reason):
- log.debug("%s: Connection was lost (%s)." % (self.name, reason.getErrorMessage()))
- self.circuit.close()
-
- def connectionFailed(self, reason):
- log.debug("%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