-
Notifications
You must be signed in to change notification settings - Fork 8
/
terminal.go
153 lines (133 loc) · 3.68 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package terminal
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/pytimer/k8sutil/wsremotecommand"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/klog/v2"
)
type ExecutorType string
const (
// EndOfTransmission end
EndOfTransmission = "\u0004"
WebsocketExecutorType ExecutorType = "websocket"
SPDYExecutorType ExecutorType = "spdy"
)
var upgrader = websocket.Upgrader{
HandshakeTimeout: 10 * time.Second,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type ExecOptions struct {
Stdin bool
Stdout bool
Stderr bool
TTY bool
Executor ExecutorType
}
type TerminalSession struct {
wsConn *websocket.Conn
sizeChan chan remotecommand.TerminalSize
doneChan chan struct{}
client kubernetes.Interface
once sync.Once
}
func NewTerminalSession(c kubernetes.Interface, w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*TerminalSession, error) {
subprotocols := websocket.Subprotocols(r)
conn, err := upgrader.Upgrade(w, r, responseHeader)
if err != nil {
return nil, err
}
upgrader.Subprotocols = subprotocols
return &TerminalSession{
wsConn: conn,
sizeChan: make(chan remotecommand.TerminalSize),
doneChan: make(chan struct{}),
client: c,
}, nil
}
func (t *TerminalSession) Close() error {
return t.wsConn.Close()
}
func (t *TerminalSession) Done() {
t.once.Do(func() {
close(t.doneChan)
})
}
// watchAndCloseRemoteExecutor close container websocket process when TerminalSession have done.
func (t *TerminalSession) watchAndCloseRemoteExecutor(executor *wsremotecommand.Executor) {
klog.V(5).Info("listen terminal session stream.")
select {
case <-t.doneChan:
klog.V(5).Info("executor closing...")
klog.V(5).Info(executor.Close())
}
}
func (t *TerminalSession) Read(p []byte) (n int, err error) {
mt, message, err := t.wsConn.ReadMessage()
if err != nil {
klog.Errorf("read message err: %v", err)
return copy(p, EndOfTransmission), err
}
klog.V(8).Info(mt, string(message))
var msg TerminalMessage
if err := json.Unmarshal([]byte(message), &msg); err != nil {
return copy(p, EndOfTransmission), err
}
switch msg.Op {
case "stdin":
return copy(p, msg.Data), nil
case "resize":
t.sizeChan <- remotecommand.TerminalSize{Width: msg.Cols, Height: msg.Rows}
return 0, nil
case "ping":
klog.Info("ping")
return 0, nil
}
return copy(p, EndOfTransmission), fmt.Errorf("unknown message type '%s'", msg.Op)
}
func (t *TerminalSession) Write(p []byte) (n int, err error) {
msg, err := json.Marshal(TerminalMessage{
Op: "stdout",
Data: string(p),
})
if err != nil {
klog.Errorf("parse write message err: %v", err)
return 0, err
}
klog.V(8).Info(string(p))
if err := t.wsConn.WriteMessage(websocket.TextMessage, msg); err != nil {
klog.Errorf("write message err: %v", err)
return 0, err
}
return len(p), nil
}
func (t *TerminalSession) Next() *remotecommand.TerminalSize {
select {
case size := <-t.sizeChan:
klog.V(8).Infof("resize container terminal: %#v", size)
return &size
case <-t.doneChan:
return nil
}
}
// TerminalMessage is the messaging protocol between ShellController and TerminalSession.
//
// OP DIRECTION FIELD(S) USED DESCRIPTION
// ---------------------------------------------------------------------
// bind fe->be SessionID Id sent back from TerminalResponse
// stdin fe->be Data Keystrokes/paste buffer
// resize fe->be Rows, Cols New terminal size
// stdout be->fe Data Output from the process
type TerminalMessage struct {
Op string `json:"op"`
Data string `json:"data"`
Rows uint16 `json:"rows"`
Cols uint16 `json:"cols"`
}