當您使用 AWS API 在遠程 docker 容器 (ECS) 上運行命令時,AWS API 會回傳一個 websocket 以從中讀取命令的輸出。使用aws命令列實用程式(也使用 AWS API)時,讀取 websocket 流由session-manager-plugin處理。
session-manager-plugin 是用 GoLang 撰寫的,我一直在嘗試用 Python 重寫它的一部分。我不會說 GoLang,但我摸索著向會話管理器插件添加一些代碼以輸出它正在發送的原始二進制資料,并在使用二進制檔案時接收。
本質上,您運行的命令的輸出被分成訊息,每條訊息都帶有標頭和有效負載。每條訊息的標頭之一是 messageID,它是一個 UUID。每條訊息都需要通過告訴服務器您收到了具有該 UUID 的訊息來確認。
我遇到的問題是,在分析原始二進制資料時,我可以看到b'\x85\xc3\x12P\n\x08\xaf)\xfd\xba\x1b8\x1asMd'session-manager-plugin 正在確認使用 UUID 接收的訊息,其中包含一個資料包,內容如下:
b'{"AcknowledgedMessageType":"output_stream_data","AcknowledgedMessageId":"fdba1b38-1a73-4d64-85c3-12500a08af29","AcknowledgedMessageSequenceNumber":4,"IsSequentialMessage":true}'
為了弄清楚b'\x85\xc3\x12P\n\x08\xaf)\xfd\xba\x1b8\x1asMd'Python 中的 UUID 是什么,我這樣做:
import uuid
print(str(uuid.UUID(bytes=b'\x85\xc3\x12P\n\x08\xaf)\xfd\xba\x1b8\x1asMd')))
# 85c31250-0a08-af29-fdba-1b381a734d64
乍一看,收到的訊息的UUID和被確認的訊息的UUID不匹配,但仔細看就會發現,原來收到的訊息的UUID和UUID倒過來了被承認。有點。在 16 位元組的 UUID 中,前 8 個位元組在最后 8 個位元組之后。
85c31250-0a08-af29 - fdba-1b381a734d64
fdba1b38-1a73-4d64 - 85c3-12500a08af29
有什么理由會發生這種情況嗎?我解碼b'\x85\xc3\x12P\n\x08\xaf)\xfd\xba\x1b8\x1asMd'錯了嗎?
注意:從上面可以看出,Acknowledgment 資料包中的 UUID 在 JSON 內部。如果我解碼錯了,整個事情就會變成亂碼。
另請注意,這只是對完美作業的會話管理器插件通信流的分析。不管怎樣,這確實有效。我只是想弄清楚如何重新創建它。
uj5u.com熱心網友回復:
查看 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
}
源代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/537787.html
下一篇:AWSS3上傳速度?
