Files
Spark/server/handler/process.go
XZB dae496d934 add: file upload
optimize: project structure
2022-05-21 19:50:01 +08:00

57 lines
1.7 KiB
Go

package handler
import (
"Spark/modules"
"Spark/server/common"
"Spark/utils"
"Spark/utils/melody"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"time"
)
// listDeviceProcesses will list processes on remote client
func listDeviceProcesses(ctx *gin.Context) {
connUUID, ok := checkForm(ctx, nil)
if !ok {
return
}
trigger := utils.GetStrUUID()
common.SendPackByUUID(modules.Packet{Act: `listProcesses`, Event: trigger}, connUUID)
ok = common.AddEventOnce(func(p modules.Packet, _ *melody.Session) {
if p.Code != 0 {
ctx.JSON(http.StatusInternalServerError, modules.Packet{Code: 1, Msg: p.Msg})
} else {
ctx.JSON(http.StatusOK, modules.Packet{Code: 0, Data: p.Data})
}
}, connUUID, trigger, 5*time.Second)
if !ok {
ctx.JSON(http.StatusGatewayTimeout, modules.Packet{Code: 1, Msg: `${i18n|responseTimeout}`})
}
}
// killDeviceProcess will try to get send a packet to
// client and let it kill the process specified.
func killDeviceProcess(ctx *gin.Context) {
var form struct {
Pid int32 `json:"pid" yaml:"pid" form:"pid" binding:"required"`
}
target, ok := checkForm(ctx, &form)
if !ok {
return
}
trigger := utils.GetStrUUID()
common.SendPackByUUID(modules.Packet{Code: 0, Act: `killProcess`, Data: gin.H{`pid`: strconv.FormatInt(int64(form.Pid), 10)}, Event: trigger}, target)
ok = common.AddEventOnce(func(p modules.Packet, _ *melody.Session) {
if p.Code != 0 {
ctx.JSON(http.StatusInternalServerError, modules.Packet{Code: 1, Msg: p.Msg})
} else {
ctx.JSON(http.StatusOK, modules.Packet{Code: 0})
}
}, target, trigger, 5*time.Second)
if !ok {
ctx.JSON(http.StatusGatewayTimeout, modules.Packet{Code: 1, Msg: `${i18n|responseTimeout}`})
}
}