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 b3f952b..cc9149e 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", "dht-size-up": "^1.0.0", "hyperswarm": "^2.13.0", "minimist": "^1.2.5", diff --git a/tunnel-server.js b/tunnel-server.js new file mode 100755 index 0000000..fa2b066 --- /dev/null +++ b/tunnel-server.js @@ -0,0 +1,64 @@ +#!/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' + } +}) + +if (argv.help) { + console.error(`Usage: ${process.argv[1]} [options] + + --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) +} + +const r = new Remote() + +r.listen(argv.port) + +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) +}) + +if (argv.announce) r.announce(Buffer.from(argv.announce, 'hex')) + +r.on('network-close', () => process.exit()) + +process.once('SIGINT', function () { + r.destroy() +}) + +process.once('SIGTERM', function () { + r.destroy() +})