/
info_handler.go
242 lines (196 loc) · 5.33 KB
/
info_handler.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
package main
import (
"github.com/Cretezy/dSock/common"
"github.com/gin-gonic/gin"
"sync"
"time"
)
func formatConnection(id string, connection map[string]string) gin.H {
// Can safely ignore, will become 0
lastPingTime, _ := time.Parse(time.RFC3339, connection["lastPing"])
connectionMap := gin.H{
"id": id,
"worker": connection["workerId"],
"lastPing": lastPingTime.Unix(),
"user": connection["user"],
}
if connection["session"] != "" {
connectionMap["session"] = connection["session"]
}
return connectionMap
}
func formatClaim(id string, claim map[string]string) gin.H {
// Can safely ignore, invalid times are already filtered out in infoHandler
expirationTime, _ := time.Parse(time.RFC3339, claim["expiration"])
claimMap := gin.H{
"id": id,
"expiration": expirationTime.Unix(),
"user": claim["user"],
}
if claim["session"] != "" {
claimMap["session"] = claim["session"]
}
return claimMap
}
func infoHandler(c *gin.Context) {
connId := c.Query("id")
user := c.Query("user")
session := c.Query("session")
var claimIds []string
if session != "" {
userSessionClaims := redisClient.SMembers("claim-user-session:" + user + "-" + session)
if userSessionClaims.Err() != nil {
c.AbortWithStatusJSON(500, map[string]interface{}{
"success": false,
"error": "Error getting claim",
"errorCode": common.ErrorGettingClaim,
})
return
}
claimIds = userSessionClaims.Val()
} else {
userClaims := redisClient.SMembers("claim-user:" + user)
if userClaims.Err() != nil {
c.AbortWithStatusJSON(500, map[string]interface{}{
"success": false,
"error": "Error getting claim",
"errorCode": common.ErrorGettingClaim,
})
return
}
claimIds = userClaims.Val()
}
var claimsWaitGroup sync.WaitGroup
claimsWaitGroup.Add(len(claimIds))
var claimsLock sync.Mutex
claims := make([]gin.H, 0)
stop := false
for _, claimId := range claimIds {
claimId := claimId
go func() {
defer claimsWaitGroup.Done()
claim := redisClient.HGetAll("claim:" + claimId)
// Stop if one of the Redis commands failed
if stop {
return
}
if claim.Err() != nil {
stop = true
c.AbortWithStatusJSON(500, map[string]interface{}{
"success": false,
"error": "Error getting claim",
"errorCode": common.ErrorGettingClaim,
})
return
}
if len(claim.Val()) == 0 {
// Connection doesn't exist
return
}
expirationTime, _ := time.Parse(time.RFC3339, claim.Val()["expiration"])
if expirationTime.Before(time.Now()) {
// Ignore invalid times (would become 0) or expired claims
return
}
claimsLock.Lock()
claims = append(claims, formatClaim(claimId, claim.Val()))
claimsLock.Unlock()
}()
}
claimsWaitGroup.Wait()
if connId != "" {
connection := redisClient.HGetAll("conn:" + connId)
if connection.Err() != nil {
c.AbortWithStatusJSON(500, map[string]interface{}{
"success": false,
"error": "Error getting connection",
"errorCode": common.ErrorGettingConnection,
})
return
}
if len(connection.Val()) == 0 {
// Connection doesn't exist
c.AbortWithStatusJSON(200, map[string]interface{}{
"success": true,
"connections": []interface{}{},
"claims": claims,
})
return
}
c.AbortWithStatusJSON(200, map[string]interface{}{
"success": true,
"connections": []gin.H{formatConnection(connId, connection.Val())},
"claims": claims,
})
return
} else if user != "" {
user := redisClient.SMembers("user:" + user)
if user.Err() != nil {
c.AbortWithStatusJSON(500, map[string]interface{}{
"success": false,
"error": "Error getting user",
"errorCode": common.ErrorGettingUser,
})
return
}
if len(user.Val()) == 0 {
// User doesn't exist
c.AbortWithStatusJSON(200, map[string]interface{}{
"success": true,
"connections": []interface{}{},
"claims": claims,
})
return
}
var connectionsWaitGroup sync.WaitGroup
connectionsWaitGroup.Add(len(user.Val()))
var connectionsLock sync.Mutex
connections := make([]gin.H, 0)
stop := false
for _, connId := range user.Val() {
connId := connId
go func() {
defer connectionsWaitGroup.Done()
connection := redisClient.HGetAll("conn:" + connId)
// Stop if one of the Redis commands failed
if stop {
return
}
if connection.Err() != nil {
stop = true
c.AbortWithStatusJSON(500, map[string]interface{}{
"success": false,
"error": "Error getting connection",
"errorCode": common.ErrorGettingConnection,
})
return
}
if len(connection.Val()) == 0 {
// Connection doesn't exist
return
}
// Target specific session(s) for user
if session != "" && connection.Val()["session"] != session {
return
}
connectionsLock.Lock()
connections = append(connections, formatConnection(connId, connection.Val()))
connectionsLock.Unlock()
}()
}
connectionsWaitGroup.Wait()
c.AbortWithStatusJSON(200, map[string]interface{}{
"success": true,
"connections": connections,
"claims": claims,
})
return
} else {
c.AbortWithStatusJSON(404, map[string]interface{}{
"success": false,
"error": "Connection ID or user ID is missing",
"errorCode": common.ErrorMissingConnectionOrUser,
})
return
}
}