增加对会话操作的优化提示
This commit is contained in:
@ -36,7 +36,7 @@ func SessionPagingEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
for i := 0; i < len(items); i++ {
|
||||
if len(items[i].Recording) > 0 {
|
||||
if status == model.Disconnected && len(items[i].Recording) > 0 {
|
||||
recording := items[i].Recording + "/recording"
|
||||
|
||||
if utils.FileExists(recording) {
|
||||
@ -82,7 +82,9 @@ func SessionContentEndpoint(c echo.Context) error {
|
||||
session.Status = model.Connected
|
||||
session.ConnectedTime = utils.NowJsonTime()
|
||||
|
||||
model.UpdateSessionById(&session, sessionId)
|
||||
if err := model.UpdateSessionById(&session, sessionId); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
@ -91,16 +93,20 @@ func SessionDiscontentEndpoint(c echo.Context) error {
|
||||
|
||||
split := strings.Split(sessionIds, ",")
|
||||
for i := range split {
|
||||
CloseSessionById(split[i], ForcedDisconnect, "管理员强制关闭了此次接入。")
|
||||
CloseSessionById(split[i], ForcedDisconnect, "强制断开")
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func CloseSessionById(sessionId string, code int, reason string) {
|
||||
tun, _ := global.Store.Get(sessionId)
|
||||
if tun != nil {
|
||||
_ = tun.Tun.Close()
|
||||
CloseSessionByWebSocket(tun.WebSocket, code, reason)
|
||||
observable, _ := global.Store.Get(sessionId)
|
||||
if observable != nil {
|
||||
_ = observable.Subject.Tunnel.Close()
|
||||
for i := 0; i < len(observable.Observers); i++ {
|
||||
_ = observable.Observers[i].Tunnel.Close()
|
||||
CloseWebSocket(observable.Observers[i].WebSocket, code, reason)
|
||||
}
|
||||
CloseWebSocket(observable.Subject.WebSocket, code, reason)
|
||||
}
|
||||
global.Store.Del(sessionId)
|
||||
|
||||
@ -126,10 +132,10 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
||||
session.Code = code
|
||||
session.Message = reason
|
||||
|
||||
model.UpdateSessionById(&session, sessionId)
|
||||
_ = model.UpdateSessionById(&session, sessionId)
|
||||
}
|
||||
|
||||
func CloseSessionByWebSocket(ws *websocket.Conn, c int, t string) {
|
||||
func CloseWebSocket(ws *websocket.Conn, c int, t string) {
|
||||
if ws == nil {
|
||||
return
|
||||
}
|
||||
@ -157,13 +163,10 @@ func SessionResizeEndpoint(c echo.Context) error {
|
||||
|
||||
intHeight, _ := strconv.Atoi(height)
|
||||
|
||||
session := model.Session{}
|
||||
session.ID = sessionId
|
||||
session.Width = intWidth
|
||||
session.Height = intHeight
|
||||
|
||||
model.UpdateSessionById(&session, sessionId)
|
||||
return Success(c, session)
|
||||
if err := model.UpdateSessionWindowSizeById(intWidth, intHeight, sessionId); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, "")
|
||||
}
|
||||
|
||||
func SessionCreateEndpoint(c echo.Context) error {
|
||||
@ -239,7 +242,7 @@ func SessionUploadEndpoint(c echo.Context) error {
|
||||
return errors.New("获取sftp客户端失败")
|
||||
}
|
||||
|
||||
dstFile, err := tun.SftpClient.Create(remoteFile)
|
||||
dstFile, err := tun.Subject.SftpClient.Create(remoteFile)
|
||||
defer dstFile.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -292,7 +295,7 @@ func SessionDownloadEndpoint(c echo.Context) error {
|
||||
return errors.New("获取sftp客户端失败")
|
||||
}
|
||||
|
||||
dstFile, err := tun.SftpClient.Open(remoteFile)
|
||||
dstFile, err := tun.Subject.SftpClient.Open(remoteFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -341,16 +344,16 @@ func SessionLsEndpoint(c echo.Context) error {
|
||||
return errors.New("获取sftp客户端失败")
|
||||
}
|
||||
|
||||
if tun.SftpClient == nil {
|
||||
if tun.Subject.SftpClient == nil {
|
||||
sftpClient, err := CreateSftpClient(session.AssetId)
|
||||
if err != nil {
|
||||
logrus.Errorf("创建sftp客户端失败:%v", err.Error())
|
||||
return err
|
||||
}
|
||||
tun.SftpClient = sftpClient
|
||||
tun.Subject.SftpClient = sftpClient
|
||||
}
|
||||
|
||||
fileInfos, err := tun.SftpClient.ReadDir(remoteDir)
|
||||
fileInfos, err := tun.Subject.SftpClient.ReadDir(remoteDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -410,7 +413,7 @@ func SessionMkDirEndpoint(c echo.Context) error {
|
||||
if !ok {
|
||||
return errors.New("获取sftp客户端失败")
|
||||
}
|
||||
if err := tun.SftpClient.Mkdir(remoteDir); err != nil {
|
||||
if err := tun.Subject.SftpClient.Mkdir(remoteDir); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
@ -441,18 +444,18 @@ func SessionRmDirEndpoint(c echo.Context) error {
|
||||
if !ok {
|
||||
return errors.New("获取sftp客户端失败")
|
||||
}
|
||||
fileInfos, err := tun.SftpClient.ReadDir(remoteDir)
|
||||
fileInfos, err := tun.Subject.SftpClient.ReadDir(remoteDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range fileInfos {
|
||||
if err := tun.SftpClient.Remove(path.Join(remoteDir, fileInfos[i].Name())); err != nil {
|
||||
if err := tun.Subject.SftpClient.Remove(path.Join(remoteDir, fileInfos[i].Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tun.SftpClient.RemoveDirectory(remoteDir); err != nil {
|
||||
if err := tun.Subject.SftpClient.RemoveDirectory(remoteDir); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
@ -483,7 +486,7 @@ func SessionRmEndpoint(c echo.Context) error {
|
||||
if !ok {
|
||||
return errors.New("获取sftp客户端失败")
|
||||
}
|
||||
if err := tun.SftpClient.Remove(remoteFile); err != nil {
|
||||
if err := tun.Subject.SftpClient.Remove(remoteFile); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
|
@ -1,6 +1,8 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -49,9 +51,13 @@ func TunEndpoint(c echo.Context) error {
|
||||
if len(connectionId) > 0 {
|
||||
session, err = model.FindSessionByConnectionId(connectionId)
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, NotFoundSession, "会话不存在")
|
||||
CloseWebSocket(ws, NotFoundSession, "会话不存在")
|
||||
return err
|
||||
}
|
||||
if session.Status != model.Connected {
|
||||
CloseWebSocket(ws, NotFoundSession, "会话未在线")
|
||||
return errors.New("会话未在线")
|
||||
}
|
||||
configuration.ConnectionID = connectionId
|
||||
} else {
|
||||
session, err = model.FindSessionById(sessionId)
|
||||
@ -131,38 +137,62 @@ func TunEndpoint(c echo.Context) error {
|
||||
|
||||
tunnel, err := guacd.NewTunnel(addr, configuration)
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, NewTunnelError, err.Error())
|
||||
if connectionId == "" {
|
||||
CloseSessionById(sessionId, NewTunnelError, err.Error())
|
||||
}
|
||||
logrus.Printf("建立连接失败: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
tun := global.Tun{
|
||||
Tun: tunnel,
|
||||
Tunnel: tunnel,
|
||||
WebSocket: ws,
|
||||
}
|
||||
|
||||
global.Store.Set(sessionId, &tun)
|
||||
|
||||
if len(session.ConnectionId) == 0 {
|
||||
var observers []global.Tun
|
||||
observable := global.Observable{
|
||||
Subject: &tun,
|
||||
Observers: observers,
|
||||
}
|
||||
|
||||
global.Store.Set(sessionId, &observable)
|
||||
// 创建新会话
|
||||
session.ConnectionId = tunnel.UUID
|
||||
session.Width = intWidth
|
||||
session.Height = intHeight
|
||||
session.Status = model.Connecting
|
||||
session.Recording = configuration.GetParameter(guacd.RecordingPath)
|
||||
|
||||
model.UpdateSessionById(&session, sessionId)
|
||||
if err := model.UpdateSessionById(&session, sessionId); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// TODO 处理监控会话的退出
|
||||
// 监控会话
|
||||
observable, ok := global.Store.Get(sessionId)
|
||||
if ok {
|
||||
observers := append(observable.Observers, tun)
|
||||
observable.Observers = observers
|
||||
global.Store.Set(sessionId, observable)
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
for true {
|
||||
instruction, err := tunnel.Read()
|
||||
fmt.Printf("<- %v \n", string(instruction))
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, TunnelClosed, "隧道已关闭")
|
||||
if connectionId == "" {
|
||||
CloseSessionById(sessionId, TunnelClosed, "远程连接关闭")
|
||||
}
|
||||
break
|
||||
}
|
||||
err = ws.WriteMessage(websocket.TextMessage, instruction)
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, TunnelClosed, "隧道已关闭")
|
||||
if connectionId == "" {
|
||||
CloseSessionById(sessionId, Normal, "正常退出")
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -171,12 +201,16 @@ func TunEndpoint(c echo.Context) error {
|
||||
for true {
|
||||
_, message, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, Normal, "用户主动关闭了会话")
|
||||
if connectionId == "" {
|
||||
CloseSessionById(sessionId, Normal, "正常退出")
|
||||
}
|
||||
break
|
||||
}
|
||||
_, err = tunnel.WriteAndFlush(message)
|
||||
if err != nil {
|
||||
CloseSessionById(sessionId, Normal, "用户主动关闭了会话")
|
||||
if connectionId == "" {
|
||||
CloseSessionById(sessionId, TunnelClosed, "远程连接关闭")
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user