This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 274
/
term_posix.go
129 lines (116 loc) · 2.93 KB
/
term_posix.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// +build !windows
package main
import (
"encoding/json"
"io"
"net/http"
"os"
"os/exec"
"syscall"
"unsafe"
"github.com/sirupsen/logrus"
"github.com/gorilla/websocket"
"github.com/kr/pty"
)
type windowSize struct {
Rows uint16 `json:"rows"`
Cols uint16 `json:"cols"`
X uint16
Y uint16
}
func lookShellPath() (string, error) {
shPath, err := exec.LookPath("bash")
if err == nil {
return shPath, nil
}
return exec.LookPath("sh")
}
func handleTerminalWebsocket(w http.ResponseWriter, r *http.Request) {
l := logrus.WithField("remoteaddr", r.RemoteAddr)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
l.WithError(err).Error("Unable to upgrade connection")
return
}
shPath, err := lookShellPath()
if err != nil {
l.WithError(err).Error("Unable find shell path")
return
}
cmd := exec.Command(shPath, "-l")
cmd.Env = append(os.Environ(), "TERM=xterm")
tty, err := pty.Start(cmd)
if err != nil {
l.WithError(err).Error("Unable to start pty/cmd")
conn.WriteMessage(websocket.TextMessage, []byte(err.Error()))
return
}
defer func() {
cmd.Process.Kill()
cmd.Process.Wait()
tty.Close()
conn.Close()
}()
go func() {
for {
buf := make([]byte, 1024)
read, err := tty.Read(buf)
if err != nil {
conn.WriteMessage(websocket.TextMessage, []byte(err.Error()))
l.WithError(err).Error("Unable to read from pty/cmd")
return
}
conn.WriteMessage(websocket.BinaryMessage, buf[:read])
}
}()
for {
messageType, reader, err := conn.NextReader()
if err != nil {
l.WithError(err).Error("Unable to grab next reader")
return
}
if messageType == websocket.TextMessage {
l.Warn("Unexpected text message")
conn.WriteMessage(websocket.TextMessage, []byte("Unexpected text message"))
continue
}
dataTypeBuf := make([]byte, 1)
read, err := reader.Read(dataTypeBuf)
if err != nil {
l.WithError(err).Error("Unable to read message type from reader")
conn.WriteMessage(websocket.TextMessage, []byte("Unable to read message type from reader"))
return
}
if read != 1 {
l.WithField("bytes", read).Error("Unexpected number of bytes read")
return
}
switch dataTypeBuf[0] {
case 0:
copied, err := io.Copy(tty, reader)
if err != nil {
l.WithError(err).Errorf("Error after copying %d bytes", copied)
}
case 1:
decoder := json.NewDecoder(reader)
resizeMessage := windowSize{}
err := decoder.Decode(&resizeMessage)
if err != nil {
conn.WriteMessage(websocket.TextMessage, []byte("Error decoding resize message: "+err.Error()))
continue
}
l.WithField("resizeMessage", resizeMessage).Info("Resizing terminal")
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
tty.Fd(),
syscall.TIOCSWINSZ,
uintptr(unsafe.Pointer(&resizeMessage)),
)
if errno != 0 {
l.WithError(syscall.Errno(errno)).Error("Unable to resize terminal")
}
default:
l.WithField("dataType", dataTypeBuf[0]).Error("Unknown data type")
}
}
}