-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnassh_stream_relay_websockify.js
203 lines (175 loc) · 5.34 KB
/
nassh_stream_relay_websockify.js
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
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Stream for connecting to a ssh server via a Websocket relay.
*/
import {lib} from '../../libdot/index.js';
import {newBuffer} from './nassh_buffer.js';
import {Stream} from './nassh_stream.js';
/**
* WebSocket backed stream.
*
* This class manages the read and write through WebSocket to communicate
* with the SSH server.
*
* Resuming of connections is not supported.
*/
export class RelayWebsockifyStream extends Stream {
/**
* @param {number} fd
*/
constructor(fd) {
super(fd);
// The relay connection settings.
this.relayHost_ = null;
this.relayPort_ = null;
this.protocol_ = null;
// The remote ssh server settings.
this.host_ = null;
this.port_ = null;
// All the data we've queued but not yet sent out.
this.writeBuffer_ = newBuffer();
// Callback function when asyncWrite is used.
this.onWriteSuccess_ = null;
// The actual WebSocket connected to the ssh server.
this.socket_ = null;
}
/**
* Open a relay socket.
*
* @param {!Object} settings
* @param {function(boolean, string=)} onComplete
* @override
*/
async asyncOpen(settings, onComplete) {
this.relayHost_ = settings.relayHost;
this.relayPort_ = settings.relayPort;
this.host_ = settings.host;
this.port_ = settings.port;
this.protocol_ = settings.protocol;
this.connect_();
onComplete(true);
}
/**
* Start a new connection to the proxy server.
*/
connect_() {
if (this.socket_) {
throw new Error('stream already connected');
}
// Since websockify will usually be running on the same host as the ssh
// server, default the relay host/port to the ssh settings.
const uri = lib.f.replaceVars(this.connectTemplate_, {
protocol: this.protocol_,
relayHost: this.relayHost_ ?? this.host_,
relayPort: this.relayPort_ || this.port_,
});
this.socket_ = new WebSocket(uri);
this.socket_.binaryType = 'arraybuffer';
this.socket_.onopen = this.onSocketOpen_.bind(this);
this.socket_.onmessage = this.onSocketData_.bind(this);
this.socket_.onclose = this.onSocketClose_.bind(this);
this.socket_.onerror = this.onSocketError_.bind(this);
}
/**
* Close the connection to the proxy server and clean up.
*
* @param {string} reason A short message explaining the reason for closing.
*/
close_(reason) {
// If we aren't open, there's nothing to do. This allows us to call it
// multiple times, perhaps from cascading events (write error/close/etc...).
if (!this.socket_) {
return;
}
console.log(`Closing socket due to ${reason}`);
this.socket_.close();
this.socket_ = null;
super.close();
}
/**
* Callback when the socket connects successfully.
*
* @param {!Event} e The event details.
*/
onSocketOpen_(e) {
// If we had any pending writes, kick them off. We can't call sendWrite
// directly as the socket isn't in the correct state until after this
// handler finishes executing.
setTimeout(this.sendWrite_.bind(this), 0);
}
/**
* Callback when the socket closes when the connection is finished.
*
* @param {!CloseEvent} e The event details.
*/
onSocketClose_(e) {
this.close_(`server closed socket: [${e.code}] ${e.reason}`);
}
/**
* Callback when the socket closes due to an error.
*
* @param {!Event} e The event details.
*/
onSocketError_(e) {
this.close_(`server sent an error: ${e}`);
}
/**
* Callback when new data is available from the server.
*
* @param {!MessageEvent} e The message with data to read.
*/
onSocketData_(e) {
this.onDataAvailable(e.data);
}
/**
* Queue up some data to write asynchronously.
*
* @param {!ArrayBuffer} data The SSH data.
* @param {function(number)=} onSuccess Optional callback.
* @override
*/
asyncWrite(data, onSuccess) {
if (!data.byteLength) {
return;
}
this.writeBuffer_.write(data);
this.onWriteSuccess_ = onSuccess;
this.sendWrite_();
}
/**
* Send out any queued data.
*/
sendWrite_() {
if (!this.socket_ || this.socket_.readyState != 1 ||
this.writeBuffer_.isEmpty()) {
// Nothing to write or socket is not ready.
return;
}
// If we've queued too much already, go back to sleep.
// NB: This check is fuzzy at best, so we don't need to include the size of
// the data we're about to write below into the calculation.
if (this.socket_.bufferedAmount >= this.maxWebSocketBufferLength) {
setTimeout(this.sendWrite_.bind(this));
return;
}
const buf = this.writeBuffer_.read(this.maxWebSocketBufferLength);
const size = buf.length;
this.socket_.send(buf);
this.writeBuffer_.ack(size);
if (this.onWriteSuccess_) {
// Notify nassh that we are ready to consume more data.
this.onWriteSuccess_(size);
}
if (!this.writeBuffer_.isEmpty()) {
// We have more data to send but due to message limit we didn't send it.
setTimeout(this.sendWrite_.bind(this), 0);
}
}
}
/**
* URI to establish a new connection to the ssh server via the relay.
*/
RelayWebsockifyStream.prototype.connectTemplate_ =
'%(protocol)://%(relayHost):%(relayPort)';