2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
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-06-30 09:54:08 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/binary"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/crypto"
|
2023-06-30 10:43:54 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/events"
|
2023-06-30 09:54:08 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/payload"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Pairer struct {
|
|
|
|
client *Client
|
|
|
|
KeyData *crypto.JWK
|
|
|
|
ticker *time.Ticker
|
|
|
|
tickerTime time.Duration
|
|
|
|
pairingKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
refreshQrCodeTime is the interval to refresh the qr code in seconds, this is usually 20 seconds.
|
|
|
|
*/
|
|
|
|
func (c *Client) NewPairer(keyData *crypto.JWK, refreshQrCodeTime int) (*Pairer, error) {
|
|
|
|
if keyData == nil {
|
|
|
|
var err error
|
2023-07-15 13:45:50 +00:00
|
|
|
keyData, err = crypto.GenerateECDSAKey()
|
2023-07-09 11:16:52 +00:00
|
|
|
c.updateJWK(keyData)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Logger.Error().Any("data", keyData).Msg(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p := &Pairer{
|
|
|
|
client: c,
|
|
|
|
KeyData: keyData,
|
|
|
|
tickerTime: time.Duration(refreshQrCodeTime) * time.Second,
|
|
|
|
}
|
|
|
|
c.pairer = p
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pairer) RegisterPhoneRelay() (*binary.RegisterPhoneRelayResponse, error) {
|
|
|
|
body, _, err := payload.RegisterPhoneRelay(p.KeyData)
|
|
|
|
if err != nil {
|
|
|
|
p.client.Logger.Err(err)
|
|
|
|
return &binary.RegisterPhoneRelayResponse{}, err
|
|
|
|
}
|
2023-07-15 23:21:53 +00:00
|
|
|
relayResponse, reqErr := p.client.MakeRelayRequest(util.RegisterPhoneRelayURL, body)
|
2023-06-30 09:54:08 +00:00
|
|
|
if reqErr != nil {
|
|
|
|
p.client.Logger.Err(reqErr)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
responseBody, err2 := io.ReadAll(relayResponse.Body)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
relayResponse.Body.Close()
|
|
|
|
res := &binary.RegisterPhoneRelayResponse{}
|
2023-07-15 17:43:28 +00:00
|
|
|
err3 := proto.Unmarshal(responseBody, res)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err3 != nil {
|
|
|
|
return nil, err3
|
|
|
|
}
|
|
|
|
p.pairingKey = res.GetPairingKey()
|
2023-07-09 11:16:52 +00:00
|
|
|
p.client.Logger.Debug().Any("response", res).Msg("Registerphonerelay response")
|
2023-06-30 10:43:54 +00:00
|
|
|
url, qrErr := p.GenerateQRCodeData()
|
2023-06-30 09:54:08 +00:00
|
|
|
if qrErr != nil {
|
|
|
|
return nil, qrErr
|
|
|
|
}
|
2023-06-30 13:09:29 +00:00
|
|
|
p.client.triggerEvent(&events.QR{URL: url})
|
2023-06-30 09:54:08 +00:00
|
|
|
p.startRefreshRelayTask()
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pairer) startRefreshRelayTask() {
|
|
|
|
if p.ticker != nil {
|
|
|
|
p.ticker.Stop()
|
|
|
|
}
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
p.ticker = ticker
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
|
|
|
p.RefreshPhoneRelay()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pairer) RefreshPhoneRelay() {
|
2023-07-15 23:16:10 +00:00
|
|
|
body, err := proto.Marshal(&binary.AuthenticationContainer{
|
|
|
|
AuthMessage: &binary.AuthMessage{
|
|
|
|
RequestID: util.RandomUUIDv4(),
|
|
|
|
Network: &payload.Network,
|
|
|
|
TachyonAuthToken: p.client.authData.TachyonAuthToken,
|
|
|
|
ConfigVersion: payload.ConfigMessage,
|
|
|
|
},
|
|
|
|
})
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
p.client.Logger.Err(err).Msg("refresh phone relay err")
|
|
|
|
return
|
|
|
|
}
|
2023-07-15 23:21:53 +00:00
|
|
|
relayResponse, reqErr := p.client.MakeRelayRequest(util.RefreshPhoneRelayURL, body)
|
2023-06-30 09:54:08 +00:00
|
|
|
if reqErr != nil {
|
|
|
|
p.client.Logger.Err(reqErr).Msg("refresh phone relay err")
|
|
|
|
}
|
|
|
|
responseBody, err2 := io.ReadAll(relayResponse.Body)
|
|
|
|
defer relayResponse.Body.Close()
|
|
|
|
if err2 != nil {
|
|
|
|
p.client.Logger.Err(err2).Msg("refresh phone relay err")
|
|
|
|
}
|
|
|
|
p.client.Logger.Debug().Any("responseLength", len(responseBody)).Msg("Response Body Length")
|
|
|
|
res := &binary.RefreshPhoneRelayResponse{}
|
2023-07-15 17:43:28 +00:00
|
|
|
err3 := proto.Unmarshal(responseBody, res)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err3 != nil {
|
|
|
|
p.client.Logger.Err(err3)
|
|
|
|
}
|
|
|
|
p.pairingKey = res.GetPairKey()
|
|
|
|
p.client.Logger.Debug().Any("res", res).Msg("RefreshPhoneRelayResponse")
|
2023-06-30 10:43:54 +00:00
|
|
|
url, qrErr := p.GenerateQRCodeData()
|
2023-06-30 09:54:08 +00:00
|
|
|
if qrErr != nil {
|
2023-06-30 11:48:50 +00:00
|
|
|
panic(qrErr)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-06-30 13:09:29 +00:00
|
|
|
p.client.triggerEvent(&events.QR{URL: url})
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
func (c *Client) GetWebEncryptionKey() (*binary.WebEncryptionKeyResponse, error) {
|
2023-07-15 23:16:10 +00:00
|
|
|
body, err := proto.Marshal(&binary.AuthenticationContainer{
|
|
|
|
AuthMessage: &binary.AuthMessage{
|
|
|
|
RequestID: uuid.NewString(),
|
|
|
|
TachyonAuthToken: c.authData.TachyonAuthToken,
|
|
|
|
ConfigVersion: payload.ConfigMessage,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
2023-07-15 23:21:53 +00:00
|
|
|
webKeyResponse, err := c.MakeRelayRequest(util.GetWebEncryptionKeyURL, body)
|
2023-07-15 23:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-15 23:16:10 +00:00
|
|
|
responseBody, err := io.ReadAll(webKeyResponse.Body)
|
2023-06-30 09:54:08 +00:00
|
|
|
defer webKeyResponse.Body.Close()
|
2023-07-15 23:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
parsedResponse := &binary.WebEncryptionKeyResponse{}
|
2023-07-15 23:16:10 +00:00
|
|
|
err = proto.Unmarshal(responseBody, parsedResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
if c.pairer != nil {
|
|
|
|
if c.pairer.ticker != nil {
|
|
|
|
c.pairer.ticker.Stop()
|
2023-07-02 14:19:00 +00:00
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
return parsedResponse, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-15 23:11:25 +00:00
|
|
|
|
|
|
|
func (c *Client) Unpair() (*binary.RevokeRelayPairingResponse, error) {
|
|
|
|
if c.authData.TachyonAuthToken == nil || c.authData.DevicePair == nil || c.authData.DevicePair.Browser == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
payload, err := proto.Marshal(&binary.RevokeRelayPairing{
|
|
|
|
AuthMessage: &binary.AuthMessage{
|
|
|
|
RequestID: uuid.NewString(),
|
|
|
|
TachyonAuthToken: c.authData.TachyonAuthToken,
|
|
|
|
ConfigVersion: payload.ConfigMessage,
|
|
|
|
},
|
|
|
|
Browser: c.authData.DevicePair.Browser,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-15 23:21:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
parsedResponse := &binary.RevokeRelayPairingResponse{}
|
|
|
|
err = proto.Unmarshal(responseBody, parsedResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parsedResponse, nil
|
|
|
|
}
|