add ws share api

This commit is contained in:
akrike
2025-06-18 22:06:42 +08:00
parent acc623e5ec
commit 88bba5d4b5
4 changed files with 52 additions and 2 deletions

View File

@@ -97,7 +97,7 @@ func (w *wsApi) WebsocketShareHandle(ctx *gin.Context) {
errCheck(ctx, data.ExpireTime.Unix() <= time.Now().Unix(), "Share expired!") errCheck(ctx, data.ExpireTime.Unix() <= time.Now().Unix(), "Share expired!")
proc, err := logic.ProcessCtlLogic.GetProcess(data.Pid) proc, err := logic.ProcessCtlLogic.GetProcess(data.Pid)
errCheck(ctx, err != nil, err) errCheck(ctx, err != nil, err)
guestName := "guest-" + strconv.Itoa(data.Id) // 构造访客用户名 guestName := "guest-" + strconv.Itoa(int(data.ID)) // 构造访客用户名
errCheck(ctx, proc.HasWsConn(guestName), "A connection already exists; unable to establish a new one!") errCheck(ctx, proc.HasWsConn(guestName), "A connection already exists; unable to establish a new one!")
errCheck(ctx, proc.State.State != 1, "The process is currently running.") errCheck(ctx, proc.State.State != 1, "The process is currently running.")
errCheck(ctx, !proc.VerifyControl(), "Insufficient permissions; please check your access rights!") errCheck(ctx, !proc.VerifyControl(), "Insufficient permissions; please check your access rights!")
@@ -105,6 +105,8 @@ func (w *wsApi) WebsocketShareHandle(ctx *gin.Context) {
errCheck(ctx, err != nil, "WebSocket connection upgrade failed!") errCheck(ctx, err != nil, "WebSocket connection upgrade failed!")
log.Logger.Infow("ws连接成功") log.Logger.Infow("ws连接成功")
data.UpdatedAt = time.Now()
repository.WsShare.Edit(data)
proc.SetTerminalSize(utils.GetIntByString(ctx.Query("cols")), utils.GetIntByString(ctx.Query("rows"))) proc.SetTerminalSize(utils.GetIntByString(ctx.Query("cols")), utils.GetIntByString(ctx.Query("rows")))
wsCtx, cancel := context.WithCancel(context.Background()) wsCtx, cancel := context.WithCancel(context.Background())
@@ -174,3 +176,13 @@ func (w *wsApi) startWsConnect(wci *WsConnetInstance, cancel context.CancelFunc,
}() }()
} }
func GetWsShareList(ctx *gin.Context) {
rOk(ctx, "Operation successful!", logic.WsSahreLogic.GetWsShareList())
}
func DeleteWsShareById(ctx *gin.Context) {
err := logic.WsSahreLogic.DeleteById(ctx.GetInt("id"))
errCheck(ctx, err != nil, err)
rOk(ctx, "Operation successful!", nil)
}

View File

@@ -0,0 +1,18 @@
package logic
import (
"github.com/lzh-1625/go_process_manager/internal/app/model"
"github.com/lzh-1625/go_process_manager/internal/app/repository"
)
type wsShareLogic struct{}
var WsSahreLogic = &wsShareLogic{}
func (w *wsShareLogic) GetWsShareList() []*model.WsShare {
return repository.WsShare.GetWsShareList()
}
func (w *wsShareLogic) DeleteById(id int) error {
return repository.WsShare.Delete(id)
}

View File

@@ -2,10 +2,12 @@ package model
import ( import (
"time" "time"
"gorm.io/gorm"
) )
type WsShare struct { type WsShare struct {
Id int `gorm:"primaryKey;autoIncrement;column:id" json:"id"` gorm.Model
Pid int `gorm:"column:pid" json:"pid"` Pid int `gorm:"column:pid" json:"pid"`
Write bool `gorm:"column:write" json:"write"` Write bool `gorm:"column:write" json:"write"`
ExpireTime time.Time `gorm:"column:expire_time" json:"expireTime"` ExpireTime time.Time `gorm:"column:expire_time" json:"expireTime"`

View File

@@ -17,3 +17,21 @@ func (p *wsShare) GetWsShareDataByToken(token string) (data *model.WsShare, err
func (p *wsShare) AddShareData(data model.WsShare) error { func (p *wsShare) AddShareData(data model.WsShare) error {
return db.Save(&data).Error return db.Save(&data).Error
} }
func (p *wsShare) GetWsShareList() (data []*model.WsShare) {
ws := query.WsShare
data, _ = ws.Find()
return
}
func (p *wsShare) Delete(id int) error {
ws := query.WsShare
_, err := ws.Where(ws.Id.Eq(id)).Delete()
return err
}
func (p *wsShare) Edit(data *model.WsShare) error {
ws := query.WsShare
_, err := ws.Where(ws.Id.Eq(int(data.ID))).Updates(data)
return err
}