-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
directmedia_id.go
287 lines (246 loc) · 7.09 KB
/
directmedia_id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// mautrix-discord - A Matrix-Discord puppeting bridge.
// Copyright (C) 2024 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
)
const MediaIDPrefix = "\U0001F408DISCORD"
const MediaIDVersion = 1
type MediaIDClass uint8
const (
MediaIDClassAttachment MediaIDClass = 1
MediaIDClassEmoji MediaIDClass = 2
MediaIDClassSticker MediaIDClass = 3
MediaIDClassUserAvatar MediaIDClass = 4
MediaIDClassGuildMemberAvatar MediaIDClass = 5
)
type MediaIDData interface {
Write(to io.Writer)
Read(from io.Reader) error
Size() int
Wrap() *MediaID
}
type MediaID struct {
Version uint8
TypeClass MediaIDClass
Data MediaIDData
}
func ParseMediaID(id string, key [32]byte) (*MediaID, error) {
data, err := base64.RawURLEncoding.DecodeString(id)
if err != nil {
return nil, fmt.Errorf("failed to decode base64: %w", err)
}
hasher := hmac.New(sha256.New, key[:])
checksum := data[len(data)-TruncatedHashLength:]
data = data[:len(data)-TruncatedHashLength]
hasher.Write(data)
if !hmac.Equal(checksum, hasher.Sum(nil)[:TruncatedHashLength]) {
return nil, ErrMediaIDChecksumMismatch
}
mid := &MediaID{}
err = mid.Read(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("failed to parse media ID: %w", err)
}
return mid, nil
}
const TruncatedHashLength = 16
func (mid *MediaID) SignedString(key [32]byte) string {
buf := bytes.NewBuffer(make([]byte, 0, mid.Size()))
mid.Write(buf)
hasher := hmac.New(sha256.New, key[:])
hasher.Write(buf.Bytes())
buf.Write(hasher.Sum(nil)[:TruncatedHashLength])
return base64.RawURLEncoding.EncodeToString(buf.Bytes())
}
func (mid *MediaID) Write(to io.Writer) {
_, _ = to.Write([]byte(MediaIDPrefix))
_ = binary.Write(to, binary.BigEndian, mid.Version)
_ = binary.Write(to, binary.BigEndian, mid.TypeClass)
mid.Data.Write(to)
}
func (mid *MediaID) Size() int {
return len(MediaIDPrefix) + 2 + mid.Data.Size() + TruncatedHashLength
}
var (
ErrInvalidMediaID = errors.New("invalid media ID")
ErrMediaIDChecksumMismatch = errors.New("invalid checksum in media ID")
ErrUnsupportedMediaID = errors.New("unsupported media ID")
)
func (mid *MediaID) Read(from io.Reader) error {
prefix := make([]byte, len(MediaIDPrefix))
_, err := io.ReadFull(from, prefix)
if err != nil || !bytes.Equal(prefix, []byte(MediaIDPrefix)) {
return fmt.Errorf("%w: prefix not found", ErrInvalidMediaID)
}
versionAndClass := make([]byte, 2)
_, err = io.ReadFull(from, versionAndClass)
if err != nil {
return fmt.Errorf("%w: version and class not found", ErrInvalidMediaID)
} else if versionAndClass[0] != MediaIDVersion {
return fmt.Errorf("%w: unknown version %d", ErrUnsupportedMediaID, versionAndClass[0])
}
switch MediaIDClass(versionAndClass[1]) {
case MediaIDClassAttachment:
mid.Data = &AttachmentMediaData{}
case MediaIDClassEmoji:
mid.Data = &EmojiMediaData{}
case MediaIDClassSticker:
mid.Data = &StickerMediaData{}
case MediaIDClassUserAvatar:
mid.Data = &UserAvatarMediaData{}
case MediaIDClassGuildMemberAvatar:
mid.Data = &GuildMemberAvatarMediaData{}
default:
return fmt.Errorf("%w: unrecognized type class %d", ErrUnsupportedMediaID, versionAndClass[1])
}
err = mid.Data.Read(from)
if err != nil {
return fmt.Errorf("failed to parse media ID data: %w", err)
}
return nil
}
type AttachmentMediaData struct {
ChannelID uint64
MessageID uint64
AttachmentID uint64
}
func (amd *AttachmentMediaData) Write(to io.Writer) {
_ = binary.Write(to, binary.BigEndian, amd)
}
func (amd *AttachmentMediaData) Read(from io.Reader) (err error) {
return binary.Read(from, binary.BigEndian, amd)
}
func (amd *AttachmentMediaData) Size() int {
return binary.Size(amd)
}
func (amd *AttachmentMediaData) Wrap() *MediaID {
return &MediaID{
Version: MediaIDVersion,
TypeClass: MediaIDClassAttachment,
Data: amd,
}
}
func (amd *AttachmentMediaData) CacheKey() AttachmentCacheKey {
return AttachmentCacheKey{
ChannelID: amd.ChannelID,
AttachmentID: amd.AttachmentID,
}
}
type StickerMediaData struct {
StickerID uint64
Format uint8
}
func (smd *StickerMediaData) Write(to io.Writer) {
_ = binary.Write(to, binary.BigEndian, smd)
}
func (smd *StickerMediaData) Read(from io.Reader) error {
return binary.Read(from, binary.BigEndian, smd)
}
func (smd *StickerMediaData) Size() int {
return binary.Size(smd)
}
func (smd *StickerMediaData) Wrap() *MediaID {
return &MediaID{
Version: MediaIDVersion,
TypeClass: MediaIDClassSticker,
Data: smd,
}
}
type EmojiMediaDataInner struct {
EmojiID uint64
Animated bool
}
type EmojiMediaData struct {
EmojiMediaDataInner
Name string
}
func (emd *EmojiMediaData) Write(to io.Writer) {
_ = binary.Write(to, binary.BigEndian, &emd.EmojiMediaDataInner)
_, _ = to.Write([]byte(emd.Name))
}
func (emd *EmojiMediaData) Read(from io.Reader) (err error) {
err = binary.Read(from, binary.BigEndian, &emd.EmojiMediaDataInner)
if err != nil {
return
}
name, err := io.ReadAll(from)
if err != nil {
return
}
emd.Name = string(name)
return
}
func (emd *EmojiMediaData) Size() int {
return binary.Size(&emd.EmojiMediaDataInner) + len(emd.Name)
}
func (emd *EmojiMediaData) Wrap() *MediaID {
return &MediaID{
Version: MediaIDVersion,
TypeClass: MediaIDClassEmoji,
Data: emd,
}
}
type UserAvatarMediaData struct {
UserID uint64
Animated bool
AvatarID [16]byte
}
func (uamd *UserAvatarMediaData) Write(to io.Writer) {
_ = binary.Write(to, binary.BigEndian, uamd)
}
func (uamd *UserAvatarMediaData) Read(from io.Reader) error {
return binary.Read(from, binary.BigEndian, uamd)
}
func (uamd *UserAvatarMediaData) Size() int {
return binary.Size(uamd)
}
func (uamd *UserAvatarMediaData) Wrap() *MediaID {
return &MediaID{
Version: MediaIDVersion,
TypeClass: MediaIDClassUserAvatar,
Data: uamd,
}
}
type GuildMemberAvatarMediaData struct {
GuildID uint64
UserID uint64
Animated bool
AvatarID [16]byte
}
func (guamd *GuildMemberAvatarMediaData) Write(to io.Writer) {
_ = binary.Write(to, binary.BigEndian, guamd)
}
func (guamd *GuildMemberAvatarMediaData) Read(from io.Reader) error {
return binary.Read(from, binary.BigEndian, guamd)
}
func (guamd *GuildMemberAvatarMediaData) Size() int {
return binary.Size(guamd)
}
func (guamd *GuildMemberAvatarMediaData) Wrap() *MediaID {
return &MediaID{
Version: MediaIDVersion,
TypeClass: MediaIDClassGuildMemberAvatar,
Data: guamd,
}
}