forked from hidu/pproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
71 lines (64 loc) · 1.4 KB
/
sessions.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
package serve
import (
"log"
"net/http"
"time"
)
type clientSession struct {
Ip string
Port string
RequestNum int
FirstRequestTime time.Time
LastRequestTime time.Time
User *User
}
func (ser *ProxyServe) regirestReq(req *http.Request, reqCtx *requestCtx) {
ip := reqCtx.GetIp()
now := time.Now()
ser.mu.Lock()
defer ser.mu.Unlock()
var session *clientSession
client, has := ser.ProxyClients[ip]
if has {
session = client
} else {
session = &clientSession{
Ip: ip,
RequestNum: 0,
FirstRequestTime: now,
LastRequestTime: now,
}
}
if reqCtx.User.Name == "" && session.User != nil {
reqCtx.User = session.User
} else if reqCtx.User.Name != "" {
session.User = reqCtx.User
}
session.LastRequestTime = now
session.RequestNum++
if ser.Debug {
log.Println("session_debug:", session)
}
ser.ProxyClients[ip] = session
reqCtx.ClientSession = session
if !has {
ser.wsSer.broadProxyClientNum()
}
}
func (ser *ProxyServe) cleanExpiredSession() {
ser.mu.Lock()
defer ser.mu.Unlock()
now := time.Now()
deleteIps := []string{}
for ip, session := range ser.ProxyClients {
t := now.Sub(session.LastRequestTime)
if t.Minutes() > 10 {
deleteIps = append(deleteIps, ip)
}
}
for _, ip := range deleteIps {
delete(ser.ProxyClients, ip)
log.Println("session expired:ip=", ip)
}
ser.wsSer.broadProxyClientNum()
}