From cc77879e91ff35dda430c985e3a14b9883aac1cb Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Wed, 20 Mar 2024 12:44:28 +0100 Subject: [PATCH 1/2] bare integration --- bin.js | 66 ++------------------------------------------------- cmd.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 71 insertions(+), 65 deletions(-) create mode 100644 cmd.js diff --git a/bin.js b/bin.js index f7963e0..8beb7c1 100755 --- a/bin.js +++ b/bin.js @@ -1,65 +1,3 @@ #!/usr/bin/env node - -const Hyperbeam = require('./') - -if (process.argv.includes('-h') || process.argv.includes('--help')) { - console.error('Usage: hyperbeam [passphrase]') - console.error('') - console.error(' Creates a 1-1 end-to-end encrypted network pipe.') - console.error(' If a passphrase is not supplied, will create a new phrase and begin listening.') - process.exit(1) -} - -let beam -try { - beam = new Hyperbeam(process.argv[2], process.argv.includes('-r')) -} catch (e) { - if (e.constructor.name === 'PassphraseError') { - console.error(e.message) - console.error('(If you are attempting to create a new pipe, do not provide a phrase and hyperbeam will generate one for you.)') - process.exit(1) - } else { - throw e - } -} - -if (beam.announce) { - console.error('[hyperbeam] Run hyperbeam ' + beam.key + ' to connect') - console.error('[hyperbeam] To restart this side of the pipe with the same key add -r to the above') -} else { - console.error('[hyperbeam] Connecting pipe...') -} - -beam.on('remote-address', function ({ host, port }) { - if (!host) console.error('[hyperbeam] Could not detect remote address') - else console.error('[hyperbeam] Joined the DHT - remote address is ' + host + ':' + port) -}) - -beam.on('connected', function () { - console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer') -}) - -beam.on('error', function (e) { - console.error('[hyperbeam] Error:', e.message) - closeASAP() -}) - -beam.on('end', () => beam.end()) - -process.stdin.pipe(beam).pipe(process.stdout) -if (typeof process.stdin.unref === 'function') process.stdin.unref() - -process.once('SIGINT', () => { - if (!beam.connected) closeASAP() - else beam.end() -}) - -function closeASAP () { - console.error('[hyperbeam] Shutting down beam...') - - const timeout = setTimeout(() => process.exit(1), 2000) - beam.destroy() - beam.on('close', function () { - clearTimeout(timeout) - }) -} +const cmd = require('./cmd') +cmd(process.argv) diff --git a/cmd.js b/cmd.js new file mode 100644 index 0000000..844cc40 --- /dev/null +++ b/cmd.js @@ -0,0 +1,67 @@ +const Hyperbeam = require('./') + +if (global.Bare) process = require('bare-process') // eslint-disable-line + +module.exports = (argv) => { + if (argv.includes('-h') || argv.includes('--help')) { + console.error('Usage: hyperbeam [passphrase]') + console.error('') + console.error(' Creates a 1-1 end-to-end encrypted network pipe.') + console.error(' If a passphrase is not supplied, will create a new phrase and begin listening.') + process.exit(1) + } + + let beam + try { + beam = new Hyperbeam(argv[2], argv.includes('-r')) + } catch (e) { + if (e.constructor.name === 'PassphraseError') { + console.error(e.message) + console.error('(If you are attempting to create a new pipe, do not provide a phrase and hyperbeam will generate one for you.)') + process.exit(1) + } else { + throw e + } + } + + if (beam.announce) { + console.error('[hyperbeam] Run hyperbeam ' + beam.key + ' to connect') + console.error('[hyperbeam] To restart this side of the pipe with the same key add -r to the above') + } else { + console.error('[hyperbeam] Connecting pipe...') + } + + beam.on('remote-address', function ({ host, port }) { + if (!host) console.error('[hyperbeam] Could not detect remote address') + else console.error('[hyperbeam] Joined the DHT - remote address is ' + host + ':' + port) + }) + + beam.on('connected', function () { + console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer') + }) + + beam.on('error', function (e) { + console.error('[hyperbeam] Error:', e.message) + closeASAP() + }) + + beam.on('end', () => beam.end()) + + process.stdin.pipe(beam).pipe(process.stdout) + if (typeof process.stdin.unref === 'function') process.stdin.unref() + + process.once('SIGINT', () => { + if (!beam.connected) closeASAP() + else beam.end() + }) + + function closeASAP () { + console.error('[hyperbeam] Shutting down beam...') + + const timeout = setTimeout(() => process.exit(1), 2000) + beam.destroy() + beam.on('close', function () { + clearTimeout(timeout) + }) + } +} diff --git a/package.json b/package.json index d342bd4..c0abb5f 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,11 @@ }, "dependencies": { "b4a": "^1.3.1", + "bare-process": "^1.3.2", "hi-base32": "^0.5.1", "hyperdht": "^6.5.3", "queue-tick": "^1.0.0", - "sodium-universal": "^3.0.4", + "sodium-universal": "^4.0.1", "streamx": "^2.10.0" }, "devDependencies": { From 2686c3a5f2daf23c1ecce77d8562f826a44e47f1 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Wed, 20 Mar 2024 13:53:07 +0100 Subject: [PATCH 2/2] added import map for bare --- cmd.js | 3 +-- package.json | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd.js b/cmd.js index 844cc40..c29e05c 100644 --- a/cmd.js +++ b/cmd.js @@ -1,6 +1,5 @@ const Hyperbeam = require('./') - -if (global.Bare) process = require('bare-process') // eslint-disable-line +global.process = require('process') // eslint-disable-line module.exports = (argv) => { if (argv.includes('-h') || argv.includes('--help')) { diff --git a/package.json b/package.json index c0abb5f..3464694 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,12 @@ "sodium-universal": "^4.0.1", "streamx": "^2.10.0" }, + "imports": { + "process": { + "bare": "bare-process", + "default": "process" + } + }, "devDependencies": { "standard": "^16.0.4" },