Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Adding tunnel server #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"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"
},
"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",
Expand Down
64 changes: 64 additions & 0 deletions tunnel-server.js
Original file line number Diff line number Diff line change
@@ -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()
})