mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-24 22:41:33 +00:00
refactor: examples/pnet (#523)
* refactor: examples/pnet * chore: rename pnet-ipfs to pnet * chore: address review
This commit is contained in:
1
examples/pnet/.gitignore
vendored
Normal file
1
examples/pnet/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
tmp/
|
29
examples/pnet/README.md
Normal file
29
examples/pnet/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# Private Networking
|
||||
This example shows how to set up a private network of libp2p nodes.
|
||||
|
||||
## Setup
|
||||
Install dependencies:
|
||||
|
||||
```
|
||||
npm install
|
||||
```
|
||||
|
||||
## Run
|
||||
Running the example will cause two nodes with the same swarm key to be started and exchange basic information.
|
||||
|
||||
```
|
||||
node index.js
|
||||
```
|
||||
|
||||
### Using different keys
|
||||
This example includes `TASK` comments that can be used to try the example with different swarm keys. This will
|
||||
allow you to see how nodes will fail to connect if they are on different private networks and try to connect to
|
||||
one another.
|
||||
|
||||
To change the swarm key of one of the nodes, look through `index.js` for comments starting with `TASK` to indicate
|
||||
where lines are that pertain to changing the swarm key of node 2.
|
||||
|
||||
### Exploring the repos
|
||||
Once you've run the example you can take a look at the repos in the `./tmp` directory to see how they differ, including
|
||||
the swarm keys. You should see a `swarm.key` file in each of the repos and when the nodes are on the same private network
|
||||
this contents of the `swarm.key` files should be the same.
|
50
examples/pnet/index.js
Normal file
50
examples/pnet/index.js
Normal file
@ -0,0 +1,50 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
'use strict'
|
||||
|
||||
const { generate } = require('libp2p/src/pnet')
|
||||
const privateLibp2pNode = require('./libp2p-node')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
// Create a buffer and write the swarm key to it
|
||||
const swarmKey = Buffer.alloc(95)
|
||||
generate(swarmKey)
|
||||
|
||||
// This key is for testing a different key not working
|
||||
const otherSwarmKey = Buffer.alloc(95)
|
||||
generate(otherSwarmKey)
|
||||
|
||||
;(async () => {
|
||||
const node1 = await privateLibp2pNode(swarmKey)
|
||||
|
||||
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
|
||||
const node2 = await privateLibp2pNode(swarmKey)
|
||||
// const node2 = await privateLibp2pNode(otherSwarmKey)
|
||||
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
|
||||
console.log('nodes started...')
|
||||
|
||||
await node1.dial(node2.peerInfo)
|
||||
|
||||
node2.handle('/private', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/private')
|
||||
|
||||
await pipe(
|
||||
['This message is sent on a private network'],
|
||||
stream
|
||||
)
|
||||
})();
|
36
examples/pnet/libp2p-node.js
Normal file
36
examples/pnet/libp2p-node.js
Normal file
@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Protector = require('libp2p/src/pnet')
|
||||
|
||||
/**
|
||||
* privateLibp2pNode returns a libp2p node function that will use the swarm
|
||||
* key at the given `swarmKeyPath` to create the Protector
|
||||
*
|
||||
* @param {Buffer} swarmKey
|
||||
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
|
||||
*/
|
||||
const privateLibp2pNode = async (swarmKeyPath) => {
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP], // We're only using the TCP transport for this example
|
||||
streamMuxer: [MPLEX], // We're only using mplex muxing
|
||||
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
|
||||
// care about node identity, and only the presence of private keys
|
||||
connEncryption: [SECIO],
|
||||
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
|
||||
// being left in for explicit readability.
|
||||
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
|
||||
peerDiscovery: [],
|
||||
connProtector: new Protector(swarmKeyPath)
|
||||
}
|
||||
})
|
||||
|
||||
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
return node
|
||||
}
|
||||
|
||||
module.exports = privateLibp2pNode
|
19
examples/pnet/package.json
Normal file
19
examples/pnet/package.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "pnet-ipfs-example",
|
||||
"version": "1.0.0",
|
||||
"description": "An example of private networking with IPFS",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"libp2p": "../..",
|
||||
"libp2p-mplex": "^0.9.3",
|
||||
"libp2p-secio": "^0.12.1",
|
||||
"libp2p-tcp": "^0.14.2"
|
||||
}
|
||||
}
|
28
examples/pnet/utils.js
Normal file
28
examples/pnet/utils.js
Normal file
@ -0,0 +1,28 @@
|
||||
'use strict'
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
/**
|
||||
* mkdirp recursively creates needed folders for the given dir path
|
||||
* @param {string} dir
|
||||
* @returns {string} The path that was created
|
||||
*/
|
||||
module.exports.mkdirp = (dir) => {
|
||||
return path
|
||||
.resolve(dir)
|
||||
.split(path.sep)
|
||||
.reduce((acc, cur) => {
|
||||
const currentPath = path.normalize(acc + path.sep + cur)
|
||||
|
||||
try {
|
||||
fs.statSync(currentPath)
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
fs.mkdirSync(currentPath)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
return currentPath
|
||||
}, '')
|
||||
}
|
Reference in New Issue
Block a user