2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-07-09 11:16:52 +00:00
|
|
|
"log"
|
2023-06-30 09:54:08 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/pblite"
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/binary"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/payload"
|
2023-07-09 11:16:52 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/routes"
|
2023-06-30 09:54:08 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/util"
|
|
|
|
)
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
/*
|
2023-06-30 09:54:08 +00:00
|
|
|
type Response struct {
|
2023-07-09 11:16:52 +00:00
|
|
|
client *Client
|
|
|
|
ResponseId string
|
2023-06-30 09:54:08 +00:00
|
|
|
RoutingOpCode int64
|
2023-07-09 11:16:52 +00:00
|
|
|
Data *binary.EncodedResponse // base64 encoded (decode -> protomessage)
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
StartExecute string
|
2023-06-30 09:54:08 +00:00
|
|
|
FinishExecute string
|
2023-07-09 11:16:52 +00:00
|
|
|
DevicePair *pblite.DevicePair
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
*/
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
type SessionHandler struct {
|
|
|
|
client *Client
|
2023-07-09 11:16:52 +00:00
|
|
|
requests map[string]map[binary.ActionType]*ResponseChan
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
ackMap []string
|
|
|
|
ackTicker *time.Ticker
|
|
|
|
|
2023-07-09 11:16:52 +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-07-09 11:16:52 +00:00
|
|
|
func (s *SessionHandler) ResetSessionId() {
|
|
|
|
s.sessionId = util.RandomUUIDv4()
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
func (s *SessionHandler) completeSendMessage(actionType binary.ActionType, addToChannel bool, encryptedData proto.Message) (string, error) {
|
|
|
|
requestId, payload, action, buildErr := s.buildMessage(actionType, encryptedData)
|
|
|
|
if buildErr != nil {
|
|
|
|
return "", buildErr
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
if addToChannel {
|
|
|
|
s.addRequestToChannel(requestId, action)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
_, reqErr := s.client.rpc.sendMessageRequest(util.SEND_MESSAGE, payload)
|
|
|
|
if reqErr != nil {
|
|
|
|
return "", reqErr
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
return requestId, nil
|
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
func (s *SessionHandler) buildMessage(actionType binary.ActionType, encryptedData proto.Message) (string, []byte, binary.ActionType, error) {
|
|
|
|
var requestId string
|
|
|
|
pairedDevice := s.client.authData.DevicePair.Mobile
|
|
|
|
sessionId := s.client.sessionHandler.sessionId
|
|
|
|
token := s.client.authData.TachyonAuthToken
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
routeInfo, ok := routes.Routes[actionType]
|
|
|
|
if !ok {
|
|
|
|
return "", nil, 0, fmt.Errorf("failed to build message: could not find route %d", actionType)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
if routeInfo.UseSessionID {
|
|
|
|
requestId = s.sessionId
|
|
|
|
} else {
|
|
|
|
requestId = util.RandomUUIDv4()
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
tmpMessage := payload.NewSendMessageBuilder(token, pairedDevice, requestId, sessionId).SetRoute(routeInfo.Action).SetSessionId(s.sessionId)
|
|
|
|
|
|
|
|
if encryptedData != nil {
|
|
|
|
tmpMessage.SetEncryptedProtoMessage(encryptedData, s.client.authData.Cryptor)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
if routeInfo.UseTTL {
|
|
|
|
tmpMessage.SetTTL(s.client.authData.TTL)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
message, buildErr := tmpMessage.Build()
|
|
|
|
if buildErr != nil {
|
|
|
|
return "", nil, 0, buildErr
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
return requestId, message, routeInfo.Action, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SessionHandler) addResponseAck(responseId string) {
|
2023-07-09 11:16:52 +00:00
|
|
|
s.client.Logger.Debug().Any("responseId", responseId).Msg("Added to ack map")
|
2023-06-30 09:54:08 +00:00
|
|
|
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-07-09 11:16:52 +00:00
|
|
|
RequestID: reqId,
|
|
|
|
TachyonAuthToken: s.client.authData.TachyonAuthToken,
|
|
|
|
ConfigVersion: payload.ConfigMessage,
|
2023-06-30 09:54:08 +00:00
|
|
|
},
|
|
|
|
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-07-09 11:16:52 +00:00
|
|
|
log.Fatal(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
ackMessages := make([][]interface{}, 0)
|
|
|
|
for _, reqId := range s.ackMap {
|
2023-07-09 11:16:52 +00:00
|
|
|
ackMessageData := &binary.AckMessageData{RequestID: reqId, Device: s.client.authData.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-07-09 11:16:52 +00:00
|
|
|
log.Fatal(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-07-09 11:16:52 +00:00
|
|
|
log.Fatal(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
_, err = s.client.rpc.sendMessageRequest(util.ACK_MESSAGES, jsonData)
|
|
|
|
if err != nil {
|
2023-07-09 11:16:52 +00:00
|
|
|
log.Fatal(err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
s.client.Logger.Debug().Any("payload", jsonData).Msg("[ACK] Sent Request")
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|