-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstreaming.go
More file actions
580 lines (501 loc) · 17.4 KB
/
Copy pathstreaming.go
File metadata and controls
580 lines (501 loc) · 17.4 KB
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package shuffle
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var streamPresenceColors = []string{
"#F24E1E", "#1ABCFE", "#0ACF83", "#FF7262", "#A259FF",
"#FFD700", "#FF3CAC", "#00CFFD", "#F5A623", "#6EE7B7",
"#818CF8", "#FB923C",
}
func presenceColor(userID string) string {
var hash int
for _, c := range userID {
hash = hash*31 + int(c)
}
if hash < 0 {
hash = -hash
}
return streamPresenceColors[hash%len(streamPresenceColors)]
}
// streamPresenceInterval: presence update every 100 poll iterations (~10s at 100ms/poll)
var streamPresenceInterval = 100
var streamPresenceTTL int32 = 5
var streamPresenceStaleMs int64 = 30000 // 30 seconds stale threshold
func HandleStreamWorkflowUpdate(resp http.ResponseWriter, request *http.Request) {
cors := HandleCors(resp, request)
if cors {
return
}
if project.Environment == "cloud" {
gceProject := os.Getenv("SHUFFLE_GCEPROJECT")
if gceProject != "shuffler" && gceProject != sandboxProject && len(gceProject) > 0 {
log.Printf("[DEBUG] Redirecting Stream Update request to main site handler (shuffler.io)")
RedirectUserRequest(resp, request)
return
}
}
//// Removed check here as it may be a public workflow
user, err := HandleApiAuthentication(resp, request)
if err != nil {
log.Printf("[AUDIT] Api authentication failed in getting specific workflow (stream update): %s. Continuing because it may be public.", err)
}
location := strings.Split(request.URL.String(), "/")
var fileId string
if location[1] == "api" {
if len(location) <= 4 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID missing from request path"}`))
return
}
fileId = location[4]
}
if strings.Contains(fileId, "?") {
fileId = strings.Split(fileId, "?")[0]
}
if len(fileId) != 36 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID when getting workflow is not valid"}`))
return
}
ctx := GetContext(request)
workflow, err := GetWorkflow(ctx, fileId)
if err != nil {
log.Printf("[WARNING] Workflow %s doesn't exist.", fileId)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Failed finding workflow."}`))
return
}
if user.Id != workflow.Owner || len(user.Id) == 0 {
if workflow.OrgId == user.ActiveOrg.Id && user.Role != "org-reader" {
log.Printf("[AUDIT] User %s is accessing workflow %s as admin (SET workflow stream)", user.Username, workflow.ID)
} else if project.Environment == "cloud" && user.Verified == true && user.SupportAccess == true && user.Role == "admin" {
log.Printf("[AUDIT] Letting verified support admin %s access workflow %s", user.Username, workflow.ID)
} else {
log.Printf("[AUDIT] Wrong user (%s) for workflow %s (SET workflow stream)", user.Username, workflow.ID)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "You do not have permission to update this workflow's stream"}`))
return
}
}
org, err := GetOrg(ctx, workflow.OrgId)
if err != nil || !org.SyncFeatures.Multiplayer.Active {
log.Printf("[AUDIT] Multiplayer not active for org %s (Workflow stream updates)", workflow.OrgId)
resp.WriteHeader(403)
resp.Write([]byte(`{"success": false, "reason": "Multiplayer collaboration is not enabled for this organization"}`))
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
log.Printf("[WARNING] Error with body read in workflow stream: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Failed to read request body"}`))
return
}
// Try to parse as a single operation and assign sequence + timestamp
var op StreamWorkflowOperation
if err := json.Unmarshal(body, &op); err == nil && len(op.Item) > 0 {
op.Timestamp = time.Now().UnixMilli()
if len(op.UserID) == 0 && len(user.Id) > 0 {
op.UserID = user.Id
}
if len(user.Username) > 0 {
op.Username = user.Username
}
sessionKey := fmt.Sprintf("%s_stream", workflow.ID)
var state StreamWorkflowState
cache, err := GetCache(ctx, sessionKey)
if err == nil {
cacheData, ok := cache.([]uint8)
if !ok {
log.Printf("[WARNING] Unexpected cache type for stream state %s", sessionKey)
} else if err := json.Unmarshal(cacheData, &state); err != nil {
log.Printf("[WARNING] Failed to unmarshal stream state for %s: %s", workflow.ID, err)
}
}
op.Sequence = state.LastSeq + 1
state.Operations = append(state.Operations, op)
state.LastSeq = op.Sequence
if len(state.Operations) > 100 {
state.Operations = state.Operations[len(state.Operations)-100:]
}
stateBytes, err := json.Marshal(state)
if err != nil {
log.Printf("[WARNING] Failed to marshal stream state: %s", err)
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed to save stream operation"}`))
return
}
err = SetCache(ctx, sessionKey, stateBytes, 120)
if err != nil {
log.Printf("[WARNING] Failed setting cache for stream: %s", err)
}
resp.WriteHeader(200)
resp.Write([]byte(fmt.Sprintf(`{"success": true, "sequence": %d}`, op.Sequence)))
return
}
// Fallback: batch of operations
var ops []StreamWorkflowOperation
if err := json.Unmarshal(body, &ops); err == nil && len(ops) > 0 {
sessionKey := fmt.Sprintf("%s_stream", workflow.ID)
var state StreamWorkflowState
cache, err := GetCache(ctx, sessionKey)
if err == nil {
cacheData, ok := cache.([]uint8)
if !ok {
log.Printf("[WARNING] Unexpected cache type for stream state %s", sessionKey)
} else if err := json.Unmarshal(cacheData, &state); err != nil {
log.Printf("[WARNING] Failed to unmarshal stream state for %s: %s", workflow.ID, err)
}
}
for i := range ops {
ops[i].Sequence = state.LastSeq + 1
state.LastSeq = ops[i].Sequence
ops[i].Timestamp = time.Now().UnixMilli()
if len(ops[i].UserID) == 0 && len(user.Id) > 0 {
ops[i].UserID = user.Id
}
state.Operations = append(state.Operations, ops[i])
}
if len(state.Operations) > 100 {
state.Operations = state.Operations[len(state.Operations)-100:]
}
stateBytes, err := json.Marshal(state)
if err != nil {
log.Printf("[WARNING] Failed to marshal stream state: %s", err)
resp.WriteHeader(500)
resp.Write([]byte(`{"success": false, "reason": "Failed to save stream operations"}`))
return
}
err = SetCache(ctx, sessionKey, stateBytes, 120)
if err != nil {
log.Printf("[WARNING] Failed setting cache for stream: %s", err)
}
resp.WriteHeader(200)
resp.Write([]byte(fmt.Sprintf(`{"success": true, "sequence": %d, "count": %d}`, state.LastSeq, len(ops))))
return
}
// Legacy fallback: raw body overwrite (backwards compat for old clients)
sessionKey := fmt.Sprintf("%s_stream", workflow.ID)
err = SetCache(ctx, sessionKey, body, 30)
if err != nil {
log.Printf("[WARNING] Failed setting cache for apikey: %s", err)
}
resp.WriteHeader(200)
resp.Write([]byte(`{"success": true}`))
}
func HandleStreamWorkflow(resp http.ResponseWriter, request *http.Request) {
cors := HandleCors(resp, request)
if cors {
return
}
if project.Environment == "cloud" {
gceProject := os.Getenv("SHUFFLE_GCEPROJECT")
if gceProject != "shuffler" && gceProject != sandboxProject && len(gceProject) > 0 {
log.Printf("[DEBUG] Redirecting Stream Start request to main site handler (shuffler.io)")
RedirectUserRequest(resp, request)
return
}
}
user, err := HandleApiAuthentication(resp, request)
if err != nil {
log.Printf("[AUDIT] Api authentication failed in getting specific workflow (stream): %s. Continuing because it may be public.", err)
}
location := strings.Split(request.URL.String(), "/")
var fileId string
if location[1] == "api" {
if len(location) <= 4 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID missing from request path"}`))
return
}
fileId = location[4]
}
if strings.Contains(fileId, "?") {
fileId = strings.Split(fileId, "?")[0]
}
if len(fileId) != 36 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID when getting workflow is not valid"}`))
return
}
ctx := GetContext(request)
workflow, err := GetWorkflow(ctx, fileId)
if err != nil {
log.Printf("[WARNING] Workflow %s doesn't exist.", fileId)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Failed finding workflow."}`))
return
}
if user.Id != workflow.Owner || len(user.Id) == 0 {
if workflow.OrgId == user.ActiveOrg.Id && user.Role != "" {
log.Printf("[AUDIT] User %s is accessing workflow %s as org member (get workflow stream)", user.Username, workflow.ID)
} else if workflow.Public {
log.Printf("[AUDIT] Letting user %s access workflow %s for streaming because it's public (get workflow stream)", user.Username, workflow.ID)
} else if project.Environment == "cloud" && user.Verified == true && user.Active == true && user.SupportAccess == true && strings.HasSuffix(user.Username, "@shuffler.io") {
log.Printf("[AUDIT] Letting verified support admin %s access workflow %s", user.Username, workflow.ID)
} else {
log.Printf("[AUDIT] Wrong user (%s) for workflow %s (get workflow stream)", user.Username, workflow.ID)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "You do not have permission to access this workflow's stream"}`))
return
}
}
org, err := GetOrg(ctx, workflow.OrgId)
if err != nil || !org.SyncFeatures.Multiplayer.Active {
log.Printf("[AUDIT] Multiplayer not active for org %s (get workflow stream)", workflow.OrgId)
resp.WriteHeader(403)
resp.Write([]byte(`{"success": false, "reason": "Multiplayer collaboration is not enabled for this organization"}`))
return
}
resp.Header().Set("Connection", "Keep-Alive")
resp.Header().Set("X-Content-Type-Options", "nosniff")
conn, ok := resp.(http.Flusher)
if !ok {
log.Printf("[ERROR] Flusher error: %t", ok)
http.Error(resp, "Streaming supported on AppEngine", http.StatusInternalServerError)
return
}
resp.Header().Set("Content-Type", "text/event-stream")
resp.WriteHeader(http.StatusOK)
sinceStr := request.URL.Query().Get("since")
var sinceSeq int64
if len(sinceStr) > 0 {
sinceSeq, _ = strconv.ParseInt(sinceStr, 10, 64)
}
sessionKey := fmt.Sprintf("%s_stream", workflow.ID)
presenceKey := fmt.Sprintf("%s_presence", workflow.ID)
var lastSentSeq int64 = sinceSeq
var pollCount int
// On initial connect (since=0), flush the delta ops since the last save so late joiners
// catch up to unsaved changes made by other users before they arrived.
if sinceSeq == 0 {
cache, err := GetCache(ctx, sessionKey)
if err == nil {
cacheData, ok := cache.([]uint8)
if ok {
var state StreamWorkflowState
if err := json.Unmarshal(cacheData, &state); err == nil && len(state.Operations) > 0 {
// Find the sequence of the last save op — that's the catch-up baseline
var lastSaveSeq int64
for _, op := range state.Operations {
if op.Item == "workflow" && op.Type == "save" {
lastSaveSeq = op.Sequence
}
}
for _, op := range state.Operations {
if op.Sequence <= lastSaveSeq {
continue
}
if op.Type == "select" || op.Type == "unselect" || op.Type == "hover" || op.Type == "enter" {
continue
}
opBytes, err := json.Marshal(op)
if err != nil {
continue
}
fmt.Fprintf(resp, "%s\n", string(opBytes))
lastSentSeq = op.Sequence
}
}
}
}
fmt.Fprintf(resp, "%s\n", `{"item":"system","type":"init_complete"}`)
conn.Flush()
}
for {
pollCount++
if pollCount%streamPresenceInterval == 1 {
var presence StreamPresenceState
presenceCache, err := GetCache(ctx, presenceKey)
if err == nil {
presenceData, ok := presenceCache.([]uint8)
if !ok {
log.Printf("[WARNING] Unexpected cache type for presence %s", presenceKey)
} else if err := json.Unmarshal(presenceData, &presence); err != nil {
log.Printf("[WARNING] Failed to unmarshal presence for %s: %s", workflow.ID, err)
}
}
now := time.Now().UnixMilli()
updated := false
activeUsers := []StreamPresenceEntry{}
for _, entry := range presence.Users {
if now-entry.LastSeen > streamPresenceStaleMs {
continue
}
if entry.UserID == user.Id {
entry.LastSeen = now
if len(user.Username) > 0 {
entry.Username = user.Username
}
updated = true
}
activeUsers = append(activeUsers, entry)
}
if !updated && len(user.Id) > 0 {
activeUsers = append(activeUsers, StreamPresenceEntry{
UserID: user.Id,
Username: user.Username,
LastSeen: now,
Color: presenceColor(user.Id),
})
}
presence.Users = activeUsers
presenceBytes, _ := json.Marshal(presence)
if err := SetCache(ctx, presenceKey, presenceBytes, streamPresenceTTL); err != nil {
log.Printf("[WARNING] Failed setting presence cache for %s: %s", workflow.ID, err)
}
// Send presence to client
type presenceOp struct {
Item string `json:"item"`
Users []StreamPresenceEntry `json:"users"`
}
presenceOpBytes, _ := json.Marshal(presenceOp{Item: "presence", Users: presence.Users})
_, err = fmt.Fprintf(resp, "%s\n", string(presenceOpBytes))
if err != nil {
if strings.Contains(err.Error(), "broken pipe") {
return
}
}
conn.Flush()
}
cache, err := GetCache(ctx, sessionKey)
if err == nil {
cacheData, ok := cache.([]uint8)
if !ok {
log.Printf("[WARNING] Unexpected cache type for stream state %s", sessionKey)
} else {
var state StreamWorkflowState
if err := json.Unmarshal(cacheData, &state); err == nil {
for _, op := range state.Operations {
if op.Sequence <= lastSentSeq {
continue
}
// Skip ops from this user (they already applied them locally)
if len(user.Id) > 0 && op.UserID == user.Id {
lastSentSeq = op.Sequence
continue
}
opBytes, err := json.Marshal(op)
if err != nil {
continue
}
_, err = fmt.Fprintf(resp, "%s\n", string(opBytes))
if err != nil {
if strings.Contains(err.Error(), "broken pipe") {
return
}
}
lastSentSeq = op.Sequence
conn.Flush()
}
} else {
// Legacy format: raw body (backwards compat)
if lastSentSeq == 0 {
if (len(user.Id) > 0 && !strings.Contains(string(cacheData), user.Id)) || len(user.Id) == 0 {
_, err := fmt.Fprintf(resp, "%s", string(cacheData))
if err != nil {
if strings.Contains(err.Error(), "broken pipe") {
return
}
} else {
conn.Flush()
}
}
lastSentSeq = 1
}
}
}
}
time.Sleep(100 * time.Millisecond)
}
}
func HandleStreamWorkflowHistory(resp http.ResponseWriter, request *http.Request) {
cors := HandleCors(resp, request)
if cors {
return
}
if project.Environment == "cloud" {
gceProject := os.Getenv("SHUFFLE_GCEPROJECT")
if gceProject != "shuffler" && gceProject != sandboxProject && len(gceProject) > 0 {
log.Printf("[DEBUG] Redirecting Stream History request to main site handler (shuffler.io)")
RedirectUserRequest(resp, request)
return
}
}
user, err := HandleApiAuthentication(resp, request)
if err != nil {
log.Printf("[AUDIT] Api authentication failed in getting workflow stream history: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Authentication required"}`))
return
}
location := strings.Split(request.URL.String(), "/")
var fileId string
if location[1] == "api" {
if len(location) <= 4 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID missing from request path"}`))
return
}
fileId = location[4]
}
if strings.Contains(fileId, "?") {
fileId = strings.Split(fileId, "?")[0]
}
if len(fileId) != 36 {
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Workflow ID is not valid"}`))
return
}
ctx := GetContext(request)
workflow, err := GetWorkflow(ctx, fileId)
if err != nil {
log.Printf("[WARNING] Workflow %s doesn't exist.", fileId)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "Failed finding workflow."}`))
return
}
if user.Id != workflow.Owner {
if workflow.OrgId == user.ActiveOrg.Id && user.Role != "org-reader" {
// org member — allowed
} else if project.Environment == "cloud" && user.Verified && user.Active && user.SupportAccess && strings.HasSuffix(user.Username, "@shuffler.io") {
// support admin — allowed
} else {
log.Printf("[AUDIT] Wrong user (%s) for workflow %s (stream history)", user.Username, workflow.ID)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "You do not have permission to view this workflow's stream history"}`))
return
}
}
org, err := GetOrg(ctx, workflow.OrgId)
if err != nil || !org.SyncFeatures.Multiplayer.Active {
log.Printf("[AUDIT] Multiplayer not active for org %s (stream history)", workflow.OrgId)
resp.WriteHeader(403)
resp.Write([]byte(`{"success": false, "reason": "Multiplayer collaboration is not enabled for this organization"}`))
return
}
sessionKey := fmt.Sprintf("%s_stream", workflow.ID)
var state StreamWorkflowState
cache, err := GetCache(ctx, sessionKey)
if err == nil {
cacheData, ok := cache.([]uint8)
if ok {
json.Unmarshal(cacheData, &state)
}
}
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(200)
result, _ := json.Marshal(map[string]interface{}{
"success": true,
"operations": state.Operations,
})
resp.Write(result)
}