2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-17 13:43:34 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2024-03-11 13:31:15 +00:00
|
|
|
"fmt"
|
2023-07-17 13:43:34 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-07-15 17:08:11 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-07-17 13:43:34 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
2023-06-30 09:54:08 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/crypto"
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-07-17 13:43:34 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/util"
|
2023-06-30 09:54:08 +00:00
|
|
|
)
|
|
|
|
|
2023-07-15 16:48:26 +00:00
|
|
|
type MediaType struct {
|
2023-06-30 09:54:08 +00:00
|
|
|
Extension string
|
|
|
|
Format string
|
2023-07-17 13:51:31 +00:00
|
|
|
Type gmproto.MediaFormats
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 17:08:11 +00:00
|
|
|
var MimeToMediaType = map[string]MediaType{
|
2023-07-17 13:51:31 +00:00
|
|
|
"image/jpeg": {Extension: "jpeg", Type: gmproto.MediaFormats_IMAGE_JPEG},
|
|
|
|
"image/jpg": {Extension: "jpg", Type: gmproto.MediaFormats_IMAGE_JPG},
|
|
|
|
"image/png": {Extension: "png", Type: gmproto.MediaFormats_IMAGE_PNG},
|
|
|
|
"image/gif": {Extension: "gif", Type: gmproto.MediaFormats_IMAGE_GIF},
|
|
|
|
"image/wbmp": {Extension: "wbmp", Type: gmproto.MediaFormats_IMAGE_WBMP},
|
|
|
|
"image/bmp": {Extension: "bmp", Type: gmproto.MediaFormats_IMAGE_X_MS_BMP},
|
|
|
|
"image/x-ms-bmp": {Extension: "bmp", Type: gmproto.MediaFormats_IMAGE_X_MS_BMP},
|
|
|
|
|
|
|
|
"video/mp4": {Extension: "mp4", Type: gmproto.MediaFormats_VIDEO_MP4},
|
|
|
|
"video/3gpp2": {Extension: "3gpp2", Type: gmproto.MediaFormats_VIDEO_3G2},
|
|
|
|
"video/3gpp": {Extension: "3gpp", Type: gmproto.MediaFormats_VIDEO_3GPP},
|
|
|
|
"video/webm": {Extension: "webm", Type: gmproto.MediaFormats_VIDEO_WEBM},
|
|
|
|
"video/x-matroska": {Extension: "mkv", Type: gmproto.MediaFormats_VIDEO_MKV},
|
|
|
|
|
|
|
|
"audio/aac": {Extension: "aac", Type: gmproto.MediaFormats_AUDIO_AAC},
|
|
|
|
"audio/amr": {Extension: "amr", Type: gmproto.MediaFormats_AUDIO_AMR},
|
|
|
|
"audio/mp3": {Extension: "mp3", Type: gmproto.MediaFormats_AUDIO_MP3},
|
|
|
|
"audio/mpeg": {Extension: "mpeg", Type: gmproto.MediaFormats_AUDIO_MPEG},
|
|
|
|
"audio/mpg": {Extension: "mpg", Type: gmproto.MediaFormats_AUDIO_MPG},
|
|
|
|
"audio/mp4": {Extension: "mp4", Type: gmproto.MediaFormats_AUDIO_MP4},
|
|
|
|
"audio/mp4-latm": {Extension: "latm", Type: gmproto.MediaFormats_AUDIO_MP4_LATM},
|
|
|
|
"audio/3gpp": {Extension: "3gpp", Type: gmproto.MediaFormats_AUDIO_3GPP},
|
|
|
|
"audio/ogg": {Extension: "ogg", Type: gmproto.MediaFormats_AUDIO_OGG},
|
|
|
|
|
|
|
|
"text/vcard": {Extension: "vcard", Type: gmproto.MediaFormats_TEXT_VCARD},
|
|
|
|
"application/pdf": {Extension: "pdf", Type: gmproto.MediaFormats_APP_PDF},
|
|
|
|
"text/plain": {Extension: "txt", Type: gmproto.MediaFormats_APP_TXT},
|
|
|
|
"text/html": {Extension: "html", Type: gmproto.MediaFormats_APP_HTML},
|
|
|
|
"application/msword": {Extension: "doc", Type: gmproto.MediaFormats_APP_DOC},
|
|
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": {Extension: "docx", Type: gmproto.MediaFormats_APP_DOCX},
|
|
|
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation": {Extension: "pptx", Type: gmproto.MediaFormats_APP_PPTX},
|
|
|
|
"application/vnd.ms-powerpoint": {Extension: "ppt", Type: gmproto.MediaFormats_APP_PPT},
|
|
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {Extension: "xlsx", Type: gmproto.MediaFormats_APP_XLSX},
|
|
|
|
"application/vnd.ms-excel": {Extension: "xls", Type: gmproto.MediaFormats_APP_XLS},
|
|
|
|
"application/vnd.android.package-archive": {Extension: "apk", Type: gmproto.MediaFormats_APP_APK},
|
|
|
|
"application/zip": {Extension: "zip", Type: gmproto.MediaFormats_APP_ZIP},
|
|
|
|
"application/java-archive": {Extension: "jar", Type: gmproto.MediaFormats_APP_JAR},
|
|
|
|
"text/x-calendar": {Extension: "vcs", Type: gmproto.MediaFormats_CAL_TEXT_VCALENDAR},
|
|
|
|
"text/calendar": {Extension: "ics", Type: gmproto.MediaFormats_CAL_TEXT_CALENDAR},
|
|
|
|
|
|
|
|
"image": {Type: gmproto.MediaFormats_IMAGE_UNSPECIFIED},
|
|
|
|
"video": {Type: gmproto.MediaFormats_VIDEO_UNSPECIFIED},
|
|
|
|
"audio": {Type: gmproto.MediaFormats_AUDIO_UNSPECIFIED},
|
|
|
|
"application": {Type: gmproto.MediaFormats_APP_UNSPECIFIED},
|
|
|
|
"text": {Type: gmproto.MediaFormats_APP_TXT},
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
var FormatToMediaType = map[gmproto.MediaFormats]MediaType{
|
|
|
|
gmproto.MediaFormats_CAL_TEXT_XVCALENDAR: MimeToMediaType["text/x-calendar"],
|
|
|
|
gmproto.MediaFormats_CAL_APPLICATION_VCS: MimeToMediaType["text/x-calendar"],
|
|
|
|
gmproto.MediaFormats_CAL_APPLICATION_ICS: MimeToMediaType["text/calendar"],
|
|
|
|
//gmproto.MediaFormats_CAL_APPLICATION_HBSVCS: ???
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 17:08:11 +00:00
|
|
|
func init() {
|
|
|
|
for key, mediaType := range MimeToMediaType {
|
|
|
|
if strings.ContainsRune(key, '/') {
|
|
|
|
mediaType.Format = key
|
|
|
|
}
|
|
|
|
FormatToMediaType[mediaType.Type] = mediaType
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) UploadMedia(data []byte, fileName, mime string) (*gmproto.MediaContent, error) {
|
2023-07-15 17:08:11 +00:00
|
|
|
mediaType := MimeToMediaType[mime]
|
|
|
|
if mediaType.Type == 0 {
|
|
|
|
mediaType = MimeToMediaType[strings.Split(mime, "/")[0]]
|
|
|
|
}
|
2023-07-16 11:36:13 +00:00
|
|
|
decryptionKey := crypto.GenerateKey(32)
|
2023-07-17 14:56:34 +00:00
|
|
|
cryptor, err := crypto.NewAESGCMHelper(decryptionKey)
|
2023-07-15 16:48:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-15 17:08:11 +00:00
|
|
|
encryptedBytes, err := cryptor.EncryptData(data)
|
|
|
|
if err != nil {
|
2024-03-11 13:31:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to encrypt media: %w", err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-15 17:08:11 +00:00
|
|
|
startUploadImage, err := c.StartUploadMedia(encryptedBytes, mime)
|
2023-07-15 16:48:26 +00:00
|
|
|
if err != nil {
|
2024-03-11 13:31:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to start upload: %w", err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-15 16:48:26 +00:00
|
|
|
upload, err := c.FinalizeUploadMedia(startUploadImage)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
2024-03-11 13:31:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to finalize upload: %w", err)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-17 13:51:31 +00:00
|
|
|
return &gmproto.MediaContent{
|
2023-07-15 17:08:11 +00:00
|
|
|
Format: mediaType.Type,
|
2023-07-15 16:48:26 +00:00
|
|
|
MediaID: upload.MediaID,
|
2023-07-15 17:08:11 +00:00
|
|
|
MediaName: fileName,
|
|
|
|
Size: int64(len(data)),
|
|
|
|
DecryptionKey: decryptionKey,
|
2024-04-16 14:44:34 +00:00
|
|
|
MimeType: mime,
|
2023-06-30 09:54:08 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-07-17 13:43:34 +00:00
|
|
|
|
|
|
|
type StartGoogleUpload struct {
|
|
|
|
UploadID string
|
|
|
|
UploadURL string
|
|
|
|
UploadStatus string
|
|
|
|
ChunkGranularity int64
|
|
|
|
ControlURL string
|
|
|
|
MimeType string
|
|
|
|
|
|
|
|
EncryptedMediaBytes []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type MediaUpload struct {
|
|
|
|
MediaID string
|
|
|
|
MediaNumber int64
|
|
|
|
}
|
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
func isBase64Character(char byte) bool {
|
|
|
|
return (char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9') || char == '+' || char == '/' || char == '='
|
|
|
|
}
|
|
|
|
|
|
|
|
func isStandardBase64(data []byte) bool {
|
|
|
|
if len(data)%4 != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, char := range data {
|
|
|
|
if !isBase64Character(char) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2023-07-17 13:43:34 +00:00
|
|
|
|
|
|
|
func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, error) {
|
|
|
|
encryptedImageSize := strconv.Itoa(len(upload.EncryptedMediaBytes))
|
|
|
|
|
|
|
|
finalizeUploadHeaders := util.NewMediaUploadHeaders(encryptedImageSize, "upload, finalize", "0", upload.MimeType, "")
|
2024-03-11 13:31:15 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, upload.UploadURL, bytes.NewBuffer(upload.EncryptedMediaBytes))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to prepare request: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
req.Header = *finalizeUploadHeaders
|
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
res, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
2023-07-19 11:19:04 +00:00
|
|
|
defer res.Body.Close()
|
2023-07-17 13:43:34 +00:00
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("unexpected status code %d", res.StatusCode)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
2024-03-11 13:31:15 +00:00
|
|
|
respData, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
|
|
|
}
|
|
|
|
if isStandardBase64(respData) {
|
|
|
|
n, err := base64.StdEncoding.Decode(respData, respData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode response: %w", err)
|
|
|
|
}
|
|
|
|
respData = respData[:n]
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
c.Logger.Debug().
|
|
|
|
Str("upload_status", res.Header.Get("x-goog-upload-status")).
|
|
|
|
Msg("Upload complete")
|
2023-07-17 13:43:34 +00:00
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
mediaIDs := &gmproto.UploadMediaResponse{}
|
2024-03-11 13:31:15 +00:00
|
|
|
err = proto.Unmarshal(respData, mediaIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
return &MediaUpload{
|
|
|
|
MediaID: mediaIDs.Media.MediaID,
|
|
|
|
MediaNumber: mediaIDs.Media.MediaNumber,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) StartUploadMedia(encryptedImageBytes []byte, mime string) (*StartGoogleUpload, error) {
|
|
|
|
encryptedImageSize := strconv.Itoa(len(encryptedImageBytes))
|
|
|
|
|
|
|
|
startUploadHeaders := util.NewMediaUploadHeaders(encryptedImageSize, "start", "", mime, "resumable")
|
2024-03-11 13:31:15 +00:00
|
|
|
startUploadPayload, err := c.buildStartUploadPayload()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build payload: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, util.UploadMediaURL, bytes.NewBuffer([]byte(startUploadPayload)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to prepare request: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
req.Header = *startUploadHeaders
|
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
res, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
2024-03-11 13:31:15 +00:00
|
|
|
_ = res.Body.Close()
|
2023-07-17 13:43:34 +00:00
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("unexpected status code %d", res.StatusCode)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 13:31:15 +00:00
|
|
|
chunkGranularity, err := strconv.Atoi(res.Header.Get("x-goog-upload-chunk-granularity"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse chunk granularity: %w", err)
|
2023-07-17 13:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uploadResponse := &StartGoogleUpload{
|
2024-03-11 13:31:15 +00:00
|
|
|
UploadID: res.Header.Get("x-guploader-uploadid"),
|
|
|
|
UploadURL: res.Header.Get("x-goog-upload-url"),
|
|
|
|
UploadStatus: res.Header.Get("x-goog-upload-status"),
|
2023-07-17 13:43:34 +00:00
|
|
|
ChunkGranularity: int64(chunkGranularity),
|
2024-03-11 13:31:15 +00:00
|
|
|
ControlURL: res.Header.Get("x-goog-upload-control-url"),
|
2023-07-17 13:43:34 +00:00
|
|
|
MimeType: mime,
|
|
|
|
|
|
|
|
EncryptedMediaBytes: encryptedImageBytes,
|
|
|
|
}
|
|
|
|
return uploadResponse, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) buildStartUploadPayload() (string, error) {
|
2023-07-17 23:57:20 +00:00
|
|
|
protoData := &gmproto.StartMediaUploadRequest{
|
|
|
|
AttachmentType: 1,
|
2023-07-17 13:51:31 +00:00
|
|
|
AuthData: &gmproto.AuthMessage{
|
2023-07-17 13:43:34 +00:00
|
|
|
RequestID: uuid.NewString(),
|
|
|
|
TachyonAuthToken: c.AuthData.TachyonAuthToken,
|
2024-03-11 13:31:15 +00:00
|
|
|
Network: c.AuthData.AuthNetwork(),
|
2023-07-17 13:43:34 +00:00
|
|
|
ConfigVersion: util.ConfigMessage,
|
|
|
|
},
|
|
|
|
Mobile: c.AuthData.Mobile,
|
|
|
|
}
|
|
|
|
|
|
|
|
protoDataBytes, err := proto.Marshal(protoData)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
protoDataEncoded := base64.StdEncoding.EncodeToString(protoDataBytes)
|
|
|
|
|
|
|
|
return protoDataEncoded, nil
|
|
|
|
}
|
2023-07-18 22:18:06 +00:00
|
|
|
|
|
|
|
func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
|
|
|
|
downloadMetadata := &gmproto.DownloadAttachmentRequest{
|
|
|
|
Info: &gmproto.AttachmentInfo{
|
|
|
|
AttachmentID: mediaID,
|
|
|
|
Encrypted: true,
|
|
|
|
},
|
|
|
|
AuthData: &gmproto.AuthMessage{
|
|
|
|
RequestID: uuid.NewString(),
|
|
|
|
TachyonAuthToken: c.AuthData.TachyonAuthToken,
|
2024-03-11 13:31:15 +00:00
|
|
|
Network: c.AuthData.AuthNetwork(),
|
2023-07-18 22:18:06 +00:00
|
|
|
ConfigVersion: util.ConfigMessage,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
downloadMetadataBytes, err := proto.Marshal(downloadMetadata)
|
|
|
|
if err != nil {
|
2024-03-11 13:31:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal download request: %w", err)
|
2023-07-18 22:18:06 +00:00
|
|
|
}
|
|
|
|
downloadMetadataEncoded := base64.StdEncoding.EncodeToString(downloadMetadataBytes)
|
2023-09-02 10:29:02 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, util.UploadMediaURL, nil)
|
2023-07-18 22:18:06 +00:00
|
|
|
if err != nil {
|
2024-03-11 13:31:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to prepare request: %w", err)
|
2023-07-18 22:18:06 +00:00
|
|
|
}
|
|
|
|
util.BuildUploadHeaders(req, downloadMetadataEncoded)
|
2024-03-11 13:31:15 +00:00
|
|
|
res, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
2023-07-18 22:18:06 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2024-03-11 13:31:15 +00:00
|
|
|
respData, err := io.ReadAll(res.Body)
|
2023-07-18 22:18:06 +00:00
|
|
|
if err != nil {
|
2024-03-11 13:31:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
2023-07-18 22:18:06 +00:00
|
|
|
}
|
|
|
|
cryptor, err := crypto.NewAESGCMHelper(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-11 13:31:15 +00:00
|
|
|
decryptedImageBytes, err := cryptor.DecryptData(respData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decrypt media: %w", err)
|
2023-07-18 22:18:06 +00:00
|
|
|
}
|
|
|
|
return decryptedImageBytes, nil
|
|
|
|
}
|