2019-02-15 15:27:03 +00:00
|
|
|
/*
|
2019-02-20 16:48:15 +00:00
|
|
|
Copyright 2019 Balena
|
2019-02-15 15:27:03 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-02-27 12:38:50 +00:00
|
|
|
import type { BalenaSDK } from 'balena-sdk';
|
2019-02-15 15:27:03 +00:00
|
|
|
import { Socket } from 'net';
|
2021-01-19 08:22:20 +00:00
|
|
|
import * as tls from 'tls';
|
2019-02-20 16:48:15 +00:00
|
|
|
import { TypedError } from 'typed-error';
|
2021-01-19 08:22:20 +00:00
|
|
|
import { ExpectedError } from '../errors';
|
2019-02-15 15:27:03 +00:00
|
|
|
|
2019-02-20 16:48:15 +00:00
|
|
|
const PROXY_CONNECT_TIMEOUT_MS = 10000;
|
|
|
|
|
2021-01-19 08:22:20 +00:00
|
|
|
class TunnelServerNotTrustedError extends ExpectedError {}
|
|
|
|
|
2019-02-20 16:48:15 +00:00
|
|
|
class UnableToConnectError extends TypedError {
|
2019-03-12 22:07:57 +00:00
|
|
|
public status: string;
|
|
|
|
public statusCode: string;
|
2019-02-20 16:48:15 +00:00
|
|
|
constructor(statusCode: string, status: string) {
|
|
|
|
super(`Unable to connect: ${statusCode} ${status}`);
|
|
|
|
this.status = status;
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemoteSocketNotListening extends TypedError {
|
2019-03-12 22:07:57 +00:00
|
|
|
public port: number;
|
2019-02-20 16:48:15 +00:00
|
|
|
constructor(port: number) {
|
|
|
|
super(`Device is not listening on port ${port}`);
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 15:27:03 +00:00
|
|
|
|
|
|
|
export const tunnelConnectionToDevice = (
|
|
|
|
uuid: string,
|
|
|
|
port: number,
|
|
|
|
sdk: BalenaSDK,
|
|
|
|
) => {
|
2020-07-01 14:26:40 +00:00
|
|
|
return Promise.all([
|
2021-01-19 08:22:20 +00:00
|
|
|
sdk.settings.get('tunnelUrl'),
|
2020-07-01 14:26:40 +00:00
|
|
|
sdk.auth.whoami(),
|
|
|
|
sdk.auth.getToken(),
|
2021-01-19 08:22:20 +00:00
|
|
|
]).then(([tunnelUrl, whoami, token]) => {
|
2019-02-15 15:27:03 +00:00
|
|
|
const auth = {
|
|
|
|
user: whoami || 'root',
|
|
|
|
password: token,
|
|
|
|
};
|
|
|
|
|
2020-06-30 20:24:35 +00:00
|
|
|
return (client: Socket): Promise<void> =>
|
2021-01-19 08:22:20 +00:00
|
|
|
openPortThroughProxy(tunnelUrl, 443, auth, uuid, port)
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((remote) => {
|
2019-02-15 15:27:03 +00:00
|
|
|
client.pipe(remote);
|
|
|
|
remote.pipe(client);
|
2020-06-15 22:53:07 +00:00
|
|
|
remote.on('error', (err) => {
|
2019-02-15 15:27:03 +00:00
|
|
|
console.error('Remote: ' + err);
|
|
|
|
client.end();
|
|
|
|
});
|
2020-06-15 22:53:07 +00:00
|
|
|
client.on('error', (err) => {
|
2019-02-15 15:27:03 +00:00
|
|
|
console.error('Client: ' + err);
|
|
|
|
remote.end();
|
|
|
|
});
|
|
|
|
remote.on('close', () => {
|
|
|
|
client.end();
|
|
|
|
});
|
|
|
|
client.on('close', () => {
|
|
|
|
remote.end();
|
|
|
|
});
|
|
|
|
})
|
2020-06-30 20:24:35 +00:00
|
|
|
.catch((e) => {
|
2019-02-15 15:27:03 +00:00
|
|
|
client.end();
|
2020-06-30 20:32:11 +00:00
|
|
|
throw e;
|
2019-02-15 15:27:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const openPortThroughProxy = (
|
|
|
|
proxyServer: string,
|
|
|
|
proxyPort: number,
|
|
|
|
proxyAuth: { user: string; password: string } | null,
|
|
|
|
deviceUuid: string,
|
|
|
|
devicePort: number,
|
|
|
|
) => {
|
|
|
|
const httpHeaders = [`CONNECT ${deviceUuid}.balena:${devicePort} HTTP/1.0`];
|
|
|
|
|
|
|
|
if (proxyAuth !== null) {
|
|
|
|
const credentials = Buffer.from(
|
|
|
|
`${proxyAuth.user}:${proxyAuth.password}`,
|
|
|
|
).toString('base64');
|
|
|
|
|
|
|
|
httpHeaders.push(`Proxy-Authorization: Basic ${credentials}`);
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:24:35 +00:00
|
|
|
return new Promise<Socket>((resolve, reject) => {
|
2021-01-19 08:22:20 +00:00
|
|
|
const proxyTunnel = tls.connect(
|
|
|
|
proxyPort,
|
|
|
|
proxyServer,
|
|
|
|
{ servername: proxyServer }, // send the hostname in the SNI field
|
|
|
|
() => {
|
|
|
|
if (!proxyTunnel.authorized) {
|
|
|
|
console.error('Unable to authorize the tunnel server');
|
2019-02-20 16:48:15 +00:00
|
|
|
reject(
|
2021-01-19 08:22:20 +00:00
|
|
|
new TunnelServerNotTrustedError(proxyTunnel.authorizationError),
|
2019-02-15 15:27:03 +00:00
|
|
|
);
|
2021-01-19 08:22:20 +00:00
|
|
|
return;
|
2019-02-15 15:27:03 +00:00
|
|
|
}
|
2021-01-19 08:22:20 +00:00
|
|
|
|
|
|
|
proxyTunnel.once('data', (data: Buffer) => {
|
|
|
|
const [httpStatus] = data.toString('utf8').split('\r\n');
|
|
|
|
const [, httpStatusCode, ...httpMessage] = httpStatus.split(' ');
|
|
|
|
|
|
|
|
if (parseInt(httpStatusCode, 10) === 200) {
|
|
|
|
proxyTunnel.setTimeout(0);
|
|
|
|
resolve(proxyTunnel);
|
|
|
|
} else {
|
|
|
|
reject(
|
|
|
|
new UnableToConnectError(httpStatusCode, httpMessage.join(' ')),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
proxyTunnel.on('timeout', () => {
|
|
|
|
reject(new RemoteSocketNotListening(devicePort));
|
|
|
|
});
|
|
|
|
|
|
|
|
proxyTunnel.setTimeout(PROXY_CONNECT_TIMEOUT_MS);
|
|
|
|
proxyTunnel.write(httpHeaders.join('\r\n').concat('\r\n\r\n'));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
proxyTunnel.on('error', reject);
|
2019-02-15 15:27:03 +00:00
|
|
|
});
|
|
|
|
};
|