2022-12-29 17:06:04 +08:00
|
|
|
package terminal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalWsSession struct {
|
|
|
|
slave *LocalCommand
|
|
|
|
wsConn *websocket.Conn
|
|
|
|
|
2024-02-01 14:33:41 +08:00
|
|
|
allowCtrlC bool
|
2022-12-29 17:06:04 +08:00
|
|
|
writeMutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
2024-02-01 14:33:41 +08:00
|
|
|
func NewLocalWsSession(cols, rows int, wsConn *websocket.Conn, slave *LocalCommand, allowCtrlC bool) (*LocalWsSession, error) {
|
2022-12-29 17:06:04 +08:00
|
|
|
if err := slave.ResizeTerminal(cols, rows); err != nil {
|
|
|
|
global.LOG.Errorf("ssh pty change windows size failed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LocalWsSession{
|
|
|
|
slave: slave,
|
|
|
|
wsConn: wsConn,
|
2024-02-01 14:33:41 +08:00
|
|
|
|
|
|
|
allowCtrlC: allowCtrlC,
|
2022-12-29 17:06:04 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sws *LocalWsSession) Start(quitChan chan bool) {
|
|
|
|
go sws.handleSlaveEvent(quitChan)
|
|
|
|
go sws.receiveWsMsg(quitChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sws *LocalWsSession) handleSlaveEvent(exitCh chan bool) {
|
|
|
|
defer setQuit(exitCh)
|
2023-02-24 18:49:34 +08:00
|
|
|
defer global.LOG.Debug("thread of handle slave event has exited now")
|
2022-12-29 17:06:04 +08:00
|
|
|
|
|
|
|
buffer := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-exitCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
n, _ := sws.slave.Read(buffer)
|
|
|
|
_ = sws.masterWrite(buffer[:n])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sws *LocalWsSession) masterWrite(data []byte) error {
|
|
|
|
sws.writeMutex.Lock()
|
|
|
|
defer sws.writeMutex.Unlock()
|
2023-04-06 16:06:09 +08:00
|
|
|
wsData, err := json.Marshal(WsMsg{
|
|
|
|
Type: WsMsgCmd,
|
|
|
|
Data: base64.StdEncoding.EncodeToString(data),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to encoding to json")
|
|
|
|
}
|
|
|
|
err = sws.wsConn.WriteMessage(websocket.TextMessage, wsData)
|
2022-12-29 17:06:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to write to master")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sws *LocalWsSession) receiveWsMsg(exitCh chan bool) {
|
|
|
|
wsConn := sws.wsConn
|
|
|
|
defer setQuit(exitCh)
|
2023-02-24 18:49:34 +08:00
|
|
|
defer global.LOG.Debug("thread of receive ws msg has exited now")
|
2022-12-29 17:06:04 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-exitCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
_, wsData, err := wsConn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("reading webSocket message failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-04-06 16:06:09 +08:00
|
|
|
msgObj := WsMsg{}
|
2022-12-29 17:06:04 +08:00
|
|
|
_ = json.Unmarshal(wsData, &msgObj)
|
|
|
|
switch msgObj.Type {
|
2023-04-06 16:06:09 +08:00
|
|
|
case WsMsgResize:
|
2022-12-29 17:06:04 +08:00
|
|
|
if msgObj.Cols > 0 && msgObj.Rows > 0 {
|
|
|
|
if err := sws.slave.ResizeTerminal(msgObj.Cols, msgObj.Rows); err != nil {
|
|
|
|
global.LOG.Errorf("ssh pty change windows size failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 16:06:09 +08:00
|
|
|
case WsMsgCmd:
|
2023-04-06 02:17:40 +08:00
|
|
|
decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Data)
|
2022-12-29 17:06:04 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("websock cmd string base64 decoding failed, err: %v", err)
|
|
|
|
}
|
2024-02-01 14:33:41 +08:00
|
|
|
if string(decodeBytes) != "\x03" || sws.allowCtrlC {
|
|
|
|
sws.sendWebsocketInputCommandToSshSessionStdinPipe(decodeBytes)
|
|
|
|
}
|
2023-04-06 16:06:09 +08:00
|
|
|
case WsMsgHeartbeat:
|
|
|
|
err = wsConn.WriteMessage(websocket.TextMessage, wsData)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("ssh sending heartbeat to webSocket failed, err: %v", err)
|
|
|
|
}
|
2022-12-29 17:06:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sws *LocalWsSession) sendWebsocketInputCommandToSshSessionStdinPipe(cmdBytes []byte) {
|
|
|
|
if _, err := sws.slave.Write(cmdBytes); err != nil {
|
|
|
|
global.LOG.Errorf("ws cmd bytes write to ssh.stdin pipe failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|