├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | # UUUUGH 107 | package-lock.json 108 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 RangerMauve 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # p2plex 2 | Multiplex encrypted connections to peers over a p2p network. 3 | 4 | ```shell 5 | npm i -s p2plex 6 | ``` 7 | 8 | ```js 9 | const p2plex = require('p2plex') 10 | 11 | const plex1 = p2plex() 12 | const plex2 = p2plex() 13 | 14 | plex1.on('connection', (peer) => { 15 | peer.receiveStream('example').on('data', (data) => { 16 | console.log('Got data from', peer.publicKey, ':', data.toString('utf8')) 17 | plex1.destroy() 18 | plex2.destroy() 19 | }) 20 | }) 21 | 22 | plex2.findByPublicKey(plex1.publicKey).then((peer) => { 23 | peer.createStream('example').end('Hello World!') 24 | }) 25 | ``` 26 | 27 | ## How it works 28 | 29 | This module combines [Hyperswarm](https://github.com/hyperswarm/hyperswarm) for P2P networking, [Noise-peer](https://github.com/emilbayes/noise-peer) for encrypted communication, and [Multiplex](https://www.npmjs.com/package/multiplex) for sending multiple streams over a single connection. 30 | The deduplication happens under the hood. Your application should focus on opening and closing streams on peers and not worry about whether the peer has just connected or it was connected already. 31 | Once all the multiplexed streams for a peer end, the connection will be dropped automatically. 32 | 33 | ## API 34 | 35 | ### `const plex = p2plex({keyPair, listenSelf, ...opts})` 36 | 37 | Initialize a new p2plex instance. 38 | 39 | - `keyPair`: is a public/private key pair for transport encryption / authentication. If you don't specify one, it will be auto-generated. 40 | - `listenSelf`: This controls whether you'll start advertising yourself for people to find you with `findByPublicKey`. `true` by default. 41 | - `opts`: These options get passed down to `hyperswarm` and `noise-peer`. This gives you more control over those modules if you know what you're doing. 42 | 43 | ### `plex.publicKey` 44 | 45 | This is a Buffer object containing your public key. Your application can use this as your identity in the swarm and can be exchanged with peers to enable connecting to you later. 46 | 47 | ### `plex.keyPair` 48 | 49 | This is an object containing your key pair with the `publicKey` and `secretKey`. You might want to save this for later if you want a persistant identity. 50 | 51 | ### `plex.peers` 52 | 53 | This is a Set of all the currently connected peers. 54 | 55 | ### `plex.swarm` 56 | 57 | This is the raw hyperswarm instance. You probably shouldn't mess with it unless you really know what you're doing. 58 | 59 | ### `plex.on('connection', (peer) => {})` 60 | 61 | This event gets emitted every time a new peer gets connected. 62 | 63 | - `peer`: is a `Peer` object representing the connection to someone in the network. 64 | 65 | ### `const peer = await plex.findByPublicKey(publicKey)` 66 | 67 | This method will attempt to discover a peer with the given public key on the network. 68 | You should probably wrap this in a timeout. PRs for introducing cancellation would be appreciated. 69 | 70 | - `publicKey` is a Buffer containing the other peers' public key. 71 | - `peer`: is a `Peer` object representing the connection to someone in the network. 72 | 73 | ### `const peer = await plex.findByTopicAndPublicKey(topic, publicKey, {announce: false, lookup: true})` 74 | 75 | This method will attempt to discover a peer under a given topic with a given public key. 76 | If you don't specify the announce and lookup options, the defaults will be used. 77 | You should probably wrap this in a timeout. PRs for introducing cancellation would be appreciated. 78 | **Note:** There's currently a bug where the deduplication logic ends up emitting errors. To mitigate this, have one side set `announce:true,lookup:false` and the other side set `announce:false,lookup:true`. 79 | 80 | 81 | - `topic` should be a 32 byte Buffer which is the key you want to use to find other peers. 82 | - `publicKey` is a Buffer containing the other peers' public key. 83 | - `announce` sets whether you will be announcing your existance for this key to get incoming peer connections. 84 | - `lookup` Sets whether you will be actively searching for peers on the network and making outgoing connections. 85 | - `peer`: is a `Peer` object representing the connection to someone in the network. 86 | 87 | ### `await plex.join(topic, {announce: false, lookup: true})` 88 | 89 | Starts finding peers on the network. 90 | Resolves once a full lookup has been completed. 91 | You usually don't need to await this promise and keep going while it does stuff in the background. 92 | 93 | - `topic` should be a 32 byte Buffer which is the key you want to use to find other peers. 94 | - `announce` sets whether you will be announcing your existance for this key to get incoming peer connections. 95 | - `lookup` Sets whether you will be actively searching for peers on the network and making outgoing connections. 96 | 97 | ### `await plex.leave(topic)` 98 | 99 | Stops finding peers on the network and removes all trace of itself from the p2p network for this key. 100 | 101 | - `topic` should be a 32 byte Buffer which is the key you want to use to find other peers. 102 | 103 | ### `await plex.destroy()` 104 | 105 | Gracefully disconnects from all peers, removes itself from the p2p network, and shuts down the p2p network instance. 106 | 107 | ### `peer.publicKey` 108 | 109 | Every peer will have their public key populated so that you can verify their identity or save it for use later. 110 | 111 | ### `peer.incoming` 112 | 113 | A boolean representing whether this peer represents an incoming connection or an outgoing connection. 114 | 115 | ### `peer.topics` 116 | 117 | This is an array of topic names that this peer has been discovered for on the network. Might be empty for incomming connections. 118 | 119 | ### `peer.on('topic', (topic) => {})` 120 | 121 | This gets emitted when a topic has been associated with a peer. 122 | This gets emitted when peer initially connects, and then whenever the swarm discovers them advertising on another topic after already connecting. 123 | 124 | ### `peer.on('stream', (stream, id) => {})` 125 | 126 | This gets emitted when the peer gets a new stream over the multiplex connections. 127 | 128 | - `stream` Is a [node-style Duplex stream](https://nodejs.org/api/stream.html) you can read and write from. 129 | - `id` is a String representing the name given to the stream when it was being created. 130 | 131 | ### `peer.on('disconnected', () => {}) 132 | 133 | This gets emitted when the peer's connection has been severed at the network level. 134 | 135 | ### `peer.on('error', (err) => {})` 136 | 137 | This gets emitted when there is an error either in the connection or the multiplex module 138 | 139 | ### `const stream = peer.createStream(id)` 140 | 141 | Create a stream over the multiplexer. The other side should use `peer.receiveStream(id)` to get the stream. 142 | 143 | - `id` is a string which identifies the stream on both sides. 144 | 145 | ### `const stream = peer.receiveStream(id)` 146 | 147 | Recieves a stream over the multiplexer. The other side should use `peer.createStream(id)` to get the stream. 148 | 149 | ### `const stream = peer.createSharedStream(id)` 150 | 151 | Create a shared stream over the multiplexer. If both sides create a shared stream with the same id, data from one end will go to the other. 152 | 153 | ### `peer.ban()` 154 | 155 | Tell the swarm to avoid connecting to this peer in the future. This can be useful if there's misbehaving peers or giving users more control. 156 | 157 | ### `await peer.disconnect()` 158 | 159 | Gracefully disconnects from the peer and resolves once the disconnection is complete. 160 | 161 | ## Credits 162 | 163 | Big thanks to [playproject.io](https://playproject.io/) for sponsoring this work for use in the [DatDot](https://playproject.io/datdot-service/) project. 164 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const hyperswarm = require('hyperswarm') 2 | const multiplex = require('multiplex') 3 | const noisePeer = require('noise-peer') 4 | const EventEmitter = require('events') 5 | const pump = require('pump') 6 | const getStream = require('get-stream') 7 | const isBuffer = require('is-buffer') 8 | 9 | const METADATA_NAME = 'p2plex-topics' 10 | 11 | module.exports = (opts) => new P2Plex(opts) 12 | 13 | class P2Plex extends EventEmitter { 14 | constructor ({ 15 | keyPair, 16 | listenSelf = true, 17 | ...opts 18 | } = {}) { 19 | super() 20 | this.opts = opts 21 | this.swarm = hyperswarm({ 22 | multiplex: true, 23 | ...opts 24 | }) 25 | 26 | let { publicKey, secretKey } = opts.keyPair || noisePeer.keygen() 27 | if (!isBuffer(publicKey)) { 28 | publicKey = Buffer.from(publicKey) 29 | } 30 | if (!isBuffer(secretKey)) { 31 | secretKey = Buffer.from(secretKey) 32 | } 33 | this.keyPair = { 34 | publicKey, 35 | secretKey 36 | } 37 | 38 | this.peers = new Set() 39 | 40 | this.swarm.on('connection', (socket, info) => this._handleConnection(socket, info)) 41 | if (listenSelf) this.swarm.join(this.publicKey, { announce: true, lookup: false }) 42 | } 43 | 44 | get publicKey () { 45 | return this.keyPair.publicKey 46 | } 47 | 48 | _handleConnection (socket, info) { 49 | const { client } = info 50 | 51 | const onPreInitError = (err) => this.emit('error', err) 52 | 53 | const sec = noisePeer(socket, client, { 54 | pattern: 'XX', 55 | ...this.opts, 56 | staticKeyPair: this.keyPair, 57 | onstatickey: (remoteStaticKey, done) => { 58 | const publicKey = Buffer.from(remoteStaticKey) 59 | done() 60 | const dropped = info.deduplicate(this.publicKey, publicKey) 61 | if (dropped) return 62 | 63 | const plex = multiplex() 64 | 65 | function disconnect () { 66 | return new Promise((resolve, reject) => { 67 | sec.end((err) => { 68 | if (err) reject(err) 69 | else resolve() 70 | }) 71 | }) 72 | } 73 | 74 | const peer = new Peer(publicKey, plex, info, disconnect) 75 | 76 | this.peers.add(peer) 77 | 78 | sec.removeListener('error', onPreInitError) 79 | 80 | pump(sec, plex, sec, (err) => { 81 | if (err) peer.emit('error', err) 82 | this.peers.delete(peer) 83 | peer.emit('disconnected') 84 | }) 85 | 86 | peer.init().then(() => { 87 | this.emit('connection', peer) 88 | 89 | info.on('topic', () => this.emit('connection', peer)) 90 | 91 | // Emit events for all the topics it already has 92 | peer.emitTopics() 93 | }, (e) => this.emit('error', e)) 94 | } 95 | }) 96 | 97 | sec.on('error', onPreInitError) 98 | 99 | info.stream = sec 100 | } 101 | 102 | // Connect to a peer based on their public key 103 | // Connects to a topic with their public key 104 | async findByPublicKey (publicKey) { 105 | return this.findByTopicAndPublicKey(publicKey, publicKey) 106 | } 107 | 108 | // Find a peer for a given topic with a given public key 109 | async findByTopicAndPublicKey (topic, publicKey, options = { announce: false, lookup: true }) { 110 | // Check if we've already connected to this peer 111 | for (const peer of this.peers) { 112 | if (peer.publicKey.equals(publicKey) && peer.hasTopic(topic)) return peer 113 | } 114 | 115 | this.join(topic, options) 116 | const peer = await new Promise((resolve) => { 117 | const onconnection = (peer) => { 118 | const { publicKey: remoteKey } = peer 119 | 120 | if (!remoteKey.equals(publicKey)) return 121 | if (!peer.hasTopic(topic)) return 122 | this.removeListener('connection', onconnection) 123 | resolve(peer) 124 | } 125 | 126 | this.on('connection', onconnection) 127 | }) 128 | 129 | // Leaving takes a long time if we announced, do it async 130 | this.leave(topic) 131 | 132 | return peer 133 | } 134 | 135 | async join (topic, options) { 136 | return new Promise((resolve) => { 137 | this.swarm.join(topic, options, resolve) 138 | }) 139 | } 140 | 141 | async leave (topic) { 142 | return new Promise((resolve) => { 143 | this.swarm.leave(topic, resolve) 144 | }) 145 | } 146 | 147 | async destroy () { 148 | const allPeers = [...this.peers] 149 | await Promise.all(allPeers.map((peer) => { 150 | return peer.disconnect() 151 | })) 152 | 153 | return new Promise((resolve, reject) => { 154 | this.swarm.destroy((err) => { 155 | if (err) reject(err) 156 | else resolve() 157 | }) 158 | }) 159 | } 160 | } 161 | 162 | class Peer extends EventEmitter { 163 | constructor (publicKey, plex, info, disconnect) { 164 | super() 165 | this.publicKey = publicKey 166 | this.plex = plex 167 | this.info = info 168 | this.incoming = !info.client 169 | this.disconnect = disconnect 170 | this.otherTopics = [] 171 | this.streamCount = 0 172 | 173 | plex.on('error', (err) => this.emit('error', err)) 174 | 175 | this.info.on('topic', (topic) => this.emit('topic', topic)) 176 | } 177 | 178 | async init () { 179 | const metadata = this.createSharedStream(METADATA_NAME, { encoding: 'utf8' }) 180 | 181 | metadata.end(JSON.stringify(this.topics)) 182 | 183 | const otherTopicsJSON = await getStream(metadata) 184 | 185 | const otherTopicsParsed = JSON.parse(otherTopicsJSON) 186 | this.otherTopics = otherTopicsParsed.map((topic) => Buffer.from(topic)) 187 | } 188 | 189 | _handleStream (stream, id) { 190 | // Count the streams we have and auto-close when we have no more 191 | if (id !== METADATA_NAME) { 192 | this.streamCount++ 193 | const cleanup = () => { 194 | this.streamCount-- 195 | process.nextTick(() => { 196 | if (!this.streamCount) this.disconnect() 197 | }) 198 | 199 | // Streams are such a pain in the ass. Why doesn't `close` always work? 200 | stream.removeListener('end', cleanup) 201 | stream.removeListener('close', cleanup) 202 | } 203 | stream.once('end', cleanup) 204 | stream.once('finish', cleanup) 205 | } 206 | this.emit('stream', stream, id) 207 | } 208 | 209 | emitTopics () { 210 | for (const topic of this.topics) { 211 | this.emit('topic', topic) 212 | } 213 | } 214 | 215 | get topics () { 216 | const topics = this.otherTopics.slice(0) 217 | if (this.info.topics) topics.push(...this.info.topics) 218 | else if (this.info.peer && this.info.peer.topic) topics.push(this.info.peer.topic) 219 | return topics 220 | } 221 | 222 | hasTopic (topic) { 223 | for (const existing of this.topics) { 224 | if (existing.equals(topic)) return true 225 | } 226 | return false 227 | } 228 | 229 | createStream (id, options = {}) { 230 | const stream = this.plex.createStream(id, { emitClose: true, ...options }) 231 | this._handleStream(stream, id) 232 | return stream 233 | } 234 | 235 | receiveStream (id, options = {}) { 236 | const stream = this.plex.receiveStream(id, { emitClose: true, ...options }) 237 | this._handleStream(stream, id) 238 | return stream 239 | } 240 | 241 | createSharedStream (id, options = {}) { 242 | const stream = this.plex.createSharedStream(id, { emitClose: true, ...options }) 243 | this._handleStream(stream, id) 244 | return stream 245 | } 246 | 247 | ban () { 248 | this.info.ban() 249 | } 250 | 251 | backoff () { 252 | this.info.backoff() 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p2plex", 3 | "version": "1.5.0", 4 | "description": "Multiplex encrypted connections to peers over a p2p network", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/RangerMauve/p2plex.git" 12 | }, 13 | "keywords": [ 14 | "p2p", 15 | "multiplex", 16 | "dweb", 17 | "hyperswarm", 18 | "swarm" 19 | ], 20 | "author": "rangermauve", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/RangerMauve/p2plex/issues" 24 | }, 25 | "homepage": "https://github.com/RangerMauve/p2plex#readme", 26 | "dependencies": { 27 | "get-stream": "^5.1.0", 28 | "hyperswarm": "^2.11.2", 29 | "is-buffer": "^2.0.4", 30 | "multiplex": "^6.7.0", 31 | "noise-peer": "^2.1.0", 32 | "pump": "^3.0.0" 33 | }, 34 | "devDependencies": { 35 | "supertape": "^2.0.1" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('supertape') 2 | const { once } = require('events') 3 | const crypto = require('crypto') 4 | 5 | const P2Plex = require('./') 6 | 7 | test('Find by public key and send data', async (t) => { 8 | const p1 = P2Plex() 9 | const p2 = P2Plex() 10 | 11 | const [[peer1], peer2] = await Promise.all([ 12 | once(p1, 'connection'), 13 | p2.findByPublicKey(p1.publicKey) 14 | ]) 15 | 16 | t.pass('Got peers') 17 | 18 | const stream1 = peer1.receiveStream('example') 19 | const stream2 = peer2.createStream('example') 20 | 21 | t.pass('Got streams') 22 | 23 | const toWrite = Buffer.from('Hello World') 24 | process.nextTick(() => { 25 | stream2.write(toWrite) 26 | }) 27 | const [data] = await once(stream1, 'data') 28 | 29 | t.deepEquals(data, toWrite, 'Got data') 30 | 31 | stream1.end() 32 | 33 | await Promise.all([ 34 | once(peer1, 'disconnected'), 35 | once(peer2, 'disconnected') 36 | ]) 37 | 38 | t.pass('Peers disconnected after closing streams') 39 | 40 | await Promise.all([ 41 | p1.destroy(), 42 | p2.destroy() 43 | ]) 44 | 45 | t.pass('Destroyed swarm') 46 | 47 | t.end() 48 | }) 49 | 50 | test('Find by public key and send data', async (t) => { 51 | const p1 = P2Plex() 52 | const p2 = P2Plex() 53 | 54 | const topic = crypto.randomBytes(32) 55 | 56 | const [peer1, peer2] = await Promise.all([ 57 | p1.findByTopicAndPublicKey(topic, p2.publicKey, { announce: true, lookup: false }), 58 | p2.findByTopicAndPublicKey(topic, p1.publicKey, { lookup: true, announce: false }) 59 | ]) 60 | 61 | t.pass('Got peers') 62 | 63 | const stream1 = peer1.receiveStream('example') 64 | const stream2 = peer2.createStream('example') 65 | 66 | t.pass('Got streams') 67 | 68 | const toWrite = Buffer.from('Hello World') 69 | process.nextTick(() => { 70 | stream2.write(toWrite) 71 | }) 72 | const [data] = await once(stream1, 'data') 73 | 74 | t.deepEquals(data, toWrite, 'Got data') 75 | 76 | stream1.end() 77 | 78 | await Promise.all([ 79 | once(peer1, 'disconnected'), 80 | once(peer2, 'disconnected') 81 | ]) 82 | 83 | t.pass('Peers disconnected after closing streams') 84 | 85 | await Promise.all([ 86 | p1.destroy(), 87 | p2.destroy() 88 | ]) 89 | 90 | t.pass('Destroyed swarm') 91 | 92 | t.end() 93 | }) 94 | --------------------------------------------------------------------------------