[Pkg-privacy-commits] [obfsproxy] 35/353: PEP 8 formatting
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:35 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 1c35c58872dc78730ea095a022eb642b436f5f12
Author: Brandon Wiley <brandon at blanu.net>
Date: Mon Aug 20 16:51:33 2012 -0500
PEP 8 formatting
---
src/cli.py | 8 ++---
src/obfsproxy/crypto/aes.py | 5 ++++
src/obfsproxy/framework/circuit.py | 47 +++++++++++++++++++++---------
src/obfsproxy/framework/managed/client.py | 5 +++-
src/obfsproxy/framework/proxy.py | 8 +++--
src/obfsproxy/framework/pump.py | 34 ++++++++++++++-------
src/obfsproxy/framework/socks.py | 18 ++++++++++--
src/obfsproxy/manager/clientManager.py | 5 +++-
src/obfsproxy/manager/manager.py | 3 ++
src/obfsproxy/manager/serverManager.py | 8 +++--
src/obfsproxy/transports/base.py | 10 +++++++
src/obfsproxy/transports/dummy.py | 6 ++++
src/obfsproxy/transports/dust_transport.py | 14 ++++++++-
src/obfsproxy/transports/obfs2.py | 30 +++++++++++++++++--
src/obfsproxy/transports/obfs3.py | 15 +++++++++-
src/obfsproxy/transports/rot13.py | 7 +++++
16 files changed, 183 insertions(+), 40 deletions(-)
diff --git a/src/cli.py b/src/cli.py
index d887e7d..8f08a80 100644
--- a/src/cli.py
+++ b/src/cli.py
@@ -28,10 +28,10 @@ sys.path.insert(0,
from pyptlib.easy.util import checkClientMode
try:
- from obfsproxy.framework.managed.server import ManagedServer
- from obfsproxy.framework.managed.client import ManagedClient
-except Exception as e:
- logging.error('Error loading framework: '+str(e))
+ from obfsproxy.framework.managed.server import ManagedServer
+ from obfsproxy.framework.managed.client import ManagedClient
+except Exception, e:
+ logging.error('Error loading framework: ' + str(e))
protocols = ['dummy', 'rot13']
diff --git a/src/obfsproxy/crypto/aes.py b/src/obfsproxy/crypto/aes.py
index fa4e572..ccddeb0 100644
--- a/src/obfsproxy/crypto/aes.py
+++ b/src/obfsproxy/crypto/aes.py
@@ -24,9 +24,12 @@ pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
class AESCoder(object):
+
""" This class a convenience wrapper for the AES cipher in CTR mode. """
+
def __init__(self, key):
""" Initialize AES with the given key and two counters, one for encryption and one for decryption. """
+
counterIn = Counter.new(128)
self.cipherIn = AES.new(key, mode=AES.MODE_CTR,
counter=counterIn)
@@ -37,10 +40,12 @@ class AESCoder(object):
def encrypt(self, data):
""" Encrypt the given data using AES in CTR mode and the key specified in __init__. """
+
return self.cipherOut.encrypt(pad(data))
def decrypt(self, data):
""" Decrypt the given data using AES in CTR mode and the key specified in __init__. """
+
return self.cipherIn.decrypt(data).rstrip(PADDING)
diff --git a/src/obfsproxy/framework/circuit.py b/src/obfsproxy/framework/circuit.py
index cc66842..73ab1d1 100644
--- a/src/obfsproxy/framework/circuit.py
+++ b/src/obfsproxy/framework/circuit.py
@@ -3,73 +3,94 @@
""" The circuit module contains the Buffer, Connection, and Circuit classes, which are used for managing connections. """
+
class Buffer(object):
+
""" The Buffer class manages an internal byte buffer, allowing for reading and writing. """
+
def __init__(self):
""" Initialize the Buffer with an empty byte buffer. """
- self.buffer=bytes('')
+
+ self.buffer = bytes('')
def read(self, x):
""" Read exactly x bytes from the buffer. If x bytes are not available, return None. """
- if len(self.buffer)<x:
+
+ if len(self.buffer) < x:
return None
else:
- data=self.buffer[:x]
- self.buffer=self.buffer[x:]
+ data = self.buffer[:x]
+ self.buffer = self.buffer[x:]
return data
def read_some(self):
""" Read all of the bytes from the buffer. """
+
return self.read(len(self.buffer))
def write(self, bs):
""" Write the bytes bs to the buffer. """
- self.buffer=self.buffer+bs
+
+ self.buffer = self.buffer + bs
+
class Connection(object):
+
""" The Connection class contains two buffers, incoming and outgoing. It allows for reading from the incoming buffer and writing to the outgoing buffer. A Connection can be inverted to flip the incoming and outgoing buffers. """
+
def __init__(self, incoming=None, outgoing=None):
""" Initialize the Connection's incoming and outgoing buffers. If either buffer is supplied as an optional parameters, reuse that Buffer, otherwise create a new empty Buffer. """
+
if incoming:
- self.incomingBuffer=incoming
+ self.incomingBuffer = incoming
else:
- self.incomingBuffer=Buffer()
+ self.incomingBuffer = Buffer()
if outgoing:
- self.outgoingBuffer=outgoing
+ self.outgoingBuffer = outgoing
else:
- self.outgoingBuffer=Buffer()
+ self.outgoingBuffer = Buffer()
def invert(self):
""" Returns a Connection with the incoming and outgoing buffers switched. """
+
return Connection(self.outgoingBuffer, self.incomingBuffer)
def read(self, x):
""" Read exactly x bytes from the incoming buffer. If x bytes are not available, return None. """
+
return self.incomingBuffer.read(x)
def read_some(self):
""" Read all of the bytes from the incoming buffer. """
+
return self.incomingBuffer.read_some()
def write(self, bs):
""" Write the bytes bs to the outgoing buffer. """
+
self.outgoingBuffer.write(bs)
+
class Circuit(object):
+
""" The Circuit class contains two connections, one upstream and one downstream. """
+
def __init__(self, downstream=None, upstream=None):
""" Initialize the Circuit's upstream and downstream conections. If either connection is supplied as an optional parameters, reuse that Connection, otherwise create a new Connection. """
+
if downstream:
- self.downstream=downstream
+ self.downstream = downstream
else:
- self.downstream=Connection()
+ self.downstream = Connection()
if remove:
- self.upstream=upstream
+ self.upstream = upstream
else:
- self.upstream=Connection()
+ self.upstream = Connection()
def invert(self):
""" Returns a Circuit with the incoming and outgoing buffers switched on the Connections. """
+
return Circuit(self.downstream.invert(), self.upstream.invert())
+
diff --git a/src/obfsproxy/framework/managed/client.py b/src/obfsproxy/framework/managed/client.py
index c928279..443bfd3 100755
--- a/src/obfsproxy/framework/managed/client.py
+++ b/src/obfsproxy/framework/managed/client.py
@@ -20,6 +20,7 @@ from obfsproxy.transports.obfs3 import Obfs3Client
from pyptlib.easy.client import init, reportSuccess, reportFailure, \
reportEnd
+
class TransportLaunchException(Exception):
pass
@@ -40,7 +41,7 @@ class ManagedClient:
matchedTransports = init(self.supportedTransports.keys())
for transport in matchedTransports:
try:
- logging.error('Launching %s' % (transport))
+ logging.error('Launching %s' % transport)
self.launchClient(transport, 8182)
reportSuccess(transport, 5, ('127.0.0.1', 8182), None,
None)
@@ -58,3 +59,5 @@ class ManagedClient:
clientClass = self.supportedTransports[name]
self.handler.setTransport(clientClass)
add_service(Service(self.handler.handle, port=port))
+
+
diff --git a/src/obfsproxy/framework/proxy.py b/src/obfsproxy/framework/proxy.py
index 03f7914..791082d 100644
--- a/src/obfsproxy/framework/proxy.py
+++ b/src/obfsproxy/framework/proxy.py
@@ -20,23 +20,27 @@ from obfsproxy.framework.pump import Pump
class ProxyHandler:
+
"""
The ProxyHandler class implements the server-side handling of pluggable transports.
"""
+
transport = None
def setTransport(self, transport):
""" setTransport sets the pluggable transport for this proxy server """
+
self.transport = transport
@_o
def handle(self, conn):
""" handle is called by the framework to establish a new proxy connection to the Tor server and start processing when an incoming client connection is established. """
+
print 'connection'
client = Client()
- yield client.connect('blanu.net', 80) # FIXME - remove hardcoded destination
+ yield client.connect('blanu.net', 80) # FIXME - remove hardcoded destination
- self.pump=Pump(conn, client, self.transport)
+ self.pump = Pump(conn, client, self.transport)
self.pump.run()
diff --git a/src/obfsproxy/framework/pump.py b/src/obfsproxy/framework/pump.py
index f840c74..08574d3 100644
--- a/src/obfsproxy/framework/pump.py
+++ b/src/obfsproxy/framework/pump.py
@@ -11,19 +11,29 @@ from monocle.stack.network import ConnectionLost
from obfsproxy.util import encode
from obfsproxy.framework.circuit import Circuit
+
class Pump(object):
+
""" The Pump class takes care of moving bytes between the upstream and downstream connections. """
- def __init__(self, downstream, upstream, transportClass):
+
+ def __init__(
+ self,
+ downstream,
+ upstream,
+ transportClass,
+ ):
""" Initializes the downstream and upstream instance variables, instantiates the transportClass, and sets up a circuit. """
- self.downstream=downstream
- self.upstream=upstream
- circuit=Circuit()
- self.transport=transportClass(circuit)
- self.circuit=circuit.invert()
+ self.downstream = downstream
+ self.upstream = upstream
+
+ circuit = Circuit()
+ self.transport = transportClass(circuit)
+ self.circuit = circuit.invert()
def run(self):
""" Calls the start event on the transport and initiates pumping between upstream and downstream connections in both directions. """
+
self.transport.start()
monocle.launch(self.pumpDownstream)
yield self.pumpUpstream()
@@ -31,8 +41,9 @@ class Pump(object):
@_o
def pumpDownstream(self):
""" Handle the downstream connection. """
+
while True:
- data=self.circuit.downstream.read_some()
+ data = self.circuit.downstream.read_some()
if data:
try:
yield self.downstream.write(data)
@@ -48,7 +59,7 @@ class Pump(object):
return
try:
- data = yield self.downstream.read_some()
+ data = (yield self.downstream.read_some())
if data:
self.circuit.downstream.write(data)
self.transport.receivedDownstream()
@@ -61,8 +72,9 @@ class Pump(object):
@_o
def pumpUpstream(self):
""" Handle the upstream connection. """
+
while True:
- data=self.circuit.upstream.read_some()
+ data = self.circuit.upstream.read_some()
if data:
try:
yield self.upstream.write(data)
@@ -78,7 +90,7 @@ class Pump(object):
return
try:
- data = yield self.upstream.read_some()
+ data = (yield self.upstream.read_some())
if data:
self.circuit.upstream.write(data)
self.transport.receivedUpstream()
@@ -87,3 +99,5 @@ class Pump(object):
return
except IOError:
return
+
+
diff --git a/src/obfsproxy/framework/socks.py b/src/obfsproxy/framework/socks.py
index 1a4015d..b71833a 100644
--- a/src/obfsproxy/framework/socks.py
+++ b/src/obfsproxy/framework/socks.py
@@ -19,8 +19,10 @@ from obfsproxy.util import encode
from obfsproxy.framework.pump import Pump
+
def uncompact(x):
""" uncompact is a convenience method for unpacking an IPv4 address from its byte representation. """
+
(ip, port) = unpack('!4sH', x)
return (inet_ntoa(ip), port)
@@ -28,8 +30,9 @@ def uncompact(x):
@_o
def readHandshake(input):
""" readHandshake reads the SOCKS handshake information to the SOCKS client. """
+
version = (yield input.read(1))
- logging.info('version: %s' % (encode(str(version))))
+ logging.info('version: %s' % encode(str(version)))
nauth = (yield input.read(1))
nauth = unpack('B', nauth)[0]
auths = []
@@ -42,12 +45,14 @@ def readHandshake(input):
@_o
def sendHandshake(output):
""" sendHandshake sends the SOCKS handshake information to the SOCKS client. """
+
yield output.write('\x05\x00')
@_o
def readRequest(input):
""" readRequest reads the SOCKS request information from the client and returns the bytes represneting the IPv4 destination. """
+
version = (yield input.read(1))
command = (yield input.read(1))
reserved = (yield input.read(1))
@@ -60,29 +65,34 @@ def readRequest(input):
@_o
def sendResponse(dest, output):
""" sendResponse sends the SOCKS response to the request. """
+
yield output.write('\x05\x00\x00\x01' + dest)
class SocksHandler:
+
"""
The SocksHandler class implements the client-side handling of pluggable transports.
"""
+
transport = None
def setTransport(self, transport):
""" setTransport sets the pluggable transport for this proxy server """
+
self.transport = transport
@_o
def handle(self, conn):
""" handle is called by the framework to establish a new connection to the proxy server and start processing when an incoming SOCKS client connection is established. """
+
logging.info('handle_socks')
yield readHandshake(conn)
logging.info('read handshake')
yield sendHandshake(conn)
logging.info('send handshake')
dest = (yield readRequest(conn))
- logging.info('read request: %s' % (str(dest)))
+ logging.info('read request: %s' % str(dest))
yield sendResponse(dest, conn)
logging.info('sent response')
@@ -95,5 +105,7 @@ class SocksHandler:
yield client.connect(addr, port)
logging.info('connected %s:%d' % (addr, port))
- self.pump=Pump(conn, client, self.transport)
+ self.pump = Pump(conn, client, self.transport)
self.pump.run()
+
+
diff --git a/src/obfsproxy/manager/clientManager.py b/src/obfsproxy/manager/clientManager.py
index c8d3ec9..ebadb26 100644
--- a/src/obfsproxy/manager/clientManager.py
+++ b/src/obfsproxy/manager/clientManager.py
@@ -14,19 +14,22 @@ from obfsproxy.manager.manager import Manager
class ClientManager(Manager):
+
""" The ClientManager class implemnts a client manager. """
+
def __init__(self, transport):
"""
Call superclass initializer to initialize the environment variables which are used by both clients and servers.
Then initialize the environment variable which is used bo just clients.
This is TOR_PT_CLIENT_TRANSPORTS.
"""
+
Manager.__init__(self)
os.environ['TOR_PT_CLIENT_TRANSPORTS'] = transport
if __name__ == '__main__':
- transport=sys.argv[1]
+ transport = sys.argv[1]
manager = ClientManager(transport)
manager.launch()
diff --git a/src/obfsproxy/manager/manager.py b/src/obfsproxy/manager/manager.py
index ffd888e..1e126ff 100644
--- a/src/obfsproxy/manager/manager.py
+++ b/src/obfsproxy/manager/manager.py
@@ -13,6 +13,7 @@ import subprocess
class Manager:
+
""" The Manager class is the base class for client and server managers. """
def __init__(self):
@@ -20,6 +21,7 @@ class Manager:
Initialize the environment variables which are used by both clients and servers.
These are TOR_PT_STATE_LOCATION and TOR_PT_MANAGED_TRANSPORT_VER
"""
+
os.environ['TOR_PT_STATE_LOCATION'] = '/'
os.environ['TOR_PT_MANAGED_TRANSPORT_VER'] = '1'
@@ -29,6 +31,7 @@ class Manager:
Launching of the client and server is identical.
The client/server behavior is determined by which environment variables have been set by the ClientManager and ServerManager subclasses.
"""
+
p = subprocess.Popen(['python', '-u', 'src/cli.py', '--managed'
], stdout=subprocess.PIPE)
line = p.stdout.readline().strip()
diff --git a/src/obfsproxy/manager/serverManager.py b/src/obfsproxy/manager/serverManager.py
index 39a95dc..cb522bc 100644
--- a/src/obfsproxy/manager/serverManager.py
+++ b/src/obfsproxy/manager/serverManager.py
@@ -14,22 +14,26 @@ from obfsproxy.manager.manager import Manager
class ServerManager(Manager):
+
""" The ServerManager class implemnts a client manager. """
+
def __init__(self, transport):
"""
Call superclass initializer to initialize the environment variables which are used by both clients and servers.
Then initialize the environment variables which are used bo just servers.
These are TOR_PT_EXTENDED_SERVER_PORT, TOR_PT_ORPORT, TOR_PT_SERVER_BINDADDR, and TOR_PT_SERVER_TRANSPORTS.
"""
+
Manager.__init__(self)
os.environ['TOR_PT_EXTENDED_SERVER_PORT'] = '127.0.0.1:22211'
os.environ['TOR_PT_ORPORT'] = '127.0.0.1:43210'
- os.environ['TOR_PT_SERVER_BINDADDR'] = transport+'-127.0.0.1:46466'
+ os.environ['TOR_PT_SERVER_BINDADDR'] = transport \
+ + '-127.0.0.1:46466'
os.environ['TOR_PT_SERVER_TRANSPORTS'] = transport
if __name__ == '__main__':
- transport=sys.argv[1]
+ transport = sys.argv[1]
manager = ServerManager(transport)
manager.launch()
diff --git a/src/obfsproxy/transports/base.py b/src/obfsproxy/transports/base.py
index 7c7fadc..8567894 100644
--- a/src/obfsproxy/transports/base.py
+++ b/src/obfsproxy/transports/base.py
@@ -7,12 +7,16 @@ 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.
"""
+
class BaseDaemon:
+
"""
The BaseDaemon class is a base class for implementing pluggable transport clients and server.
"""
+
def __init__(self, downstreamConnection, upstreamConnection):
""" Store the upstream and downstream sockets for use in other methods. """
+
self.downstreamConnection = downstreamConnection
self.upstreamConnection = upstreamConnection
@@ -27,6 +31,7 @@ class BaseDaemon:
It reads bytes from socket and appends them to data until data is equal to maxlen, or the socket has no more bytes ready.
It returns a new data object which is a combination of data and the bytes read from socket and which is <= maxlen.
"""
+
remaining = maxlen - len(data)
return data + socket.read(remaining)
@@ -42,6 +47,7 @@ class BaseDaemon:
If len(data) == maxlen then the state is set to the state is set to newState and True is returned.
Otherwise, the state stays the same and False is returned.
"""
+
if len(data) == maxlen:
state = newState
return True
@@ -54,6 +60,7 @@ class BaseDaemon:
In BaseDaemon it does nothing.
It is overridden by subclasses.
"""
+
pass
def receivedDownstream(self):
@@ -62,6 +69,7 @@ class BaseDaemon:
In BaseDaemon it does nothing.
It is overridden by subclasses.
"""
+
pass
def receivedUpstream(self):
@@ -70,6 +78,7 @@ class BaseDaemon:
In BaseDaemon it does nothing.
It is overridden by subclasses.
"""
+
pass
def end(self):
@@ -78,6 +87,7 @@ class BaseDaemon:
In BaseDaemon it does nothing.
It is overridden by subclasses.
"""
+
pass
diff --git a/src/obfsproxy/transports/dummy.py b/src/obfsproxy/transports/dummy.py
index eccc918..808ddbc 100644
--- a/src/obfsproxy/transports/dummy.py
+++ b/src/obfsproxy/transports/dummy.py
@@ -7,15 +7,18 @@ from obfsproxy.transports.base import BaseDaemon
class DummyDaemon(BaseDaemon):
+
"""
DummyDaemon is the base class for DummyClient and DummyServer.
Since the protocol is so simple, DummyDaemon provides all of the functionality for the dummy protocol implementation.
"""
+
def receivedDownstream(self):
"""
receivedDownstream is the event which is called when bytes are received from the downstream socket.
The dummy protocol just writes these to the upstream socket.
"""
+
data = self.downstreamConnection.readAll()
self.upstreamConnection.write(data)
@@ -24,11 +27,13 @@ class DummyDaemon(BaseDaemon):
receivedUpstream is the event which is called when bytes are received from the upstream socket.
The dummy protocol just writes these to the downstream socket.
"""
+
data = self.upstreamConnection.readAll()
self.downstreamConnection.write(data)
class DummyClient(DummyDaemon):
+
"""
DummyClient is a client for the 'dummy' protocol.
Since this protocol is so simple, the client and the server are identical and both just trivially subclass DummyDaemon.
@@ -38,6 +43,7 @@ class DummyClient(DummyDaemon):
class DummyServer(DummyDaemon):
+
"""
DummyServer is a server for the 'dummy' protocol.
Since this protocol is so simple, the client and the server are identical and both just trivially subclass DummyDaemon.
diff --git a/src/obfsproxy/transports/dust_transport.py b/src/obfsproxy/transports/dust_transport.py
index fd7ad86..14d46ed 100644
--- a/src/obfsproxy/transports/dust_transport.py
+++ b/src/obfsproxy/transports/dust_transport.py
@@ -18,16 +18,20 @@ HANDSHAKE_SIZE = IV_SIZE + KEY_SIZE
class DustDaemon(BaseDaemon):
+
"""
DustDaemon is the base class for DustClient and DustServer.
It implements the functionality which is common across both the client and server implementations.
"""
+
def __init__(self, downstreamConnection, upstreamConnection):
"""
Initializes the daemon with a downstream and upstream socket.
This also sets the protocol state to HANDSHAKE_WRITE and generates an ephemeral keypair.
"""
- BaseDaemon.__init__(self, downstreamConnection, upstreamConnection)
+
+ BaseDaemon.__init__(self, downstreamConnection,
+ upstreamConnection)
self.state = HANDSHAKE_WRITE
self.ekeypair = createEphemeralKeypair()
@@ -38,6 +42,7 @@ class DustDaemon(BaseDaemon):
This is the callback method which is called by the framework when a new connection has been made.
In the Dust protocol, on start the public part of the ephemeral keypair is written upstream.
"""
+
self.upstreamConnection.write(self.ekeypair.public.bytes)
def receivedDownstream(self):
@@ -45,6 +50,7 @@ class DustDaemon(BaseDaemon):
This is the callback method which is called by the framework when bytes have been received on the downstream socket.
In the Dust protocol, downstream bytes are buffered until the handshake is complete and the protocol is in STREAM mode, at which point all bytes received from downstream are encrypted and sent upstream.
"""
+
# If we're in streaming mode, encode and write the incoming data
if self.state == STREAM:
@@ -59,6 +65,7 @@ class DustDaemon(BaseDaemon):
This is the callback method which is called by the framework when bytes have been received on the upstream socket.
In the Dust protocol, the upstream handshake is read, and then the protocol is switched to STREAM mode, at which point all bytes received from upstream are encrypted and sent downstream.
"""
+
if self.state == HANDSHAKE:
self.epub = self.read(self.upstreamConnection, self.epub,
HANDSHAKE_SIZE)
@@ -84,22 +91,27 @@ class DustDaemon(BaseDaemon):
This is the callback method which is called by the framework when the connection is closed.
In DustDaemon it does nothing.
"""
+
pass
class DustClient(DustDaemon):
+
"""
DustClient is a client for the Dust protocol.
In this simplified implementation of the protocol, the client and the server are identical and both just trivially subclass DustDaemon.
"""
+
pass
class DustServer(DustDaemon):
+
"""
DustServer is a server for the Dust protocol.
In this simplified implementation of the protocol, the client and the server are identical and both just trivially subclass DustDaemon.
"""
+
pass
diff --git a/src/obfsproxy/transports/obfs2.py b/src/obfsproxy/transports/obfs2.py
index e18c3e4..f1c0988 100644
--- a/src/obfsproxy/transports/obfs2.py
+++ b/src/obfsproxy/transports/obfs2.py
@@ -35,6 +35,7 @@ STREAM = 4
def h(x):
""" H(x) is SHA256 of x. """
+
hasher = hashlib.sha256()
hasher.update(x)
return hasher.digest()
@@ -42,6 +43,7 @@ def h(x):
def hn(x, n):
""" H^n(x) is H(x) called iteratively n times. """
+
data = x
for x in range(n):
data = h(data)
@@ -50,18 +52,21 @@ def hn(x, n):
def e(k, s):
""" E(k,s) is the AES-CTR-128 encryption of s using K as key. """
+
cipher = AESCoder(k)
return cipher.encode(s)
def d(k, s):
""" D(k, s) is the AES-CTR-128 decryption of s using K as key. """
+
cipher = AESCoder(k)
return cipher.decode(s)
def uint32(n):
""" UINT32(n) is the 4 byte value of n in big-endian (network) order. """
+
return struct.pack('!I', n)
@@ -70,34 +75,41 @@ def decodeUint32(bs):
decodeUint32(bs) is the reverse of uint32(n).
It returns the int value represneted by the 4 byte big-endian (network) order encoding represented by bs.
"""
+
return struct.unpack('!I', bs)[0]
def sr(n):
""" SR(n) is n bytes of strong random data. """
+
return os.urandom(n)
def wr(n):
""" WR(n) is n bytes of weaker random data. """
+
return ''.join(chr(random.randint(0, 255)) for _ in range(n))
def mac(s, x):
""" # MAC(s, x) = H(s | x | s) """
+
return h(s + x + s)
class Obfs2Daemon(BaseDaemon):
+
"""
Obfs2Daemon implements the obfs2 protocol.
It is subclassed by Obfs2Client and Obfs2Server.
"""
+
def __init__(self, downstreamConnection, upstreamConnection):
"""
Initializes the Obfs2Daemon instance with the upstream and downstream connections.
Also initializes the seed, padkey, and padlen buffers to be empty byte strings.
"""
+
self.downstreamConnection = downstreamConnection
self.upstreamConnection = upstreamConnection
self.otherSeed = bytes('')
@@ -109,6 +121,7 @@ class Obfs2Daemon(BaseDaemon):
This is the callback method which is called by the framework when a new connection has been made.
In the obfs2 protocol, on start the seed, encrypted magic value, padding length, and padding are written upstream.
"""
+
# The initiator generates:
# INIT_SEED = SR(SEED_LENGTH)
@@ -133,6 +146,7 @@ class Obfs2Daemon(BaseDaemon):
This is the callback method which is called by the framework when bytes have been received on the downstream socket.
In the obfs2 protocol, downstream bytes are buffered until the handshake is complete and the protocol is in STREAM mode, at which point all bytes received from downstream are encrypted and sent upstream.
"""
+
if state == STREAM:
data = self.downstreamConnection.readAll()
encodedData = encode(data)
@@ -148,6 +162,7 @@ class Obfs2Daemon(BaseDaemon):
- padding
The protocol is then switched to STREAM mode, at which point all bytes received from upstream are encrypted and sent downstream.
"""
+
if state == READ_SEED:
self.otherSeed = self.read(self.upstreamConnection,
self.otherSeed, SEED_LENGTH)
@@ -156,8 +171,9 @@ class Obfs2Daemon(BaseDaemon):
self.otherPadKeyDerived = \
self.derivePadKey(self.otherSeed, not self.server)
elif state == READ_PADKEY:
- self.otherPadKeyEncrypted = self.read(self.upstreamConnection,
- self.otherPadKeyEncrypted, KEYLEN)
+ self.otherPadKeyEncrypted = \
+ self.read(self.upstreamConnection,
+ self.otherPadKeyEncrypted, KEYLEN)
if self.checkTransition(self.otherPadKeyEncrypted, KEYLEN,
READ_PADLEN):
if self.otherPadKeyEncrypted != self.otherPadKeyDerived:
@@ -204,6 +220,7 @@ class Obfs2Daemon(BaseDaemon):
def derivePadKey(self, seed, padString):
""" derivePadKey returns the MAC of the padString and seed. """
+
return mac(padString, seed)[:KEYLEN]
def deriveSecret(
@@ -213,6 +230,7 @@ class Obfs2Daemon(BaseDaemon):
server,
):
""" deriveSecret returns the MAC of the stored padString, the seed, and the otherSeed. """
+
if server:
# RESP_SECRET = MAC("Responder obfuscated data", INIT_SEED|RESP_SEED)
@@ -226,6 +244,7 @@ class Obfs2Daemon(BaseDaemon):
def initCipher(self, iv, key):
""" initCipher initializes the AES cipher using the given key and IV. """
+
coder = AESCoder(key)
coder.encode(iv)
return coder
@@ -235,24 +254,31 @@ class Obfs2Daemon(BaseDaemon):
This is the callback method which is called by the framework when the connection is closed.
In Obfs2Daemon it does nothing.
"""
+
pass
class Obfs2Client(Obfs2Daemon):
+
"""
Obfs2Client is a client for the obfs2 protocol.
The client and server differ in terms of their padding strings.
"""
+
def __init__(self, downstreamConnection, upstreamConnection):
self.padString = 'Initiator obfuscation padding'
self.otherPadString = 'Responder obfuscation padding'
class Obfs2Server(Obfs2Daemon):
+
"""
Obfs2Server is a server for the obfs2 protocol.
The client and server differ in terms of their padding strings.
"""
+
def __init__(self, downstreamConnection, upstreamConnection):
self.padString = 'Responder obfuscation padding'
self.otherPadString = 'Initiator obfuscation padding'
+
+
diff --git a/src/obfsproxy/transports/obfs3.py b/src/obfsproxy/transports/obfs3.py
index 0b9ec6a..9e4ca7b 100644
--- a/src/obfsproxy/transports/obfs3.py
+++ b/src/obfsproxy/transports/obfs3.py
@@ -19,16 +19,20 @@ HANDSHAKE_SIZE = IV_SIZE + KEY_SIZE
class Obfs3Daemon(BaseDaemon):
+
"""
Obfs2Daemon implements the obfs2 protocol.
It is subclassed by Obfs2Client and Obfs2Server.
"""
+
def __init__(self, downstreamConnection, upstreamConnection):
"""
Initializes the daemon with a downstream and upstream socket.
This also sets the protocol state to HANDSHAKE_WRITE and generates an ephemeral keypair.
"""
- BaseDaemon.__init__(self, downstreamConnection, upstreamConnection)
+
+ BaseDaemon.__init__(self, downstreamConnection,
+ upstreamConnection)
self.state = HANDSHAKE_WRITE
self.ekeypair = createEphemeralKeypair()
@@ -39,6 +43,7 @@ class Obfs3Daemon(BaseDaemon):
This is the callback method which is called by the framework when a new connection has been made.
In the obfs3 protocol, on start the public part of the ephemeral keypair is written upstream.
"""
+
self.upstreamConnection.write(self.ekeypair.public.bytes)
def receivedDownstream(self):
@@ -61,6 +66,7 @@ class Obfs3Daemon(BaseDaemon):
This is the callback method which is called by the framework when bytes have been received on the upstream socket.
In the obfs3 protocol, the upstream handshake is read, and then the protocol is switched to STREAM mode, at which point all bytes received from upstream are encrypted and sent downstream.
"""
+
if self.state == HANDSHAKE:
self.epub = self.read(self.upstreamConnection, self.epub,
HANDSHAKE_SIZE)
@@ -86,20 +92,27 @@ class Obfs3Daemon(BaseDaemon):
This is the callback method which is called by the framework when the connection is closed.
In Obfs3Daemon it does nothing.
"""
+
pass
class Obfs3Client(Obfs3Daemon):
+
"""
Obfs3Client is a client for the obfs3 protocol.
In this simplified implementation of the protocol, the client and the server are identical and both just trivially subclass DustDaemon.
"""
+
pass
class Obfs3Server(Obfs3Daemon):
+
"""
Obfs3Server is a server for the obfs3 protocol.
In this simplified implementation of the protocol, the client and the server are identical and both just trivially subclass DustDaemon.
"""
+
pass
+
+
diff --git a/src/obfsproxy/transports/rot13.py b/src/obfsproxy/transports/rot13.py
index 79029cf..6e8dfe4 100644
--- a/src/obfsproxy/transports/rot13.py
+++ b/src/obfsproxy/transports/rot13.py
@@ -3,7 +3,9 @@
""" This module contains an implementation of the 'rot13' transport. """
+
class Rot13Daemon:
+
"""
Rot13Daemon is the base class for Rot13Client and Rot13Server.
Since the protocol is so simple, Rot13Daemon provides all of the functionality for the rot13 protocol implementation.
@@ -13,6 +15,7 @@ class Rot13Daemon:
"""
The rot13 method performs a rot13 transformation on just the alphabetical characters in the data.
"""
+
for x in range(len(data)):
ascii = ord(data[x])
if ascii >= 97 and ascii <= 122: # a-z
@@ -30,6 +33,7 @@ class Rot13Daemon:
receivedDownstream is the event which is called when bytes are received from the downstream socket.
The rot13 protocol encodes them with the rot13 function and then writes the result to the upstream socket.
"""
+
return self.rot13(data)
def receivedUpstream(self, data):
@@ -37,10 +41,12 @@ class Rot13Daemon:
receivedUpstream is the event which is called when bytes are received from the upstream socket.
The rot13 protocol encodes them with the rot13 function and then writes the result to the downstream socket.
"""
+
return self.rot13(data)
class Rot13Client(Rot13Daemon):
+
"""
Rot13Client is a client for the 'rot13' protocol.
Since this protocol is so simple, the client and the server are identical and both just trivially subclass Rot13Daemon.
@@ -50,6 +56,7 @@ class Rot13Client(Rot13Daemon):
class Rot13Server:
+
"""
Rot13Server is a server for the 'rot13' protocol.
Since this protocol is so simple, the client and the server are identical and both just trivially subclass Rot13Daemon.
--
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