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

Major changes: Library format, -R support, etc #40

Open
wants to merge 21 commits into
base: main
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
407 changes: 316 additions & 91 deletions README.md

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const { program } = require('commander')
const safetyCatch = require('safety-catch')
const pkg = require('./package.json')

const main = program
.version(pkg.version)
.description(pkg.description)
.addCommand(require('./bin/server.js'))
.addCommand(require('./bin/login.js'))
.addCommand(require('./bin/copy.js'))
.addCommand(require('./bin/tunnel.js'))
.addCommand(require('./bin/keygen.js'))
.addCommand(require('./bin/keys.js'))

main.parseAsync().catch(err => {
safetyCatch(err)
console.error('error: ' + err.message)
process.exit(1)
})
76 changes: 0 additions & 76 deletions bin/client.js

This file was deleted.

73 changes: 6 additions & 67 deletions bin/copy.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
#!/usr/bin/env node
const { createCommand } = require('commander')

const fs = require('fs')
const path = require('path')
const Protomux = require('protomux')
const { Command } = require('commander')
const { SHELLDIR } = require('../constants.js')
const { ClientSocket } = require('../lib/client-socket.js')
const { UploadClient } = require('../lib/upload.js')
const { DownloadClient } = require('../lib/download.js')
const keygen = require('./keygen.js')

const publicKeyExpr = /^([a-fA-F0-9]{64}|[ybndrfg8ejkmcpqxot1uwisza345h769]{52}):/i

const program = new Command()

program
.description('Transfers files using a P2P shell server as transport.')
module.exports = createCommand('copy')
.description('transfer files using a P2P server')
.argument('<source>', 'Source')
.argument('<target>', 'Target')
.option('-f <filename>', 'Filename of the client seed key.', path.join(SHELLDIR, 'peer'))
// .option('--key <hex or z32>', 'Inline key for the client.')
.option('--testnet', 'Use a local testnet.', false)
.action(cmd)
.parseAsync()

async function cmd (sourcePath, targetPath, options = {}) {
const fileOperation = sourcePath[0] === '@' || publicKeyExpr.test(sourcePath) ? 'download' : 'upload'
let serverPublicKey = null

const keyfile = path.resolve(options.f)

if (!fs.existsSync(keyfile)) {
await keygen({ f: keyfile })
}

if (sourcePath[0] === '@' || publicKeyExpr.test(sourcePath)) {
[serverPublicKey, sourcePath] = parseRemotePath(sourcePath)
if (!serverPublicKey || !sourcePath) errorAndExit('Invalid source path.')
} else if (targetPath[0] === '@' || publicKeyExpr.test(targetPath)) {
[serverPublicKey, targetPath] = parseRemotePath(targetPath)
if (!serverPublicKey || !targetPath) errorAndExit('Invalid target path.')
} else {
errorAndExit('Invalid source or target path.')
}

const { node, socket } = ClientSocket({ keyfile, serverPublicKey, testnet: options.testnet })
const mux = new Protomux(socket)

if (fileOperation === 'upload') {
const upload = new UploadClient({ sourcePath, targetPath }, { node, socket, mux })
upload.open()
} else {
const download = new DownloadClient({ sourcePath, targetPath }, { node, socket, mux })
download.open()
}
}

function errorAndExit (message) {
console.error('Error:', message)
process.exit(1)
}

function parseRemotePath (str) {
const i = str.indexOf(':')
if (i === -1) return [null, null]

const isName = str[0] === '@'
return [str.slice(isName ? 1 : 0, i), str.slice(i + 1)] // [host, path]
}
.option('-f <filename>', 'filename of the seed key file')
.option('--bootstrap <nodes...>', 'custom dht nodes')
.action(require('../lib/bin/copy.js'))
87 changes: 6 additions & 81 deletions bin/keygen.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,82 +1,7 @@
#!/usr/bin/env node
const { createCommand } = require('commander')

const fs = require('fs')
const path = require('path')
const readline = require('readline')
const { Command } = require('commander')
const Keychain = require('keypear')
const DHT = require('hyperdht')
const HypercoreId = require('hypercore-id-encoding')
const { SHELLDIR } = require('../constants.js')

const isModule = require.main !== module

if (isModule) {
module.exports = cmd
} else {
const program = new Command()

program
.description('Create keys of type ed25519 for use by hypercore-protocol.')
.option('-f <filename>', 'Filename of the seed key file.')
.option('-c <comment>', 'Provides a new comment.')
.action(cmd)
.parseAsync()
}

async function cmd (options = {}) {
console.log('Generating key.')

let keyfile
if (!options.f) {
keyfile = path.join(SHELLDIR, 'peer')

const answer = await question('Enter file in which to save the key (' + keyfile + '): ')
const filename = answer.trim()
if (filename) {
keyfile = path.resolve(filename)
}
} else {
keyfile = path.resolve(options.f)
}

const comment = options.c ? (' # ' + options.c) : ''

if (fs.existsSync(keyfile)) {
if (isModule) {
console.log()
return
}

errorAndExit(keyfile + ' already exists.') // Overwrite (y/n)?
}

const seed = Keychain.seed()
fs.mkdirSync(path.dirname(keyfile), { recursive: true })
fs.writeFileSync(keyfile, HypercoreId.encode(seed) + comment + '\n', { flag: 'wx', mode: '600' })

console.log('Your key has been saved in', keyfile)
console.log('The public key is:')
console.log(HypercoreId.encode(DHT.keyPair(seed).publicKey))

if (isModule) console.log()
}

function question (query = '') {
return new Promise(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})

rl.question(query, function (answer) {
rl.close()
resolve(answer)
})
})
}

function errorAndExit (message) {
console.error('Error:', message)
process.exit(1)
}
module.exports = createCommand('keygen')
.description('create keys of type ed25519')
.option('-f <filename>', 'filename of the seed key file')
.option('-c <comment>', 'provides a comment')
.action(require('../lib/bin/keygen.js'))
22 changes: 22 additions & 0 deletions bin/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { createCommand } = require('commander')

const keys = createCommand('keys')
.description('keys management')

keys
.command('list')
.description('list keys')
.action(require('../lib/bin/keys-list.js'))

keys
.command('add <name> <public-key>')
.description('add a known peer by name')
.action(require('../lib/bin/keys-add.js'))

keys
.command('allow <public-key-or-name>')
.description('authorize a peer into the server')
.option('--firewall <filename>', 'list of allowed public keys')
.action(require('../lib/bin/keys-allow.js'))

module.exports = keys
9 changes: 9 additions & 0 deletions bin/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { createCommand } = require('commander')

module.exports = createCommand('login')
.description('connect to a P2P shell')
.argument('<key or name>', 'public key or name of the server')
.option('--invite [token]', 'create or use a token to join the server')
.option('-f <filename>', 'filename of the client seed key')
.option('--bootstrap <nodes...>', 'custom dht nodes')
.action(require('../lib/bin/login.js'))
Loading
Loading