forked from Jrohy/webssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.go
55 lines (51 loc) · 1.29 KB
/
terminal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package controller
import (
"fmt"
"net/http"
"strconv"
"time"
"webssh/core"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// TermWs 获取终端ws
func TermWs(c *gin.Context, timeout time.Duration) *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
sshInfo := c.DefaultQuery("sshInfo", "")
cols := c.DefaultQuery("cols", "150")
rows := c.DefaultQuery("rows", "35")
closeTip := c.DefaultQuery("closeTip", "Connection timed out!")
col, _ := strconv.Atoi(cols)
row, _ := strconv.Atoi(rows)
sshClient, err := core.DecodedMsgToSSHClient(sshInfo)
if err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
wsConn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
err = sshClient.GenerateClient()
if err != nil {
wsConn.WriteMessage(1, []byte(err.Error()))
wsConn.Close()
fmt.Println(err)
responseBody.Msg = err.Error()
return &responseBody
}
sshClient.InitTerminal(wsConn, row, col)
sshClient.Connect(wsConn, timeout, closeTip)
return &responseBody
}