From c35f701190da7a7d8c62cd900f620057fbcd4eec Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 1 Jul 2020 14:34:08 +0100 Subject: [PATCH] Convert lib/actions-oclif/tunnel to async/await Change-type: patch --- lib/actions-oclif/tunnel.ts | 103 ++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/lib/actions-oclif/tunnel.ts b/lib/actions-oclif/tunnel.ts index f62a3037..6feb1a43 100644 --- a/lib/actions-oclif/tunnel.ts +++ b/lib/actions-oclif/tunnel.ts @@ -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,59 +138,59 @@ 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(() => { - 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((resolve, reject) => { - server.on('error', reject); - server.listen(localPort, localAddress, () => { - resolve(server); - }); - }), - ) - .then(() => { - logger.logInfo( - ` - tunnelling ${localAddress}:${localPort} to ${device.uuid}:${remotePort}`, - ); + .map(async ({ localPort, localAddress, remotePort }) => { + try { + const handler = await tunnelConnectionToDevice( + device.uuid, + remotePort, + sdk, + ); - return true; - }) - .catch((err: Error) => { - logger.logWarn( - ` - not tunnelling ${localAddress}:${localPort} to ${ - device.uuid - }:${remotePort}, failed ${JSON.stringify(err.message)}`, - ); - - return false; + const server = createServer(async (client: Socket) => { + try { + await handler(client); + 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, + ); + } }); + + await new Promise((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();