From 0f09d2909ce960b1070f5e4b2b2d7b0845867588 Mon Sep 17 00:00:00 2001 From: speyrefitte Date: Wed, 16 Oct 2013 15:00:35 +0200 Subject: [PATCH] use new python object style to send packet --- rdpy/protocol/common/layer.py | 10 ++- rdpy/protocol/common/network.py | 42 +++++++++++- rdpy/protocol/rdp/tpdu.py | 2 +- rdpy/protocol/rfb/rfb.py | 53 +++++++-------- rdpy/protocol/rfb/types.py | 117 ++++++++++++++++---------------- rdpy/utils/__init__.py | 0 rdpy/utils/const.py | 48 +++++++++++++ 7 files changed, 181 insertions(+), 91 deletions(-) create mode 100644 rdpy/utils/__init__.py create mode 100644 rdpy/utils/const.py diff --git a/rdpy/protocol/common/layer.py b/rdpy/protocol/common/layer.py index 48b69f8..68b3202 100644 --- a/rdpy/protocol/common/layer.py +++ b/rdpy/protocol/common/layer.py @@ -70,7 +70,7 @@ class LayerAutomata(Layer): #twitsed layer concept from twisted.internet import protocol #first that handle stream -from stream import Stream +from network import Stream class RawLayer(protocol.Protocol, LayerAutomata): ''' @@ -118,4 +118,10 @@ class RawLayer(protocol.Protocol, LayerAutomata): ''' self._expectedLen = expectedLen #default callback is recv from LayerAutomata - self.setNextState(callback) \ No newline at end of file + self.setNextState(callback) + + def send(self, s): + ''' + send stream on tcp layer + ''' + self.transport.write(s.getvalue()) \ No newline at end of file diff --git a/rdpy/protocol/common/network.py b/rdpy/protocol/common/network.py index 4013481..41b90a6 100644 --- a/rdpy/protocol/common/network.py +++ b/rdpy/protocol/common/network.py @@ -26,14 +26,23 @@ class SimpleType(Type): ''' simple type ''' - def __init__(self, typeSize, structFormat, value): + def __init__(self, structFormat, typeSize, value): self._typeSize = typeSize self._structFormat = structFormat self._value = value @property def value(self): - return self._value; + ''' + shortcut to access inner value + ''' + return self._value + + def __cmp__(self, other): + ''' + compare inner value + ''' + return self._value.__cmp__(other.value) def write(self, s): ''' @@ -211,7 +220,34 @@ class UInt24Le(SimpleType): special read for a special type ''' self._value = struct.unpack(" 0: - securityList.append(data.read_uint8()) + securityElement = UInt8() + data.readType(securityElement) + securityList.append(securityElement) #select high security level for s in securityList: if s in [SecurityType.NONE, SecurityType.VNC] and s > self._securityLevel: @@ -147,15 +144,16 @@ class Rfb(RawLayer): break #send back security level choosen s = Stream() - s.write_uint8(self._securityLevel) - self.transport.write(s.getvalue()) + s.writeType(self._securityLevel) + self.send(s) self.expect(4, self.readSecurityResult) def readSecurityResult(self, data): ''' Read security result packet ''' - result = data.read_beuint32() + result = UInt32Be() + data.readType(result) if result == 1: print "Authentification failed" if self._version == ProtocolVersion.RFB003008: @@ -196,8 +194,9 @@ class Rfb(RawLayer): ''' read order receive from server ''' - packet_type = data.read_uint8() - if packet_type == 0: + packet_type = UInt8() + data.readType(packet_type) + if packet_type == UInt8(0): self.expect(3, self.readFrameBufferUpdateHeader) def readFrameBufferUpdateHeader(self, data): @@ -205,7 +204,7 @@ class Rfb(RawLayer): read frame buffer update packet header ''' #padding - data.read_uint8() + data.readType(UInt8()) self._nbRect = data.read_beuint16(); self.expect(12, self.readRectHeader) @@ -242,8 +241,8 @@ class Rfb(RawLayer): write client init packet ''' s = Stream() - s.write_uint8(self._sharedFlag) - self.transport.write(s.getvalue()) + s.writeType(self._sharedFlag) + self.send(s) self.expect(20, self.readServerInit) def sendSetPixelFormat(self, pixelFormat): diff --git a/rdpy/protocol/rfb/types.py b/rdpy/protocol/rfb/types.py index 04322e6..af8415c 100644 --- a/rdpy/protocol/rfb/types.py +++ b/rdpy/protocol/rfb/types.py @@ -1,72 +1,73 @@ ''' -Created on 22 aout 2013 - @author: sylvain ''' + +from rdpy.protocol.common.network import UInt8, UInt16Be, UInt32Be, String +from rdpy.protocol.common.network import CompositeType +from rdpy.utils.const import ConstAttributes + +@ConstAttributes class ProtocolVersion(object): ''' different ptotocol version ''' - UNKNOWN = 0 - RFB003003 = 1 - RFB003007 = 2 - RFB003008 = 3 - + UNKNOWN = String() + RFB003003 = String("RFB 003.003\n") + RFB003007 = String("RFB 003.007\n") + RFB003008 = String("RFB 003.008\n") + +@ConstAttributes class SecurityType: ''' - security type supported by twisted remote desktop + security type supported + (or will be supported) + by rdpy ''' - INVALID = 0 - NONE = 1 - VNC = 2 - -class PixelFormat(object): - def __init__(self): - self.BitsPerPixel = 32 - self.Depth = 24 - self.BigEndianFlag = False - self.TrueColorFlag = True - self.RedMax = 255 - self.GreenMax = 255 - self.BlueMax = 255 - self.RedShift = 16 - self.GreenShift = 8 - self.BlueShift = 0 - - def read(self, data): - self.BitsPerPixel = data.read_uint8() - self.Depth = data.read_uint8() - self.BigEndianFlag = data.read_uint8() - self.TrueColorFlag = data.read_uint8() - self.RedMax = data.read_beuint16() - self.GreenMax = data.read_beuint16() - self.BlueMax = data.read_beuint16() - self.RedShift = data.read_uint8() - self.GreenShift = data.read_uint8() - self.BlueShift = data.read_uint8() - #padding - data.read(3) - - def write(self, data): - data.write_uint8(self.BitsPerPixel) - data.write_uint8(self.Depth) - data.write_uint8(self.BigEndianFlag) - data.write_uint8(self.TrueColorFlag) - data.write_beuint16(self.RedMax) - data.write_beuint16(self.GreenMax) - data.write_beuint16(self.BlueMax) - data.write_uint8(self.RedShift) - data.write_uint8(self.GreenShift) - data.write_uint8(self.BlueShift) - #padding - data.write_uint8(0) - data.write_uint8(0) - data.write_uint8(0) - + INVALID = UInt8(0) + NONE = UInt8(1) + VNC = UInt8(2) + +@ConstAttributes class Pointer: - BUTTON1 = 0x1 - BUTTON2 = 0x2 - BUTTON3 = 0x4 + ''' + mouse event code (which button) + actually in RFB specification only$ + three buttons are supported + ''' + BUTTON1 = UInt32Be(0x1) + BUTTON2 = UInt32Be(0x2) + BUTTON3 = UInt32Be(0x4) + +class PixelFormat(CompositeType): + ''' + pixel format structure + ''' + def __init__(self): + CompositeType.__init__(self) + self.BitsPerPixel = UInt8(32) + self.Depth = UInt8(24) + self.BigEndianFlag = UInt8(False) + self.TrueColorFlag = UInt8(True) + self.RedMax = UInt16Be(255) + self.GreenMax = UInt16Be(255) + self.BlueMax = UInt16Be(255) + self.RedShift = UInt8(16) + self.GreenShift = UInt8(8) + self.BlueShift = UInt8(0) + self.padding1 = UInt16Be() + self.padding2 = UInt8() + +class ServerInit(CompositeType): + ''' + message send by server to indicate + framebuffer configuration + ''' + def __init__(self): + CompositeType.__init__(self) + self.width = UInt16Be() + self.height = UInt16Be() + self.pixelFormat = PixelFormat() + class Rectangle: def __init__(self): diff --git a/rdpy/utils/__init__.py b/rdpy/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rdpy/utils/const.py b/rdpy/utils/const.py new file mode 100644 index 0000000..6c1b012 --- /dev/null +++ b/rdpy/utils/const.py @@ -0,0 +1,48 @@ +''' +@author: sylvain +''' + +from copy import deepcopy + +class Constant(object): + ''' + Constant descriptor that deep copy value on get + ''' + def __init__(self, value): + ''' + Constructor keep value + ''' + self._value = value + + def __get__(self, obj, objType): + ''' + on get constant return deep copy of wrapped value + ''' + return deepcopy(self._value) + + def __set__(self, obj, value): + ''' + set is forbidden + in python 2.7 this function work only + on instanciate object + ''' + raise Exception("can't assign constant") + + def __delete__(self, obj): + ''' + delete is forbidden on constant + ''' + raise Exception("can't delete constant") + + +def ConstAttributes(cls): + ''' + transform all attributes of class + in constant attribute + only attributes which are not begining with '_' char + and are not callable + ''' + for c_name, c_value in cls.__dict__.iteritems(): + if c_name[0] != '_' and not callable(c_value): + setattr(cls, c_name, Constant(c_value)) + return cls