From 78d0bcd56db2912b21a422c47906969a25c4d0dd Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 18 Jul 2023 03:08:01 +0300 Subject: [PATCH] Remove RPC message builder --- libgm/gmproto/rpc.pb.go | 22 +++--- libgm/gmproto/rpc.pb.raw | Bin 3291 -> 3263 bytes libgm/gmproto/rpc.proto | 6 +- libgm/payload/sendMessage.go | 129 ----------------------------------- libgm/session_handler.go | 72 ++++++++++++------- 5 files changed, 62 insertions(+), 167 deletions(-) delete mode 100644 libgm/payload/sendMessage.go diff --git a/libgm/gmproto/rpc.pb.go b/libgm/gmproto/rpc.pb.go index f14ff80..9f5816f 100644 --- a/libgm/gmproto/rpc.pb.go +++ b/libgm/gmproto/rpc.pb.go @@ -644,11 +644,11 @@ type OutgoingRPCMessage struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - 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"` - MessageAuth *OutgoingRPCMessage_Auth `protobuf:"bytes,3,opt,name=messageAuth,proto3" json:"messageAuth,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"` + Mobile *Device `protobuf:"bytes,1,opt,name=mobile,proto3" json:"mobile,omitempty"` + Data *OutgoingRPCMessage_Data `protobuf:"bytes,2,opt,name=data,proto3" json:"data,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"` + EmptyArr *EmptyArr `protobuf:"bytes,9,opt,name=emptyArr,proto3" json:"emptyArr,omitempty"` } func (x *OutgoingRPCMessage) Reset() { @@ -690,16 +690,16 @@ func (x *OutgoingRPCMessage) GetMobile() *Device { return nil } -func (x *OutgoingRPCMessage) GetMessageData() *OutgoingRPCMessage_Data { +func (x *OutgoingRPCMessage) GetData() *OutgoingRPCMessage_Data { if x != nil { - return x.MessageData + return x.Data } return nil } -func (x *OutgoingRPCMessage) GetMessageAuth() *OutgoingRPCMessage_Auth { +func (x *OutgoingRPCMessage) GetAuth() *OutgoingRPCMessage_Auth { if x != nil { - return x.MessageAuth + return x.Auth } return nil } @@ -1026,8 +1026,8 @@ var file_rpc_proto_depIdxs = []int32{ 13, // 7: rpc.IncomingRPCMessage.browser:type_name -> authentication.Device 1, // 8: rpc.RPCMessageData.action:type_name -> rpc.ActionType 13, // 9: rpc.OutgoingRPCMessage.mobile:type_name -> authentication.Device - 10, // 10: rpc.OutgoingRPCMessage.messageData:type_name -> rpc.OutgoingRPCMessage.Data - 9, // 11: rpc.OutgoingRPCMessage.messageAuth:type_name -> rpc.OutgoingRPCMessage.Auth + 10, // 10: rpc.OutgoingRPCMessage.data:type_name -> rpc.OutgoingRPCMessage.Data + 9, // 11: rpc.OutgoingRPCMessage.auth:type_name -> rpc.OutgoingRPCMessage.Auth 12, // 12: rpc.OutgoingRPCMessage.emptyArr:type_name -> util.EmptyArr 1, // 13: rpc.OutgoingRPCData.action:type_name -> rpc.ActionType 14, // 14: rpc.OutgoingRPCMessage.Auth.configVersion:type_name -> authentication.ConfigVersion diff --git a/libgm/gmproto/rpc.pb.raw b/libgm/gmproto/rpc.pb.raw index 5c6344838eeb7ada7eea6bacb5fe2effe50664d1..944c83321bf70047fa53df206ee46713e1cc4a47 100644 GIT binary patch delta 45 ycmcaDxnFXF4h!Sj$+|3VECyUGDU<71G=NlMNum&tn>cwNiv|-*;^y}(Z`c7UQVkve delta 83 zcmdlld0TRW4h!SS$+|3V0(M;7xv9m)iRq~>i6w~=Oq18K$jO43LCE4l2xX3wSF&hu N!XzCxUuJp34ghE&8@&Jk diff --git a/libgm/gmproto/rpc.proto b/libgm/gmproto/rpc.proto index 69f0226..493f20b 100644 --- a/libgm/gmproto/rpc.proto +++ b/libgm/gmproto/rpc.proto @@ -70,8 +70,8 @@ message OutgoingRPCMessage { } authentication.Device mobile = 1; - Data messageData = 2; - Auth messageAuth = 3; + Data data = 2; + Auth auth = 3; int64 TTL = 5; @@ -86,7 +86,7 @@ message OutgoingRPCData { } enum BugleRoute { - UNKNOWN_BUGLE_ROUTE = 0; + Unknown = 0; DataEvent = 19; PairEvent = 14; } diff --git a/libgm/payload/sendMessage.go b/libgm/payload/sendMessage.go deleted file mode 100644 index 59a37ee..0000000 --- a/libgm/payload/sendMessage.go +++ /dev/null @@ -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 -} diff --git a/libgm/session_handler.go b/libgm/session_handler.go index be7d297..9a3b440 100644 --- a/libgm/session_handler.go +++ b/libgm/session_handler.go @@ -12,7 +12,6 @@ import ( "go.mau.fi/mautrix-gmessages/libgm/pblite" "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/util" ) @@ -37,7 +36,7 @@ func (s *SessionHandler) ResetSessionID() { } 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 { 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) { - requestID, payload, _, buildErr := s.buildMessage(actionType, encryptedData) - if buildErr != nil { - return nil, buildErr + requestID, payload, err := s.buildMessage(actionType, encryptedData) + if err != nil { + return nil, err } ch := s.waitResponse(requestID) @@ -84,15 +83,14 @@ func (s *SessionHandler) sendMessage(actionType gmproto.ActionType, encryptedDat 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 - pairedDevice := s.client.AuthData.Mobile - sessionId := s.client.sessionHandler.sessionID - token := s.client.AuthData.TachyonAuthToken + var err error + sessionID := s.client.sessionHandler.sessionID routeInfo, ok := routes.Routes[actionType] 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 { @@ -101,22 +99,48 @@ func (s *SessionHandler) buildMessage(actionType gmproto.ActionType, encryptedDa requestID = uuid.NewString() } - tmpMessage := payload.NewSendMessageBuilder(token, pairedDevice, requestID, sessionId).SetRoute(routeInfo.Action).SetSessionId(s.sessionID) - - if encryptedData != nil { - tmpMessage.SetEncryptedProtoMessage(encryptedData, s.client.AuthData.RequestCrypto) + message := &gmproto.OutgoingRPCMessage{ + Mobile: s.client.AuthData.Mobile, + Data: &gmproto.OutgoingRPCMessage_Data{ + 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 { - tmpMessage.SetTTL(s.client.AuthData.TachyonTTL) - } - - message, buildErr := tmpMessage.Build() - if buildErr != nil { - return "", nil, 0, buildErr - } - - return requestID, message, routeInfo.Action, nil + var marshaledMessage []byte + marshaledMessage, err = pblite.Marshal(message) + return requestID, marshaledMessage, err } func (s *SessionHandler) queueMessageAck(messageID string) {