为什么来自AWS网络套接字的uuid是反向的?



当您使用AWS API在远程docker容器(ECS)上运行命令时,AWS API会返回一个websocket来读取命令的输出。当使用aws命令行实用程序(也使用AWS API)时,读取websocket流由session-manager-plugin处理。

session-manager-plugin是用GoLang编写的,我一直在尝试用Python重写部分内容。我不会说GoLang,但我摸索着通过添加一些代码到session-manager-plugin来输出它正在发送的原始二进制数据,并在使用二进制时接收。

实际上,您运行的命令的输出被分割成消息,每个消息都带有头和有效负载。每条消息的标头之一是messageID,这是一个UUID。每条消息都需要通过告诉服务器您已收到具有该UUID的消息来得到确认。

我遇到的问题是,在分析原始二进制数据时,我可以看到收到UUIDb'x85xc3x12Pnx08xaf)xfdxbax1b8x1asMd'的消息正在被session-manager-plugin确认,其中有一个数据包,上面写着:

b'{"AcknowledgedMessageType":"output_stream_data","AcknowledgedMessageId":"fdba1b38-1a73-4d64-85c3-12500a08af29","AcknowledgedMessageSequenceNumber":4,"IsSequentialMessage":true}'

要找出Python中的UUIDb'x85xc3x12Pnx08xaf)xfdxbax1b8x1asMd'是什么,我这样做:

import uuid
print(str(uuid.UUID(bytes=b'x85xc3x12Pnx08xaf)xfdxbax1b8x1asMd')))
# 85c31250-0a08-af29-fdba-1b381a734d64

乍一看,接收到的消息的UUID和被确认的消息的UUID不匹配,但是如果仔细观察,您会发现接收到的原始消息的UUID与被确认的UUID相反。排序的。在16字节的UUID中,前8个字节位于后8个字节之后。

85 c31250-0a08-af29——fdba-1b381a734d64

fdba1b38-1a73-4d64-85 c3 - 12500 a08af29

有什么原因会发生这种情况吗?我解码b'x85xc3x12Pnx08xaf)xfdxbax1b8x1asMd'错了吗?

注意:从上面可以看到,确认包中的UUID在JSON中。如果我解码错了,整个东西就是胡言乱语。

还要注意,这只是对一个完美工作的会话管理器插件通信流的分析。不管怎样,这是可行的。我只是想知道怎么做,这样我就能重新创造它。

查看session-manager-plugin的源代码,它似乎读取前八个字节作为最不重要的字节,然后读取接下来的八个字节作为最重要的字节,然后按MSB, LSB的顺序追加。在我看来,这会产生你所看到的行为。

// getUuid gets the 128bit uuid from an array of bytes starting from the offset.
func getUuid(log log.T, byteArray []byte, offset int) (result uuid.UUID, err error) {
byteArrayLength := len(byteArray)
if offset > byteArrayLength-1 || offset+16-1 > byteArrayLength-1 || offset < 0 {
log.Error("getUuid failed: Offset is invalid.")
return nil, errors.New("Offset is outside the byte array.")
}
leastSignificantLong, err := getLong(log, byteArray, offset)
if err != nil {
log.Error("getUuid failed: failed to get uuid LSBs Long value.")
return nil, errors.New("Failed to get uuid LSBs long value.")
}
leastSignificantBytes, err := longToBytes(log, leastSignificantLong)
if err != nil {
log.Error("getUuid failed: failed to get uuid LSBs bytes value.")
return nil, errors.New("Failed to get uuid LSBs bytes value.")
}
mostSignificantLong, err := getLong(log, byteArray, offset+8)
if err != nil {
log.Error("getUuid failed: failed to get uuid MSBs Long value.")
return nil, errors.New("Failed to get uuid MSBs long value.")
}
mostSignificantBytes, err := longToBytes(log, mostSignificantLong)
if err != nil {
log.Error("getUuid failed: failed to get uuid MSBs bytes value.")
return nil, errors.New("Failed to get uuid MSBs bytes value.")
}
uuidBytes := append(mostSignificantBytes, leastSignificantBytes...)
return uuid.New(uuidBytes), nil
}

源代码

相关内容

最新更新