58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
|
||
"next-terminal/server/guacd"
|
||
"next-terminal/server/log"
|
||
"next-terminal/server/utils"
|
||
|
||
"github.com/gorilla/websocket"
|
||
)
|
||
|
||
type GuacamoleHandler struct {
|
||
ws *websocket.Conn
|
||
tunnel *guacd.Tunnel
|
||
ctx context.Context
|
||
cancel context.CancelFunc
|
||
}
|
||
|
||
func NewGuacamoleHandler(ws *websocket.Conn, tunnel *guacd.Tunnel) *GuacamoleHandler {
|
||
ctx, cancel := context.WithCancel(context.Background())
|
||
return &GuacamoleHandler{
|
||
ws: ws,
|
||
tunnel: tunnel,
|
||
ctx: ctx,
|
||
cancel: cancel,
|
||
}
|
||
}
|
||
|
||
func (r GuacamoleHandler) Start() {
|
||
go func() {
|
||
for {
|
||
select {
|
||
case <-r.ctx.Done():
|
||
return
|
||
default:
|
||
instruction, err := r.tunnel.Read()
|
||
if err != nil {
|
||
utils.Disconnect(r.ws, TunnelClosed, "远程连接已关闭")
|
||
return
|
||
}
|
||
if len(instruction) == 0 {
|
||
continue
|
||
}
|
||
err = r.ws.WriteMessage(websocket.TextMessage, instruction)
|
||
if err != nil {
|
||
log.Debugf("WebSocket写入失败,即将关闭Guacd连接...")
|
||
return
|
||
}
|
||
}
|
||
}
|
||
}()
|
||
}
|
||
|
||
func (r GuacamoleHandler) Stop() {
|
||
r.cancel()
|
||
}
|