Convert lib/actions-oclif/tunnel to async/await

Change-type: patch
This commit is contained in:
Pagan Gazzard 2020-07-01 14:34:08 +01:00 committed by Balena CI
parent 9e465217b2
commit c35f701190

View File

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