gmessages/libgm/pair.go

148 lines
3.7 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 (
2023-07-16 11:36:13 +00:00
"crypto/x509"
2023-06-30 09:54:08 +00:00
"io"
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/util"
)
2023-07-16 12:55:30 +00:00
func (c *Client) RegisterPhoneRelay() (*binary.RegisterPhoneRelayResponse, error) {
key, err := x509.MarshalPKIXPublicKey(c.AuthData.RefreshKey.GetPublicKey())
2023-07-16 11:36:13 +00:00
if err != nil {
return nil, err
}
body, err := proto.Marshal(&binary.AuthenticationContainer{
AuthMessage: &binary.AuthMessage{
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-16 11:36:13 +00:00
Data: &binary.AuthenticationContainer_KeyData{
KeyData: &binary.KeyData{
EcdsaKeys: &binary.ECDSAKeys{
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
}
2023-07-16 12:55:30 +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()
res := &binary.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) {
body, err := proto.Marshal(&binary.AuthenticationContainer{
AuthMessage: &binary.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
}
2023-07-16 12:55:30 +00:00
relayResponse, err := c.MakeRelayRequest(util.RefreshPhoneRelayURL, body)
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
}
res := &binary.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
}
func (c *Client) GetWebEncryptionKey() (*binary.WebEncryptionKeyResponse, error) {
body, err := proto.Marshal(&binary.AuthenticationContainer{
AuthMessage: &binary.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
}
2023-07-15 23:21:53 +00:00
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
}
parsedResponse := &binary.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
func (c *Client) Unpair() (*binary.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
}
payload, err := proto.Marshal(&binary.RevokeRelayPairing{
AuthMessage: &binary.AuthMessage{
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
}
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
}