mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-05 22:30:28 +08:00
使用数据库存储SESSION
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/sessions"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -85,10 +84,15 @@ func (this *AdminNode) Run() {
|
|||||||
this.startAPINode()
|
this.startAPINode()
|
||||||
|
|
||||||
// 启动Web服务
|
// 启动Web服务
|
||||||
|
sessionManager, err := NewSessionManager()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("start session failed: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
TeaGo.NewServer(false).
|
TeaGo.NewServer(false).
|
||||||
AccessLog(false).
|
AccessLog(false).
|
||||||
EndAll().
|
EndAll().
|
||||||
Session(sessions.NewFileSessionManager(86400, secret), teaconst.CookieSID).
|
Session(sessionManager, teaconst.CookieSID).
|
||||||
ReadHeaderTimeout(3 * time.Second).
|
ReadHeaderTimeout(3 * time.Second).
|
||||||
ReadTimeout(1200 * time.Second).
|
ReadTimeout(1200 * time.Second).
|
||||||
Start()
|
Start()
|
||||||
|
|||||||
73
internal/nodes/session_manager.go
Normal file
73
internal/nodes/session_manager.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
|
||||||
|
package nodes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/iwind/TeaGo/actions"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SessionManager struct {
|
||||||
|
life uint
|
||||||
|
rpcClient *rpc.RPCClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSessionManager() (*SessionManager, error) {
|
||||||
|
rpcClient, err := rpc.SharedRPC()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &SessionManager{
|
||||||
|
rpcClient: rpcClient,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SessionManager) Init(config *actions.SessionConfig) {
|
||||||
|
this.life = config.Life
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SessionManager) Read(sid string) map[string]string {
|
||||||
|
var result = map[string]string{}
|
||||||
|
|
||||||
|
resp, err := this.rpcClient.LoginSessionRPC().FindLoginSession(this.rpcClient.Context(0), &pb.FindLoginSessionRequest{Sid: sid})
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("SESSION", "read '"+sid+"' failed: "+err.Error())
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
var session = resp.LoginSession
|
||||||
|
if session == nil || len(session.ValuesJSON) == 0 {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(session.ValuesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("SESSION", "decode '"+sid+"' values failed: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SessionManager) WriteItem(sid string, key string, value string) bool {
|
||||||
|
_, err := this.rpcClient.LoginSessionRPC().WriteLoginSessionValue(this.rpcClient.Context(0), &pb.WriteLoginSessionValueRequest{
|
||||||
|
Sid: sid,
|
||||||
|
Key: key,
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("SESSION", "write sid:'"+sid+"' key:'"+key+"' failed: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SessionManager) Delete(sid string) bool {
|
||||||
|
_, err := this.rpcClient.LoginSessionRPC().DeleteLoginSession(this.rpcClient.Context(0), &pb.DeleteLoginSessionRequest{Sid: sid})
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("SESSION", "delete '"+sid+"' failed: "+err.Error())
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -407,6 +407,10 @@ func (this *RPCClient) LoginRPC() pb.LoginServiceClient {
|
|||||||
return pb.NewLoginServiceClient(this.pickConn())
|
return pb.NewLoginServiceClient(this.pickConn())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *RPCClient) LoginSessionRPC() pb.LoginSessionServiceClient {
|
||||||
|
return pb.NewLoginSessionServiceClient(this.pickConn())
|
||||||
|
}
|
||||||
|
|
||||||
func (this *RPCClient) NodeTaskRPC() pb.NodeTaskServiceClient {
|
func (this *RPCClient) NodeTaskRPC() pb.NodeTaskServiceClient {
|
||||||
return pb.NewNodeTaskServiceClient(this.pickConn())
|
return pb.NewNodeTaskServiceClient(this.pickConn())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user