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

add whisperlinks #63

Merged
merged 4 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"memdb": "^1.3.1",
"mkdirp": "^1.0.4",
"monotonic-timestamp": "0.0.9",
"paperslip": "^2.2.1",
"pump": "^3.0.0",
"qrcode": "^1.4.4",
"random-access-memory": "^3.1.1",
Expand Down
34 changes: 26 additions & 8 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')
const mkdirp = require('mkdirp')
const os = require('os')
const defaultCommands = require('./commands')
const paperslip = require("paperslip")

class Client {
/**
Expand Down Expand Up @@ -104,17 +105,34 @@ class Client {
/**
* Resolve the DNS shortname `name`. If `name` is already a cabal key, it will
* be returned and the DNS lookup is aborted.
* If `name` is a whisper:// key, a DHT lookup for the passed-in key will occur.
* Once a match is found, it is assumed to be a cabal key, which is returned.
* Returns the cabal key in `cb`. If `cb` is null a Promise is returned.
* @param {string} name the DNS shortname
* @param {function(string)} [cb] The callback to be called when DNS lookup succeeds
* @param {string} name the DNS shortname, or whisper:// shortname
* @param {function(string)} [cb] The callback to be called when lookup succeeds
*/
resolveName (name, cb) {
return this.cabalDns.resolveName(name).then((key) => {
if (key === null) return null
key = Client.scrubKey(key)
if (!cb) return key
else cb(key)
})
if (name.startsWith("whisper://")) {
cblgh marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve, reject) => {
let slip = ""
const stream = paperslip.read(name.slice(10))
stream.on("data", (datum) => {
cblgh marked this conversation as resolved.
Show resolved Hide resolved
if (datum) {
slip += datum.toString()
}
})
stream.on("end", () => {
cblgh marked this conversation as resolved.
Show resolved Hide resolved
resolve(slip)
})
})
} else {
return this.cabalDns.resolveName(name).then((key) => {
if (key === null) return null
key = Client.scrubKey(key)
if (!cb) return key
else cb(key)
})
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const qr = require('qrcode')
const pump = require('pump')
const to = require('to2')
const strftime = require('strftime')
const paperslip = require("paperslip")

module.exports = {
add: {
Expand All @@ -19,6 +20,23 @@ module.exports = {
}
}
},
whisper: {
help: () => 'create a whisper link, a shortlived shortname alias for this cabal\'s key',
call: (cabal, res, arg) => {
if (arg === '') {
return res.error("you need to provide a shortname, e.g. 'workshop'")
}
const topic = `${arg}-${cabal.key.slice(0,3)}`
const whisperlink = `whisper://${topic}`
res.info("whispering on " + whisperlink + " for the next 5 minutes")
// currently this will logout ip addresses that join via the whisper key
const swarm = paperslip.write(topic, `cabal://${cabal.key}`, res.info)
setTimeout(() => {
paperslip.stop(swarm)
res.info("stopped whispering " + topic)
}, 5 * 60 * 1000)
}
},
new: {
help: () => 'create a new cabal',
call: (cabal, res, arg) => {
Expand Down