简体   繁体   English

适用于python3的AMF序列化

[英]AMF serialization for python3

I am trying to write a python3 encoder/decoder for AMF. 我正在尝试为AMF写一个python3编码器/解码器。

The reason I'm doing it is because I didn't find a suitable library that works on python3 (I'm looking for a non-obtrusive library - one that will provide me with the methods and let me handle the gateway myself) 我这样做的原因是因为我找不到适合python3的合适的库(我正在寻找一种非干扰性的库-一个可以为我提供方法并让我自己处理网关的库)

Avaialble libraries I tested for python are amfast, pyamf and amfy. 我为python测试的Avaialble库为amfast,pyamf和amfy。 While the first 2 are for python2 (several forks of pyamf suggest that they support python3 but I coudn't get it to work), amfy was designed for python3 but lacks some features that I need (specifically object serialization). 虽然前两个是针对python2的(pyamf的多个分支表明它们支持python3,但我无法使其正常工作),但amfy是为python3设计的,但缺少我需要的一些功能(特别是对象序列化)。

Reading through the specification of AMF0 and AMF3, I was able to add a package encoder/decoder but I stumbled on object serialization and the available documentation was not enough (would love to see some examples). 通读AMF0和AMF3的规范,我能够添加一个包编码器/解码器,但是我偶然发现了对象序列化并且可用的文档还不够(很想看看一些示例)。 Existing libraries were of no help either. 现有的库也没有帮助。

Using remoteObject (in flex), I managed to send the following request to my parser: 通过使用remoteObject(在flex中),我设法将以下请求发送到解析器:

b'\x00\x03\x00\x00\x00\x01\x00\x04null\x00\x02/1\x00\x00\x00\xe0\n\x00\x00\x00\x01\x11
\n\x81\x13Mflex.messaging.messages.CommandMessage\x13operation\x1bcorrelationId\x13
timestamp\x11clientId\x15timeToLive\tbody\x0fheaders\x17destination\x13messageId\x04\x05
\x06\x01\x04\x00\x01\x04\x00\n\x0b\x01\x01\n\x05\tDSId\x06\x07nil%DSMessagingVersion\x04
\x01\x01\x06\x01\x06I03ACB769-9733-6A6C-0923-79F667AE8249'

(notice that newlines were introduced to make the request more readable) (请注意,引入了换行符以使请求更具可读性)

The headers are parsed OK but when I get to the first object (\\n near the end of the first line), it is marked as a reference (LSB = 0) while there is no other object it can reference to. 头被解析好了,但是当我到达第一个对象(\\ n在第一行的结尾附近)时,它被标记为引用(LSB = 0),而没有其他对象可以引用。

am I reading this wrong? 我读错了吗? Is this a malformed bytes request? 这是格式错误的字节请求吗? Any help decoding these bytes will be welcomed. 欢迎对这些字节进行解码的任何帮助。

From the AMF3 spec , section 4.1 NetConnection and AMF3 : 根据AMF3规范的4.1NetConnection和AMF3

The format of this messaging structure is AMF 0 (See [AMF0]. A context header value or message body can switch to AMF 3 encoding using the special avmplus-object-marker type. 此消息传递结构的格式为AMF 0(请参阅[AMF0]。上下文标头值或消息正文可以使用特殊的avmplus-object-marker类型切换为AMF 3编码。

What this means is that by default, the message body must be parsed as AMF0. 这意味着默认情况下,消息正文必须解析为AMF0。 Only when encountering an avmplus-object-marker (0x11) should you switch to AMF3. 仅当遇到avmplus-object-marker(0x11)时,才应切换到AMF3。 As a result, the 0x0a type marker in your value is not actually an AMF3 object-marker, but an AMF0 strict-array-marker. 结果,您值中的0x0a类型标记实际上不是AMF3对象标记,而是AMF0严格数组标记。

Looking at section 2.12 Strict Array Type in the AMF0 spec , we can see that this type is simply defined as an u32 array-count, followed that number of value-types. 查看AMF0规范中的2.12严格数组类型 ,我们可以看到此类型只是定义为u32数组计数,后跟该数量的值类型。

In your data, the array-count is 0x00, 0x00, 0x00, 0x01 (ie 1), and the value following that has a type marker of 0x11 - which is the avmplus-object-marker mentioned above. 在您的数据中,数组计数为0x00, 0x00, 0x00, 0x01 (即1),其后的值带有类型标记0x11,即上面提到的avmplus-object-marker。 Thus, only after starting to parse the AMF0 array contents should you actually switch to AMF3 to parse the following object. 因此,只有在开始解析AMF0数组内容之后,才应实际切换到AMF3来解析以下对象。

In this case, the object then is an actual AMF3 object (type marker 0x0a ), followed by a non-dynamic U29O-traits with 9 sealed members. 在这种情况下,该对象就是一个实际的AMF3对象(类型标记0x0a ),后跟具有9个密封成员的非动态U29O特性。 But I'm sure you can take it from here. 但我相信您可以从这里拿走它。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM