@libp2p/mdns 6.0.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -11
- package/dist/index.min.js +4 -4
- package/dist/src/index.d.ts +1 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +24 -55
- package/dist/src/index.js.map +1 -1
- package/dist/src/query.d.ts +4 -5
- package/dist/src/query.d.ts.map +1 -1
- package/dist/src/query.js +38 -82
- package/dist/src/query.js.map +1 -1
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +9 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +13 -15
- package/src/index.ts +38 -67
- package/src/query.ts +42 -101
- package/src/utils.ts +9 -0
- package/dist/src/compat/constants.d.ts +0 -5
- package/dist/src/compat/constants.d.ts.map +0 -1
- package/dist/src/compat/constants.js +0 -5
- package/dist/src/compat/constants.js.map +0 -1
- package/dist/src/compat/index.d.ts +0 -20
- package/dist/src/compat/index.d.ts.map +0 -1
- package/dist/src/compat/index.js +0 -50
- package/dist/src/compat/index.js.map +0 -1
- package/dist/src/compat/querier.d.ts +0 -31
- package/dist/src/compat/querier.d.ts.map +0 -1
- package/dist/src/compat/querier.js +0 -123
- package/dist/src/compat/querier.js.map +0 -1
- package/dist/src/compat/responder.d.ts +0 -13
- package/dist/src/compat/responder.d.ts.map +0 -1
- package/dist/src/compat/responder.js +0 -94
- package/dist/src/compat/responder.js.map +0 -1
- package/dist/src/compat/utils.d.ts +0 -5
- package/dist/src/compat/utils.d.ts.map +0 -1
- package/dist/src/compat/utils.js +0 -70
- package/dist/src/compat/utils.js.map +0 -1
- package/src/compat/constants.ts +0 -4
- package/src/compat/index.ts +0 -73
- package/src/compat/querier.ts +0 -171
- package/src/compat/responder.ts +0 -116
- package/src/compat/utils.ts +0 -82
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events';
|
|
2
|
-
import MDNS from 'multicast-dns';
|
|
3
|
-
import { logger } from '@libp2p/logger';
|
|
4
|
-
import { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } from './constants.js';
|
|
5
|
-
import { findPeerInfoInAnswers } from './utils.js';
|
|
6
|
-
import { symbol } from '@libp2p/interface-peer-discovery';
|
|
7
|
-
const log = logger('libp2p:mdns:compat:querier');
|
|
8
|
-
export class Querier extends EventEmitter {
|
|
9
|
-
constructor(components, init = {}) {
|
|
10
|
-
super();
|
|
11
|
-
const { queryInterval, queryPeriod } = init;
|
|
12
|
-
this.components = components;
|
|
13
|
-
this._init = {
|
|
14
|
-
// Re-query in leu of network change detection (every 60s by default)
|
|
15
|
-
queryInterval: queryInterval ?? 60000,
|
|
16
|
-
// Time for which the MDNS server will stay alive waiting for responses
|
|
17
|
-
// Must be less than options.queryInterval!
|
|
18
|
-
queryPeriod: Math.min(queryInterval ?? 60000, queryPeriod ?? 5000)
|
|
19
|
-
};
|
|
20
|
-
this._onResponse = this._onResponse.bind(this);
|
|
21
|
-
}
|
|
22
|
-
get [symbol]() {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
get [Symbol.toStringTag]() {
|
|
26
|
-
return '@libp2p/go-mdns-querier';
|
|
27
|
-
}
|
|
28
|
-
isStarted() {
|
|
29
|
-
return Boolean(this._handle);
|
|
30
|
-
}
|
|
31
|
-
start() {
|
|
32
|
-
this._handle = periodically(() => {
|
|
33
|
-
// Create a querier that queries multicast but gets responses unicast
|
|
34
|
-
const mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 });
|
|
35
|
-
mdns.on('response', this._onResponse);
|
|
36
|
-
// @ts-expect-error @types/multicast-dns are wrong
|
|
37
|
-
mdns.query({
|
|
38
|
-
id: nextId(),
|
|
39
|
-
questions: [{
|
|
40
|
-
name: SERVICE_TAG_LOCAL,
|
|
41
|
-
type: 'PTR',
|
|
42
|
-
class: 'IN'
|
|
43
|
-
}]
|
|
44
|
-
}, null, {
|
|
45
|
-
address: MULTICAST_IP,
|
|
46
|
-
port: MULTICAST_PORT
|
|
47
|
-
});
|
|
48
|
-
return {
|
|
49
|
-
stop: async () => {
|
|
50
|
-
mdns.removeListener('response', this._onResponse);
|
|
51
|
-
return await new Promise(resolve => mdns.destroy(resolve));
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}, {
|
|
55
|
-
period: this._init.queryPeriod,
|
|
56
|
-
interval: this._init.queryInterval
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
_onResponse(event, info) {
|
|
60
|
-
log.trace('received mDNS query response');
|
|
61
|
-
const answers = event.answers ?? [];
|
|
62
|
-
const peerInfo = findPeerInfoInAnswers(answers, this.components.peerId);
|
|
63
|
-
if (peerInfo == null) {
|
|
64
|
-
log('could not read peer data from query response');
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (peerInfo.multiaddrs.length === 0) {
|
|
68
|
-
log('could not parse multiaddrs from mDNS response');
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
log('discovered peer in mDNS qeury response %p', peerInfo.id);
|
|
72
|
-
this.dispatchEvent(new CustomEvent('peer', {
|
|
73
|
-
detail: peerInfo
|
|
74
|
-
}));
|
|
75
|
-
}
|
|
76
|
-
async stop() {
|
|
77
|
-
if (this._handle != null) {
|
|
78
|
-
await this._handle.stop();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Run `fn` for a certain period of time, and then wait for an interval before
|
|
84
|
-
* running it again. `fn` must return an object with a stop function, which is
|
|
85
|
-
* called when the period expires.
|
|
86
|
-
*/
|
|
87
|
-
function periodically(fn, options) {
|
|
88
|
-
let handle;
|
|
89
|
-
let timeoutId;
|
|
90
|
-
let stopped = false;
|
|
91
|
-
const reRun = () => {
|
|
92
|
-
handle = fn();
|
|
93
|
-
timeoutId = setTimeout(() => {
|
|
94
|
-
if (handle != null) {
|
|
95
|
-
handle.stop().catch(log);
|
|
96
|
-
}
|
|
97
|
-
if (!stopped) {
|
|
98
|
-
timeoutId = setTimeout(reRun, options.interval);
|
|
99
|
-
}
|
|
100
|
-
handle = null;
|
|
101
|
-
}, options.period);
|
|
102
|
-
};
|
|
103
|
-
reRun();
|
|
104
|
-
return {
|
|
105
|
-
async stop() {
|
|
106
|
-
stopped = true;
|
|
107
|
-
clearTimeout(timeoutId);
|
|
108
|
-
if (handle != null) {
|
|
109
|
-
await handle.stop();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
const nextId = (() => {
|
|
115
|
-
let id = 0;
|
|
116
|
-
return () => {
|
|
117
|
-
id++;
|
|
118
|
-
if (id === Number.MAX_SAFE_INTEGER)
|
|
119
|
-
id = 1;
|
|
120
|
-
return id;
|
|
121
|
-
};
|
|
122
|
-
})();
|
|
123
|
-
//# sourceMappingURL=querier.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"querier.js","sourceRoot":"","sources":["../../../src/compat/querier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACrE,OAAO,IAAI,MAAM,eAAe,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAIhF,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAA;AAIzD,MAAM,GAAG,GAAG,MAAM,CAAC,4BAA4B,CAAC,CAAA;AAehD,MAAM,OAAO,OAAQ,SAAQ,YAAiC;IAK5D,YAAa,UAA6B,EAAE,OAAoB,EAAE;QAChE,KAAK,EAAE,CAAA;QAEP,MAAM,EAAE,aAAa,EAAE,WAAW,EA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,KAAK,CAAA;IAEnB,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,MAAM,GAAG,EAAE,EAAE,CAAA;QACb,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;YAC1B,IAAI,MAAM,IAAI,IAAI,EAAE;gBAClB,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;aACzB;YAED,IAAI,CAAC,OAAO,EAAE;gBACZ,SAAS,GAAG,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;aAChD;YAED,MAAM,GAAG,IAAI,CAAA;QACf,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;IACpB,CAAC,CAAA;IAED,KAAK,EAAE,CAAA;IAEP,OAAO;QACL,KAAK,CAAC,IAAI;YACR,OAAO,GAAG,IAAI,CAAA;YACd,YAAY,CAAC,SAAS,CAAC,CAAA;YACvB,IAAI,MAAM,IAAI,IAAI,EAAE;gBAClB,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;aACpB;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE;IACnB,IAAI,EAAE,GAAG,CAAC,CAAA;IACV,OAAO,GAAG,EAAE;QACV,EAAE,EAAE,CAAA;QACJ,IAAI,EAAE,KAAK,MAAM,CAAC,gBAAgB;YAAE,EAAE,GAAG,CAAC,CAAA;QAC1C,OAAO,EAAE,CAAA;IACX,CAAC,CAAA;AACH,CAAC,CAAC,EAAE,CAAA"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { QueryPacket } from 'multicast-dns';
|
|
3
|
-
import type { RemoteInfo } from 'dgram';
|
|
4
|
-
import type { MulticastDNSComponents } from '../index.js';
|
|
5
|
-
export declare class Responder {
|
|
6
|
-
private readonly components;
|
|
7
|
-
private _mdns?;
|
|
8
|
-
constructor(components: MulticastDNSComponents);
|
|
9
|
-
start(): void;
|
|
10
|
-
_onQuery(event: QueryPacket, info: RemoteInfo): void;
|
|
11
|
-
stop(): Promise<void> | undefined;
|
|
12
|
-
}
|
|
13
|
-
//# sourceMappingURL=responder.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"responder.d.ts","sourceRoot":"","sources":["../../../src/compat/responder.ts"],"names":[],"mappings":";AACA,OAAa,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAIjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAEvC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AAIzD,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAwB;IACnD,OAAO,CAAC,KAAK,CAAC,CAAmB;gBAEpB,UAAU,EAAE,sBAAsB;IAK/C,KAAK;IAKL,QAAQ,CAAE,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU;IA8E9C,IAAI;CAYL"}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import OS from 'os';
|
|
2
|
-
import MDNS from 'multicast-dns';
|
|
3
|
-
import { logger } from '@libp2p/logger';
|
|
4
|
-
import { SERVICE_TAG_LOCAL } from './constants.js';
|
|
5
|
-
import { protocols } from '@multiformats/multiaddr';
|
|
6
|
-
const log = logger('libp2p:mdns:compat:responder');
|
|
7
|
-
export class Responder {
|
|
8
|
-
constructor(components) {
|
|
9
|
-
this.components = components;
|
|
10
|
-
this._onQuery = this._onQuery.bind(this);
|
|
11
|
-
}
|
|
12
|
-
start() {
|
|
13
|
-
this._mdns = MDNS();
|
|
14
|
-
this._mdns.on('query', this._onQuery);
|
|
15
|
-
}
|
|
16
|
-
_onQuery(event, info) {
|
|
17
|
-
const addresses = this.components.addressManager.getAddresses().reduce((acc, addr) => {
|
|
18
|
-
addr = addr.decapsulateCode(protocols('p2p').code);
|
|
19
|
-
if (addr.isThinWaistAddress()) {
|
|
20
|
-
acc.push(addr.toOptions());
|
|
21
|
-
}
|
|
22
|
-
return acc;
|
|
23
|
-
}, []);
|
|
24
|
-
// Only announce TCP for now
|
|
25
|
-
if (addresses.length === 0) {
|
|
26
|
-
log('no tcp addresses configured so cannot respond to mDNS query');
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const questions = event.questions ?? [];
|
|
30
|
-
// Only respond to queries for our service tag
|
|
31
|
-
if (!questions.some(q => q.name === SERVICE_TAG_LOCAL))
|
|
32
|
-
return;
|
|
33
|
-
log.trace('got query', event, info);
|
|
34
|
-
const answers = [];
|
|
35
|
-
const peerServiceTagLocal = `${this.components.peerId.toString()}.${SERVICE_TAG_LOCAL}`;
|
|
36
|
-
answers.push({
|
|
37
|
-
name: SERVICE_TAG_LOCAL,
|
|
38
|
-
type: 'PTR',
|
|
39
|
-
class: 'IN',
|
|
40
|
-
ttl: 120,
|
|
41
|
-
data: peerServiceTagLocal
|
|
42
|
-
});
|
|
43
|
-
answers.push({
|
|
44
|
-
name: peerServiceTagLocal,
|
|
45
|
-
type: 'TXT',
|
|
46
|
-
class: 'IN',
|
|
47
|
-
ttl: 120,
|
|
48
|
-
data: [Buffer.from(this.components.peerId.toString())]
|
|
49
|
-
});
|
|
50
|
-
addresses.forEach(ma => {
|
|
51
|
-
if (![4, 6].includes(ma.family)) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
answers.push({
|
|
55
|
-
name: peerServiceTagLocal,
|
|
56
|
-
type: 'SRV',
|
|
57
|
-
class: 'IN',
|
|
58
|
-
ttl: 120,
|
|
59
|
-
data: {
|
|
60
|
-
priority: 10,
|
|
61
|
-
weight: 1,
|
|
62
|
-
port: ma.port,
|
|
63
|
-
target: OS.hostname()
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
answers.push({
|
|
67
|
-
name: OS.hostname(),
|
|
68
|
-
type: ma.family === 4 ? 'A' : 'AAAA',
|
|
69
|
-
class: 'IN',
|
|
70
|
-
ttl: 120,
|
|
71
|
-
data: ma.host
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
if (this._mdns != null) {
|
|
75
|
-
log.trace('responding to query');
|
|
76
|
-
log.trace('query answers', answers);
|
|
77
|
-
this._mdns.respond(answers, info);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
stop() {
|
|
81
|
-
if (this._mdns != null) {
|
|
82
|
-
this._mdns.removeListener('query', this._onQuery);
|
|
83
|
-
return new Promise(resolve => {
|
|
84
|
-
if (this._mdns != null) {
|
|
85
|
-
this._mdns.destroy(resolve);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
resolve();
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
//# sourceMappingURL=responder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"responder.js","sourceRoot":"","sources":["../../../src/compat/responder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,IAAqB,MAAM,eAAe,CAAA;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,EAAmB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAKpE,MAAM,GAAG,GAAG,MAAM,CAAC,8BAA8B,CAAC,CAAA;AAElD,MAAM,OAAO,SAAS;IAIpB,YAAa,UAAkC;QAC7C,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC1C,CAAC;IAED,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,IAAI,EAAE,CAAA;QACnB,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IACvC,CAAC;IA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B,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;YACjD,OAAO,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE;gBACjC,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE;oBACtB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;iBAC5B;qBAAM;oBACL,OAAO,EAAE,CAAA;iBACV;YACH,CAAC,CAAC,CAAA;SACH;IACH,CAAC;CACF"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { PeerInfo } from '@libp2p/interface-peer-info';
|
|
2
|
-
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
3
|
-
import type { Answer } from 'dns-packet';
|
|
4
|
-
export declare function findPeerInfoInAnswers(answers: Answer[], ourPeerId: PeerId): PeerInfo | undefined;
|
|
5
|
-
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/compat/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAA;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAKvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAKxC,wBAAgB,qBAAqB,CAAE,OAAO,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS,CAsEjG"}
|
package/dist/src/compat/utils.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger';
|
|
2
|
-
import { peerIdFromString } from '@libp2p/peer-id';
|
|
3
|
-
import { multiaddr } from '@multiformats/multiaddr';
|
|
4
|
-
import { SERVICE_TAG_LOCAL } from './constants.js';
|
|
5
|
-
const log = logger('libp2p:mdns:compat:utils');
|
|
6
|
-
export function findPeerInfoInAnswers(answers, ourPeerId) {
|
|
7
|
-
const ptrRecord = answers.find(a => a.type === 'PTR' && a.name === SERVICE_TAG_LOCAL);
|
|
8
|
-
// Only deal with responses for our service tag
|
|
9
|
-
if (ptrRecord == null) {
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
log.trace('got response', SERVICE_TAG_LOCAL);
|
|
13
|
-
const txtRecord = answers.find(a => a.type === 'TXT');
|
|
14
|
-
if (txtRecord == null || txtRecord.type !== 'TXT') {
|
|
15
|
-
log('missing TXT record in response');
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
let peerIdStr;
|
|
19
|
-
try {
|
|
20
|
-
peerIdStr = txtRecord.data[0].toString();
|
|
21
|
-
}
|
|
22
|
-
catch (err) {
|
|
23
|
-
log('failed to extract peer ID from TXT record data', txtRecord, err);
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
let peerId;
|
|
27
|
-
try {
|
|
28
|
-
peerId = peerIdFromString(peerIdStr);
|
|
29
|
-
}
|
|
30
|
-
catch (err) {
|
|
31
|
-
log('failed to create peer ID from TXT record data', peerIdStr, err);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (ourPeerId.equals(peerId)) {
|
|
35
|
-
log('ignoring reply to myself');
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
const multiaddrs = [];
|
|
39
|
-
const hosts = {
|
|
40
|
-
A: {},
|
|
41
|
-
AAAA: {}
|
|
42
|
-
};
|
|
43
|
-
answers.forEach(answer => {
|
|
44
|
-
if (answer.type === 'A') {
|
|
45
|
-
hosts.A[answer.name] = answer.data;
|
|
46
|
-
}
|
|
47
|
-
if (answer.type === 'AAAA') {
|
|
48
|
-
hosts.AAAA[answer.name] = answer.data;
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
answers.forEach(answer => {
|
|
52
|
-
if (answer.type === 'SRV') {
|
|
53
|
-
if (hosts.A[answer.data.target] != null) {
|
|
54
|
-
multiaddrs.push(multiaddr(`/ip4/${hosts.A[answer.data.target]}/tcp/${answer.data.port}/p2p/${peerId.toString()}`));
|
|
55
|
-
}
|
|
56
|
-
else if (hosts.AAAA[answer.data.target] != null) {
|
|
57
|
-
multiaddrs.push(multiaddr(`/ip6/${hosts.AAAA[answer.data.target]}/tcp/${answer.data.port}/p2p/${peerId.toString()}`));
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
multiaddrs.push(multiaddr(`/dnsaddr/${answer.data.target}/tcp/${answer.data.port}/p2p/${peerId.toString()}`));
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
return {
|
|
65
|
-
id: peerId,
|
|
66
|
-
multiaddrs,
|
|
67
|
-
protocols: []
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/compat/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAGnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAElD,MAAM,GAAG,GAAG,MAAM,CAAC,0BAA0B,CAAC,CAAA;AAE9C,MAAM,UAAU,qBAAqB,CAAE,OAAiB,EAAE,SAAiB;IACzE,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC,IAAI,KAAK,iBAAiB,CAAC,CAAA;IAErF,+CAA+C;IAC/C,IAAI,SAAS,IAAI,IAAI,EAAE;QACrB,OAAM;KACP;IAED,GAAG,CAAC,KAAK,CAAC,cAAc,EAAE,iBAAiB,CAAC,C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AC,CAAA;IAEF,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACvB,IAAI,MAAM,CAAC,IAAI,KAAK,KAAK,EAAE;YACzB,IAAI,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE;gBACvC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,MAAM,CAAC,IAAI,CAAC,IAAI,QAAQ,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAA;aACnH;iBAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE;gBACjD,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,MAAM,CAAC,IAAI,CAAC,IAAI,QAAQ,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAA;aACtH;iBAAM;gBACL,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,MAAM,CAAC,IAAI,CAAC,MAAM,QAAQ,MAAM,CAAC,IAAI,CAAC,IAAI,QAAQ,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAA;aAC9G;SACF;IACH,CAAC,CAAC,CAAA;IAEF,OAAO;QACL,EAAE,EAAE,MAAM;QACV,UAAU;QACV,SAAS,EAAE,EAAE;KACd,CAAA;AACH,CAAC"}
|
package/src/compat/constants.ts
DELETED
package/src/compat/index.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Compatibility with Go libp2p MDNS
|
|
2
|
-
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
|
|
3
|
-
import { Responder } from './responder.js'
|
|
4
|
-
import { Querier } from './querier.js'
|
|
5
|
-
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
|
|
6
|
-
import { symbol } from '@libp2p/interface-peer-discovery'
|
|
7
|
-
import type { MulticastDNSComponents } from '../index.js'
|
|
8
|
-
|
|
9
|
-
export interface GoMulticastDNSInit {
|
|
10
|
-
queryPeriod?: number
|
|
11
|
-
queryInterval?: number
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class GoMulticastDNS extends EventEmitter<PeerDiscoveryEvents> implements PeerDiscovery {
|
|
15
|
-
private _started: boolean
|
|
16
|
-
private readonly _responder: Responder
|
|
17
|
-
private readonly _querier: Querier
|
|
18
|
-
|
|
19
|
-
constructor (components: MulticastDNSComponents, options: GoMulticastDNSInit = {}) {
|
|
20
|
-
super()
|
|
21
|
-
const { queryPeriod, queryInterval } = options
|
|
22
|
-
|
|
23
|
-
this._started = false
|
|
24
|
-
|
|
25
|
-
this._responder = new Responder(components)
|
|
26
|
-
this._querier = new Querier(components, {
|
|
27
|
-
queryInterval,
|
|
28
|
-
queryPeriod
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
this._querier.addEventListener('peer', (evt) => {
|
|
32
|
-
this.dispatchEvent(new CustomEvent('peer', { detail: evt.detail }))
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
get [symbol] (): true {
|
|
37
|
-
return true
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
get [Symbol.toStringTag] () {
|
|
41
|
-
return '@libp2p/go-mdns'
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
isStarted () {
|
|
45
|
-
return this._started
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async start () {
|
|
49
|
-
if (this.isStarted()) {
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this._started = true
|
|
54
|
-
|
|
55
|
-
await Promise.all([
|
|
56
|
-
this._responder.start(),
|
|
57
|
-
this._querier.start()
|
|
58
|
-
])
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async stop () {
|
|
62
|
-
if (!this.isStarted()) {
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
this._started = false
|
|
67
|
-
|
|
68
|
-
await Promise.all([
|
|
69
|
-
this._responder.stop(),
|
|
70
|
-
this._querier.stop()
|
|
71
|
-
])
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/compat/querier.ts
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
2
|
-
import MDNS from 'multicast-dns'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
|
-
import { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } from './constants.js'
|
|
5
|
-
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
|
|
6
|
-
import type { ResponsePacket } from 'multicast-dns'
|
|
7
|
-
import type { RemoteInfo } from 'dgram'
|
|
8
|
-
import { findPeerInfoInAnswers } from './utils.js'
|
|
9
|
-
import { symbol } from '@libp2p/interface-peer-discovery'
|
|
10
|
-
import type { Startable } from '@libp2p/interfaces/dist/src/startable.js'
|
|
11
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
12
|
-
|
|
13
|
-
const log = logger('libp2p:mdns:compat:querier')
|
|
14
|
-
|
|
15
|
-
export interface QuerierInit {
|
|
16
|
-
queryInterval?: number
|
|
17
|
-
queryPeriod?: number
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface QuerierComponents {
|
|
21
|
-
peerId: PeerId
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface Handle {
|
|
25
|
-
stop: () => Promise<void>
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class Querier extends EventEmitter<PeerDiscoveryEvents> implements PeerDiscovery, Startable {
|
|
29
|
-
private readonly _init: Required<QuerierInit>
|
|
30
|
-
private _handle?: Handle
|
|
31
|
-
private readonly components: QuerierComponents
|
|
32
|
-
|
|
33
|
-
constructor (components: QuerierComponents, init: QuerierInit = {}) {
|
|
34
|
-
super()
|
|
35
|
-
|
|
36
|
-
const { queryInterval, queryPeriod } = init
|
|
37
|
-
|
|
38
|
-
this.components = components
|
|
39
|
-
this._init = {
|
|
40
|
-
// Re-query in leu of network change detection (every 60s by default)
|
|
41
|
-
queryInterval: queryInterval ?? 60000,
|
|
42
|
-
// Time for which the MDNS server will stay alive waiting for responses
|
|
43
|
-
// Must be less than options.queryInterval!
|
|
44
|
-
queryPeriod: Math.min(
|
|
45
|
-
queryInterval ?? 60000,
|
|
46
|
-
queryPeriod ?? 5000
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
this._onResponse = this._onResponse.bind(this)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get [symbol] (): true {
|
|
53
|
-
return true
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
get [Symbol.toStringTag] () {
|
|
57
|
-
return '@libp2p/go-mdns-querier'
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
isStarted () {
|
|
61
|
-
return Boolean(this._handle)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
start () {
|
|
65
|
-
this._handle = periodically(() => {
|
|
66
|
-
// Create a querier that queries multicast but gets responses unicast
|
|
67
|
-
const mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 })
|
|
68
|
-
|
|
69
|
-
mdns.on('response', this._onResponse)
|
|
70
|
-
|
|
71
|
-
// @ts-expect-error @types/multicast-dns are wrong
|
|
72
|
-
mdns.query({
|
|
73
|
-
id: nextId(), // id > 0 for unicast response
|
|
74
|
-
questions: [{
|
|
75
|
-
name: SERVICE_TAG_LOCAL,
|
|
76
|
-
type: 'PTR',
|
|
77
|
-
class: 'IN'
|
|
78
|
-
}]
|
|
79
|
-
}, null, {
|
|
80
|
-
address: MULTICAST_IP,
|
|
81
|
-
port: MULTICAST_PORT
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
stop: async () => {
|
|
86
|
-
mdns.removeListener('response', this._onResponse)
|
|
87
|
-
return await new Promise(resolve => mdns.destroy(resolve))
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}, {
|
|
91
|
-
period: this._init.queryPeriod,
|
|
92
|
-
interval: this._init.queryInterval
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
_onResponse (event: ResponsePacket, info: RemoteInfo) {
|
|
97
|
-
log.trace('received mDNS query response')
|
|
98
|
-
const answers = event.answers ?? []
|
|
99
|
-
|
|
100
|
-
const peerInfo = findPeerInfoInAnswers(answers, this.components.peerId)
|
|
101
|
-
|
|
102
|
-
if (peerInfo == null) {
|
|
103
|
-
log('could not read peer data from query response')
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (peerInfo.multiaddrs.length === 0) {
|
|
108
|
-
log('could not parse multiaddrs from mDNS response')
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
log('discovered peer in mDNS qeury response %p', peerInfo.id)
|
|
113
|
-
|
|
114
|
-
this.dispatchEvent(new CustomEvent('peer', {
|
|
115
|
-
detail: peerInfo
|
|
116
|
-
}))
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async stop () {
|
|
120
|
-
if (this._handle != null) {
|
|
121
|
-
await this._handle.stop()
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Run `fn` for a certain period of time, and then wait for an interval before
|
|
128
|
-
* running it again. `fn` must return an object with a stop function, which is
|
|
129
|
-
* called when the period expires.
|
|
130
|
-
*/
|
|
131
|
-
function periodically (fn: () => Handle, options: { period: number, interval: number }) {
|
|
132
|
-
let handle: Handle | null
|
|
133
|
-
let timeoutId: NodeJS.Timer
|
|
134
|
-
let stopped = false
|
|
135
|
-
|
|
136
|
-
const reRun = () => {
|
|
137
|
-
handle = fn()
|
|
138
|
-
timeoutId = setTimeout(() => {
|
|
139
|
-
if (handle != null) {
|
|
140
|
-
handle.stop().catch(log)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (!stopped) {
|
|
144
|
-
timeoutId = setTimeout(reRun, options.interval)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
handle = null
|
|
148
|
-
}, options.period)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
reRun()
|
|
152
|
-
|
|
153
|
-
return {
|
|
154
|
-
async stop () {
|
|
155
|
-
stopped = true
|
|
156
|
-
clearTimeout(timeoutId)
|
|
157
|
-
if (handle != null) {
|
|
158
|
-
await handle.stop()
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const nextId = (() => {
|
|
165
|
-
let id = 0
|
|
166
|
-
return () => {
|
|
167
|
-
id++
|
|
168
|
-
if (id === Number.MAX_SAFE_INTEGER) id = 1
|
|
169
|
-
return id
|
|
170
|
-
}
|
|
171
|
-
})()
|