修复关闭的会话仍然显示在线的问题 fixes #25
This commit is contained in:
@ -104,10 +104,6 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
||||
}
|
||||
|
||||
global.Store.Del(sessionId)
|
||||
|
||||
if code == Normal {
|
||||
return
|
||||
}
|
||||
session := model.Session{}
|
||||
session.ID = sessionId
|
||||
session.Status = model.Disconnected
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
TunnelClosed int = -1
|
||||
Normal int = 0
|
||||
NotFoundSession int = 2000
|
||||
NewTunnelError int = 2001
|
||||
@ -155,12 +156,12 @@ func TunEndpoint(c echo.Context) error {
|
||||
for true {
|
||||
instruction, err := tunnel.Read()
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, Normal, "")
|
||||
CloseSessionById(sessionId, TunnelClosed, "隧道已关闭")
|
||||
break
|
||||
}
|
||||
err = ws.WriteMessage(websocket.TextMessage, instruction)
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, Normal, "")
|
||||
CloseSessionById(sessionId, TunnelClosed, "隧道已关闭")
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -169,12 +170,12 @@ func TunEndpoint(c echo.Context) error {
|
||||
for true {
|
||||
_, message, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, Normal, "")
|
||||
CloseSessionById(sessionId, Normal, "用户主动关闭了会话")
|
||||
break
|
||||
}
|
||||
_, err = tunnel.WriteAndFlush(message)
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, Normal, "")
|
||||
CloseSessionById(sessionId, Normal, "用户主动关闭了会话")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +1,53 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"next-terminal/pkg/api"
|
||||
"next-terminal/pkg/global"
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/pkg/model"
|
||||
"next-terminal/pkg/utils"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RunTicker() {
|
||||
var ch chan int
|
||||
|
||||
c := cron.New(cron.WithSeconds()) //精确到秒
|
||||
|
||||
// 定时任务,每隔一小时删除一次未使用的会话信息
|
||||
ticker := time.NewTicker(time.Minute * 60)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
sessions, _ := model.FindSessionByStatus(model.NoConnect)
|
||||
if sessions != nil && len(sessions) > 0 {
|
||||
now := time.Now()
|
||||
for i := range sessions {
|
||||
if now.Sub(sessions[i].ConnectedTime.Time) > time.Hour*1 {
|
||||
model.DeleteSessionById(sessions[i].ID)
|
||||
}
|
||||
_, _ = c.AddFunc("0 0/1 0/1 * * ?", func() {
|
||||
sessions, _ := model.FindSessionByStatusIn([]string{model.NoConnect, model.Connecting})
|
||||
if sessions != nil && len(sessions) > 0 {
|
||||
now := time.Now()
|
||||
for i := range sessions {
|
||||
if now.Sub(sessions[i].ConnectedTime.Time) > time.Hour*1 {
|
||||
model.DeleteSessionById(sessions[i].ID)
|
||||
s := sessions[i].Username + "@" + sessions[i].IP + ":" + strconv.Itoa(sessions[i].Port)
|
||||
logrus.Debugf("会话「%v」ID「%v」超过1小时未打开,已删除。", s, sessions[i].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
ch <- 1
|
||||
}()
|
||||
<-ch
|
||||
})
|
||||
|
||||
// 定时任务,每隔一分钟校验一次运行中的会话信息
|
||||
_, _ = c.AddFunc("0 0/1 0/1 * * ?", func() {
|
||||
sessions, _ := model.FindSessionByStatus(model.Connected)
|
||||
if sessions != nil && len(sessions) > 0 {
|
||||
for i := range sessions {
|
||||
_, found := global.Cache.Get(sessions[i].ID)
|
||||
if !found {
|
||||
api.CloseSessionById(sessions[i].ID, api.Normal, "")
|
||||
s := sessions[i].Username + "@" + sessions[i].IP + ":" + strconv.Itoa(sessions[i].Port)
|
||||
logrus.Debugf("会话「%v」ID「%v」已离线,修改状态为「关闭」。", s, sessions[i].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
c.Start()
|
||||
}
|
||||
|
||||
func RunDataFix() {
|
||||
|
@ -112,6 +112,11 @@ func FindSessionByStatus(status string) (o []Session, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func FindSessionByStatusIn(statuses []string) (o []Session, err error) {
|
||||
err = global.DB.Where("status in ?", statuses).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func CreateNewSession(o *Session) (err error) {
|
||||
err = global.DB.Create(o).Error
|
||||
return
|
||||
|
Reference in New Issue
Block a user