-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
274 lines (236 loc) · 7.07 KB
/
session.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
package handler
import (
"bytes"
"encoding/gob"
"fmt"
"strconv"
"strings"
"time"
"github.com/bnch/lan/packets"
)
const packetChanBufSize = 500
// Sessions is a slice containing all the sessions of the online users.
var Sessions = SessionCollection("")
// AdminPassword is md5 hash of the password.
var AdminPassword string
// Session is an user's session on Bancho.
type Session struct {
Username string
UserID int32
Token string
Admin bool
LastSeen time.Time
State packets.OsuSendUserState
Spectating string
}
// Send sends a packets to the session.
func (s Session) Send(ps ...packets.Packet) {
packetifiers := make([]packets.Packetifier, len(ps))
for i, p := range ps {
packetifiers[i] = packets.Packetifier(p)
}
b, err := packets.Packetify(packetifiers)
if err != nil {
fmt.Println(err)
return
}
s.SendBytes(b)
}
// SendBytes sends bytes to the session to be send to the client. These are
// usually packetified packets.
func (s Session) SendBytes(b []byte) {
Redis.RPush("lan/queues/"+s.Token, string(b))
}
// Permissions converts the bool Admin to osu!'s permissions.
func (s Session) Permissions() int32 {
if s.Admin {
// all permissions
return 63
}
// supporter
return 5
}
// Subscribe adds the Session to a stream in streams.
func (s Session) Subscribe(stream string) bool {
if s.In(stream) {
return false
}
SessionCollection(stream).Add(s)
Redis.SAdd("lan/my_collections/"+s.Token, stream)
return true
}
// Unsubscribe removes the user from a stream.
func (s Session) Unsubscribe(stream string) {
SessionCollection(stream).Delete(s)
Redis.SRem("lan/my_collections/"+s.Token, stream)
}
// In checks whether a Session is in a stream.
func (s Session) In(stream string) bool {
return Redis.SIsMember("lan/my_collections/"+s.Token, stream).Val()
}
// Dispose finalizes the session removing all references to it
// (including Sessions) and setting s.disposed to true.
func (s Session) Dispose() {
Redis.Del("lan/queues/" + s.Token)
Redis.Del("lan/sessions/" + s.Token)
SessionCollection("spec/" + s.Token).Destroy()
Sessions.Delete(s)
for _, x := range Redis.SMembers("lan/my_collections/" + s.Token).Val() {
SessionCollection(x).Delete(s)
}
Redis.Del("lan/my_collections/" + s.Token)
}
// NewSession creates a new session.
func NewSession(username string, passMD5 string) Session {
if Sessions.TokenFromUsername(username) != "" || username == "BanchoBot" {
return Session{
UserID: authFailed,
Token: GenerateGUID(),
}
}
// user not found, all good
if Redis.Get("lan/user_id").Val() == "" {
Redis.Set("lan/user_id", 49, 0)
}
newSess := Session{
Username: username,
UserID: int32(Redis.Incr("lan/user_id").Val()),
Token: GenerateGUID(),
Admin: passMD5 == AdminPassword,
}
Sessions.Add(newSess)
SaveSession(newSess)
return newSess
}
// SessionCollection is simply a string, and it mainly indicates a redis list
// of tokens.
type SessionCollection string
// AllTokens retrieves all the tokens in the session collection.
func (s SessionCollection) AllTokens() []string {
return Redis.SMembers("lan/session_list/" + string(s)).Val()
}
// TokenFromUsername retrieves a token of a session using its user's username.
func (s SessionCollection) TokenFromUsername(u string) string {
return Redis.HGet("lan/session_lookup_tables/username/"+string(s), u).Val()
}
// TokenFromID retrieves a Session by its user's ID.
func (s SessionCollection) TokenFromID(u int32) string {
return Redis.HGet("lan/session_lookup_tables/id/"+string(s), strconv.Itoa(int(u))).Val()
}
// Add adds a Session to the collection.
func (s SessionCollection) Add(sess Session) {
if Redis.SAdd("lan/session_list/"+string(s), sess.Token).Val() == 0 {
// Value already present
return
}
Redis.HSet("lan/session_lookup_tables/username/"+string(s), sess.Username, sess.Token)
Redis.HSet("lan/session_lookup_tables/id/"+string(s), strconv.Itoa(int(sess.UserID)), sess.Token)
}
// Delete removes a Session from the collection.
func (s SessionCollection) Delete(sess Session) {
Redis.SRem("lan/session_list/"+string(s), sess.Token)
Redis.HDel("lan/session_lookup_tables/username/"+string(s), sess.Username)
Redis.HDel("lan/session_lookup_tables/id/"+string(s), strconv.Itoa(int(sess.UserID)))
}
// AllUserIDs fetches all the user IDs in the SessionCollection.
func (s SessionCollection) AllUserIDs() []int32 {
m := Redis.HGetAll("lan/session_lookup_tables/id/" + string(s)).Val()
ids := make([]int32, len(m))
i := 0
for v := range m {
id, _ := strconv.ParseInt(v, 10, 32)
ids[i] = int32(id)
i++
}
return ids
}
// Destroy removes from redis all references to this SessionCollection.
func (s SessionCollection) Destroy() {
for _, token := range Redis.SMembers("lan/session_list/" + string(s)).Val() {
Redis.SRem("lan/my_collections/"+token, string(s))
}
Redis.Del(
"lan/session_list/"+string(s),
"lan/session_lookup_tables/username/"+string(s),
"lan/session_lookup_tables/id/"+string(s),
)
}
// GetSession retrieves a Session by knowing its token.
func GetSession(token string) (sess *Session) {
val := Redis.Get("lan/sessions/" + token).Val()
if val == "" {
return nil
}
sess = new(Session)
gob.NewDecoder(strings.NewReader(val)).Decode(sess)
return sess
}
// SaveSession saves the session currently being handled.
func SaveSession(sess Session) {
buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(sess)
Redis.Set("lan/sessions/"+sess.Token, buf.String(), 0)
}
// Send sends packets to all members of the SessionCollection
func (s SessionCollection) Send(ps ...packets.Packet) {
packetifiers := make([]packets.Packetifier, len(ps))
for i, p := range ps {
packetifiers[i] = packets.Packetifier(p)
}
b, err := packets.Packetify(packetifiers)
if err != nil {
return
}
c := s.AllTokens()
for _, e := range c {
Redis.RPush("lan/queues/"+e, string(b))
}
}
// SendExcept sends packets to all members of the SessionCollection, except for
// those with the ID specified.
func (s SessionCollection) SendExcept(except []int32, ps ...packets.Packet) {
packetifiers := make([]packets.Packetifier, len(ps))
for i, p := range ps {
packetifiers[i] = packets.Packetifier(p)
}
b, err := packets.Packetify(packetifiers)
if err != nil {
return
}
m := Redis.HGetAll("lan/session_lookup_tables/id/" + string(s)).Val()
SessionLooper:
for k, v := range m {
for _, x := range except {
if strconv.Itoa(int(x)) == k {
continue SessionLooper
}
}
Redis.RPush("lan/queues/"+v, string(b))
}
}
// Len returns the length of the SessionCollection's slice.
func (s SessionCollection) Len() int {
return int(Redis.SCard("lan/session_list/" + string(s)).Val())
}
// disposer automatically disposes sessions older than 120 seconds.
func disposer() {
for {
time.Sleep(time.Second * 10)
cp := Sessions.AllTokens()
for _, c := range cp {
sess := GetSession(c)
if sess == nil {
continue
}
if time.Now().Sub(sess.LastSeen) > time.Second*120 {
fmt.Println(">", sess.Username, "timed out")
sess.Dispose()
Sessions.Send(&packets.BanchoUserQuit{ID: sess.UserID, State: 0})
}
}
}
}
func init() {
go disposer()
gob.Register(Session{})
}