Fix usages of log package

This commit is contained in:
Tulir Asokan 2023-07-09 23:32:19 +03:00
parent a33e01d124
commit a350e4fc00
9 changed files with 31 additions and 41 deletions

View file

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
@ -102,7 +101,7 @@ func (c *Client) Connect() error {
c.Logger.Error().Any("expired", hasExpired).Any("secondsSince", authenticatedAtSeconds).Msg("TachyonToken has expired! attempting to refresh")
refreshErr := c.refreshAuthToken()
if refreshErr != nil {
log.Fatal(refreshErr)
panic(refreshErr)
}
}
c.Logger.Info().Any("secondsSince", authenticatedAtSeconds).Any("token", c.authData.TachyonAuthToken).Msg("TachyonToken has not expired, attempting to connect...")
@ -115,7 +114,7 @@ func (c *Client) Connect() error {
c.updateWebEncryptionKey(webEncryptionKeyResponse.GetKey())
rpcPayload, receiveMessageSessionId, err := payload.ReceiveMessages(c.authData.TachyonAuthToken)
if err != nil {
log.Fatal(err)
panic(err)
return err
}
c.rpc.rpcSessionId = receiveMessageSessionId
@ -124,19 +123,19 @@ func (c *Client) Connect() error {
bugleRes, bugleErr := c.Session.IsBugleDefault()
if bugleErr != nil {
log.Fatal(bugleErr)
panic(bugleErr)
}
c.Logger.Info().Any("isBugle", bugleRes.Success).Msg("IsBugleDefault")
sessionErr := c.Session.SetActiveSession()
if sessionErr != nil {
log.Fatal(sessionErr)
panic(sessionErr)
}
//c.Logger.Debug().Any("tachyonAuthToken", c.authData.TachyonAuthToken).Msg("Successfully connected to server")
return nil
} else {
pairer, err := c.NewPairer(nil, 20)
if err != nil {
log.Fatal(err)
panic(err)
}
c.pairer = pairer
registered, err2 := c.pairer.RegisterPhoneRelay()
@ -146,7 +145,7 @@ func (c *Client) Connect() error {
c.authData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken
rpcPayload, receiveMessageSessionId, err := payload.ReceiveMessages(c.authData.TachyonAuthToken)
if err != nil {
log.Fatal(err)
panic(err)
return err
}
c.rpc.rpcSessionId = receiveMessageSessionId
@ -214,7 +213,7 @@ func (c *Client) decryptMedias(messages *binary.FetchMessagesResponse) error {
case *binary.MessageInfo_MediaContent:
decryptedMediaData, err := c.DownloadMedia(data.MediaContent.MediaID, data.MediaContent.DecryptionKey)
if err != nil {
log.Fatal(err)
panic(err)
return err
}
data.MediaContent.MediaData = decryptedMediaData
@ -261,7 +260,6 @@ func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
c.imageCryptor.UpdateDecryptionKey(key)
decryptedImageBytes, decryptionErr := c.imageCryptor.DecryptData(encryptedBuffImg)
if decryptionErr != nil {
log.Println("Error:", decryptionErr)
return nil, decryptionErr
}
return decryptedImageBytes, nil
@ -270,22 +268,22 @@ func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
func (c *Client) FetchConfigVersion() {
req, bErr := http.NewRequest("GET", util.CONFIG_URL, nil)
if bErr != nil {
log.Fatal(bErr)
panic(bErr)
}
configRes, requestErr := c.http.Do(req)
if requestErr != nil {
log.Fatal(requestErr)
panic(requestErr)
}
responseBody, readErr := io.ReadAll(configRes.Body)
if readErr != nil {
log.Fatal(readErr)
panic(readErr)
}
version, parseErr := util.ParseConfigVersion(responseBody)
if parseErr != nil {
log.Fatal(parseErr)
panic(parseErr)
}
currVersion := payload.ConfigMessage
@ -352,6 +350,10 @@ func LoadAuthSession(path string) (*AuthData, error) {
return sessionData, nil
}
func (c *Client) RefreshAuthToken() error {
return c.refreshAuthToken()
}
func (c *Client) refreshAuthToken() error {
jwk := c.authData.JWK

View file

@ -2,7 +2,6 @@ package libgm
import (
"fmt"
"log"
"google.golang.org/protobuf/proto"
@ -60,7 +59,7 @@ func (cb *ConversationBuilder) Build(protoMessage proto.Message) (proto.Message,
}
return payload, nil
default:
log.Fatal("Invalid protoMessage conversation builder type")
panic("Invalid protoMessage conversation builder type")
}
return nil, &ConversationBuilderError{errMsg: "failed to build for unknown reasons"}
}

View file

@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"io"
"log"
"net/http"
"strconv"
@ -40,7 +39,6 @@ func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, e
imageType := upload.Image.GetImageType()
encryptedImageSize := strconv.Itoa(len(upload.EncryptedMediaBytes))
log.Println("EncryptedImageSize:", encryptedImageSize)
finalizeUploadHeaders := util.NewMediaUploadHeaders(encryptedImageSize, "upload, finalize", "0", imageType.Format, "")
req, reqErr := http.NewRequest("POST", upload.UploadURL, bytes.NewBuffer(upload.EncryptedMediaBytes))
if reqErr != nil {
@ -51,7 +49,7 @@ func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, e
res, resErr := c.http.Do(req)
if resErr != nil {
log.Fatal(resErr)
panic(resErr)
}
statusCode := res.StatusCode
@ -68,7 +66,7 @@ func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, e
}
uploadStatus := rHeaders.Get("x-goog-upload-status")
log.Println("Upload Status: ", uploadStatus)
c.Logger.Debug().Str("upload_status", uploadStatus).Msg("Upload complete")
mediaIDs := &binary.UploadMediaResponse{}
err3 = crypto.DecodeAndEncodeB64(string(googleResponse), mediaIDs)
@ -106,7 +104,7 @@ func (c *Client) StartUploadMedia(image *Image) (*StartGoogleUpload, error) {
res, resErr := c.http.Do(req)
if resErr != nil {
log.Fatal(resErr)
panic(resErr)
}
statusCode := res.StatusCode

View file

@ -1,8 +1,6 @@
package libgm
import (
"log"
"go.mau.fi/mautrix-gmessages/libgm/events"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
@ -21,7 +19,7 @@ func (c *Client) handlePairingEvent(response *pblite.Response) {
case *binary.PairEvents_Paired:
callbackErr := c.pairCallback(evt.Paired)
if callbackErr != nil {
log.Fatal(callbackErr)
panic(callbackErr)
}
case *binary.PairEvents_Revoked:
c.Logger.Debug().Any("data", evt).Msg("Revoked Device")

View file

@ -3,7 +3,6 @@ package payload
import (
"encoding/json"
"fmt"
"log"
"google.golang.org/protobuf/proto"
@ -116,7 +115,7 @@ func (sm *SendMessageBuilder) Build() ([]byte, error) {
messageProtoJSON, serializeErr := pblite.Serialize(sm.message.ProtoReflect())
if serializeErr != nil {
log.Fatal(serializeErr)
panic(serializeErr)
return nil, serializeErr
}

View file

@ -2,7 +2,6 @@ package libgm
import (
"fmt"
"log"
"sync"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
@ -20,8 +19,7 @@ type ResponseChan struct {
func (s *SessionHandler) addRequestToChannel(requestId string, actionType binary.ActionType) {
_, notOk := routes.Routes[actionType]
if !notOk {
log.Println("Missing action type: ", actionType)
log.Fatal(notOk)
panic(fmt.Errorf("missing action type: %v", actionType))
}
if msgMap, ok := s.requests[requestId]; ok {
responseChan := &ResponseChan{

View file

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
@ -137,7 +136,7 @@ func (r *RPC) isAcknowledgeMessage(data []byte) bool {
if len(data) >= 3 && data[0] == 91 && data[1] == 91 && data[2] == 91 {
parsed, parseErr := r.parseAckMessage(data)
if parseErr != nil {
log.Fatal(parseErr)
panic(parseErr)
}
skipCount = parsed.Container.Data.GetAckAmount().Count
r.client.Logger.Info().Any("count", skipCount).Msg("Messages To Skip")
@ -184,13 +183,13 @@ func (r *RPC) CloseConnection() {
func (r *RPC) sendMessageRequest(url string, payload []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
if err != nil {
log.Fatalf("Error creating request: %v", err)
return nil, fmt.Errorf("error creating request: %w", err)
}
util.BuildRelayHeaders(req, "application/json+protobuf", "*/*")
resp, reqErr := r.client.http.Do(req)
//r.client.Logger.Info().Any("bodyLength", len(payload)).Any("url", url).Any("headers", resp.Request.Header).Msg("RPC Request Headers")
if reqErr != nil {
log.Fatalf("Error making request: %v", err)
return nil, fmt.Errorf("error making request: %w", err)
}
return resp, reqErr
}

View file

@ -3,7 +3,6 @@ package libgm
import (
"encoding/json"
"fmt"
"log"
"time"
"golang.org/x/exp/slices"
@ -138,14 +137,14 @@ func (s *SessionHandler) sendAckRequest() {
}
dataArray, err := pblite.Serialize(ackMessagePayload.ProtoReflect())
if err != nil {
log.Fatal(err)
panic(err)
}
ackMessages := make([][]interface{}, 0)
for _, reqId := range s.ackMap {
ackMessageData := &binary.AckMessageData{RequestID: reqId, Device: s.client.authData.DevicePair.Browser}
ackMessageDataArr, err := pblite.Serialize(ackMessageData.ProtoReflect())
if err != nil {
log.Fatal(err)
panic(err)
}
ackMessages = append(ackMessages, ackMessageDataArr)
s.ackMap = util.RemoveFromSlice(s.ackMap, reqId)
@ -153,11 +152,11 @@ func (s *SessionHandler) sendAckRequest() {
dataArray = append(dataArray, ackMessages)
jsonData, jsonErr := json.Marshal(dataArray)
if jsonErr != nil {
log.Fatal(err)
panic(err)
}
_, err = s.client.rpc.sendMessageRequest(util.ACK_MESSAGES, jsonData)
if err != nil {
log.Fatal(err)
panic(err)
}
s.client.Logger.Debug().Any("payload", jsonData).Msg("[ACK] Sent Request")
}

View file

@ -1,8 +1,6 @@
package libgm
import (
"log"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
"go.mau.fi/mautrix-gmessages/libgm/binary"
@ -22,12 +20,12 @@ func (c *Client) handleUserAlertEvent(res *pblite.Response, data *binary.UserAle
c.Logger.Info().Any("sessionId", newSessionId).Msg("Client is ready!")
conversations, convErr := c.Conversations.List(25)
if convErr != nil {
log.Fatal(convErr)
panic(convErr)
}
c.Logger.Debug().Any("conversations", conversations).Msg("got conversations")
notifyErr := c.Session.NotifyDittoActivity()
if notifyErr != nil {
log.Fatal(notifyErr)
panic(notifyErr)
}
readyEvt := events.NewClientReady(newSessionId, conversations)
c.triggerEvent(readyEvt)