Change more things

This commit is contained in:
Tulir Asokan 2023-07-02 17:19:00 +03:00
parent 6e7143d532
commit 27b48bc6e8
9 changed files with 42 additions and 19 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"io" "io"
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
"time" "time"
@ -49,13 +50,16 @@ func NewClient(devicePair *DevicePair, cryptor *crypto.Cryptor, logger zerolog.L
if cryptor == nil { if cryptor == nil {
cryptor = crypto.NewCryptor(nil, nil) cryptor = crypto.NewCryptor(nil, nil)
} }
jar, _ := cookiejar.New(nil)
cli := &Client{ cli := &Client{
Logger: logger, Logger: logger,
devicePair: devicePair, devicePair: devicePair,
sessionHandler: sessionHandler, sessionHandler: sessionHandler,
cryptor: cryptor, cryptor: cryptor,
imageCryptor: &crypto.ImageCryptor{}, imageCryptor: &crypto.ImageCryptor{},
http: &http.Client{}, http: &http.Client{
Jar: jar,
},
} }
sessionHandler.client = cli sessionHandler.client = cli
cli.instructions = NewInstructions(cli.cryptor) cli.instructions = NewInstructions(cli.cryptor)
@ -69,6 +73,16 @@ func NewClient(devicePair *DevicePair, cryptor *crypto.Cryptor, logger zerolog.L
return cli return cli
} }
var baseURL, _ = url.Parse("https://messages.google.com/")
func (c *Client) GetCookies() []*http.Cookie {
return c.http.Jar.Cookies(baseURL)
}
func (c *Client) SetCookies(cookies []*http.Cookie) {
c.http.Jar.SetCookies(baseURL, cookies)
}
func (c *Client) SetEventHandler(eventHandler EventHandler) { func (c *Client) SetEventHandler(eventHandler EventHandler) {
if eventHandler == nil { if eventHandler == nil {
eventHandler = func(_ interface{}) {} eventHandler = func(_ interface{}) {}
@ -200,7 +214,7 @@ func (c *Client) decryptImageData(imageId string, key []byte) ([]byte, error) {
Date: &binary.Date{ Date: &binary.Date{
Year: 2023, Year: 2023,
Seq1: 6, Seq1: 6,
Seq2: 8, Seq2: 22,
Seq3: 4, Seq3: 4,
Seq4: 6, Seq4: 6,
}, },

View file

@ -141,7 +141,7 @@ func (c *Client) buildStartUploadPayload() (string, error) {
Date: &binary.Date{ Date: &binary.Date{
Year: 2023, Year: 2023,
Seq1: 6, Seq1: 6,
Seq2: 8, Seq2: 22,
Seq3: 4, Seq3: 4,
Seq4: 6, Seq4: 6,
}, },

View file

@ -116,11 +116,11 @@ func (p *Pairer) RefreshPhoneRelay() {
p.client.triggerEvent(&events.QR{URL: url}) p.client.triggerEvent(&events.QR{URL: url})
} }
func (p *Pairer) GetWebEncryptionKey() { func (p *Pairer) GetWebEncryptionKey(oldKey []byte) []byte {
body, _, err2 := payload.GetWebEncryptionKey(p.client.rpc.webAuthKey) body, _, err2 := payload.GetWebEncryptionKey(oldKey)
if err2 != nil { if err2 != nil {
p.client.Logger.Err(err2).Msg("web encryption key err") p.client.Logger.Err(err2).Msg("web encryption key err")
return return nil
} }
//p.client.Logger.Debug().Any("keyByteLength", len(rawData.PhoneRelay.RpcKey)).Any("json", rawData).Any("base64", body).Msg("GetWebEncryptionKey Payload") //p.client.Logger.Debug().Any("keyByteLength", len(rawData.PhoneRelay.RpcKey)).Any("json", rawData).Any("base64", body).Msg("GetWebEncryptionKey Payload")
webKeyResponse, reqErr := p.client.MakeRelayRequest(util.GET_WEB_ENCRYPTION_KEY, body) webKeyResponse, reqErr := p.client.MakeRelayRequest(util.GET_WEB_ENCRYPTION_KEY, body)
@ -131,7 +131,7 @@ func (p *Pairer) GetWebEncryptionKey() {
defer webKeyResponse.Body.Close() defer webKeyResponse.Body.Close()
if err2 != nil { if err2 != nil {
p.client.Logger.Err(err2).Msg("Web encryption key read response err") p.client.Logger.Err(err2).Msg("Web encryption key read response err")
return return nil
} }
//p.client.Logger.Debug().Any("responseLength", len(responseBody)).Any("raw", responseBody).Msg("Response Body Length") //p.client.Logger.Debug().Any("responseLength", len(responseBody)).Any("raw", responseBody).Msg("Response Body Length")
parsedResponse := &binary.WebEncryptionKeyResponse{} parsedResponse := &binary.WebEncryptionKeyResponse{}
@ -140,18 +140,22 @@ func (p *Pairer) GetWebEncryptionKey() {
p.client.Logger.Err(err2).Msg("Parse webkeyresponse into proto struct error") p.client.Logger.Err(err2).Msg("Parse webkeyresponse into proto struct error")
} }
p.client.Logger.Debug().Any("parsedResponse", parsedResponse).Msg("WebEncryptionKeyResponse") p.client.Logger.Debug().Any("parsedResponse", parsedResponse).Msg("WebEncryptionKeyResponse")
if p.ticker != nil {
p.client.Logger.Info().Msg("Reconnecting")
p.ticker.Stop() p.ticker.Stop()
reconnectErr := p.client.Reconnect(p.client.rpc.webAuthKey) reconnectErr := p.client.Reconnect(p.client.rpc.webAuthKey)
if reconnectErr != nil { if reconnectErr != nil {
panic(reconnectErr) panic(reconnectErr)
} }
}
return parsedResponse.GetKey()
} }
func (p *Pairer) pairCallback(pairData *binary.Container) { func (p *Pairer) pairCallback(pairData *binary.Container) {
p.client.rpc.webAuthKey = pairData.PairDeviceData.WebAuthKeyData.WebAuthKey p.client.rpc.webAuthKey = pairData.PairDeviceData.WebAuthKeyData.WebAuthKey
p.client.ttl = pairData.PairDeviceData.WebAuthKeyData.ValidFor p.client.ttl = pairData.PairDeviceData.WebAuthKeyData.ValidFor
p.client.devicePair = &DevicePair{Mobile: pairData.PairDeviceData.Mobile, Browser: pairData.PairDeviceData.Browser} p.client.devicePair = &DevicePair{Mobile: pairData.PairDeviceData.Mobile, Browser: pairData.PairDeviceData.Browser}
p.client.pairer.GetWebEncryptionKey() p.client.pairer.GetWebEncryptionKey(p.client.rpc.webAuthKey)
p.client.triggerEvent(&events.PairSuccessful{Container: pairData}) p.client.triggerEvent(&events.PairSuccessful{Container: pairData})
p.client.pairer = nil p.client.pairer = nil
} }

View file

@ -14,7 +14,7 @@ func GetWebEncryptionKey(WebPairKey []byte) ([]byte, *binary.Container, error) {
Date: &binary.Date{ Date: &binary.Date{
Year: 2023, Year: 2023,
Seq1: 6, Seq1: 6,
Seq2: 8, Seq2: 22,
Seq3: 4, Seq3: 4,
Seq4: 6, Seq4: 6,
}, },

View file

@ -17,7 +17,7 @@ func ReceiveMessages(rpcKey []byte) ([]byte, string, error) {
Date: &binary.Date{ Date: &binary.Date{
Year: 2023, Year: 2023,
Seq1: 6, Seq1: 6,
Seq2: 8, Seq2: 22,
Seq3: 4, Seq3: 4,
Seq4: 6, Seq4: 6,
}, },

View file

@ -14,7 +14,7 @@ func RefreshPhoneRelay(rpcKey []byte) ([]byte, *binary.Container, error) {
Date: &binary.Date{ Date: &binary.Date{
Year: 2023, Year: 2023,
Seq1: 6, Seq1: 6,
Seq2: 8, Seq2: 22,
Seq3: 4, Seq3: 4,
Seq4: 6, Seq4: 6,
}, },

View file

@ -37,7 +37,7 @@ func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.Container, error) {
Date: &binary.Date{ Date: &binary.Date{
Year: 2023, Year: 2023,
Seq1: 6, Seq1: 6,
Seq2: 8, Seq2: 22,
Seq3: 4, Seq3: 4,
Seq4: 6, Seq4: 6,
}, },

View file

@ -19,10 +19,13 @@ type RPC struct {
conn io.ReadCloser conn io.ReadCloser
rpcSessionID string rpcSessionID string
webAuthKey []byte webAuthKey []byte
listenID int
} }
func (r *RPC) ListenReceiveMessages(payload []byte) { func (r *RPC) ListenReceiveMessages(payload []byte) {
for { r.listenID++
listenID := r.listenID
for r.listenID == listenID {
r.client.Logger.Debug().Msg("Starting new long-polling request") r.client.Logger.Debug().Msg("Starting new long-polling request")
req, err := http.NewRequest("POST", util.RECEIVE_MESSAGES, bytes.NewReader(payload)) req, err := http.NewRequest("POST", util.RECEIVE_MESSAGES, bytes.NewReader(payload))
if err != nil { if err != nil {
@ -34,6 +37,7 @@ func (r *RPC) ListenReceiveMessages(payload []byte) {
if reqErr != nil { if reqErr != nil {
panic(fmt.Errorf("Error making request: %v", err)) panic(fmt.Errorf("Error making request: %v", err))
} }
r.client.Logger.Debug().Int("statusCode", resp.StatusCode).Msg("Long polling opened")
r.conn = resp.Body r.conn = resp.Body
r.startReadingData(resp.Body) r.startReadingData(resp.Body)
} }
@ -133,6 +137,7 @@ func (r *RPC) startReadingData(rc io.ReadCloser) {
func (r *RPC) CloseConnection() { func (r *RPC) CloseConnection() {
if r.conn != nil { if r.conn != nil {
r.listenID++
r.client.Logger.Debug().Msg("Attempting to connection...") r.client.Logger.Debug().Msg("Attempting to connection...")
r.conn.Close() r.conn.Close()
r.conn = nil r.conn = nil

View file

@ -74,7 +74,7 @@ func (c *Client) createAndSendRequest(instructionId int64, ttl int64, newSession
panic(fmt.Errorf("Failed to encode data: %w", encodeErr)) panic(fmt.Errorf("Failed to encode data: %w", encodeErr))
} }
messageData := payload.NewMessageData(requestId, encodedStr, instruction.RoutingOpCode, instruction.MsgType) messageData := payload.NewMessageData(requestId, encodedStr, instruction.RoutingOpCode, instruction.MsgType)
authMessage := payload.NewAuthData(requestId, c.rpcKey, &binary.Date{Year: 2023, Seq1: 6, Seq2: 8, Seq3: 4, Seq4: 6}) authMessage := payload.NewAuthData(requestId, c.rpcKey, &binary.Date{Year: 2023, Seq1: 6, Seq2: 22, Seq3: 4, Seq4: 6})
sendMessage := payload.NewSendMessage(c.devicePair.Mobile, messageData, authMessage, ttl) sendMessage := payload.NewSendMessage(c.devicePair.Mobile, messageData, authMessage, ttl)
sentRequestID, reqErr := c.sessionHandler.completeSendMessage(encodedData.RequestID, instruction.Opcode, sendMessage) sentRequestID, reqErr := c.sessionHandler.completeSendMessage(encodedData.RequestID, instruction.Opcode, sendMessage)
@ -139,7 +139,7 @@ func (s *SessionHandler) sendAckRequest() {
AuthData: &binary.AuthMessage{ AuthData: &binary.AuthMessage{
RequestID: reqId, RequestID: reqId,
RpcKey: s.client.rpcKey, RpcKey: s.client.rpcKey,
Date: &binary.Date{Year: 2023, Seq1: 6, Seq2: 8, Seq3: 4, Seq4: 6}, Date: &binary.Date{Year: 2023, Seq1: 6, Seq2: 22, Seq3: 4, Seq4: 6},
}, },
EmptyArr: &binary.EmptyArr{}, EmptyArr: &binary.EmptyArr{},
NoClue: nil, NoClue: nil,