修复关闭的会话仍然显示在线的问题 fixes #25

This commit is contained in:
dushixiang 2021-01-12 21:37:27 +08:00
parent 6f2bf6c9ba
commit 3bb7d2d49b
7 changed files with 49 additions and 23 deletions

1
go.mod
View File

@ -11,6 +11,7 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/sftp v1.12.0
github.com/pquerna/otp v1.3.0
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.7.1

2
go.sum
View File

@ -182,6 +182,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

View File

@ -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

View File

@ -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
}
}

View File

@ -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() {

View File

@ -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

View File

@ -1,6 +1,6 @@
{
"name": "next-terminal",
"version": "0.0.6",
"version": "0.0.7",
"private": true,
"dependencies": {
"@ant-design/icons": "^4.3.0",