From 7a1dc93b48a24fbc1abd9f08fa44df2af3dbd6f8 Mon Sep 17 00:00:00 2001 From: Martin Heidegger Date: Thu, 23 Apr 2020 06:54:44 +0900 Subject: [PATCH 1/2] Moving the cli tool from @hyperswarm/tunnel --- bin.js | 9 +++++---- package.json | 2 ++ tunnel-server.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100755 tunnel-server.js diff --git a/bin.js b/bin.js index e271149..7ab7b2c 100755 --- a/bin.js +++ b/bin.js @@ -2,14 +2,15 @@ const cmd = process.argv[1] const op = process.argv[2] -if (!['discovery', 'swarm', 'dht'].includes(op)) { +if (!['discovery', 'swarm', 'dht', 'tunnel-server'].includes(op)) { console.error(`Usage: ${cmd} [command] --help Commands: - discovery ... Interact with the discovery network (DHT and MDNS) - swarm ....... Use the discovery to make connections - dht ......... Start a dht node + discovery ....... Interact with the discovery network (DHT and MDNS) + swarm ........... Use the discovery to make connections + dht ............. Start a dht node + tunnel-server ... Start a tunnel server Example: diff --git a/package.json b/package.json index 01d4641..9a29a84 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "CLI tool to interact with Hyperswarm", "bin": { "hyperswarm": "./bin.js", + "hyperswarm-tunnel-server": "./tunnel-server.js", "hyperswarm-discovery": "./discovery.js", "hyperswarm-swarm": "./swarm.js", "hyperswarm-dht": "./dht.js" @@ -11,6 +12,7 @@ "dependencies": { "@hyperswarm/dht": "^3.6.2", "@hyperswarm/discovery": "^1.11.4", + "@hyperswarm/tunnel": "^1.1.2", "hyperswarm": "^2.13.0", "minimist": "^1.2.5", "pump": "^3.0.0", diff --git a/tunnel-server.js b/tunnel-server.js new file mode 100755 index 0000000..9b76e74 --- /dev/null +++ b/tunnel-server.js @@ -0,0 +1,50 @@ +#!/usr/bin/env node + +const { Remote } = require('@hyperswarm/tunnel') + +const r = new Remote() + +r.listen(port()) + +r.on('forward-listening', function (port, topic) { + console.log('Announcing ' + topic.toString('hex') + ' ' + port) +}) + +r.on('forward-close', function (port, topic) { + console.log('Unannouncing ' + topic.toString('hex') + ' ' + port) +}) + +r.on('forward-connect', function (socket, topic) { + console.log('Doing a lookup for ' + topic.toString('hex')) +}) + +r.on('listening', function () { + console.log('Listening on port ' + r.address().port) +}) + +const a = announce() +if (a) r.announce(a) + +r.on('network-close', () => process.exit()) + +process.once('SIGINT', function () { + r.destroy() +}) + +process.once('SIGTERM', function () { + r.destroy() +}) + +function port () { + let i = process.argv.indexOf('-p') + if (i === -1) i = process.argv.indexOf('--port') + if (i === -1) return 0 + return Number(process.argv[i + 1]) || 0 +} + +function announce () { + let i = process.argv.indexOf('-a') + if (i === -1) i = process.argv.indexOf('--announce') + if (i === -1) return null + return Buffer.from(process.argv[i + 1], 'hex') +} From 593f602412108faee3fdff3901acc7827495bbac Mon Sep 17 00:00:00 2001 From: Martin Heidegger Date: Thu, 23 Apr 2020 07:25:24 +0900 Subject: [PATCH 2/2] Using minimist for argument parsing. --- tunnel-server.js | 68 +++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/tunnel-server.js b/tunnel-server.js index 9b76e74..fa2b066 100755 --- a/tunnel-server.js +++ b/tunnel-server.js @@ -1,29 +1,57 @@ #!/usr/bin/env node +const minimist = require('minimist') const { Remote } = require('@hyperswarm/tunnel') +const argv = minimist(process.argv, { + boolean: [ + 'verbose', + 'help' + ], + string: [ + 'announce' + ], + default: { + port: 0 + }, + alias: { + announce: 'a', + verbose: 'V', + port: 'p', + help: 'h' + } +}) -const r = new Remote() +if (argv.help) { + console.error(`Usage: ${process.argv[1]} [options] -r.listen(port()) + --announce, -a [key] Announce a key immediately at start of the tunnel + --port, -p [port] Specify port to listen to tunnel + --verbose, -V Print all lookups,announces,unannounces +`) + process.exit(1) +} -r.on('forward-listening', function (port, topic) { - console.log('Announcing ' + topic.toString('hex') + ' ' + port) -}) +const r = new Remote() -r.on('forward-close', function (port, topic) { - console.log('Unannouncing ' + topic.toString('hex') + ' ' + port) -}) +r.listen(argv.port) -r.on('forward-connect', function (socket, topic) { - console.log('Doing a lookup for ' + topic.toString('hex')) -}) +if (argv.verbose) { + r.on('forward-listening', function (port, topic) { + console.log('Announcing ' + topic.toString('hex') + ' ' + port) + }) + r.on('forward-close', function (port, topic) { + console.log('Unannouncing ' + topic.toString('hex') + ' ' + port) + }) + r.on('forward-connect', function (_, topic) { + console.log('Doing a lookup for ' + topic.toString('hex')) + }) +} r.on('listening', function () { console.log('Listening on port ' + r.address().port) }) -const a = announce() -if (a) r.announce(a) +if (argv.announce) r.announce(Buffer.from(argv.announce, 'hex')) r.on('network-close', () => process.exit()) @@ -34,17 +62,3 @@ process.once('SIGINT', function () { process.once('SIGTERM', function () { r.destroy() }) - -function port () { - let i = process.argv.indexOf('-p') - if (i === -1) i = process.argv.indexOf('--port') - if (i === -1) return 0 - return Number(process.argv[i + 1]) || 0 -} - -function announce () { - let i = process.argv.indexOf('-a') - if (i === -1) i = process.argv.indexOf('--announce') - if (i === -1) return null - return Buffer.from(process.argv[i + 1], 'hex') -}