libp2p-mdns
Advanced tools
Comparing version 0.5.2 to 0.6.0
{ | ||
"name": "libp2p-mdns", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "Node.js libp2p mDNS discovery implementation for peer discovery", | ||
@@ -33,13 +33,13 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"aegir": "^9.2.1", | ||
"aegir": "^9.4.0", | ||
"async": "^2.1.4", | ||
"chai": "^3.5.0", | ||
"pre-commit": "^1.2.0" | ||
"pre-commit": "^1.2.2" | ||
}, | ||
"dependencies": { | ||
"libp2p-tcp": "^0.9.1", | ||
"multiaddr": "^2.1.1", | ||
"multiaddr": "^2.2.0", | ||
"multicast-dns": "^6.1.0", | ||
"peer-id": "^0.8.0", | ||
"peer-info": "^0.8.1" | ||
"peer-id": "^0.8.1", | ||
"peer-info": "^0.8.2" | ||
}, | ||
@@ -46,0 +46,0 @@ "contributors": [ |
libp2p-mdns JavaScript implementation | ||
=============================================== | ||
===================================== | ||
@@ -7,8 +7,8 @@ [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) | ||
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) | ||
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-mdns-discovery/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-mdns-discovery?branch=master) | ||
[![Travis CI](https://travis-ci.org/libp2p/js-libp2p-mdns-discovery.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-mdns-discovery) | ||
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-mdns-discovery.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-mdns-discovery) | ||
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-mdns-discovery.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-mdns-discovery) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-mdns/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-mdns?branch=master) | ||
[![Travis CI](https://travis-ci.org/libp2p/js-libp2p-mdns.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-mdns) | ||
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-mdns.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-mdns) | ||
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-mdns.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-mdns) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
> Node.js libp2p mDNS discovery implementation | ||
> JavaScript libp2p MulticastDNS discovery implementation | ||
@@ -18,5 +18,5 @@ # Usage | ||
```JavaScript | ||
var MulticastDNS = require('libp2p-mdns') | ||
var MDNS = require('libp2p-mdns') | ||
var mdns = new MulticastDNS(libp2pNodeInstance, options) | ||
var mdns = new MDNS(peerInfo, options) | ||
@@ -26,2 +26,5 @@ mdns.on('peer', (peerInfo) => { | ||
}) | ||
// Broadcast for 20 seconds | ||
mdns.start(() => setTimeout(() => mdns.stop(() => {}), 20 * 1000)) | ||
``` | ||
@@ -33,2 +36,1 @@ | ||
- `serviceTag` - name of the service announced | ||
- `verify` - Verifies if we can establish a connection with the peer, before emitting a `peer` event |
179
src/index.js
'use strict' | ||
const multicastDNS = require('multicast-dns') | ||
const Id = require('peer-id') | ||
const Peer = require('peer-info') | ||
const Multiaddr = require('multiaddr') | ||
const EventEmitter = require('events').EventEmitter | ||
const debug = require('debug') | ||
const log = debug('libp2p:mdns') | ||
const EventEmitter = require('events').EventEmitter | ||
const os = require('os') | ||
const TCP = require('libp2p-tcp') | ||
const tcp = new TCP() | ||
const query = require('./query') | ||
@@ -17,157 +12,41 @@ class MulticastDNS extends EventEmitter { | ||
super() | ||
options = options || {} | ||
const broadcast = options.broadcast !== false | ||
const interval = options.interval || (1e3 * 10) // default: 10 seconds | ||
const serviceTag = options.serviceTag || 'libp2p-mdns-default.local' | ||
const port = options.port || 5353 | ||
const self = this // for event emitter | ||
this.broadcast = options.broadcast !== false | ||
this.interval = options.interval || (1e3 * 10) | ||
this.serviceTag = options.serviceTag || '_ipfs-discovery._udp' | ||
this.port = options.port || 5353 | ||
this.peerInfo = peerInfo | ||
} | ||
const mdns = multicastDNS({ port: port }) | ||
start (callback) { | ||
const self = this | ||
const mdns = multicastDNS({ port: this.port }) | ||
// query the network | ||
this.mdns = mdns | ||
mdns.on('response', gotResponse) | ||
queryLAN() | ||
query.queryLAN(this.mdns, this.serviceTag, this.interval) | ||
function queryLAN () { | ||
setInterval(() => { | ||
mdns.query({ | ||
questions: [{ | ||
name: serviceTag, | ||
type: 'PTR' | ||
}] | ||
}) | ||
}, interval) | ||
} | ||
function gotResponse (rsp) { | ||
if (!rsp.answers) { return } | ||
const answers = { | ||
ptr: {}, | ||
srv: {}, | ||
txt: {}, | ||
a: [], | ||
aaaa: [] | ||
} | ||
rsp.answers.forEach((answer) => { | ||
switch (answer.type) { | ||
case 'PTR': answers.ptr = answer; break | ||
case 'SRV': answers.srv = answer; break | ||
case 'TXT': answers.txt = answer; break | ||
case 'A': answers.a.push(answer); break | ||
case 'AAAA': answers.aaaa.push(answer); break | ||
default: break | ||
} | ||
}) | ||
if (answers.ptr.name !== serviceTag) { | ||
return | ||
} | ||
const b58Id = answers.txt.data.toString() | ||
const port = answers.srv.data.port | ||
const multiaddrs = [] | ||
answers.a.forEach((a) => { | ||
multiaddrs.push(new Multiaddr('/ip4/' + a.data + '/tcp/' + port)) | ||
}) | ||
// TODO Create multiaddrs from AAAA (IPv6) records as well | ||
if (peerInfo.id.toB58String() === b58Id) { | ||
return // replied to myself, ignore | ||
} | ||
log('peer found -', b58Id) | ||
const peerId = Id.createFromB58String(b58Id) | ||
Peer.create(peerId, (err, peerFound) => { | ||
mdns.on('response', (event) => { | ||
query.gotResponse(event, this.peerInfo, this.serviceTag, (err, foundPeer) => { | ||
if (err) { | ||
return log('Error creating PeerInfo from new found peer', err) | ||
return log('Error processing peer response', err) | ||
} | ||
multiaddrs.forEach((addr) => { | ||
peerFound.multiaddr.add(addr) | ||
}) | ||
self.emit('peer', peerFound) | ||
self.emit('peer', foundPeer) | ||
}) | ||
} | ||
}) | ||
// answer to queries | ||
mdns.on('query', (event) => { | ||
query.gotQuery(event, this.mdns, this.peerInfo, this.serviceTag, this.broadcast) | ||
}) | ||
mdns.on('query', gotQuery) | ||
setImmediate(() => callback()) | ||
} | ||
function gotQuery (qry) { | ||
if (!broadcast) { return } | ||
// Only announce TCP multiaddrs for now | ||
const multiaddrs = tcp.filter(peerInfo.multiaddrs) | ||
if (multiaddrs.length === 0) { return } | ||
if (qry.questions[0] && qry.questions[0].name === serviceTag) { | ||
const answers = [] | ||
answers.push({ | ||
name: serviceTag, | ||
type: 'PTR', | ||
class: 1, | ||
ttl: 120, | ||
data: peerInfo.id.toB58String() + '.' + serviceTag | ||
}) | ||
// TODO announce all the TCP multiaddrs | ||
const port = multiaddrs[0].toString().split('/')[4] | ||
answers.push({ | ||
name: peerInfo.id.toB58String() + '.' + serviceTag, | ||
type: 'SRV', | ||
class: 1, | ||
ttl: 120, | ||
data: { | ||
priority: 10, | ||
weight: 1, | ||
port: port, | ||
target: os.hostname() | ||
} | ||
}) | ||
answers.push({ | ||
name: peerInfo.id.toB58String() + '.' + serviceTag, | ||
type: 'TXT', | ||
class: 1, | ||
ttl: 120, | ||
data: peerInfo.id.toB58String() | ||
}) | ||
multiaddrs.forEach((ma) => { | ||
if (ma.protoNames()[0] === 'ip4') { | ||
answers.push({ | ||
name: os.hostname(), | ||
type: 'A', | ||
class: 1, | ||
ttl: 120, | ||
data: ma.toString().split('/')[2] | ||
}) | ||
return | ||
} | ||
if (ma.protoNames()[0] === 'ip6') { | ||
answers.push({ | ||
name: os.hostname(), | ||
type: 'AAAA', | ||
class: 1, | ||
ttl: 120, | ||
data: ma.toString().split('/')[2] | ||
}) | ||
return | ||
} | ||
}) | ||
mdns.respond(answers) | ||
} | ||
stop (callback) { | ||
if (!this.mdns) { | ||
callback(new Error('MulticastDNS service had not started yet')) | ||
} else { | ||
this.mdns.destroy(callback) | ||
this.mdns = undefined | ||
} | ||
@@ -174,0 +53,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33
11526
200
1
Updatedmultiaddr@^2.2.0
Updatedpeer-id@^0.8.1
Updatedpeer-info@^0.8.2