修复关闭的会话仍然显示在线的问题 fixes #25
This commit is contained in:
parent
6f2bf6c9ba
commit
3bb7d2d49b
1
go.mod
1
go.mod
@ -11,6 +11,7 @@ require (
|
|||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
github.com/pquerna/otp v1.3.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/sirupsen/logrus v1.4.2
|
||||||
github.com/spf13/pflag v1.0.3
|
github.com/spf13/pflag v1.0.3
|
||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
|
2
go.sum
2
go.sum
@ -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-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||||
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
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/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/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 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
|
||||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||||
|
@ -104,10 +104,6 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
global.Store.Del(sessionId)
|
global.Store.Del(sessionId)
|
||||||
|
|
||||||
if code == Normal {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
session := model.Session{}
|
session := model.Session{}
|
||||||
session.ID = sessionId
|
session.ID = sessionId
|
||||||
session.Status = model.Disconnected
|
session.Status = model.Disconnected
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
TunnelClosed int = -1
|
||||||
Normal int = 0
|
Normal int = 0
|
||||||
NotFoundSession int = 2000
|
NotFoundSession int = 2000
|
||||||
NewTunnelError int = 2001
|
NewTunnelError int = 2001
|
||||||
@ -155,12 +156,12 @@ func TunEndpoint(c echo.Context) error {
|
|||||||
for true {
|
for true {
|
||||||
instruction, err := tunnel.Read()
|
instruction, err := tunnel.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
CloseSessionById(sessionId, Normal, "")
|
CloseSessionById(sessionId, TunnelClosed, "隧道已关闭")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
err = ws.WriteMessage(websocket.TextMessage, instruction)
|
err = ws.WriteMessage(websocket.TextMessage, instruction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
CloseSessionById(sessionId, Normal, "")
|
CloseSessionById(sessionId, TunnelClosed, "隧道已关闭")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,12 +170,12 @@ func TunEndpoint(c echo.Context) error {
|
|||||||
for true {
|
for true {
|
||||||
_, message, err := ws.ReadMessage()
|
_, message, err := ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
CloseSessionById(sessionId, Normal, "")
|
CloseSessionById(sessionId, Normal, "用户主动关闭了会话")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
_, err = tunnel.WriteAndFlush(message)
|
_, err = tunnel.WriteAndFlush(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
CloseSessionById(sessionId, Normal, "")
|
CloseSessionById(sessionId, Normal, "用户主动关闭了会话")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,53 @@
|
|||||||
package handle
|
package handle
|
||||||
|
|
||||||
import (
|
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/guacd"
|
||||||
"next-terminal/pkg/model"
|
"next-terminal/pkg/model"
|
||||||
"next-terminal/pkg/utils"
|
"next-terminal/pkg/utils"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunTicker() {
|
func RunTicker() {
|
||||||
var ch chan int
|
|
||||||
|
c := cron.New(cron.WithSeconds()) //精确到秒
|
||||||
|
|
||||||
// 定时任务,每隔一小时删除一次未使用的会话信息
|
// 定时任务,每隔一小时删除一次未使用的会话信息
|
||||||
ticker := time.NewTicker(time.Minute * 60)
|
_, _ = c.AddFunc("0 0/1 0/1 * * ?", func() {
|
||||||
go func() {
|
sessions, _ := model.FindSessionByStatusIn([]string{model.NoConnect, model.Connecting})
|
||||||
for range ticker.C {
|
if sessions != nil && len(sessions) > 0 {
|
||||||
sessions, _ := model.FindSessionByStatus(model.NoConnect)
|
now := time.Now()
|
||||||
if sessions != nil && len(sessions) > 0 {
|
for i := range sessions {
|
||||||
now := time.Now()
|
if now.Sub(sessions[i].ConnectedTime.Time) > time.Hour*1 {
|
||||||
for i := range sessions {
|
model.DeleteSessionById(sessions[i].ID)
|
||||||
if now.Sub(sessions[i].ConnectedTime.Time) > time.Hour*1 {
|
s := sessions[i].Username + "@" + sessions[i].IP + ":" + strconv.Itoa(sessions[i].Port)
|
||||||
model.DeleteSessionById(sessions[i].ID)
|
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() {
|
func RunDataFix() {
|
||||||
|
@ -112,6 +112,11 @@ func FindSessionByStatus(status string) (o []Session, err error) {
|
|||||||
return
|
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) {
|
func CreateNewSession(o *Session) (err error) {
|
||||||
err = global.DB.Create(o).Error
|
err = global.DB.Create(o).Error
|
||||||
return
|
return
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "next-terminal",
|
"name": "next-terminal",
|
||||||
"version": "0.0.6",
|
"version": "0.0.7",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ant-design/icons": "^4.3.0",
|
"@ant-design/icons": "^4.3.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user