nezha/cmd/dashboard/controller/terminal.go

109 lines
2.8 KiB
Go
Raw Normal View History

2024-10-22 10:01:01 -04:00
package controller
import (
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/hashicorp/go-uuid"
2024-12-17 09:33:47 -05:00
2024-11-28 06:38:54 -05:00
"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/pkg/utils"
"github.com/nezhahq/nezha/pkg/websocketx"
"github.com/nezhahq/nezha/proto"
"github.com/nezhahq/nezha/service/rpc"
"github.com/nezhahq/nezha/service/singleton"
2024-10-22 10:01:01 -04:00
)
// Create web ssh terminal
// @Summary Create web ssh terminal
// @Description Create web ssh terminal
// @Tags auth required
// @Accept json
// @Param terminal body model.TerminalForm true "TerminalForm"
// @Produce json
// @Success 200 {object} model.CreateTerminalResponse
// @Router /terminal [post]
2024-10-23 05:56:51 -04:00
func createTerminal(c *gin.Context) (*model.CreateTerminalResponse, error) {
2024-10-22 10:01:01 -04:00
var createTerminalReq model.TerminalForm
if err := c.ShouldBind(&createTerminalReq); err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-22 10:01:01 -04:00
}
singleton.ServerLock.RLock()
server := singleton.ServerList[createTerminalReq.ServerID]
singleton.ServerLock.RUnlock()
if server == nil || server.TaskStream == nil {
2024-10-31 17:07:04 -04:00
return nil, singleton.Localizer.ErrorT("server not found or not connected")
2024-10-22 10:01:01 -04:00
}
2024-12-17 09:33:47 -05:00
if !server.HasPermission(c) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
streamId, err := uuid.GenerateUUID()
if err != nil {
return nil, err
}
rpc.NezhaHandlerSingleton.CreateStream(streamId)
2024-10-22 10:01:01 -04:00
terminalData, _ := utils.Json.Marshal(&model.TerminalTask{
StreamID: streamId,
})
if err := server.TaskStream.Send(&proto.Task{
Type: model.TaskTypeTerminalGRPC,
Data: string(terminalData),
}); err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-22 10:01:01 -04:00
}
2024-10-23 05:56:51 -04:00
return &model.CreateTerminalResponse{
SessionID: streamId,
ServerID: server.ID,
ServerName: server.Name,
}, nil
2024-10-22 10:01:01 -04:00
}
// TerminalStream web ssh terminal stream
// @Summary Terminal stream
// @Description Terminal stream
// @Tags auth required
2024-10-24 21:09:08 -04:00
// @Param id path string true "Stream UUID"
// @Success 200 {object} model.CommonResponse[any]
2024-10-24 21:09:08 -04:00
// @Router /ws/terminal/{id} [get]
2024-10-23 05:56:51 -04:00
func terminalStream(c *gin.Context) (any, error) {
2024-10-22 10:01:01 -04:00
streamId := c.Param("id")
if _, err := rpc.NezhaHandlerSingleton.GetStream(streamId); err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-22 10:01:01 -04:00
}
defer rpc.NezhaHandlerSingleton.CloseStream(streamId)
wsConn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return nil, newWsError("%v", err)
2024-10-22 10:01:01 -04:00
}
defer wsConn.Close()
conn := websocketx.NewConn(wsConn)
go func() {
// PING 保活
for {
if err = conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return
}
time.Sleep(time.Second * 10)
}
}()
if err = rpc.NezhaHandlerSingleton.UserConnected(streamId, conn); err != nil {
return nil, newWsError("%v", err)
2024-10-22 10:01:01 -04:00
}
if err = rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10); err != nil {
return nil, newWsError("%v", err)
}
return nil, newWsError("")
2024-10-22 10:01:01 -04:00
}