protocol buffer is now raw layer

This commit is contained in:
speyrefitte
2013-10-14 15:04:35 +02:00
parent 5b0eeb562a
commit 1f4fa04d3b
5 changed files with 78 additions and 11 deletions

View File

@@ -0,0 +1 @@
depend : python-qt4, python-twisted, python-qt4reactor

View File

@@ -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_())

View File

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

View File

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

View File

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