diff --git a/libgm/client.go b/libgm/client.go index a218c79..cbcf168 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -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 diff --git a/libgm/conversation_builder.go b/libgm/conversation_builder.go index 34990a1..d9823ee 100644 --- a/libgm/conversation_builder.go +++ b/libgm/conversation_builder.go @@ -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"} } diff --git a/libgm/media_processor.go b/libgm/media_processor.go index 2f63210..03878ae 100644 --- a/libgm/media_processor.go +++ b/libgm/media_processor.go @@ -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 diff --git a/libgm/pairing_handler.go b/libgm/pairing_handler.go index 236c4e7..ab324e9 100644 --- a/libgm/pairing_handler.go +++ b/libgm/pairing_handler.go @@ -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") diff --git a/libgm/payload/sendMessage.go b/libgm/payload/sendMessage.go index f9bf28c..c9fe2fa 100644 --- a/libgm/payload/sendMessage.go +++ b/libgm/payload/sendMessage.go @@ -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 } diff --git a/libgm/response_handler.go b/libgm/response_handler.go index bcdbd09..fce1fa2 100644 --- a/libgm/response_handler.go +++ b/libgm/response_handler.go @@ -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{ diff --git a/libgm/rpc.go b/libgm/rpc.go index 7831a1b..912fc33 100644 --- a/libgm/rpc.go +++ b/libgm/rpc.go @@ -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 } diff --git a/libgm/session_handler.go b/libgm/session_handler.go index b48fa30..f1e7b38 100644 --- a/libgm/session_handler.go +++ b/libgm/session_handler.go @@ -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") } diff --git a/libgm/useralert_handler.go b/libgm/useralert_handler.go index 69fa73c..27d85dd 100644 --- a/libgm/useralert_handler.go +++ b/libgm/useralert_handler.go @@ -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)