/
session.go
207 lines (188 loc) · 4.21 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
// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webgo
import (
"crypto/rand"
"fmt"
"log"
"net/http"
"sync"
"time"
)
// SessionManager manager sessions.
type SessionManager struct {
mutex sync.RWMutex
mutexOnEnd sync.RWMutex
sessionMap map[string]*Session
onStart func(*Session)
onTouch func(*Session)
onEnd func(*Session)
path string
timeout uint
}
// Session stores the id and value for a session.
type Session struct {
Id string
Value interface{}
manager *SessionManager
res http.ResponseWriter
expire int64
}
func (session *Session) Cookie() string {
tm := time.Unix(session.expire, 0).UTC()
return fmt.Sprintf(
"SessionId=%s; path=%s; expires=%s;",
session.Id,
session.manager.path,
tm.Format("Fri, 02-Jan-2006 15:04:05 -0700"),
)
}
func (session *Session) Abandon() {
if _, found := session.manager.sessionMap[session.Id]; found {
delete((*session.manager).sessionMap, session.Id)
}
if session.res != nil {
session.res.Header().Set(
"Set-Cookie", fmt.Sprintf("SessionId=; path=%s;", session.manager.path),
)
}
}
func NewSessionManager(logger *log.Logger) *SessionManager {
p := &SessionManager{
path: "/",
sessionMap: make(map[string]*Session),
timeout: 300,
}
go func(p *SessionManager) {
for { // never stop !!!
l := time.Now().Unix()
if f := p.onEnd; f != nil {
p.mutexOnEnd.Lock()
for id, v := range p.sessionMap {
if v.expire < l {
if logger != nil {
logger.Printf("Expired session(id:%s)", id)
}
f(v)
delete(p.sessionMap, id)
}
}
p.mutexOnEnd.Unlock()
} else {
for id, v := range p.sessionMap {
if v.expire < l {
if logger != nil {
logger.Printf("Expired session(id:%s)", id)
}
delete(p.sessionMap, id)
}
}
}
time.Sleep(time.Second)
}
}(p)
return p
}
func (p *SessionManager) Has(id string) (found bool) {
_, found = p.sessionMap[id]
return
}
func (p *SessionManager) GetSessionById(id string) (session *Session) {
p.mutex.Lock()
defer p.mutex.Unlock()
if id == "" || !p.Has(id) {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return
}
id = fmt.Sprintf("%x", b)
}
tm := time.Unix(time.Now().Unix()+int64(p.timeout), 0).UTC()
var found bool
if session, found = p.sessionMap[id]; found {
session.expire = tm.Unix()
if f := p.onTouch; f != nil {
f(session)
}
return
} else {
session = &Session{Id: id, expire: tm.Unix(), manager: p}
p.sessionMap[id] = session
if f := p.onStart; f != nil {
f(session)
}
}
return
}
func (p *SessionManager) GetSession(res http.ResponseWriter, req *http.Request) (session *Session) {
if c, _ := req.Cookie("SessionId"); c != nil {
session = p.GetSessionById(c.Value)
} else {
session = p.GetSessionById("")
}
if res != nil {
session.res = res
res.Header().Add("Set-Cookie",
fmt.Sprintf("SessionId=%s; path=%s; expires=%s;",
session.Id,
session.manager.path,
time.Unix(session.expire, 0).UTC().Format(
"Fri, 02-Jan-2006 15:04:05 GMT",
),
),
)
}
return
}
func (p *SessionManager) Abandon() {
p.mutex.Lock()
defer p.mutex.Unlock()
if f := p.onEnd; f != nil {
p.mutexOnEnd.Lock()
for id, v := range p.sessionMap {
f(v)
delete(p.sessionMap, id)
}
p.mutexOnEnd.Unlock()
} else {
for id, _ := range p.sessionMap {
delete(p.sessionMap, id)
}
}
}
func (p *SessionManager) OnStart(f func(*Session)) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.onStart = f
}
func (p *SessionManager) OnTouch(f func(*Session)) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.onTouch = f
}
func (p *SessionManager) OnEnd(f func(*Session)) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.onEnd = f
}
func (p *SessionManager) SetTimeout(t uint) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.timeout = t
}
func (p *SessionManager) GetTimeout() uint {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.timeout
}
func (p *SessionManager) SetPath(t string) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.path = t
}
func (p *SessionManager) GetPath() string {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.path
}