2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-10 22:20:50 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2023-07-17 23:01:06 +00:00
|
|
|
"fmt"
|
2023-07-01 09:51:13 +00:00
|
|
|
|
2023-07-17 23:01:06 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/routes"
|
2023-07-01 09:51:13 +00:00
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-06-30 09:54:08 +00:00
|
|
|
)
|
|
|
|
|
2023-07-17 23:01:06 +00:00
|
|
|
type IncomingRPCMessage struct {
|
|
|
|
*gmproto.IncomingRPCMessage
|
|
|
|
|
|
|
|
Pair *gmproto.RPCPairData
|
|
|
|
|
|
|
|
Message *gmproto.RPCMessageData
|
|
|
|
DecryptedData []byte
|
|
|
|
DecryptedMessage proto.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RPC) decryptInternalMessage(data *gmproto.IncomingRPCMessage) (*IncomingRPCMessage, error) {
|
|
|
|
msg := &IncomingRPCMessage{
|
|
|
|
IncomingRPCMessage: data,
|
|
|
|
}
|
|
|
|
switch data.BugleRoute {
|
|
|
|
case gmproto.BugleRoute_PairEvent:
|
|
|
|
msg.Pair = &gmproto.RPCPairData{}
|
|
|
|
err := proto.Unmarshal(data.GetMessageData(), msg.Pair)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case gmproto.BugleRoute_DataEvent:
|
|
|
|
msg.Message = &gmproto.RPCMessageData{}
|
|
|
|
err := proto.Unmarshal(data.GetMessageData(), msg.Message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if msg.Message.EncryptedData != nil {
|
|
|
|
msg.DecryptedData, err = r.client.AuthData.RequestCrypto.Decrypt(msg.Message.EncryptedData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
responseStruct := routes.Routes[msg.Message.GetAction()].ResponseStruct
|
|
|
|
msg.DecryptedMessage = responseStruct.ProtoReflect().New().Interface()
|
|
|
|
err = proto.Unmarshal(msg.DecryptedData, msg.DecryptedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown bugle route %d", data.BugleRoute)
|
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:20:50 +00:00
|
|
|
func (r *RPC) deduplicateHash(hash [32]byte) bool {
|
|
|
|
const recentUpdatesLen = len(r.recentUpdates)
|
|
|
|
for i := r.recentUpdatesPtr + recentUpdatesLen - 1; i >= r.recentUpdatesPtr; i-- {
|
|
|
|
if r.recentUpdates[i%recentUpdatesLen] == hash {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.recentUpdates[r.recentUpdatesPtr] = hash
|
|
|
|
r.recentUpdatesPtr = (r.recentUpdatesPtr + 1) % recentUpdatesLen
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:01:06 +00:00
|
|
|
func (r *RPC) logContent(res *IncomingRPCMessage) {
|
|
|
|
if r.client.Logger.Trace().Enabled() && res.DecryptedData != nil {
|
|
|
|
evt := r.client.Logger.Trace()
|
|
|
|
if res.DecryptedMessage != nil {
|
|
|
|
evt.Str("proto_name", string(res.DecryptedMessage.ProtoReflect().Descriptor().FullName()))
|
|
|
|
}
|
|
|
|
if res.DecryptedData != nil {
|
|
|
|
evt.Str("data", base64.StdEncoding.EncodeToString(res.DecryptedData))
|
|
|
|
} else {
|
|
|
|
evt.Str("data", "<null>")
|
|
|
|
}
|
|
|
|
evt.Msg("Got event")
|
2023-07-15 13:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:01:06 +00:00
|
|
|
func (r *RPC) deduplicateUpdate(msg *IncomingRPCMessage) bool {
|
|
|
|
if msg.DecryptedData != nil {
|
|
|
|
contentHash := sha256.Sum256(msg.DecryptedData)
|
2023-07-10 22:20:50 +00:00
|
|
|
if r.deduplicateHash(contentHash) {
|
|
|
|
r.client.Logger.Trace().Hex("data_hash", contentHash[:]).Msg("Ignoring duplicate update")
|
|
|
|
return true
|
|
|
|
}
|
2023-07-17 23:01:06 +00:00
|
|
|
r.logContent(msg)
|
2023-07-10 22:20:50 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-17 23:01:06 +00:00
|
|
|
func (r *RPC) HandleRPCMsg(rawMsg *gmproto.IncomingRPCMessage) {
|
|
|
|
msg, err := r.decryptInternalMessage(rawMsg)
|
|
|
|
if err != nil {
|
|
|
|
r.client.Logger.Err(err).Msg("Failed to decode incoming RPC message")
|
2023-07-09 11:16:52 +00:00
|
|
|
return
|
2023-07-01 09:51:13 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-17 23:01:06 +00:00
|
|
|
r.client.sessionHandler.queueMessageAck(msg.ResponseID)
|
|
|
|
if r.client.sessionHandler.receiveResponse(msg) {
|
2023-07-15 22:45:57 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-17 23:01:06 +00:00
|
|
|
switch msg.BugleRoute {
|
2023-07-17 13:51:31 +00:00
|
|
|
case gmproto.BugleRoute_PairEvent:
|
2023-07-17 23:01:06 +00:00
|
|
|
go r.client.handlePairingEvent(msg)
|
2023-07-17 13:51:31 +00:00
|
|
|
case gmproto.BugleRoute_DataEvent:
|
2023-07-15 22:45:57 +00:00
|
|
|
if r.skipCount > 0 {
|
|
|
|
r.skipCount--
|
|
|
|
r.client.Logger.Debug().
|
2023-07-17 23:01:06 +00:00
|
|
|
Any("action", msg.Message.GetAction()).
|
2023-07-15 22:45:57 +00:00
|
|
|
Int("remaining_skip_count", r.skipCount).
|
|
|
|
Msg("Skipped DataEvent")
|
2023-07-17 23:01:06 +00:00
|
|
|
if msg.DecryptedMessage != nil {
|
2023-07-15 22:45:57 +00:00
|
|
|
r.client.Logger.Trace().
|
2023-07-17 23:01:06 +00:00
|
|
|
Str("proto_name", string(msg.DecryptedMessage.ProtoReflect().Descriptor().FullName())).
|
|
|
|
Str("data", base64.StdEncoding.EncodeToString(msg.DecryptedData)).
|
2023-07-15 22:45:57 +00:00
|
|
|
Msg("Skipped event data")
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
2023-07-15 22:45:57 +00:00
|
|
|
return
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
2023-07-17 23:01:06 +00:00
|
|
|
r.client.handleUpdatesEvent(msg)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
}
|