-
Notifications
You must be signed in to change notification settings - Fork 26
/
message.go
288 lines (267 loc) · 7.17 KB
/
message.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
288
package bot
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/ed25519"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/json"
"io"
"strings"
"golang.org/x/crypto/curve25519"
)
type LiveMessagePayload struct {
Width int `json:"width"`
Height int `json:"height"`
ThumbUrl string `json:"thumb_url"`
Url string `json:"url"`
}
type ImageMessagePayload struct {
AttachmentId string `json:"attachment_id"`
Width int `json:"width"`
Height int `json:"height"`
MimeType string `json:"mime_type"`
Thumbnail string `json:"thumbnail"`
Size int64 `json:"size"`
}
type RecallMessagePayload struct {
MessageId string `json:"message_id"`
}
type MessageRequest struct {
ConversationId string `json:"conversation_id"`
RecipientId string `json:"recipient_id"`
MessageId string `json:"message_id"`
Category string `json:"category"`
Data string `json:"data"`
RepresentativeId string `json:"representative_id"`
QuoteMessageId string `json:"quote_message_id"`
}
type ReceiptAcknowledgementRequest struct {
MessageId string `json:"message_id"`
Status string `json:"status"`
}
func PostMessages(ctx context.Context, messages []*MessageRequest, clientId, sessionId, secret string) error {
msg, err := json.Marshal(messages)
if err != nil {
return err
}
accessToken, err := SignAuthenticationToken(clientId, sessionId, secret, "POST", "/messages", string(msg))
if err != nil {
return err
}
body, err := Request(ctx, "POST", "/messages", msg, accessToken)
if err != nil {
return err
}
var resp struct {
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
if resp.Error.Code > 0 {
return resp.Error
}
return nil
}
func PostMessage(ctx context.Context, conversationId, recipientId, messageId, category, data string, clientId, sessionId, secret string) error {
request := MessageRequest{
ConversationId: conversationId,
RecipientId: recipientId,
MessageId: messageId,
Category: category,
Data: data,
}
return PostMessages(ctx, []*MessageRequest{&request}, clientId, sessionId, secret)
}
func PostAcknowledgements(ctx context.Context, requests []*ReceiptAcknowledgementRequest, clientId, sessionId, secret string) error {
array, err := json.Marshal(requests)
if err != nil {
return err
}
path := "/acknowledgements"
accessToken, err := SignAuthenticationToken(clientId, sessionId, secret, "POST", path, string(array))
if err != nil {
return err
}
body, err := Request(ctx, "POST", path, array, accessToken)
if err != nil {
return err
}
var resp struct {
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
if resp.Error.Code > 0 {
return resp.Error
}
return nil
}
func UniqueConversationId(userId, recipientId string) string {
minId, maxId := userId, recipientId
if strings.Compare(userId, recipientId) > 0 {
maxId, minId = userId, recipientId
}
h := md5.New()
io.WriteString(h, minId)
io.WriteString(h, maxId)
sum := h.Sum(nil)
sum[6] = (sum[6] & 0x0f) | 0x30
sum[8] = (sum[8] & 0x3f) | 0x80
id, _ := UuidFromBytes(sum)
return id.String()
}
func Chunked(source []interface{}, size int) [][]interface{} {
var result [][]interface{}
index := 0
for index < len(source) {
end := index + size
if end >= len(source) {
end = len(source)
}
result = append(result, source[index:end])
index += size
}
return result
}
func EncryptMessageData(data string, sessions []*Session, privateKey string) (string, error) {
dataBytes, err := base64.RawURLEncoding.DecodeString(data)
if err != nil {
return "", err
}
key := make([]byte, 16)
_, err = rand.Read(key)
if err != nil {
return "", err
}
nonce := make([]byte, 12)
_, err = rand.Read(nonce)
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
ciphertext := aesgcm.Seal(nil, nonce, dataBytes, nil)
var sessionLen [2]byte
binary.LittleEndian.PutUint16(sessionLen[:], uint16(len(sessions)))
privateBytes, err := base64.RawURLEncoding.DecodeString(privateKey)
if err != nil {
return "", err
}
private := ed25519.PrivateKey(privateBytes)
pub, _ := PublicKeyToCurve25519(ed25519.PublicKey(private[32:]))
var sessionsBytes []byte
for _, s := range sessions {
clientPublic, err := base64.RawURLEncoding.DecodeString(s.PublicKey)
if err != nil {
return "", err
}
var priv [32]byte
PrivateKeyToCurve25519(&priv, private)
dst, err := curve25519.X25519(priv[:], clientPublic)
if err != nil {
return "", err
}
block, err := aes.NewCipher(dst)
if err != nil {
return "", err
}
padding := aes.BlockSize - len(key)%aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
shared := make([]byte, len(key))
copy(shared[:], key[:])
shared = append(shared, padtext...)
ciphertext := make([]byte, aes.BlockSize+len(shared))
iv := ciphertext[:aes.BlockSize]
_, err = rand.Read(iv)
if err != nil {
return "", err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[aes.BlockSize:], shared)
id, err := UuidFromString(s.SessionID)
if err != nil {
return "", err
}
sessionsBytes = append(sessionsBytes, id.Bytes()...)
sessionsBytes = append(sessionsBytes, ciphertext...)
}
result := []byte{1}
result = append(result, sessionLen[:]...)
result = append(result, pub[:]...)
result = append(result, sessionsBytes...)
result = append(result, nonce[:]...)
result = append(result, ciphertext...)
return base64.RawURLEncoding.EncodeToString(result), nil
}
func DecryptMessageData(data string, sessionId, private string) (string, error) {
bytes, err := base64.RawURLEncoding.DecodeString(data)
if err != nil {
return "", err
}
size := 16 + 48 // session id bytes + encypted key bytes size
total := len(bytes)
if total < 1+2+32+size+12 {
return "", nil
}
sessionLen := int(binary.LittleEndian.Uint16(bytes[1:3]))
prefixSize := 35 + sessionLen*size
var key []byte
for i := 35; i < prefixSize; i += size {
if uid, _ := UuidFromBytes(bytes[i : i+16]); uid.String() == sessionId {
private, err := base64.RawURLEncoding.DecodeString(private)
if err != nil {
return "", err
}
var priv [32]byte
var pub []byte
copy(pub[:], bytes[3:35])
PrivateKeyToCurve25519(&priv, ed25519.PrivateKey(private))
dst, err := curve25519.X25519(priv[:], pub)
if err != nil {
return "", err
}
block, err := aes.NewCipher(dst[:])
if err != nil {
return "", err
}
iv := bytes[i+16 : i+16+aes.BlockSize]
key = bytes[i+16+aes.BlockSize : i+size]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(key, key)
key = key[:16]
break
}
}
if len(key) != 16 {
return "", nil
}
nonce := bytes[prefixSize : prefixSize+12]
block, err := aes.NewCipher(key)
if err != nil {
return "", nil // TODO
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return "", nil // TODO
}
plaintext, err := aesgcm.Open(nil, nonce, bytes[prefixSize+12:], nil)
if err != nil {
return "", nil // TODO
}
return base64.RawURLEncoding.EncodeToString(plaintext), nil
}