-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnels.ts
335 lines (304 loc) · 10.6 KB
/
tunnels.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import type { KubernetesTunnel } from "./deps.ts";
export type TerminalSize = {
columns: number;
rows: number;
}
export type ExecStatus = {
status: 'Success' | 'Failure';
exitCode?: number; // We add this ourselves
message?: string;
reason?: string;
code?: number;
details?: { causes: Array<{
reason: string;
message: string;
}> };
}
export class StdioTunnel {
static readonly supportedProtocols = [
// proposed v5: adds stdin closing
'v4.channel.k8s.io', // v4: switches error stream to JSON
'v3.channel.k8s.io', // v3: adds terminal resizing
// 'v2.channel.k8s.io',
// 'channel.k8s.io',
];
constructor(
private readonly tunnel: KubernetesTunnel,
originalParams: URLSearchParams,
) {
if (this.tunnel.subProtocol.startsWith('v')) {
this.channelVersion = parseInt(this.tunnel.subProtocol.slice(1));
} else {
this.channelVersion = 1;
}
function getWritable(streamIndex: number, streamType: string) {
return tunnel.getChannel({
streamIndex,
spdyHeaders: { streamType },
readable: false,
writable: true,
}).then(x => x.writable);
}
function getReadable(streamIndex: number, streamType: string) {
return tunnel.getChannel({
streamIndex,
spdyHeaders: { streamType },
readable: true,
writable: false,
}).then(x => x.readable);
}
this.ready = Promise.all([
originalParams.get('stdin') == '1' ? getWritable(0, 'stdin') : null,
originalParams.get('stdout') == '1' ? getReadable(1, 'stdout') : null,
originalParams.get('stderr') == '1' ? getReadable(2, 'stderr') : null,
getReadable(3, 'error'),
(originalParams.get('tty') == '1' && this.channelVersion >= 3) ? getWritable(4, 'resize') : null,
]).then(streams => {
this.#stdin = streams[0];
this.#stdout = streams[1];
this.#stderr = streams[2];
this.status = new Response(streams[3]).text()
.then<ExecStatus>(text => this.channelVersion >= 4
? addExitCode(JSON.parse(text))
: { status: 'Failure', message: text });
this.#resize = streams[4] ? wrapResizeStream(streams[4]) : null;
if (this.#stdin && originalParams.get('tty') == '1') {
const writer = this.#stdinWriter = this.#stdin.getWriter();
// TODO: check if this breaks stdin backpressure
this.#stdin = new WritableStream({
write(chunk) { return writer.write(chunk); },
abort(reason) { return writer.abort(reason); },
close() { return writer.close(); },
});
}
return tunnel.ready();
});
}
readonly channelVersion: number;
#stdin: WritableStream<Uint8Array> | null = null;
#stdinWriter: WritableStreamDefaultWriter<Uint8Array> | null = null;
#stdout: ReadableStream<Uint8Array> | null = null;
#stderr: ReadableStream<Uint8Array> | null = null;
#resize: WritableStream<TerminalSize> | null = null;
status!: Promise<ExecStatus>;
ready: Promise<void>;
// Take a cue from Deno.Command which presents the streams unconditionally
// but throws if they are used when not configured
get stdin(): WritableStream<Uint8Array> {
if (!this.#stdin) throw new TypeError("stdin was not requested");
return this.#stdin;
}
get stdout(): ReadableStream<Uint8Array> {
if (!this.#stdout) throw new TypeError("stdout was not requested");
return this.#stdout;
}
get stderr(): ReadableStream<Uint8Array> {
if (!this.#stderr) throw new TypeError("stderr was not requested");
return this.#stderr;
}
/** If tty was requested, an outbound stream for dynamically changing the TTY dimensions */
get ttyResizeStream(): WritableStream<TerminalSize> {
if (!this.#resize) throw new TypeError("tty was not requested");
return this.#resize;
}
/** Shorthand for injecting Ctrl-C and others when running an interactive TTY */
async ttyWriteSignal(signal: 'INTR' | 'QUIT' | 'SUSP'): Promise<void> {
if (!this.#stdinWriter) throw new TypeError("tty and stdin were not requested together, cannot write signals");
switch (signal) {
// via https://man7.org/linux/man-pages/man3/termios.3.html
case 'INTR': await this.#stdinWriter.write(new Uint8Array([0o003])); break;
case 'QUIT': await this.#stdinWriter.write(new Uint8Array([0o034])); break;
case 'SUSP': await this.#stdinWriter.write(new Uint8Array([0o032])); break;
default: throw new TypeError(`cannot write unrecognized signal ${signal}`);
}
}
/** Shorthand for writing to the tty resize stream, especially useful for setting an initial size */
async ttySetSize(size: TerminalSize): Promise<void> {
const sizeWriter = this.ttyResizeStream.getWriter();
await sizeWriter.write(size);
sizeWriter.releaseLock();
}
// Based on https://github.com/denoland/deno/blob/ca9ba87d9956e3f940e0116866e19461f008390b/runtime/js/40_process.js#L282C1-L319C1
/** Buffers all data for stdout and/or stderr and returns the buffers when the remote command exits. */
async output(): Promise<ExecStatus & {
stdout: Uint8Array;
stderr: Uint8Array;
}> {
if (this.#stdout?.locked) {
throw new TypeError(
"Can't collect output because stdout is locked",
);
}
if (this.#stderr?.locked) {
throw new TypeError(
"Can't collect output because stderr is locked",
);
}
const [ status, stdout, stderr ] = await Promise.all([
this.status,
this.#stdout ? new Response(this.#stdout).arrayBuffer().then(x => new Uint8Array(x)) : null,
this.#stderr ? new Response(this.#stderr).arrayBuffer().then(x => new Uint8Array(x)) : null,
]);
return {
...status,
get stdout() {
if (!stdout) throw new TypeError("stdout was not requested");
return stdout;
},
get stderr() {
if (!stderr) throw new TypeError("stderr was not requested");
return stderr;
},
};
}
/** Immediately disconnects the network tunnel, likely dropping any in-flight data. */
kill(): void {
this.tunnel.stop();
}
}
function wrapResizeStream(byteStream: WritableStream<Uint8Array>) {
const writer = byteStream.getWriter();
return new WritableStream<TerminalSize>({
async abort(reason) {
await writer.abort(reason);
},
async close() {
await writer.close();
},
async write(chunk) {
const bytes = new TextEncoder().encode(JSON.stringify({
Width: chunk.columns,
Height: chunk.rows,
}));
await writer.write(bytes);
},
});
}
// Process exit code, if any, is given as a 'cause' for the non-success
function addExitCode(status: ExecStatus) {
if (status.status == 'Success') {
status.exitCode = 0;
} else if (status.reason == 'NonZeroExitCode') {
const cause = status.details?.causes.find(x => x.reason == 'ExitCode');
if (cause) {
status.exitCode = parseInt(cause.message);
}
}
return status;
}
export class PortforwardTunnel {
static readonly supportedProtocols = [
// Apparently this differs between SPDY and WebSocket.
// TODO: get to the bottom of that.
'portforward.k8s.io', // SPDY - dynamic multi-port multiplexing
'v4.channel.k8s.io', // WebSocket - static based on 'ports' param
];
constructor(
private tunnel: KubernetesTunnel,
originalParams: URLSearchParams,
) {
if (tunnel.subProtocol !== 'portforward.k8s.io') {
const requestedPorts = originalParams.getAll('ports').map(x => parseInt(x));
if (requestedPorts.length > 0) {
this.remainingPorts = requestedPorts;
} else {
tunnel.stop();
throw new Error(`Kubernetes PortForwarding can only use WebSocket with a predefined list of ports. For dynamic multiplexing, try a SPDY client instead.`);
}
}
}
private nextRequestId = 0;
private remainingPorts = new Array<number | false>;
get ready(): Promise<void> {
return this.tunnel.ready();
}
async connectToPort(port: number): Promise<{
result: Promise<null>;
readable: ReadableStream<Uint8Array>;
writable: WritableStream<Uint8Array>;
}> {
let dataIdx: number | undefined = undefined;
let errorIdx: number | undefined = undefined;
if (this.remainingPorts) {
const nextAvailableIdx = this.remainingPorts.findIndex(x => x == port);
if (nextAvailableIdx < 0) throw new Error(`No remaining pre-established connections to port ${port}.`);
this.remainingPorts[nextAvailableIdx] = false;
dataIdx = nextAvailableIdx*2;
errorIdx = dataIdx + 1;
}
const requestID = this.nextRequestId++;
const [dataStream, errorStream] = await Promise.all([
this.tunnel.getChannel({
spdyHeaders: {
streamType: 'data',
port,
requestID,
},
streamIndex: dataIdx,
readable: true,
writable: true,
}),
this.tunnel.getChannel({
spdyHeaders: {
streamType: 'error',
port,
requestID,
},
streamIndex: errorIdx,
writable: false,
readable: true,
}),
]);
// console.error('Got streams');
const errorBody = new Response(errorStream.readable.pipeThrough(new DropPrefix(port))).text();
return {
result: errorBody.then(x => x
? Promise.reject(new Error(`Portforward stream failed. ${x}`))
: null),
readable: dataStream.readable.pipeThrough(new DropPrefix(port)),
writable: dataStream.writable,
};
}
servePortforward(opts: Deno.ListenOptions & {
targetPort: number;
}): Deno.Listener {
const listener = Deno.listen(opts);
(async () => {
for await (const downstream of listener) {
const upstream = await this.connectToPort(opts.targetPort);
console.error(`Client connection opened to ${opts.targetPort}`);
Promise.all([
upstream.result,
downstream.readable.pipeThrough(upstream).pipeTo(downstream.writable),
]).then(x => {
console.error('Client connection closed.', x[0]);
}).catch(err => {
console.error('Client connection crashed:', err.stack);
});
}
})();
return listener;
}
disconnect(): void {
this.tunnel.stop();
}
}
class DropPrefix extends TransformStream<Uint8Array, Uint8Array> {
constructor(_expectedPort: number) {
let remaining = 2;
super({
transform(chunk, ctlr) {
if (remaining > 0) {
if (chunk.byteLength > remaining) {
ctlr.enqueue(chunk.slice(remaining));
remaining = 0;
} else {
remaining -= chunk.byteLength;
}
} else {
ctlr.enqueue(chunk);
}
},
});
}
}