Remove RPC message builder

This commit is contained in:
Tulir Asokan 2023-07-18 03:08:01 +03:00
parent 9680040ea1
commit 78d0bcd56d
5 changed files with 62 additions and 167 deletions

View file

@ -644,11 +644,11 @@ type OutgoingRPCMessage struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Mobile *Device `protobuf:"bytes,1,opt,name=mobile,proto3" json:"mobile,omitempty"` Mobile *Device `protobuf:"bytes,1,opt,name=mobile,proto3" json:"mobile,omitempty"`
MessageData *OutgoingRPCMessage_Data `protobuf:"bytes,2,opt,name=messageData,proto3" json:"messageData,omitempty"` Data *OutgoingRPCMessage_Data `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
MessageAuth *OutgoingRPCMessage_Auth `protobuf:"bytes,3,opt,name=messageAuth,proto3" json:"messageAuth,omitempty"` Auth *OutgoingRPCMessage_Auth `protobuf:"bytes,3,opt,name=auth,proto3" json:"auth,omitempty"`
TTL int64 `protobuf:"varint,5,opt,name=TTL,proto3" json:"TTL,omitempty"` TTL int64 `protobuf:"varint,5,opt,name=TTL,proto3" json:"TTL,omitempty"`
EmptyArr *EmptyArr `protobuf:"bytes,9,opt,name=emptyArr,proto3" json:"emptyArr,omitempty"` EmptyArr *EmptyArr `protobuf:"bytes,9,opt,name=emptyArr,proto3" json:"emptyArr,omitempty"`
} }
func (x *OutgoingRPCMessage) Reset() { func (x *OutgoingRPCMessage) Reset() {
@ -690,16 +690,16 @@ func (x *OutgoingRPCMessage) GetMobile() *Device {
return nil return nil
} }
func (x *OutgoingRPCMessage) GetMessageData() *OutgoingRPCMessage_Data { func (x *OutgoingRPCMessage) GetData() *OutgoingRPCMessage_Data {
if x != nil { if x != nil {
return x.MessageData return x.Data
} }
return nil return nil
} }
func (x *OutgoingRPCMessage) GetMessageAuth() *OutgoingRPCMessage_Auth { func (x *OutgoingRPCMessage) GetAuth() *OutgoingRPCMessage_Auth {
if x != nil { if x != nil {
return x.MessageAuth return x.Auth
} }
return nil return nil
} }
@ -1026,8 +1026,8 @@ var file_rpc_proto_depIdxs = []int32{
13, // 7: rpc.IncomingRPCMessage.browser:type_name -> authentication.Device 13, // 7: rpc.IncomingRPCMessage.browser:type_name -> authentication.Device
1, // 8: rpc.RPCMessageData.action:type_name -> rpc.ActionType 1, // 8: rpc.RPCMessageData.action:type_name -> rpc.ActionType
13, // 9: rpc.OutgoingRPCMessage.mobile:type_name -> authentication.Device 13, // 9: rpc.OutgoingRPCMessage.mobile:type_name -> authentication.Device
10, // 10: rpc.OutgoingRPCMessage.messageData:type_name -> rpc.OutgoingRPCMessage.Data 10, // 10: rpc.OutgoingRPCMessage.data:type_name -> rpc.OutgoingRPCMessage.Data
9, // 11: rpc.OutgoingRPCMessage.messageAuth:type_name -> rpc.OutgoingRPCMessage.Auth 9, // 11: rpc.OutgoingRPCMessage.auth:type_name -> rpc.OutgoingRPCMessage.Auth
12, // 12: rpc.OutgoingRPCMessage.emptyArr:type_name -> util.EmptyArr 12, // 12: rpc.OutgoingRPCMessage.emptyArr:type_name -> util.EmptyArr
1, // 13: rpc.OutgoingRPCData.action:type_name -> rpc.ActionType 1, // 13: rpc.OutgoingRPCData.action:type_name -> rpc.ActionType
14, // 14: rpc.OutgoingRPCMessage.Auth.configVersion:type_name -> authentication.ConfigVersion 14, // 14: rpc.OutgoingRPCMessage.Auth.configVersion:type_name -> authentication.ConfigVersion

Binary file not shown.

View file

@ -70,8 +70,8 @@ message OutgoingRPCMessage {
} }
authentication.Device mobile = 1; authentication.Device mobile = 1;
Data messageData = 2; Data data = 2;
Auth messageAuth = 3; Auth auth = 3;
int64 TTL = 5; int64 TTL = 5;
@ -86,7 +86,7 @@ message OutgoingRPCData {
} }
enum BugleRoute { enum BugleRoute {
UNKNOWN_BUGLE_ROUTE = 0; Unknown = 0;
DataEvent = 19; DataEvent = 19;
PairEvent = 14; PairEvent = 14;
} }

View file

@ -1,129 +0,0 @@
package payload
import (
"fmt"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
"go.mau.fi/mautrix-gmessages/libgm/routes"
"go.mau.fi/mautrix-gmessages/libgm/util"
)
type SendMessageBuilder struct {
message *gmproto.OutgoingRPCMessage
b64Message *gmproto.OutgoingRPCData
err error
}
func (sm *SendMessageBuilder) Err() error {
return sm.err
}
func NewSendMessageBuilder(tachyonAuthToken []byte, pairedDevice *gmproto.Device, requestId string, sessionId string) *SendMessageBuilder {
return &SendMessageBuilder{
message: &gmproto.OutgoingRPCMessage{
Mobile: pairedDevice,
MessageData: &gmproto.OutgoingRPCMessage_Data{
RequestID: requestId,
},
MessageAuth: &gmproto.OutgoingRPCMessage_Auth{
RequestID: requestId,
TachyonAuthToken: tachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
EmptyArr: &gmproto.EmptyArr{},
},
b64Message: &gmproto.OutgoingRPCData{
RequestID: requestId,
SessionID: sessionId,
},
}
}
func (sm *SendMessageBuilder) SetPairedDevice(device *gmproto.Device) *SendMessageBuilder {
sm.message.Mobile = device
return sm
}
func (sm *SendMessageBuilder) setBugleRoute(bugleRoute gmproto.BugleRoute) *SendMessageBuilder {
sm.message.MessageData.BugleRoute = bugleRoute
return sm
}
func (sm *SendMessageBuilder) SetRequestId(requestId string) *SendMessageBuilder {
sm.message.MessageAuth.RequestID = requestId
sm.message.MessageData.RequestID = requestId
sm.b64Message.RequestID = requestId
return sm
}
func (sm *SendMessageBuilder) SetSessionId(sessionId string) *SendMessageBuilder {
sm.b64Message.SessionID = sessionId
return sm
}
func (sm *SendMessageBuilder) SetRoute(actionType gmproto.ActionType) *SendMessageBuilder {
action, ok := routes.Routes[actionType]
if !ok {
sm.err = fmt.Errorf("invalid action type")
return sm
}
sm.setBugleRoute(action.BugleRoute)
sm.setMessageType(action.MessageType)
sm.b64Message.Action = action.Action
return sm
}
func (sm *SendMessageBuilder) setMessageType(eventType gmproto.MessageType) *SendMessageBuilder {
sm.message.MessageData.MessageTypeData = &gmproto.OutgoingRPCMessage_Data_Type{
EmptyArr: &gmproto.EmptyArr{},
MessageType: eventType,
}
return sm
}
func (sm *SendMessageBuilder) SetTTL(ttl int64) *SendMessageBuilder {
sm.message.TTL = ttl
return sm
}
func (sm *SendMessageBuilder) SetEncryptedProtoMessage(message proto.Message, cryptor *crypto.AESCTRHelper) *SendMessageBuilder {
plaintextBytes, err := proto.Marshal(message)
if err != nil {
sm.err = err
return sm
}
encryptedBytes, err := cryptor.Encrypt(plaintextBytes)
if err != nil {
sm.err = err
return sm
}
sm.b64Message.EncryptedProtoData = encryptedBytes
return sm
}
func (sm *SendMessageBuilder) Build() ([]byte, error) {
if sm.err != nil {
return nil, sm.err
}
encodedMessage, err := proto.Marshal(sm.b64Message)
if err != nil {
return nil, err
}
sm.message.MessageData.MessageData = encodedMessage
protoJSONBytes, serializeErr := pblite.Marshal(sm.message)
if serializeErr != nil {
panic(serializeErr)
return nil, serializeErr
}
return protoJSONBytes, nil
}

View file

@ -12,7 +12,6 @@ import (
"go.mau.fi/mautrix-gmessages/libgm/pblite" "go.mau.fi/mautrix-gmessages/libgm/pblite"
"go.mau.fi/mautrix-gmessages/libgm/gmproto" "go.mau.fi/mautrix-gmessages/libgm/gmproto"
"go.mau.fi/mautrix-gmessages/libgm/payload"
"go.mau.fi/mautrix-gmessages/libgm/routes" "go.mau.fi/mautrix-gmessages/libgm/routes"
"go.mau.fi/mautrix-gmessages/libgm/util" "go.mau.fi/mautrix-gmessages/libgm/util"
) )
@ -37,7 +36,7 @@ func (s *SessionHandler) ResetSessionID() {
} }
func (s *SessionHandler) sendMessageNoResponse(actionType gmproto.ActionType, encryptedData proto.Message) error { func (s *SessionHandler) sendMessageNoResponse(actionType gmproto.ActionType, encryptedData proto.Message) error {
_, payload, _, err := s.buildMessage(actionType, encryptedData) _, payload, err := s.buildMessage(actionType, encryptedData)
if err != nil { if err != nil {
return err return err
} }
@ -47,9 +46,9 @@ func (s *SessionHandler) sendMessageNoResponse(actionType gmproto.ActionType, en
} }
func (s *SessionHandler) sendAsyncMessage(actionType gmproto.ActionType, encryptedData proto.Message) (<-chan *IncomingRPCMessage, error) { func (s *SessionHandler) sendAsyncMessage(actionType gmproto.ActionType, encryptedData proto.Message) (<-chan *IncomingRPCMessage, error) {
requestID, payload, _, buildErr := s.buildMessage(actionType, encryptedData) requestID, payload, err := s.buildMessage(actionType, encryptedData)
if buildErr != nil { if err != nil {
return nil, buildErr return nil, err
} }
ch := s.waitResponse(requestID) ch := s.waitResponse(requestID)
@ -84,15 +83,14 @@ func (s *SessionHandler) sendMessage(actionType gmproto.ActionType, encryptedDat
return <-ch, nil return <-ch, nil
} }
func (s *SessionHandler) buildMessage(actionType gmproto.ActionType, encryptedData proto.Message) (string, []byte, gmproto.ActionType, error) { func (s *SessionHandler) buildMessage(actionType gmproto.ActionType, data proto.Message) (string, []byte, error) {
var requestID string var requestID string
pairedDevice := s.client.AuthData.Mobile var err error
sessionId := s.client.sessionHandler.sessionID sessionID := s.client.sessionHandler.sessionID
token := s.client.AuthData.TachyonAuthToken
routeInfo, ok := routes.Routes[actionType] routeInfo, ok := routes.Routes[actionType]
if !ok { if !ok {
return "", nil, 0, fmt.Errorf("failed to build message: could not find route %d", actionType) return "", nil, fmt.Errorf("failed to build message: could not find route %d", actionType)
} }
if routeInfo.UseSessionID { if routeInfo.UseSessionID {
@ -101,22 +99,48 @@ func (s *SessionHandler) buildMessage(actionType gmproto.ActionType, encryptedDa
requestID = uuid.NewString() requestID = uuid.NewString()
} }
tmpMessage := payload.NewSendMessageBuilder(token, pairedDevice, requestID, sessionId).SetRoute(routeInfo.Action).SetSessionId(s.sessionID) message := &gmproto.OutgoingRPCMessage{
Mobile: s.client.AuthData.Mobile,
if encryptedData != nil { Data: &gmproto.OutgoingRPCMessage_Data{
tmpMessage.SetEncryptedProtoMessage(encryptedData, s.client.AuthData.RequestCrypto) RequestID: requestID,
BugleRoute: routeInfo.BugleRoute,
MessageTypeData: &gmproto.OutgoingRPCMessage_Data_Type{
EmptyArr: &gmproto.EmptyArr{},
MessageType: routeInfo.MessageType,
},
},
Auth: &gmproto.OutgoingRPCMessage_Auth{
RequestID: requestID,
TachyonAuthToken: s.client.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
EmptyArr: &gmproto.EmptyArr{},
}
var encryptedData []byte
if data != nil {
var serializedData []byte
serializedData, err = proto.Marshal(data)
if err != nil {
return "", nil, err
}
encryptedData, err = s.client.AuthData.RequestCrypto.Encrypt(serializedData)
if err != nil {
return "", nil, err
}
}
message.Data.MessageData, err = proto.Marshal(&gmproto.OutgoingRPCData{
RequestID: requestID,
Action: actionType,
EncryptedProtoData: encryptedData,
SessionID: sessionID,
})
if err != nil {
return "", nil, err
} }
if routeInfo.UseTTL { var marshaledMessage []byte
tmpMessage.SetTTL(s.client.AuthData.TachyonTTL) marshaledMessage, err = pblite.Marshal(message)
} return requestID, marshaledMessage, err
message, buildErr := tmpMessage.Build()
if buildErr != nil {
return "", nil, 0, buildErr
}
return requestID, message, routeInfo.Action, nil
} }
func (s *SessionHandler) queueMessageAck(messageID string) { func (s *SessionHandler) queueMessageAck(messageID string) {