2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-16 11:36:13 +00:00
|
|
|
"crypto/x509"
|
2023-07-18 22:19:04 +00:00
|
|
|
"encoding/base64"
|
2023-07-18 22:08:13 +00:00
|
|
|
"fmt"
|
2023-08-24 18:51:58 +00:00
|
|
|
"time"
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 23:11:25 +00:00
|
|
|
"github.com/google/uuid"
|
2023-07-15 17:43:28 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
2023-07-18 22:08:13 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/events"
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-06-30 09:54:08 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/util"
|
|
|
|
)
|
|
|
|
|
2023-07-18 22:19:04 +00:00
|
|
|
func (c *Client) StartLogin() (string, error) {
|
|
|
|
registered, err := c.RegisterPhoneRelay()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-02-22 20:37:49 +00:00
|
|
|
c.updateTachyonAuthToken(registered.GetAuthKeyData())
|
2023-07-19 11:13:02 +00:00
|
|
|
go c.doLongPoll(false)
|
2023-07-18 22:19:04 +00:00
|
|
|
qr, err := c.GenerateQRCodeData(registered.GetPairingKey())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate QR code: %w", err)
|
|
|
|
}
|
|
|
|
return qr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GenerateQRCodeData(pairingKey []byte) (string, error) {
|
|
|
|
urlData := &gmproto.URLData{
|
|
|
|
PairingKey: pairingKey,
|
|
|
|
AESKey: c.AuthData.RequestCrypto.AESKey,
|
|
|
|
HMACKey: c.AuthData.RequestCrypto.HMACKey,
|
|
|
|
}
|
|
|
|
encodedURLData, err := proto.Marshal(urlData)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cData := base64.StdEncoding.EncodeToString(encodedURLData)
|
|
|
|
return util.QRCodeURLBase + cData, nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 22:08:13 +00:00
|
|
|
func (c *Client) handlePairingEvent(msg *IncomingRPCMessage) {
|
|
|
|
switch evt := msg.Pair.Event.(type) {
|
|
|
|
case *gmproto.RPCPairData_Paired:
|
|
|
|
c.completePairing(evt.Paired)
|
|
|
|
case *gmproto.RPCPairData_Revoked:
|
|
|
|
c.triggerEvent(evt.Revoked)
|
|
|
|
default:
|
|
|
|
c.Logger.Debug().Any("evt", evt).Msg("Unknown pair event type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) completePairing(data *gmproto.PairedData) {
|
2024-02-22 20:37:49 +00:00
|
|
|
c.updateTachyonAuthToken(data.GetTokenData())
|
2023-07-18 22:08:13 +00:00
|
|
|
c.AuthData.Mobile = data.Mobile
|
|
|
|
c.AuthData.Browser = data.Browser
|
|
|
|
|
2024-02-23 20:52:04 +00:00
|
|
|
c.triggerEvent(&events.PairSuccessful{PhoneID: data.GetMobile().GetSourceID(), QRData: data})
|
2023-07-18 22:08:13 +00:00
|
|
|
|
2023-08-10 12:40:43 +00:00
|
|
|
go func() {
|
2023-08-24 18:51:58 +00:00
|
|
|
// Sleep for a bit to let the phone save the pair data. If we reconnect too quickly,
|
|
|
|
// the phone won't recognize the session the bridge will get unpaired.
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2023-08-10 12:40:43 +00:00
|
|
|
err := c.Reconnect()
|
|
|
|
if err != nil {
|
|
|
|
c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)})
|
|
|
|
}
|
|
|
|
}()
|
2023-07-18 22:08:13 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) RegisterPhoneRelay() (*gmproto.RegisterPhoneRelayResponse, error) {
|
2023-07-16 12:55:30 +00:00
|
|
|
key, err := x509.MarshalPKIXPublicKey(c.AuthData.RefreshKey.GetPublicKey())
|
2023-07-16 11:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-19 10:58:53 +00:00
|
|
|
payload := &gmproto.AuthenticationContainer{
|
2023-07-17 13:51:31 +00:00
|
|
|
AuthMessage: &gmproto.AuthMessage{
|
2023-07-16 11:36:13 +00:00
|
|
|
RequestID: uuid.NewString(),
|
2024-02-22 20:37:49 +00:00
|
|
|
Network: util.QRNetwork,
|
2023-07-16 12:55:30 +00:00
|
|
|
ConfigVersion: util.ConfigMessage,
|
2023-07-16 11:36:13 +00:00
|
|
|
},
|
2023-07-16 12:55:30 +00:00
|
|
|
BrowserDetails: util.BrowserDetailsMessage,
|
2023-07-17 13:51:31 +00:00
|
|
|
Data: &gmproto.AuthenticationContainer_KeyData{
|
|
|
|
KeyData: &gmproto.KeyData{
|
|
|
|
EcdsaKeys: &gmproto.ECDSAKeys{
|
2023-07-16 11:36:13 +00:00
|
|
|
Field1: 2,
|
|
|
|
EncryptedKeys: key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-19 10:58:53 +00:00
|
|
|
return typedHTTPResponse[*gmproto.RegisterPhoneRelayResponse](
|
|
|
|
c.makeProtobufHTTPRequest(util.RegisterPhoneRelayURL, payload, ContentTypeProtobuf),
|
|
|
|
)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 12:55:30 +00:00
|
|
|
func (c *Client) RefreshPhoneRelay() (string, error) {
|
2023-07-19 10:58:53 +00:00
|
|
|
payload := &gmproto.AuthenticationContainer{
|
2023-07-17 13:51:31 +00:00
|
|
|
AuthMessage: &gmproto.AuthMessage{
|
2023-07-16 10:23:44 +00:00
|
|
|
RequestID: uuid.NewString(),
|
2024-02-22 20:37:49 +00:00
|
|
|
Network: util.QRNetwork,
|
2023-07-16 12:55:30 +00:00
|
|
|
TachyonAuthToken: c.AuthData.TachyonAuthToken,
|
|
|
|
ConfigVersion: util.ConfigMessage,
|
2023-07-15 23:16:10 +00:00
|
|
|
},
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-19 10:58:53 +00:00
|
|
|
res, err := typedHTTPResponse[*gmproto.RefreshPhoneRelayResponse](
|
|
|
|
c.makeProtobufHTTPRequest(util.RefreshPhoneRelayURL, payload, ContentTypeProtobuf),
|
|
|
|
)
|
2023-07-16 12:55:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-16 12:55:30 +00:00
|
|
|
qr, err := c.GenerateQRCodeData(res.GetPairKey())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-16 12:55:30 +00:00
|
|
|
return qr, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) GetWebEncryptionKey() (*gmproto.WebEncryptionKeyResponse, error) {
|
2023-07-19 10:58:53 +00:00
|
|
|
payload := &gmproto.AuthenticationContainer{
|
2023-07-17 13:51:31 +00:00
|
|
|
AuthMessage: &gmproto.AuthMessage{
|
2023-07-15 23:16:10 +00:00
|
|
|
RequestID: uuid.NewString(),
|
2023-07-16 12:55:30 +00:00
|
|
|
TachyonAuthToken: c.AuthData.TachyonAuthToken,
|
|
|
|
ConfigVersion: util.ConfigMessage,
|
2023-07-15 23:16:10 +00:00
|
|
|
},
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-19 10:58:53 +00:00
|
|
|
return typedHTTPResponse[*gmproto.WebEncryptionKeyResponse](
|
|
|
|
c.makeProtobufHTTPRequest(util.GetWebEncryptionKeyURL, payload, ContentTypeProtobuf),
|
|
|
|
)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-15 23:11:25 +00:00
|
|
|
|
2024-02-23 14:32:12 +00:00
|
|
|
func (c *Client) UnpairBugle() (*gmproto.RevokeRelayPairingResponse, error) {
|
2023-07-16 12:55:30 +00:00
|
|
|
if c.AuthData.TachyonAuthToken == nil || c.AuthData.Browser == nil {
|
2023-07-15 23:11:25 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-07-19 10:58:53 +00:00
|
|
|
payload := &gmproto.RevokeRelayPairingRequest{
|
2023-07-17 13:51:31 +00:00
|
|
|
AuthMessage: &gmproto.AuthMessage{
|
2023-07-15 23:11:25 +00:00
|
|
|
RequestID: uuid.NewString(),
|
2023-07-16 12:55:30 +00:00
|
|
|
TachyonAuthToken: c.AuthData.TachyonAuthToken,
|
|
|
|
ConfigVersion: util.ConfigMessage,
|
2023-07-15 23:11:25 +00:00
|
|
|
},
|
2023-07-16 12:55:30 +00:00
|
|
|
Browser: c.AuthData.Browser,
|
2023-07-15 23:11:25 +00:00
|
|
|
}
|
2023-07-19 10:58:53 +00:00
|
|
|
return typedHTTPResponse[*gmproto.RevokeRelayPairingResponse](
|
|
|
|
c.makeProtobufHTTPRequest(util.RevokeRelayPairingURL, payload, ContentTypeProtobuf),
|
|
|
|
)
|
2023-07-15 23:11:25 +00:00
|
|
|
}
|
2024-02-23 14:32:12 +00:00
|
|
|
|
|
|
|
func (c *Client) Unpair() (err error) {
|
|
|
|
if c.AuthData.Cookies != nil {
|
|
|
|
err = c.UnpairGaia()
|
|
|
|
} else {
|
|
|
|
_, err = c.UnpairBugle()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|