gmessages/libgm/pair.go

218 lines
5.6 KiB
Go
Raw Normal View History

2023-06-30 11:05:33 +00:00
package libgm
2023-06-30 09:54:08 +00:00
import (
"bytes"
2023-07-16 11:36:13 +00:00
"crypto/x509"
2023-07-18 22:19:04 +00:00
"encoding/base64"
"fmt"
2023-06-30 09:54:08 +00:00
"io"
"net/http"
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"
"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
}
c.AuthData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken
go c.rpc.ListenReceiveMessages(false)
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
}
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) {
c.updateTachyonAuthToken(data.GetTokenData().GetTachyonAuthToken(), data.GetTokenData().GetTTL())
c.AuthData.Mobile = data.Mobile
c.AuthData.Browser = data.Browser
c.triggerEvent(&events.PairSuccessful{PairedData: data})
err := c.Reconnect()
if err != nil {
c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)})
}
}
func (c *Client) makeRelayRequest(url string, body []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
return nil, err
}
util.BuildRelayHeaders(req, "application/x-protobuf", "*/*")
res, reqErr := c.http.Do(req)
if reqErr != nil {
return res, reqErr
}
return res, nil
}
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-17 13:51:31 +00:00
body, err := proto.Marshal(&gmproto.AuthenticationContainer{
AuthMessage: &gmproto.AuthMessage{
2023-07-16 11:36:13 +00:00
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
Network: &util.Network,
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
if err != nil {
2023-07-16 12:55:30 +00:00
return nil, err
2023-06-30 09:54:08 +00:00
}
relayResponse, reqErr := c.makeRelayRequest(util.RegisterPhoneRelayURL, body)
2023-06-30 09:54:08 +00:00
if reqErr != nil {
return nil, err
}
2023-07-16 12:55:30 +00:00
responseBody, err := io.ReadAll(relayResponse.Body)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
relayResponse.Body.Close()
2023-07-17 13:51:31 +00:00
res := &gmproto.RegisterPhoneRelayResponse{}
2023-07-16 12:55:30 +00:00
err = proto.Unmarshal(responseBody, res)
if err != nil {
return nil, err
}
2023-06-30 09:54:08 +00:00
return res, err
}
2023-07-16 12:55:30 +00:00
func (c *Client) RefreshPhoneRelay() (string, error) {
2023-07-17 13:51:31 +00:00
body, err := proto.Marshal(&gmproto.AuthenticationContainer{
AuthMessage: &gmproto.AuthMessage{
2023-07-16 10:23:44 +00:00
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
Network: &util.Network,
TachyonAuthToken: c.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
})
2023-06-30 09:54:08 +00:00
if err != nil {
2023-07-16 12:55:30 +00:00
return "", err
2023-06-30 09:54:08 +00:00
}
relayResponse, err := c.makeRelayRequest(util.RefreshPhoneRelayURL, body)
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
responseBody, err := io.ReadAll(relayResponse.Body)
2023-06-30 09:54:08 +00:00
defer relayResponse.Body.Close()
2023-07-16 12:55:30 +00:00
if err != nil {
return "", err
2023-06-30 09:54:08 +00:00
}
2023-07-17 13:51:31 +00:00
res := &gmproto.RefreshPhoneRelayResponse{}
2023-07-16 12:55:30 +00:00
err = proto.Unmarshal(responseBody, res)
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) {
body, err := proto.Marshal(&gmproto.AuthenticationContainer{
AuthMessage: &gmproto.AuthMessage{
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
TachyonAuthToken: c.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
})
if err != nil {
return nil, err
}
webKeyResponse, err := c.makeRelayRequest(util.GetWebEncryptionKeyURL, body)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
responseBody, err := io.ReadAll(webKeyResponse.Body)
2023-06-30 09:54:08 +00:00
defer webKeyResponse.Body.Close()
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
2023-07-17 13:51:31 +00:00
parsedResponse := &gmproto.WebEncryptionKeyResponse{}
err = proto.Unmarshal(responseBody, parsedResponse)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
return parsedResponse, nil
2023-06-30 09:54:08 +00:00
}
2023-07-15 23:11:25 +00:00
2023-07-17 13:51:31 +00:00
func (c *Client) Unpair() (*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-17 23:57:20 +00:00
payload, err := proto.Marshal(&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
})
if err != nil {
return nil, err
}
revokeResp, err := c.makeRelayRequest(util.RevokeRelayPairingURL, payload)
2023-07-15 23:11:25 +00:00
if err != nil {
return nil, err
}
responseBody, err := io.ReadAll(revokeResp.Body)
defer revokeResp.Body.Close()
if err != nil {
return nil, err
}
2023-07-17 13:51:31 +00:00
parsedResponse := &gmproto.RevokeRelayPairingResponse{}
2023-07-15 23:11:25 +00:00
err = proto.Unmarshal(responseBody, parsedResponse)
if err != nil {
return nil, err
}
return parsedResponse, nil
}