-
Notifications
You must be signed in to change notification settings - Fork 22
/
streaming.go
228 lines (184 loc) · 6.67 KB
/
streaming.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
package shuffle
import (
"log"
"strings"
"net/http"
"io/ioutil"
"fmt"
"time"
)
func HandleStreamWorkflowUpdate(resp http.ResponseWriter, request *http.Request) {
cors := HandleCors(resp, request)
if cors {
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}`))
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 workflow.Public {
//log.Printf("[AUDIT] Letting user %s access workflow %s for streaming because it's public (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}`))
return
}
}
body, err := ioutil.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}`))
return
}
/*
streamKey := fmt.Sprintf("%s_stream_users", workflow.ID)
cache, err = GetCache(ctx, streamKey, user.Id, 30)
if err != nil {
log.Printf("[WARNING] Failed setting cache for apikey: %s", err)
} else {
// We are here to get the users in the stream
cacheData := []byte(cache.([]uint8))
}
*/
// FIXME: Should append to the stream and keep some items in memory
// Not just purely overwrite it
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
}
//// 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): %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}`))
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)
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 == "admin" || user.Role == "org-reader") {
log.Printf("[AUDIT] User %s is accessing workflow %s as admin (stream edit workflow)", 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}`))
return
}
}
// FIXME: If public, it should ONLY allow you to set certain actions
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)
sessionKey := fmt.Sprintf("%s_stream", workflow.ID)
previousCache := []byte{}
for {
cache, err := GetCache(ctx, sessionKey)
if err == nil {
cacheData := []byte(cache.([]uint8))
if string(previousCache) == string(cacheData) {
//log.Printf("[DEBUG] Still same cache for %s", user.Id)
} else {
// A way to only check for data from other people
if (len(user.Id) > 0 && !strings.Contains(string(cacheData), user.Id)) || len(user.Id) == 0 {
//log.Printf("[DEBUG] NEW cache for %s (1) - sending: %s.", user.Id, cacheData)
//fw.Write(cacheData)
//w.Write(cacheData)
_, err := fmt.Fprintf(resp, string(cacheData))
if err != nil {
log.Printf("[ERROR] Failed in writing stream to user '%s' (%s): %s", user.Username, user.Id, err)
if strings.Contains(err.Error(), "broken pipe") {
break
}
} else {
previousCache = cacheData
conn.Flush()
}
} else {
//log.Printf("[ERROR] NEW cache for %s (2) - NOT sending: %s.", user.Id, cacheData)
previousCache = cacheData
}
}
} else {
//log.Printf("[DEBUG] Failed getting cache for %s: %s", user.Id, err)
}
// FIXME: This is a hack to make sure we don't fully utilize the thread
time.Sleep(100 * time.Millisecond)
}
}