Browse Source

minor rearchitecting to support client..

tags/v0.1.0
John-Mark Gurney 5 years ago
parent
commit
01b664048e
1 changed files with 16 additions and 9 deletions
  1. +16
    -9
      twistednoise.py

+ 16
- 9
twistednoise.py View File

@@ -76,7 +76,7 @@ def genkeypair():


return pub, priv return pub, priv


class TwistedNoiseServerProtocol(twisted.internet.protocol.Protocol):
class TwistedNoiseProtocol(twisted.internet.protocol.Protocol):
'''This class acts as a Noise Protocol responder. The factory that '''This class acts as a Noise Protocol responder. The factory that
creates this Protocol is required to have the properties server_key creates this Protocol is required to have the properties server_key
and endpoint. and endpoint.
@@ -109,21 +109,28 @@ class TwistedNoiseServerProtocol(twisted.internet.protocol.Protocol):
if not self.noise.handshake_finished: if not self.noise.handshake_finished:
self.transport.write(self.noise.write_message()) self.transport.write(self.noise.write_message())
if self.noise.handshake_finished: if self.noise.handshake_finished:
self.transport.pauseProducing()

# start the connection to the endpoint
ep = endpoints.clientFromString(reactor, self.factory.endpoint)
epdef = ep.connect(ClientProxyFactory(self))
epdef.addCallback(self.proxyConnected)
self.handshakeFinished()
else: else:
r = self.noise.decrypt(data) r = self.noise.decrypt(data)


self.endpoint.transport.write(r) self.endpoint.transport.write(r)


def proxyConnected(self, endpoint):
def handshakeFinished(self):
raise NotImplementedError

def plaintextConnected(self, endpoint):
self.endpoint = endpoint self.endpoint = endpoint
self.transport.resumeProducing() self.transport.resumeProducing()


class TwistedNoiseServerProtocol(TwistedNoiseProtocol):
def handshakeFinished(self):
self.transport.pauseProducing()

# start the connection to the endpoint
ep = endpoints.clientFromString(reactor, self.factory.endpoint)
epdef = ep.connect(ClientProxyFactory(self))
epdef.addCallback(self.plaintextConnected)

class ClientProxyProtocol(twisted.internet.protocol.Protocol): class ClientProxyProtocol(twisted.internet.protocol.Protocol):
def dataReceived(self, data): def dataReceived(self, data):
self.factory.noiseproto.encData(data) self.factory.noiseproto.encData(data)
@@ -189,7 +196,7 @@ class TNServerTest(unittest.TestCase):
self.tempdir = None self.tempdir = None


@defer.inlineCallbacks @defer.inlineCallbacks
def test_testprotocol(self):
def test_testserver(self):
# #
# How this test is plumbed: # How this test is plumbed:
# #


Loading…
Cancel
Save