From 1f4fa04d3b3d357cb921f12de42708d1d744b536 Mon Sep 17 00:00:00 2001 From: speyrefitte Date: Mon, 14 Oct 2013 15:04:35 +0200 Subject: [PATCH] protocol buffer is now raw layer --- README.md | 1 + rdpy/main.py | 2 +- rdpy/protocol/common/layer.py | 70 ++++++++++++++++++++++++++++++++++- rdpy/protocol/rdp/tpkt.py | 10 ++--- rdpy/protocol/rfb/rfb.py | 6 +-- 5 files changed, 78 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e69de29..7c7814d 100644 --- a/README.md +++ b/README.md @@ -0,0 +1 @@ +depend : python-qt4, python-twisted, python-qt4reactor diff --git a/rdpy/main.py b/rdpy/main.py index c49c49e..50e293a 100644 --- a/rdpy/main.py +++ b/rdpy/main.py @@ -31,6 +31,6 @@ if __name__ == '__main__': #w.show() from twisted.internet import reactor #reactor.connectTCP("127.0.0.1", 5901, factory.RfbFactory(protocol)) - reactor.connectTCP("192.168.122.184", 3389, factory.RfbFactory(tpkt.TPKT(tpdu.TPDU()))) + reactor.connectTCP("192.168.135.160", 3389, factory.RfbFactory(tpkt.TPKT(tpdu.TPDU()))) reactor.run() sys.exit(app.exec_()) \ No newline at end of file diff --git a/rdpy/protocol/common/layer.py b/rdpy/protocol/common/layer.py index 4085184..989b9d7 100644 --- a/rdpy/protocol/common/layer.py +++ b/rdpy/protocol/common/layer.py @@ -1,6 +1,7 @@ ''' @author: sylvain ''' + class Layer(object): ''' Network abstraction for protocol @@ -43,4 +44,71 @@ class Layer(object): default pass data to transport layer ''' if not self._transport is None: - self._transport.write(data) \ No newline at end of file + self._transport.write(data) + +class LayerAutomata(Layer): + ''' + layer with automata state + we can set next recv function used + ''' + def __init__(self, presentation = None): + ''' + Constructor + ''' + #call parent constructor + Layer.__init__(self, presentation) + + def setNextState(self, callback = None): + ''' + set recv function to next callback or + ''' + if callback is None: + callback = self.__class__.recv + + self.recv = callback + +#twitsed layer concept +from twisted.internet import protocol +#first that handle stream +from stream import Stream + +class RawLayer(protocol.Protocol, LayerAutomata): + ''' + Inherit from protocol twisted class + allow this protocol to wait until expected size of packet + and use Layer automata to call next automata state + ''' + def __init__(self, presentation = None): + ''' + Constructor + ''' + #call parent automata + LayerAutomata.__init__(self, presentation) + #data buffer received from twisted network layer + self._buffer = "" + #len of next packet pass to next state function + self._expectedLen = 0 + + def dataReceived(self, data): + ''' + inherit from protocol class + main event of received data + ''' + #add in buffer + self._buffer += data + #while buffer have expected size call local callback + while len(self._buffer) >= self._expectedLen: + #expected data is first expected bytes + expectedData = Stream(self._buffer[0:self._expectedLen]) + #rest is for next event of automata + self._buffer = self._buffer[self._expectedLen:] + #call recv function + self.recv(expectedData) + + def expect(self, expectedLen, callback = None): + ''' + new expected len + ''' + self._expectedLen = expectedLen + #default callback is recv from LayerAutomata + self.setNextState(callback) \ No newline at end of file diff --git a/rdpy/protocol/rdp/tpkt.py b/rdpy/protocol/rdp/tpkt.py index a7992cb..9b8bc90 100644 --- a/rdpy/protocol/rdp/tpkt.py +++ b/rdpy/protocol/rdp/tpkt.py @@ -1,11 +1,10 @@ ''' @author: sylvain ''' -from rdpy.protocol.common.protocolbuffer import ProtocolBuffer -from rdpy.protocol.common.layer import Layer +from rdpy.protocol.common.layer import RawLayer from rdpy.protocol.common.stream import Stream -class TPKT(ProtocolBuffer, Layer): +class TPKT(RawLayer): ''' TPKT layer in RDP protocol stack this layer only handle size of packet @@ -15,8 +14,7 @@ class TPKT(ProtocolBuffer, Layer): ''' Constructor ''' - ProtocolBuffer.__init__(self) - Layer.__init__(self, presentation) + RawLayer.__init__(self, presentation) #last packet version read from header self._lastPacketVersion = 0 #length may be coded on more than 1 bytes @@ -25,7 +23,7 @@ class TPKT(ProtocolBuffer, Layer): def connectionMade(self): ''' call when transport layer connection - is made (inherit from ProtocolBuffer) + is made (inherit from RawLayer) ''' #header is on two bytes self.expect(2, self.readHeader) diff --git a/rdpy/protocol/rfb/rfb.py b/rdpy/protocol/rfb/rfb.py index 1dc72ce..bfe5f7a 100644 --- a/rdpy/protocol/rfb/rfb.py +++ b/rdpy/protocol/rfb/rfb.py @@ -5,10 +5,10 @@ Created on 12 aout 2013 ''' from rdpy.protocol.common.stream import Stream -from rdpy.protocol.common.protocolbuffer import ProtocolBuffer +from rdpy.protocol.common.layer import RawLayer from types import PixelFormat,ProtocolVersion,SecurityType, Rectangle, Encoding -class Rfb(ProtocolBuffer): +class Rfb(RawLayer): ''' implements rfb protocol message ''' @@ -20,7 +20,7 @@ class Rfb(ProtocolBuffer): constructor mode can be only client or server mode ''' - ProtocolBuffer.__init__(self) + RawLayer.__init__(self) #usefull for rfb protocol self._callbackBody = None #mode of automata