| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package main
- import (
- "crypto/rand"
- "encoding/hex"
- "net/http"
- "sync"
- "time"
- )
- type sessionInfo struct {
- last time.Time
- }
- var (
- sessionMu sync.Mutex
- sessions = make(map[string]*sessionInfo)
- )
- const (
- idleTimeout = 5 * time.Minute
- gcPeriod = 1 * time.Hour
- )
- func createSession() string {
- buf := make([]byte, 32)
- rand.Read(buf)
- return hex.EncodeToString(buf)
- }
- func addSession(id string) {
- sessionMu.Lock()
- sessions[id] = &sessionInfo{last: time.Now()}
- sessionMu.Unlock()
- }
- func hasSession(id string) bool {
- now := time.Now()
- sessionMu.Lock()
- defer sessionMu.Unlock()
- s, ok := sessions[id]
- if !ok {
- return false
- }
- if now.Sub(s.last) > idleTimeout {
- delete(sessions, id)
- return false
- }
- s.last = now
- return true
- }
- func checkSession(r *http.Request) bool {
- c, err := r.Cookie("session_id")
- if err != nil {
- return false
- }
- return hasSession(c.Value)
- }
- func startSessionCleaner() {
- go func() {
- ticker := time.NewTicker(gcPeriod)
- defer ticker.Stop()
- for range ticker.C {
- now := time.Now()
- sessionMu.Lock()
- for k, s := range sessions {
- if now.Sub(s.last) > idleTimeout {
- delete(sessions, k)
- }
- }
- sessionMu.Unlock()
- }
- }()
- }
|