2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"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/payload"
|
2023-06-30 10:04:17 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/pblite"
|
2023-06-30 09:54:08 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
client *Client
|
2023-06-30 13:26:46 +00:00
|
|
|
ResponseID string
|
2023-06-30 09:54:08 +00:00
|
|
|
RoutingOpCode int64
|
|
|
|
Data *binary.EncodedResponse // base64 encoded (decode -> protomessage)
|
|
|
|
|
|
|
|
StartExecute string
|
|
|
|
FinishExecute string
|
|
|
|
DevicePair *DevicePair
|
|
|
|
}
|
|
|
|
|
|
|
|
type SessionHandler struct {
|
|
|
|
client *Client
|
|
|
|
requests map[string]map[int64]*ResponseChan
|
|
|
|
|
|
|
|
ackMap []string
|
|
|
|
ackTicker *time.Ticker
|
|
|
|
|
2023-06-30 13:26:46 +00:00
|
|
|
sessionID string
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
responseTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) SetResponseTimeout(milliSeconds int) {
|
|
|
|
s.responseTimeout = time.Duration(milliSeconds) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:26:46 +00:00
|
|
|
func (s *SessionHandler) ResetSessionID() {
|
|
|
|
s.sessionID = util.RandomUUIDv4()
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) createAndSendRequest(instructionId int64, ttl int64, newSession bool, encryptedProtoMessage protoreflect.Message) (string, error) {
|
|
|
|
requestId := util.RandomUUIDv4()
|
|
|
|
instruction, ok := c.instructions.GetInstruction(instructionId)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("failed to get instruction: %v does not exist", instructionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newSession {
|
2023-06-30 13:26:46 +00:00
|
|
|
requestId = c.sessionHandler.sessionID
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var encryptedData []byte
|
|
|
|
var encryptErr error
|
|
|
|
if encryptedProtoMessage != nil {
|
|
|
|
encryptedData, encryptErr = c.EncryptPayloadData(encryptedProtoMessage)
|
|
|
|
if encryptErr != nil {
|
|
|
|
return "", fmt.Errorf("failed to encrypt payload data for opcode: %v", instructionId)
|
|
|
|
}
|
|
|
|
c.Logger.Info().Any("encryptedData", encryptedData).Msg("Sending request with encrypted data")
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:26:46 +00:00
|
|
|
encodedData := payload.NewEncodedPayload(requestId, instruction.Opcode, encryptedData, c.sessionHandler.sessionID)
|
2023-06-30 09:54:08 +00:00
|
|
|
encodedStr, encodeErr := crypto.EncodeProtoB64(encodedData)
|
|
|
|
if encodeErr != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(fmt.Errorf("Failed to encode data: %w", encodeErr))
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
messageData := payload.NewMessageData(requestId, encodedStr, instruction.RoutingOpCode, instruction.MsgType)
|
|
|
|
authMessage := payload.NewAuthData(requestId, c.rpcKey, &binary.Date{Year: 2023, Seq1: 6, Seq2: 8, Seq3: 4, Seq4: 6})
|
|
|
|
sendMessage := payload.NewSendMessage(c.devicePair.Mobile, messageData, authMessage, ttl)
|
|
|
|
|
2023-06-30 13:26:46 +00:00
|
|
|
sentRequestID, reqErr := c.sessionHandler.completeSendMessage(encodedData.RequestID, instruction.Opcode, sendMessage)
|
2023-06-30 09:54:08 +00:00
|
|
|
if reqErr != nil {
|
|
|
|
return "", fmt.Errorf("failed to send message request for opcode: %v", instructionId)
|
|
|
|
}
|
2023-06-30 13:26:46 +00:00
|
|
|
return sentRequestID, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) completeSendMessage(requestId string, opCode int64, msg *binary.SendMessage) (string, error) {
|
|
|
|
jsonData, err := s.toJSON(msg.ProtoReflect())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
//s.client.Logger.Debug().Any("payload", string(jsonData)).Msg("Sending message request")
|
|
|
|
s.addRequestToChannel(requestId, opCode)
|
|
|
|
_, reqErr := s.client.rpc.sendMessageRequest(util.SEND_MESSAGE, jsonData)
|
|
|
|
if reqErr != nil {
|
|
|
|
return "", reqErr
|
|
|
|
}
|
|
|
|
return requestId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) toJSON(message protoreflect.Message) ([]byte, error) {
|
2023-06-30 10:04:17 +00:00
|
|
|
interfaceArr, err := pblite.Serialize(message)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jsonData, jsonErr := json.Marshal(interfaceArr)
|
|
|
|
if jsonErr != nil {
|
|
|
|
return nil, jsonErr
|
|
|
|
}
|
|
|
|
return jsonData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) addResponseAck(responseId string) {
|
|
|
|
hasResponseId := slices.Contains(s.ackMap, responseId)
|
|
|
|
if !hasResponseId {
|
|
|
|
s.ackMap = append(s.ackMap, responseId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) startAckInterval() {
|
|
|
|
if s.ackTicker != nil {
|
|
|
|
s.ackTicker.Stop()
|
|
|
|
}
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
s.ackTicker = ticker
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
|
|
|
s.sendAckRequest()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) sendAckRequest() {
|
|
|
|
if len(s.ackMap) <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqId := util.RandomUUIDv4()
|
|
|
|
ackMessagePayload := &binary.AckMessagePayload{
|
|
|
|
AuthData: &binary.AuthMessage{
|
2023-06-30 13:26:46 +00:00
|
|
|
RequestID: reqId,
|
2023-06-30 09:54:08 +00:00
|
|
|
RpcKey: s.client.rpcKey,
|
|
|
|
Date: &binary.Date{Year: 2023, Seq1: 6, Seq2: 8, Seq3: 4, Seq4: 6},
|
|
|
|
},
|
|
|
|
EmptyArr: &binary.EmptyArr{},
|
|
|
|
NoClue: nil,
|
|
|
|
}
|
2023-06-30 10:04:17 +00:00
|
|
|
dataArray, err := pblite.Serialize(ackMessagePayload.ProtoReflect())
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
ackMessages := make([][]interface{}, 0)
|
|
|
|
for _, reqId := range s.ackMap {
|
2023-06-30 13:26:46 +00:00
|
|
|
ackMessageData := &binary.AckMessageData{RequestID: reqId, Device: s.client.devicePair.Browser}
|
2023-06-30 10:04:17 +00:00
|
|
|
ackMessageDataArr, err := pblite.Serialize(ackMessageData.ProtoReflect())
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
ackMessages = append(ackMessages, ackMessageDataArr)
|
|
|
|
s.ackMap = util.RemoveFromSlice(s.ackMap, reqId)
|
|
|
|
}
|
|
|
|
dataArray = append(dataArray, ackMessages)
|
|
|
|
jsonData, jsonErr := json.Marshal(dataArray)
|
|
|
|
if jsonErr != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
_, err = s.client.rpc.sendMessageRequest(util.ACK_MESSAGES, jsonData)
|
|
|
|
if err != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-06-30 11:48:50 +00:00
|
|
|
s.client.Logger.Debug().Msg("[ACK] Sent Request")
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) NewResponse(response *binary.RPCResponse) (*Response, error) {
|
|
|
|
//s.client.Logger.Debug().Any("rpcResponse", response).Msg("Raw rpc response")
|
|
|
|
decodedData, err := crypto.DecodeEncodedResponse(response.Data.EncodedData)
|
|
|
|
if err != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Response{
|
|
|
|
client: s.client,
|
2023-06-30 13:26:46 +00:00
|
|
|
ResponseID: response.Data.RequestID,
|
2023-06-30 09:54:08 +00:00
|
|
|
RoutingOpCode: response.Data.RoutingOpCode,
|
|
|
|
StartExecute: response.Data.Ts1,
|
|
|
|
FinishExecute: response.Data.Ts2,
|
|
|
|
DevicePair: &DevicePair{
|
|
|
|
Mobile: response.Data.Mobile,
|
|
|
|
Browser: response.Data.Browser,
|
|
|
|
},
|
|
|
|
Data: decodedData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) decryptData() (proto.Message, error) {
|
|
|
|
if r.Data.EncryptedData != nil {
|
|
|
|
instruction, ok := r.client.instructions.GetInstruction(r.Data.Opcode)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed to decrypt data for unknown opcode: %v", r.Data.Opcode)
|
|
|
|
}
|
|
|
|
decryptedBytes, errDecrypt := instruction.cryptor.Decrypt(r.Data.EncryptedData)
|
|
|
|
if errDecrypt != nil {
|
|
|
|
return nil, errDecrypt
|
|
|
|
}
|
|
|
|
//os.WriteFile("opcode_"+strconv.Itoa(int(instruction.Opcode))+".bin", decryptedBytes, os.ModePerm)
|
|
|
|
|
|
|
|
protoMessageData := instruction.DecryptedProtoMessage.ProtoReflect().Type().New().Interface()
|
|
|
|
decodeProtoErr := binary.DecodeProtoMessage(decryptedBytes, protoMessageData)
|
|
|
|
if decodeProtoErr != nil {
|
|
|
|
return nil, decodeProtoErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return protoMessageData, nil
|
|
|
|
}
|
2023-06-30 13:26:46 +00:00
|
|
|
return nil, fmt.Errorf("no encrypted data to decrypt for requestID: %s", r.Data.RequestID)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|