-
-
Save mcclure/c05ffc31329cfd80b40b04b9a9618bbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
'use strict' | |
const Libp2p = require('libp2p') | |
const PeerId = require('peer-id') | |
const Websockets = require('libp2p-websockets') | |
const WebRTCStar = require('libp2p-webrtc-star') | |
const Mplex = require('libp2p-mplex') | |
const { NOISE } = require('libp2p-noise') | |
const KadDHT = require('libp2p-kad-dht') | |
const commander = require('commander') | |
const commandError = require('commander.js-error') | |
const wrtc = require('wrtc') | |
const fs = require('fs') | |
const os = require('os') | |
const port = 10001 | |
commander | |
.version("0.0.1") | |
.option('-f, --id-file <file>', 'Store/load peer ID here') | |
.option('-t, --id-temp', 'Single-use ID (not recommended)') | |
.parse(process.argv) | |
const delay = require('delay') | |
const transportKey = WebRTCStar.prototype[Symbol.toStringTag] | |
if (!(commander.idFile || commander.idTemp)) | |
commandError("Must specify peer file, for example -f peer.json") | |
if (commander.idFile && commander.idTemp) | |
commandError("Included both --id-file and --id-temp options, which doesn't make sense") | |
const createNode = async (peerId) => { | |
const node = await Libp2p.create({ | |
addresses: { | |
listen: [`/ip4/0.0.0.0/tcp/${port}/ws`] | |
}, | |
modules: { | |
transport: [Websockets, WebRTCStar], | |
streamMuxer: [Mplex], | |
connEncryption: [NOISE], | |
dht: KadDHT | |
}, | |
config: { | |
dht: { | |
enabled: true | |
}, | |
transport: { | |
[transportKey]: { | |
wrtc // You can use `wrtc` when running in Node.js | |
} | |
} | |
}, | |
peerId | |
}) | |
await node.start() | |
return node | |
} | |
let node1 | |
;(async () => { | |
let peerId | |
// Load peer from file (if any) | |
if (commander.idFile && fs.existsSync(commander.idFile)) { | |
const json = JSON.parse( fs.readFileSync(commander.idFile) ) | |
peerId = await PeerId.createFromJSON(json) | |
} | |
// Create peer (if needed) | |
if (!peerId) { | |
peerId = await PeerId.create({keyType:'ed25519', bits:2048}) | |
console.log("Generating new peer keys") | |
if (commander.idFile) { | |
fs.writeFileSync(commander.idFile, JSON.stringify( peerId.toJSON() )) | |
} | |
} | |
// Create node with peer | |
node1 = await createNode(peerId) | |
console.log("Self is", node1.peerId.toB58String()) | |
// Print access info | |
console.log("\nYou can access the server at these multiaddrs:") | |
const nets = os.networkInterfaces(); | |
const results = Object.create(null); // or just '{}', an empty object | |
for (const internal of [true, false]) { | |
let header | |
for (const name of Object.keys(nets)) { | |
for (const net of nets[name]) { | |
if (net.internal != internal || (net.family !== 'IPv4')) { // && net.family !== 'IPv6' | |
continue | |
} | |
if (!header) { | |
console.log(internal ? "\n(local)" : "\n(global)") | |
header = true | |
} | |
console.log(`/ip4/${net.address}/tcp/${port}/ws/p2p/${peerId.toB58String()}`); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment