2023-07-09 11:16:52 +00:00
|
|
|
package pblite
|
|
|
|
|
|
|
|
import (
|
2023-07-10 22:20:50 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-07-09 11:16:52 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/binary"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/crypto"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/routes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DevicePair struct {
|
|
|
|
Mobile *binary.Device `json:"mobile,omitempty"`
|
|
|
|
Browser *binary.Device `json:"browser,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestData struct {
|
2023-07-15 22:45:57 +00:00
|
|
|
RequestID string `json:"requestId,omitempty"`
|
2023-07-09 11:16:52 +00:00
|
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
|
|
Action binary.ActionType `json:"action,omitempty"`
|
|
|
|
Bool1 bool `json:"bool1,omitempty"`
|
|
|
|
Bool2 bool `json:"bool2,omitempty"`
|
|
|
|
EncryptedData []byte `json:"requestData,omitempty"`
|
2023-07-10 22:20:50 +00:00
|
|
|
RawDecrypted []byte `json:"-,omitempty"`
|
|
|
|
Decrypted proto.Message `json:"decrypted,omitempty"`
|
2023-07-09 11:16:52 +00:00
|
|
|
Bool3 bool `json:"bool3,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2023-07-15 22:45:57 +00:00
|
|
|
ResponseID string `json:"responseId,omitempty"`
|
2023-07-09 11:16:52 +00:00
|
|
|
BugleRoute binary.BugleRoute `json:"bugleRoute,omitempty"`
|
|
|
|
StartExecute string `json:"startExecute,omitempty"`
|
|
|
|
MessageType binary.MessageType `json:"eventType,omitempty"`
|
|
|
|
FinishExecute string `json:"finishExecute,omitempty"`
|
|
|
|
MillisecondsTaken string `json:"millisecondsTaken,omitempty"`
|
|
|
|
Devices *DevicePair `json:"devices,omitempty"`
|
|
|
|
Data RequestData `json:"data,omitempty"`
|
|
|
|
SignatureId string `json:"signatureId,omitempty"`
|
|
|
|
Timestamp string `json:"timestamp"`
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:49:51 +00:00
|
|
|
func DecryptInternalMessage(internalMessage *binary.InternalMessage, cryptor *crypto.Cryptor) (*Response, error) {
|
2023-07-09 11:16:52 +00:00
|
|
|
var resp *Response
|
|
|
|
switch internalMessage.Data.BugleRoute {
|
|
|
|
case binary.BugleRoute_PairEvent:
|
|
|
|
decodedData := &binary.PairEvents{}
|
2023-07-15 17:43:28 +00:00
|
|
|
decodeErr := proto.Unmarshal(internalMessage.Data.ProtobufData, decodedData)
|
2023-07-09 11:16:52 +00:00
|
|
|
if decodeErr != nil {
|
|
|
|
return nil, decodeErr
|
|
|
|
}
|
|
|
|
resp = newResponseFromPairEvent(internalMessage.GetData(), decodedData)
|
|
|
|
case binary.BugleRoute_DataEvent:
|
|
|
|
internalRequestData := &binary.InternalRequestData{}
|
2023-07-15 17:43:28 +00:00
|
|
|
decodeErr := proto.Unmarshal(internalMessage.Data.ProtobufData, internalRequestData)
|
2023-07-09 11:16:52 +00:00
|
|
|
if decodeErr != nil {
|
|
|
|
return nil, decodeErr
|
|
|
|
}
|
|
|
|
if internalRequestData.EncryptedData != nil {
|
2023-07-10 22:20:50 +00:00
|
|
|
decryptedBytes, err := cryptor.Decrypt(internalRequestData.EncryptedData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
2023-07-10 22:20:50 +00:00
|
|
|
responseStruct := routes.Routes[internalRequestData.GetAction()].ResponseStruct
|
|
|
|
deserializedData := responseStruct.ProtoReflect().New().Interface()
|
|
|
|
err = proto.Unmarshal(decryptedBytes, deserializedData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp = newResponseFromDataEvent(internalMessage.GetData(), internalRequestData, decryptedBytes, deserializedData)
|
2023-07-09 11:16:52 +00:00
|
|
|
} else {
|
2023-07-10 22:20:50 +00:00
|
|
|
resp = newResponseFromDataEvent(internalMessage.GetData(), internalRequestData, nil, nil)
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newResponseFromPairEvent(internalMsg *binary.InternalMessageData, data *binary.PairEvents) *Response {
|
|
|
|
resp := &Response{
|
2023-07-15 22:45:57 +00:00
|
|
|
ResponseID: internalMsg.GetResponseID(),
|
2023-07-09 11:16:52 +00:00
|
|
|
BugleRoute: internalMsg.GetBugleRoute(),
|
|
|
|
StartExecute: internalMsg.GetStartExecute(),
|
|
|
|
MessageType: internalMsg.GetMessageType(),
|
|
|
|
FinishExecute: internalMsg.GetFinishExecute(),
|
|
|
|
MillisecondsTaken: internalMsg.GetMillisecondsTaken(),
|
|
|
|
Devices: &DevicePair{
|
|
|
|
Mobile: internalMsg.GetMobile(),
|
|
|
|
Browser: internalMsg.GetBrowser(),
|
|
|
|
},
|
|
|
|
Data: RequestData{
|
|
|
|
Decrypted: data,
|
|
|
|
},
|
|
|
|
Timestamp: internalMsg.GetTimestamp(),
|
|
|
|
SignatureId: internalMsg.GetSignatureID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:20:50 +00:00
|
|
|
func newResponseFromDataEvent(internalMsg *binary.InternalMessageData, internalRequestData *binary.InternalRequestData, rawData []byte, decrypted protoreflect.ProtoMessage) *Response {
|
2023-07-09 11:16:52 +00:00
|
|
|
resp := &Response{
|
2023-07-15 22:45:57 +00:00
|
|
|
ResponseID: internalMsg.GetResponseID(),
|
2023-07-09 11:16:52 +00:00
|
|
|
BugleRoute: internalMsg.GetBugleRoute(),
|
|
|
|
StartExecute: internalMsg.GetStartExecute(),
|
|
|
|
MessageType: internalMsg.GetMessageType(),
|
|
|
|
FinishExecute: internalMsg.GetFinishExecute(),
|
|
|
|
MillisecondsTaken: internalMsg.GetMillisecondsTaken(),
|
|
|
|
Devices: &DevicePair{
|
|
|
|
Mobile: internalMsg.GetMobile(),
|
|
|
|
Browser: internalMsg.GetBrowser(),
|
|
|
|
},
|
|
|
|
Data: RequestData{
|
2023-07-15 22:45:57 +00:00
|
|
|
RequestID: internalRequestData.GetSessionID(),
|
2023-07-09 11:16:52 +00:00
|
|
|
Timestamp: internalRequestData.GetTimestamp(),
|
|
|
|
Action: internalRequestData.GetAction(),
|
|
|
|
Bool1: internalRequestData.GetBool1(),
|
|
|
|
Bool2: internalRequestData.GetBool2(),
|
|
|
|
EncryptedData: internalRequestData.GetEncryptedData(),
|
|
|
|
Decrypted: decrypted,
|
2023-07-10 22:20:50 +00:00
|
|
|
RawDecrypted: rawData,
|
2023-07-09 11:16:52 +00:00
|
|
|
Bool3: internalRequestData.GetBool3(),
|
|
|
|
},
|
|
|
|
SignatureId: internalMsg.GetSignatureID(),
|
|
|
|
Timestamp: internalMsg.GetTimestamp(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|