forked from gravitational/teleport
/
proto.go
285 lines (228 loc) · 7.26 KB
/
proto.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package streamproto
import (
"io"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/remotecommand"
"github.com/zmb3/teleport/api/types"
"github.com/zmb3/teleport/lib/utils"
)
// metaMessage is a control message containing one or more payloads.
type metaMessage struct {
Resize *remotecommand.TerminalSize `json:"resize,omitempty"`
ForceTerminate bool `json:"force_terminate,omitempty"`
ClientHandshake *ClientHandshake `json:"client_handshake,omitempty"`
ServerHandshake *ServerHandshake `json:"server_handshake,omitempty"`
}
// ClientHandshake is the first message sent by a client to inform a server of it's intentions.
type ClientHandshake struct {
Mode types.SessionParticipantMode `json:"mode"`
}
// ServerHandshake is the first message sent by a server to inform a client of the session settings.
type ServerHandshake struct {
MFARequired bool `json:"mfa_required"`
}
// SessionStream represents one end of the bidirectional session connection.
type SessionStream struct {
// The underlying websocket connection.
conn *websocket.Conn
// A stream of incoming session packets.
in chan []byte
// Optionally contains a partially read session packet.
currentIn []byte
// A list of resize requests.
resizeQueue chan *remotecommand.TerminalSize
// A notification channel for force termination requests.
forceTerminate chan struct{}
writeSync sync.Mutex
done chan struct{}
closeOnce sync.Once
closed int32
MFARequired bool
Mode types.SessionParticipantMode
}
// NewSessionStream creates a new session stream.
// The type of the handshake parameter determines if this is the client or server end.
func NewSessionStream(conn *websocket.Conn, handshake interface{}) (*SessionStream, error) {
s := &SessionStream{
conn: conn,
in: make(chan []byte),
done: make(chan struct{}),
resizeQueue: make(chan *remotecommand.TerminalSize, 1),
forceTerminate: make(chan struct{}),
}
clientHandshake, isClient := handshake.(ClientHandshake)
serverHandshake, ok := handshake.(ServerHandshake)
if !isClient && !ok {
return nil, trace.BadParameter("Handshake must be either client or server handshake, got %T", handshake)
}
if isClient {
ty, data, err := conn.ReadMessage()
if err != nil {
return nil, trace.Wrap(err)
}
if ty != websocket.TextMessage {
return nil, trace.Errorf("Expected websocket control message, got %v", ty)
}
var msg metaMessage
if err := utils.FastUnmarshal(data, &msg); err != nil {
return nil, trace.Wrap(err)
}
if msg.ServerHandshake == nil {
return nil, trace.Errorf("Expected websocket server handshake, got %v", msg)
}
s.MFARequired = msg.ServerHandshake.MFARequired
handshakeMsg := metaMessage{ClientHandshake: &clientHandshake}
dataClientHandshake, err := utils.FastMarshal(handshakeMsg)
if err != nil {
return nil, trace.Wrap(err)
}
if err := conn.WriteMessage(websocket.TextMessage, dataClientHandshake); err != nil {
return nil, trace.Wrap(err)
}
} else {
handshakeMsg := metaMessage{ServerHandshake: &serverHandshake}
dataServerHandshake, err := utils.FastMarshal(handshakeMsg)
if err != nil {
return nil, trace.Wrap(err)
}
if err := conn.WriteMessage(websocket.TextMessage, dataServerHandshake); err != nil {
return nil, trace.Wrap(err)
}
ty, data, err := conn.ReadMessage()
if err != nil {
return nil, trace.Wrap(err)
}
if ty != websocket.TextMessage {
return nil, trace.Errorf("Expected websocket control message, got %v", ty)
}
var msg metaMessage
if err := utils.FastUnmarshal(data, &msg); err != nil {
return nil, trace.Wrap(err)
}
if msg.ClientHandshake == nil {
return nil, trace.Errorf("Expected websocket client handshake")
}
s.Mode = msg.ClientHandshake.Mode
}
go s.readTask()
return s, nil
}
func (s *SessionStream) readTask() {
for {
defer s.closeOnce.Do(func() { close(s.done) })
ty, data, err := s.conn.ReadMessage()
if err != nil {
if err != io.EOF && !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseAbnormalClosure, websocket.CloseNoStatusReceived) {
log.WithError(err).Warn("Failed to read message from websocket")
}
return
}
if ty == websocket.BinaryMessage {
s.in <- data
}
if ty == websocket.TextMessage {
var msg metaMessage
if err := utils.FastUnmarshal(data, &msg); err != nil {
return
}
if msg.Resize != nil {
s.resizeQueue <- msg.Resize
}
if msg.ForceTerminate {
close(s.forceTerminate)
}
}
if ty == websocket.CloseMessage {
s.conn.Close()
atomic.StoreInt32(&s.closed, 1)
return
}
}
}
func (s *SessionStream) Read(p []byte) (int, error) {
if len(s.currentIn) == 0 {
select {
case s.currentIn = <-s.in:
case <-s.done:
return 0, io.EOF
}
}
n := copy(p, s.currentIn)
s.currentIn = s.currentIn[n:]
return n, nil
}
func (s *SessionStream) Write(data []byte) (int, error) {
s.writeSync.Lock()
defer s.writeSync.Unlock()
err := s.conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
return 0, trace.Wrap(err)
}
return len(data), nil
}
// Resize sends a resize request to the other party.
func (s *SessionStream) Resize(size *remotecommand.TerminalSize) error {
msg := metaMessage{Resize: size}
json, err := utils.FastMarshal(msg)
if err != nil {
return trace.Wrap(err)
}
s.writeSync.Lock()
defer s.writeSync.Unlock()
return trace.Wrap(s.conn.WriteMessage(websocket.TextMessage, json))
}
// ResizeQueue returns a channel that will receive resize requests.
func (s *SessionStream) ResizeQueue() <-chan *remotecommand.TerminalSize {
return s.resizeQueue
}
// ForceTerminateQueue returns the channel used for force termination requests.
func (s *SessionStream) ForceTerminateQueue() <-chan struct{} {
return s.forceTerminate
}
// ForceTerminate sends a force termination request to the other end.
func (s *SessionStream) ForceTerminate() error {
msg := metaMessage{ForceTerminate: true}
json, err := utils.FastMarshal(msg)
if err != nil {
return trace.Wrap(err)
}
s.writeSync.Lock()
defer s.writeSync.Unlock()
return trace.Wrap(s.conn.WriteMessage(websocket.TextMessage, json))
}
func (s *SessionStream) Done() <-chan struct{} {
return s.done
}
// Close closes the stream.
func (s *SessionStream) Close() error {
if atomic.LoadInt32(&s.closed) == 0 {
atomic.StoreInt32(&s.closed, 1)
err := s.conn.WriteMessage(websocket.CloseMessage, []byte{})
if err != nil {
log.Warnf("Failed to gracefully close websocket connection: %v", err)
}
select {
case <-s.done:
case <-time.After(time.Second * 5):
s.conn.Close()
}
}
return nil
}