Merge pull request #1905 from balena-io/async-tunnel

Convert lib/actions-oclif/tunnel to async/await
This commit is contained in:
bulldozer-balena[bot] 2020-07-02 12:54:42 +00:00 committed by GitHub
commit 9dbdf7db38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,7 +28,6 @@ import { getOnlineTargetUuid } from '../utils/patterns';
import * as _ from 'lodash';
import { tunnelConnectionToDevice } from '../utils/tunnel';
import { createServer, Server, Socket } from 'net';
import * as Bluebird from 'bluebird';
import { tryAsInteger } from '../utils/validation';
import { IArg } from '@oclif/parser/lib/args';
@ -139,12 +138,17 @@ export default class TunnelCmd extends Command {
.map((mapping) => {
return this.parsePortMapping(mapping);
})
.map(({ localPort, localAddress, remotePort }) => {
return tunnelConnectionToDevice(device.uuid, remotePort, sdk)
.then((handler) =>
createServer((client: Socket) => {
return handler(client)
.then(() => {
.map(async ({ localPort, localAddress, remotePort }) => {
try {
const handler = await tunnelConnectionToDevice(
device.uuid,
remotePort,
sdk,
);
const server = createServer(async (client: Socket) => {
try {
await handler(client);
logConnection(
client.remoteAddress || '',
client.remotePort || 0,
@ -153,8 +157,7 @@ export default class TunnelCmd extends Command {
device.vpn_address || '',
remotePort,
);
})
.catch((err) =>
} catch (err) {
logConnection(
client.remoteAddress || '',
client.remotePort || 0,
@ -163,27 +166,23 @@ export default class TunnelCmd extends Command {
device.vpn_address || '',
remotePort,
err,
),
);
}),
)
.then(
(server) =>
new Bluebird.Promise<Server>((resolve, reject) => {
}
});
await new Promise<Server>((resolve, reject) => {
server.on('error', reject);
server.listen(localPort, localAddress, () => {
resolve(server);
});
}),
)
.then(() => {
});
logger.logInfo(
` - tunnelling ${localAddress}:${localPort} to ${device.uuid}:${remotePort}`,
);
return true;
})
.catch((err: Error) => {
} catch (err) {
logger.logWarn(
` - not tunnelling ${localAddress}:${localPort} to ${
device.uuid
@ -191,7 +190,7 @@ export default class TunnelCmd extends Command {
);
return false;
});
}
})
.value();