/
port-forward.ts
217 lines (179 loc) · 7.11 KB
/
port-forward.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
import { Flags } from '@oclif/core';
import chalk from 'chalk';
import inquirer from 'inquirer';
import * as net from 'net';
import { Transform } from 'stream';
import WebSocket, { createWebSocketStream } from 'ws';
import { ArchitectError, Dictionary, parseUnknownSlug } from '..';
import Account from '../architect/account/account.entity';
import AccountUtils from '../architect/account/account.utils';
import { EnvironmentUtils, GetEnvironmentOptions, Replica } from '../architect/environment/environment.utils';
import BaseCommand from '../base-command';
import PortUtil from '../common/utils/port';
export default class PortForward extends BaseCommand {
static description = 'Port forward service to localhost';
static usage = 'port-forward [RESOURCE] [FLAGS]';
static examples = [
'architect port-forward',
'architect port-forward --account myaccount --environment myenvironment mycomponent.services.app',
'architect port-forward --account myaccount --environment myenvironment mycomponent.services.app --replica 0',
'architect port-forward --address 0.0.0.0 --port 8080',
];
static flags = {
...BaseCommand.flags,
...AccountUtils.flags,
...EnvironmentUtils.flags,
address: Flags.string({
description: `Addresses to listen on. Only accepts IP addresses or localhost as a value.`,
sensitive: false,
default: 'localhost',
}),
port: Flags.integer({
description: 'The port to listen on for the address provided.',
sensitive: false,
}),
[`target-port`]: Flags.integer({
description: 'The target port for the service.',
sensitive: false,
}),
replica: Flags.integer({
description: `Replica index for service. Only works on remote deploys.`,
char: 'r',
sensitive: false,
min: 0,
}),
};
static args = [{
sensitive: false,
name: 'resource',
description: 'Name of resource',
required: false,
parse: async (value: string): Promise<string> => value.toLowerCase(),
}];
// These values correspond with the values defined in kubernetes remotecommand websocket handling:
// https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/cri/streaming/remotecommand/websocket.go#L30
public static readonly StdinStream = 0;
async portForward(uri: string, socket: net.Socket): Promise<void> {
const ws = await this.getWebSocket(uri);
await new Promise((resolve, reject) => {
const websocket = createWebSocketStream(ws);
const input_transform = new Transform({
transform(chunk, encoding, callback) {
const buff = Buffer.alloc(chunk.length + 1);
buff.writeInt8(PortForward.StdinStream, 0);
if (chunk instanceof Buffer) {
chunk.copy(buff, 1);
} else {
buff.write(chunk, 1);
}
callback(null, buff);
},
});
const capture_port: Dictionary<boolean | undefined> = {
0: true,
1: true,
};
const output_transform = new Transform({
transform(chunk, encoding, callback) {
if (!(chunk instanceof Buffer)) {
callback(new Error('Not a buffer'));
}
const stream_num = chunk.readInt8(0) as number;
chunk = chunk.slice(1);
// First two bytes of each stream are the port number
if (capture_port[stream_num]) {
chunk = chunk.slice(2);
capture_port[stream_num] = false;
return callback();
}
if (stream_num % 2 === 1) {
callback(new ArchitectError(chunk.toString()));
} else {
callback(null, chunk);
}
},
});
socket
.pipe(input_transform)
.pipe(websocket)
.pipe(output_transform)
.pipe(socket);
websocket.on('error', (err) => {
reject(err);
});
});
}
async getWebSocket(uri: string): Promise<WebSocket> {
const auth_result = await this.app.auth.getPersistedTokenJSON();
const protocols = ['v4.channel.k8s.io', 'v3.channel.k8s.io', 'v2.channel.k8s.io', 'channel.k8s.io'];
const headers: Dictionary<string> = {
Authorization: `${auth_result?.token_type} ${auth_result?.access_token}`,
};
const url = new URL(uri);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
// Set HOST header for local dev
if (url.hostname.endsWith('.localhost') && process.env.TEST !== '1') {
headers.HOST = url.hostname;
url.hostname = 'localhost';
}
const ws = new WebSocket(url.toString(), protocols, {
handshakeTimeout: 5000,
timeout: 10000,
headers,
});
return ws;
}
async runRemote(account: Account): Promise<void> {
const { args, flags } = await this.parse(PortForward);
const get_environment_options: GetEnvironmentOptions = { environment_name: flags.environment };
const environment = await EnvironmentUtils.getEnvironment(this.app.api, account, get_environment_options);
let component_name: string | undefined;
let resource_name: string | undefined;
let instance_name: string | undefined;
if (args.resource) {
const parsed = parseUnknownSlug(args.resource);
component_name = parsed.component_name;
resource_name = parsed.resource_name;
instance_name = parsed.instance_name;
}
const replica_query = {
component_name,
component_resource_name: resource_name,
component_instance_name: instance_name,
};
const { data: replicas }: { data: Replica[] } = await this.app.api.get(`/environments/${environment.id}/replicas`, {
params: replica_query,
});
if (replicas.length === 0)
throw new ArchitectError(`No replicas found for ${args.resource ? args.resource : 'environment'}`);
const replica = await EnvironmentUtils.getReplica(replicas, flags.replica);
if (replica.ports.length === 0) {
throw new ArchitectError(`The service does not expose any ports to forward.`);
}
if (flags['target-port'] && !replica.ports.includes(flags['target-port'])) {
throw new ArchitectError(`The target-port=${flags['target-port']} does not exist. Available ports are ${replica.ports}.`);
}
const target_port = flags['target-port'] || replica.ports[0];
const query = new URLSearchParams({
ext_ref: replica.ext_ref,
port: target_port.toString(),
});
const uri = `${this.app.config.api_host}/environments/${environment.id}/ws/port-forward?${query}`;
const server = net.createServer((socket) => {
this.portForward(uri, socket).catch((err) => {
console.error(err.message);
socket.end();
});
});
const port = flags.port || await PortUtil.getAvailablePort(40000);
server.listen(port, flags.address);
this.log(chalk.blue(`Forwarding from ${flags.address}:${port} -> ${target_port}`));
}
async run(): Promise<void> {
// eslint-disable-next-line unicorn/prefer-module
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
const { flags } = await this.parse(PortForward);
const account = await AccountUtils.getAccount(this.app, flags.account);
await this.runRemote(account);
}
}