2020-07-22 14:50:08 +00:00
|
|
|
package ssdb
|
|
|
|
|
|
|
|
import (
|
2020-08-30 15:39:07 +00:00
|
|
|
"context"
|
2020-10-21 14:12:25 +00:00
|
|
|
"encoding/json"
|
2020-07-22 14:50:08 +00:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ssdb/gossdb/ssdb"
|
2020-10-05 11:04:22 +00:00
|
|
|
|
2020-10-08 09:17:15 +00:00
|
|
|
"github.com/astaxie/beego/server/web/session"
|
2020-07-22 14:50:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ssdbProvider = &Provider{}
|
|
|
|
|
|
|
|
// Provider holds ssdb client and configs
|
|
|
|
type Provider struct {
|
|
|
|
client *ssdb.Client
|
2020-10-21 14:12:25 +00:00
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
2020-07-22 14:50:08 +00:00
|
|
|
maxLifetime int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) connectInit() error {
|
|
|
|
var err error
|
2020-10-21 14:12:25 +00:00
|
|
|
if p.Host == "" || p.Port == 0 {
|
2020-07-22 14:50:08 +00:00
|
|
|
return errors.New("SessionInit First")
|
|
|
|
}
|
2020-10-21 14:12:25 +00:00
|
|
|
p.client, err = ssdb.Connect(p.Host, p.Port)
|
2020-07-22 14:50:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionInit init the ssdb with the config
|
2020-10-21 14:12:25 +00:00
|
|
|
func (p *Provider) SessionInit(ctx context.Context, maxLifetime int64, cfg string) error {
|
2020-07-22 14:50:08 +00:00
|
|
|
p.maxLifetime = maxLifetime
|
|
|
|
|
2020-10-21 14:12:25 +00:00
|
|
|
cfg = strings.TrimSpace(cfg)
|
2020-07-22 14:50:08 +00:00
|
|
|
var err error
|
2020-10-21 14:12:25 +00:00
|
|
|
// we think this is v2.0, using json to init the session
|
|
|
|
if strings.HasPrefix(cfg, "{") {
|
|
|
|
err = json.Unmarshal([]byte(cfg), p)
|
|
|
|
} else {
|
|
|
|
err = p.initOldStyle(cfg)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-07-22 14:50:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.connectInit()
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:12:25 +00:00
|
|
|
// for v1.x
|
|
|
|
func (p *Provider) initOldStyle(savePath string) error {
|
|
|
|
address := strings.Split(savePath, ":")
|
|
|
|
p.Host = address[0]
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p.Port, err = strconv.Atoi(address[1])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:50:08 +00:00
|
|
|
// SessionRead return a ssdb client session Store
|
2020-08-30 15:39:07 +00:00
|
|
|
func (p *Provider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
|
2020-07-22 14:50:08 +00:00
|
|
|
if p.client == nil {
|
|
|
|
if err := p.connectInit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var kv map[interface{}]interface{}
|
|
|
|
value, err := p.client.Get(sid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if value == nil || len(value.(string)) == 0 {
|
|
|
|
kv = make(map[interface{}]interface{})
|
|
|
|
} else {
|
|
|
|
kv, err = session.DecodeGob([]byte(value.(string)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionExist judged whether sid is exist in session
|
2020-08-30 15:39:07 +00:00
|
|
|
func (p *Provider) SessionExist(ctx context.Context, sid string) (bool, error) {
|
2020-07-22 14:50:08 +00:00
|
|
|
if p.client == nil {
|
|
|
|
if err := p.connectInit(); err != nil {
|
2020-08-05 16:29:22 +00:00
|
|
|
return false, err
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
value, err := p.client.Get(sid)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if value == nil || len(value.(string)) == 0 {
|
2020-08-05 16:29:22 +00:00
|
|
|
return false, nil
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
2020-08-05 16:29:22 +00:00
|
|
|
return true, nil
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionRegenerate regenerate session with new sid and delete oldsid
|
2020-08-30 15:39:07 +00:00
|
|
|
func (p *Provider) SessionRegenerate(ctx context.Context, oldsid, sid string) (session.Store, error) {
|
2020-10-05 11:04:22 +00:00
|
|
|
// conn.Do("setx", key, v, ttl)
|
2020-07-22 14:50:08 +00:00
|
|
|
if p.client == nil {
|
|
|
|
if err := p.connectInit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value, err := p.client.Get(oldsid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var kv map[interface{}]interface{}
|
|
|
|
if value == nil || len(value.(string)) == 0 {
|
|
|
|
kv = make(map[interface{}]interface{})
|
|
|
|
} else {
|
|
|
|
kv, err = session.DecodeGob([]byte(value.(string)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = p.client.Del(oldsid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, e := p.client.Do("setx", sid, value, p.maxLifetime)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionDestroy destroy the sid
|
2020-08-30 15:39:07 +00:00
|
|
|
func (p *Provider) SessionDestroy(ctx context.Context, sid string) error {
|
2020-07-22 14:50:08 +00:00
|
|
|
if p.client == nil {
|
|
|
|
if err := p.connectInit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := p.client.Del(sid)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionGC not implemented
|
2020-08-30 15:39:07 +00:00
|
|
|
func (p *Provider) SessionGC(context.Context) {
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionAll not implemented
|
2020-08-30 15:39:07 +00:00
|
|
|
func (p *Provider) SessionAll(context.Context) int {
|
2020-07-22 14:50:08 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionStore holds the session information which stored in ssdb
|
|
|
|
type SessionStore struct {
|
|
|
|
sid string
|
|
|
|
lock sync.RWMutex
|
|
|
|
values map[interface{}]interface{}
|
|
|
|
maxLifetime int64
|
|
|
|
client *ssdb.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the key and value
|
2020-08-30 15:39:07 +00:00
|
|
|
func (s *SessionStore) Set(ctx context.Context, key, value interface{}) error {
|
2020-07-22 14:50:08 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.values[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get return the value by the key
|
2020-08-30 15:39:07 +00:00
|
|
|
func (s *SessionStore) Get(ctx context.Context, key interface{}) interface{} {
|
2020-07-22 14:50:08 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
if value, ok := s.values[key]; ok {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the key in session store
|
2020-08-30 15:39:07 +00:00
|
|
|
func (s *SessionStore) Delete(ctx context.Context, key interface{}) error {
|
2020-07-22 14:50:08 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
delete(s.values, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush delete all keys and values
|
2020-08-30 15:39:07 +00:00
|
|
|
func (s *SessionStore) Flush(context.Context) error {
|
2020-07-22 14:50:08 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.values = make(map[interface{}]interface{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionID return the sessionID
|
2020-08-30 15:39:07 +00:00
|
|
|
func (s *SessionStore) SessionID(context.Context) string {
|
2020-07-22 14:50:08 +00:00
|
|
|
return s.sid
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionRelease Store the keyvalues into ssdb
|
2020-08-30 15:39:07 +00:00
|
|
|
func (s *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWriter) {
|
2020-07-22 14:50:08 +00:00
|
|
|
b, err := session.EncodeGob(s.values)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.client.Do("setx", s.sid, string(b), s.maxLifetime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
session.Register("ssdb", ssdbProvider)
|
|
|
|
}
|