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-01 09:51:13 +00:00
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/pblite"
|
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-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-15 13:17:02 +00:00
|
|
|
func (r *RPC) logContent(res *pblite.Response) {
|
|
|
|
if r.client.Logger.Trace().Enabled() && res.Data.Decrypted != nil {
|
|
|
|
r.client.Logger.Trace().
|
|
|
|
Str("proto_name", string(res.Data.Decrypted.ProtoReflect().Descriptor().FullName())).
|
|
|
|
Str("data", base64.StdEncoding.EncodeToString(res.Data.RawDecrypted)).
|
|
|
|
Msg("Got event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:20:50 +00:00
|
|
|
func (r *RPC) deduplicateUpdate(response *pblite.Response) bool {
|
|
|
|
if response.Data.RawDecrypted != nil {
|
|
|
|
contentHash := sha256.Sum256(response.Data.RawDecrypted)
|
|
|
|
if r.deduplicateHash(contentHash) {
|
|
|
|
r.client.Logger.Trace().Hex("data_hash", contentHash[:]).Msg("Ignoring duplicate update")
|
|
|
|
return true
|
|
|
|
}
|
2023-07-15 13:17:02 +00:00
|
|
|
r.logContent(response)
|
2023-07-10 22:20:50 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (r *RPC) HandleRPCMsg(msg *gmproto.InternalMessage) {
|
2023-07-16 12:55:30 +00:00
|
|
|
response, decodeErr := pblite.DecryptInternalMessage(msg, r.client.AuthData.RequestCrypto)
|
2023-07-09 11:16:52 +00:00
|
|
|
if decodeErr != nil {
|
2023-07-15 12:49:51 +00:00
|
|
|
r.client.Logger.Error().Err(decodeErr).Msg("rpc decrypt msg err")
|
2023-07-09 11:16:52 +00:00
|
|
|
return
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
if response == nil {
|
2023-07-15 12:49:51 +00:00
|
|
|
r.client.Logger.Error().Msg("nil response in rpc handler")
|
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-15 22:45:57 +00:00
|
|
|
r.client.sessionHandler.queueMessageAck(response.ResponseID)
|
|
|
|
if r.client.sessionHandler.receiveResponse(response) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch response.BugleRoute {
|
2023-07-17 13:51:31 +00:00
|
|
|
case gmproto.BugleRoute_PairEvent:
|
2023-07-15 22:45:57 +00:00
|
|
|
go r.client.handlePairingEvent(response)
|
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().
|
|
|
|
Any("action", response.Data.Action).
|
|
|
|
Int("remaining_skip_count", r.skipCount).
|
|
|
|
Msg("Skipped DataEvent")
|
|
|
|
if response.Data.Decrypted != nil {
|
|
|
|
r.client.Logger.Trace().
|
|
|
|
Str("proto_name", string(response.Data.Decrypted.ProtoReflect().Descriptor().FullName())).
|
|
|
|
Str("data", base64.StdEncoding.EncodeToString(response.Data.RawDecrypted)).
|
|
|
|
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-15 22:45:57 +00:00
|
|
|
r.client.handleUpdatesEvent(response)
|
|
|
|
default:
|
2023-07-17 13:43:34 +00:00
|
|
|
r.client.Logger.Debug().Any("res", response).Msg("Got unknown bugle route")
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
}
|