/
webapp.go
499 lines (461 loc) · 11.9 KB
/
webapp.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package core
import (
"crypto/hmac"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/panjf2000/ants/v2"
log "github.com/sirupsen/logrus"
tele "gopkg.in/telebot.v3"
)
func InitWebAppServer() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
u, err := url.Parse(msbconf.WebappUrl)
if err != nil {
log.Error("Failed parsing WebApp URL! Consider disable --webapp ?")
log.Fatalln(err.Error())
}
p := u.Path
webappApi := r.Group(path.Join(p, "api"))
{
//Group: /webapp/api
webappApi.POST("/initData", apiInitData)
webappApi.GET("/ss", apiSS)
webappApi.POST("/edit/result", apiEditResult)
webappApi.POST("/edit/move", apiEditMove)
webappApi.GET("/export", apiExport)
}
go func() {
err := r.Run(msbconf.WebappListenAddr)
if err != nil {
log.Fatalln("WebApp: Gin Run failed! Check your addr or disable webapp.\n", err)
}
log.Infoln("WebApp: Listening on ", msbconf.WebappListenAddr)
}()
}
func apiExport(c *gin.Context) {
sn := c.Query("sn")
qid := c.Query("qid")
hex := c.Query("hex")
url := fmt.Sprintf("msb://app/export/%s/?qid=%s&hex=%s", sn, qid, hex)
c.Redirect(http.StatusFound, url)
}
type webappStickerSet struct {
//Sticker objects
SS []webappStickerObject `json:"ss"`
//StickerSet Name
SSName string `json:"ssname"`
//StickerSet Title
SSTitle string `json:"sstitle"`
//StickerSet PNG Thumbnail
SSThumb string `json:"ssthumb"`
//Is Animated WebP
Animated bool `json:"animated"`
Amount int `json:"amount"`
//Indicates that all sticker files are ready
Ready bool `json:"ready"`
}
type webappStickerObject struct {
//Sticker index with offset of +1
Id int `json:"id"`
//Sticker emojis.
Emoji string `json:"emoji"`
//Sticker emoji changed on front-end.
EmojiChanged bool `json:"emoji_changed"`
//Sticker file path on server.
FilePath string `json:"file_path"`
//Sticker file ID
FileID string `json:"file_id"`
//Sticker unique ID
UniqueID string `json:"unique_id"`
//URL of sticker image.
Surl string `json:"surl"`
}
// GET <- ?uid&qid&sn&cmd
// -------------------------------------------
// -> [webappStickerObject, ...]
// -------------------------------------------
// id starts from 1 !!!!
// surl might be 404 when preparing stickers.
func apiSS(c *gin.Context) {
cmd := c.Query("cmd")
sn := c.Query("sn")
uid := c.Query("uid")
qid := c.Query("qid")
hex := c.Query("hex")
var ss *tele.StickerSet
var err error
switch cmd {
case "edit":
ud, err := checkGetUd(uid, qid)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
// Refresh SS data since it might already changed.
retrieveSSDetails(ud.lastContext, ud.stickerData.id, ud.stickerData)
ss = ud.stickerData.stickerSet
case "export":
if sn == "" || qid == "" {
c.String(http.StatusBadRequest, "no_sn_or_qid")
return
}
ss, err = b.StickerSet(sn)
if err != nil {
c.String(http.StatusBadRequest, "bad_sn")
return
}
default:
c.String(http.StatusBadRequest, "no_cmd")
return
}
wss := webappStickerSet{
SSTitle: ss.Title,
SSName: ss.Name,
Animated: ss.Video,
}
sl := []webappStickerObject{}
ready := true
for i, s := range ss.Stickers {
var surl string
var fpath string
if s.Video {
fpath = filepath.Join(msbconf.WebappDataDir, hex, s.SetName, s.UniqueID+".webm")
} else {
fpath = filepath.Join(msbconf.WebappDataDir, hex, s.SetName, s.UniqueID+".webp")
}
surl, _ = url.JoinPath(msbconf.WebappUrl, "data", hex, s.SetName, s.UniqueID+".webp")
sl = append(sl, webappStickerObject{
Id: i + 1,
Emoji: s.Emoji,
Surl: surl,
UniqueID: s.UniqueID,
FileID: s.FileID,
FilePath: fpath,
})
if i == 0 {
wss.SSThumb, _ = url.JoinPath(msbconf.WebappUrl, "data", hex, s.SetName, s.UniqueID+".png")
}
if st, _ := os.Stat(fpath); st == nil {
ready = false
}
}
wss.SS = sl
wss.Ready = ready
jsonWSS, err := json.Marshal(wss)
if err != nil {
log.Errorln("json marshal jsonWSS in apiSS error!")
c.String(http.StatusInternalServerError, "json marshal jsonWSS in apiSS error!")
return
}
c.String(http.StatusOK, string(jsonWSS))
}
// <- ?qid&qid&sha256sum [{"index", "emoji", "surl"}, ...]
// -------------------------------------------
// -> STATUS
func apiEditResult(c *gin.Context) {
uid := c.Query("uid")
qid := c.Query("qid")
body, _ := io.ReadAll(c.Request.Body)
// if !validateSHA256(body, sum) {
// c.String(http.StatusBadRequest, "bad result csum!")
// return
// }
if string(body) == "" {
//user did nothing
return
}
so := []webappStickerObject{}
err := json.Unmarshal(body, &so)
if err != nil {
c.String(http.StatusBadRequest, "bad_json")
return
}
ud, err := checkGetUd(uid, qid)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
if ud.state == ST_PROCESSING {
c.String(http.StatusOK, "already processing...")
return
}
log.Debugln(so)
c.String(http.StatusOK, "")
ud.udSetState(ST_PROCESSING)
go func() {
err := commitEmojiChange(ud, so)
if err != nil {
sendFatalError(err, ud.lastContext)
endManageSession(ud.lastContext)
endSession(ud.lastContext)
}
}()
}
func commitEmojiChange(ud *UserData, so []webappStickerObject) error {
waitTime := 0
for ud.webAppWorkerPool.Waiting() > 0 {
time.Sleep(500 * time.Millisecond)
waitTime++
if waitTime > 20 {
break
}
}
ud.webAppWorkerPool.ReleaseTimeout(10 * time.Second)
// retrieveSSDetails(ud.lastContext, ud.stickerData.id, ud.stickerData)
//copy slice
ss := ud.stickerData.stickerSet.Stickers
notificationSent := false
emojiChanged := false
for _, s := range so {
if s.EmojiChanged {
emojiChanged = true
}
}
if !emojiChanged {
goto NEXT
}
for i, s := range ss {
if s.UniqueID != so[i].UniqueID {
log.Error("sticker order mismatch! index:", i)
return errors.New("sticker order mismatch, no emoji change committed")
}
if !so[i].EmojiChanged {
continue
}
oldEmoji := findEmojis(s.Emoji)
newEmoji := findEmojis(so[i].Emoji)
if newEmoji == "" || newEmoji == oldEmoji {
log.Warn("webapp: ignored one invalid emoji.")
continue
}
log.Debugln("Old:", i, s.Emoji, s.FileID)
log.Debugln("New", i, newEmoji)
if !notificationSent {
sendEditingEmoji(ud.lastContext)
notificationSent = true
}
err := editStickerEmoji(newEmoji, i, s.FileID, so[i].FilePath, len(ss), ud)
if err != nil {
return err
}
// Have a rest.
time.Sleep(2 * time.Second)
}
NEXT:
sendSEditOK(ud.lastContext)
sendSFromSS(ud.lastContext, ud.stickerData.id, nil)
endManageSession(ud.lastContext)
endSession(ud.lastContext)
return nil
}
// <- ?uid&qid POST_FORM:{"oldIndex", "newIndex"}
// -------------------------------------------
// -> STATUS
func apiEditMove(c *gin.Context) {
uid := c.Query("uid")
qid := c.Query("qid")
oldIndex, _ := strconv.Atoi(c.PostForm("oldIndex"))
newIndex, _ := strconv.Atoi(c.PostForm("newIndex"))
ud, err := checkGetUd(uid, qid)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
smo := &StickerMoveObject{
wg: sync.WaitGroup{},
sd: ud.stickerData,
oldIndex: oldIndex,
newIndex: newIndex,
}
smo.wg.Add(1)
ud.webAppWorkerPool.Invoke(smo)
smo.wg.Wait()
if smo.err != nil {
c.String(http.StatusInternalServerError, smo.err.Error())
return
}
}
func apiInitData(c *gin.Context) {
//We must verify the initData before using it
queryID := c.PostForm("query_id")
authDate := c.PostForm("auth_date")
user := c.PostForm("user")
hash := c.PostForm("hash")
dataCheckString := strings.Join([]string{
"auth_date=" + authDate,
"query_id=" + queryID,
"user=" + user}, "\n")
if !validateHMAC(dataCheckString, hash) {
log.Warning("WebApp DCS HMAC failed, corrupt or attack?")
c.String(http.StatusBadRequest, "data_check_string HMAC validation failed!!")
return
}
log.Debug("WebApp initData DCS HMAC OK.")
initWebAppRequest(c)
}
func initWebAppRequest(c *gin.Context) {
user := c.PostForm("user")
queryID := c.PostForm("query_id")
cmd := c.Query("cmd")
cmd = path.Base(cmd)
webAppUser := &WebAppUser{}
err := json.Unmarshal([]byte(user), webAppUser)
if err != nil {
log.Error("json unmarshal webappuser error.")
return
}
switch cmd {
case "edit":
ud, err := GetUd(strconv.Itoa(webAppUser.Id))
if err != nil {
c.String(http.StatusBadRequest, "bad_state")
return
}
ud.webAppWorkerPool, _ = ants.NewPoolWithFunc(1, wSubmitSMove)
ud.webAppQID = queryID
case "export":
sn := c.Query("sn")
hex := c.Query("hex")
if sn == "" || hex == "" {
c.String(http.StatusBadRequest, "no_sn_or_hex")
return
}
ss, err := b.StickerSet(sn)
if err != nil {
c.String(http.StatusBadRequest, "bad_sn")
return
}
// appendSStoQIDAuthList(sn, queryID)
prepareWebAppExportStickers(ss, hex)
default:
c.String(http.StatusBadRequest, "bad_or_no_cmd")
return
}
c.String(http.StatusOK, "webapp init ok")
}
// Telegram WebApp Regulation.
func validateHMAC(dataCheckString string, hash string) bool {
// This calculated secret will be used to "decrypt" DCS
h := hmac.New(sha256.New, []byte("WebAppData"))
h.Write([]byte(msbconf.BotToken))
secretByte := h.Sum(nil)
h = hmac.New(sha256.New, secretByte)
h.Write([]byte(dataCheckString))
dcsHash := fmt.Sprintf("%x", h.Sum(nil))
return hash == dcsHash
}
// func validateSHA256(dataToCheck []byte, hash string) bool {
// h := sha256.New()
// h.Write(dataToCheck)
// csum := fmt.Sprintf("%x", h.Sum(nil))
// return hash == csum
// }
func checkGetUd(uid string, qid string) (*UserData, error) {
ud, err := GetUd(uid)
if err != nil {
return nil, errors.New("no such user")
}
if ud.webAppQID != qid {
return nil, errors.New("qid not valid")
}
return ud, nil
}
func prepareWebAppEditStickers(ud *UserData) error {
dest := filepath.Join(msbconf.WebappDataDir, ud.stickerData.id)
os.RemoveAll(dest)
os.MkdirAll(dest, 0755)
for _, s := range ud.stickerData.stickerSet.Stickers {
var f string
if ud.stickerData.stickerSet.Video {
f = filepath.Join(dest, s.UniqueID+".webm")
} else {
f = filepath.Join(dest, s.UniqueID+".webp")
}
obj := &StickerDownloadObject{
dest: f,
sticker: s,
forWebApp: true,
}
obj.wg.Add(1)
ud.stickerData.sDnObjects = append(ud.stickerData.sDnObjects, obj)
go wpDownloadStickerSet.Invoke(obj)
}
return nil
}
func prepareWebAppExportStickers(ss *tele.StickerSet, hex string) error {
dest := filepath.Join(msbconf.WebappDataDir, hex, ss.Name)
// If the user is reusing the generated link to export.
// Do not re-download for every initData.
stat, _ := os.Stat(dest)
if stat != nil {
mtime := stat.ModTime().Unix()
// Less than 5 minutes, do not re-download
if time.Now().Unix()-mtime < 300 {
log.Debug("prepareWebAppExportStickers: dir still fresh, don't overwrite.")
return nil
}
}
os.RemoveAll(dest)
os.MkdirAll(dest, 0755)
for i, s := range ss.Stickers {
var f string
if ss.Video {
f = filepath.Join(dest, s.UniqueID+".webm")
} else {
f = filepath.Join(dest, s.UniqueID+".webp")
}
obj := &StickerDownloadObject{
dest: f,
sticker: s,
// forWebApp: true,
forWhatsApp: true,
}
//Use first image to create a thumbnail image
//for WhatsApp.
if i == 0 {
obj.forWhatsAppThumb = true
}
obj.wg.Add(1)
go wpDownloadStickerSet.Invoke(obj)
}
return nil
}
// func appendSStoQIDAuthList(sn string, qid string) {
// webAppSSAuthList.mu.Lock()
// defer webAppSSAuthList.mu.Unlock()
// obj := &WebAppQIDAuthObject{sn: sn, dt: time.Now().Unix()}
// webAppSSAuthList.sa[qid] = obj
// }
// func removeSSfromQIDAuthList(sn string, qid string) {
// webAppSSAuthList.mu.Lock()
// defer webAppSSAuthList.mu.Unlock()
// _, exist := webAppSSAuthList.sa[qid]
// if exist {
// delete(webAppSSAuthList.sa, qid)
// }
// }
// func veriyQIDfromAuthList(sn string, qid string) bool {
// sa := webAppSSAuthList.sa[qid]
// if sa == nil {
// return false
// }
// if sa.sn == sn {
// return true
// } else {
// return false
// }
// }