Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bare integration #21

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
66 changes: 2 additions & 64 deletions bin.js
Original file line number Diff line number Diff line change
@@ -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)
66 changes: 66 additions & 0 deletions cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const Hyperbeam = require('./')
global.process = require('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)
})
}
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
},
"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"
},
"imports": {
"process": {
"bare": "bare-process",
"default": "process"
}
},
"devDependencies": {
"standard": "^16.0.4"
},
Expand Down