@libp2p/webrtc 5.1.1-ff951f1a0 → 5.2.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.
@@ -15,6 +15,7 @@ export declare class WebRTCPeerListener extends TypedEventEmitter<ListenerEvents
15
15
  constructor(components: WebRTCPeerListenerComponents, init: WebRTCPeerListenerInit);
16
16
  listen(): Promise<void>;
17
17
  getAddrs(): Multiaddr[];
18
+ updateAnnounceAddrs(): void;
18
19
  close(): Promise<void>;
19
20
  }
20
21
  //# sourceMappingURL=listener.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"listener.d.ts","sourceRoot":"","sources":["../../../src/private-to-private/listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAGrD,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAIxD,MAAM,WAAW,4BAA4B;IAC3C,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,gBAAgB,CAAA;CACnC;AAED,MAAM,WAAW,sBAAsB;IACrC,kBAAkB,EAAE,eAAe,CAAA;CACpC;AAED,qBAAa,kBAAmB,SAAQ,iBAAiB,CAAC,cAAc,CAAE,YAAW,QAAQ;IAC3F,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IACnD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAiB;gBAEvC,UAAU,EAAE,4BAA4B,EAAE,IAAI,EAAE,sBAAsB;IAO7E,MAAM,IAAK,OAAO,CAAC,IAAI,CAAC;IAM9B,QAAQ,IAAK,SAAS,EAAE;IAalB,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;CAM9B"}
1
+ {"version":3,"file":"listener.d.ts","sourceRoot":"","sources":["../../../src/private-to-private/listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAGrD,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAIxD,MAAM,WAAW,4BAA4B;IAC3C,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,gBAAgB,CAAA;CACnC;AAED,MAAM,WAAW,sBAAsB;IACrC,kBAAkB,EAAE,eAAe,CAAA;CACpC;AAED,qBAAa,kBAAmB,SAAQ,iBAAiB,CAAC,cAAc,CAAE,YAAW,QAAQ;IAC3F,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IACnD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAiB;gBAEvC,UAAU,EAAE,4BAA4B,EAAE,IAAI,EAAE,sBAAsB;IAO7E,MAAM,IAAK,OAAO,CAAC,IAAI,CAAC;IAM9B,QAAQ,IAAK,SAAS,EAAE;IAaxB,mBAAmB,IAAK,IAAI;IAItB,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;CAM9B"}
@@ -26,6 +26,8 @@ export class WebRTCPeerListener extends TypedEventEmitter {
26
26
  }))
27
27
  .flat();
28
28
  }
29
+ updateAnnounceAddrs() {
30
+ }
29
31
  async close() {
30
32
  this.shutdownController.abort();
31
33
  queueMicrotask(() => {
@@ -1 +1 @@
1
- {"version":3,"file":"listener.js","sourceRoot":"","sources":["../../../src/private-to-private/listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,EAAE,GAAG,EAAE,MAAM,iCAAiC,CAAA;AACrD,OAAO,EAAE,GAAG,EAAE,OAAO,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AA;QAC/B,cAAc,CAAC,GAAG,EAAE;YAClB,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAA;QACjC,CAAC,CAAC,CAAA;IACJ,CAAC;CACF"}
@@ -12,6 +12,7 @@ export interface WebRTCDirectListenerInit {
12
12
  export declare class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> implements Listener {
13
13
  listen(): Promise<void>;
14
14
  getAddrs(): Multiaddr[];
15
+ updateAnnounceAddrs(): void;
15
16
  close(): Promise<void>;
16
17
  }
17
18
  //# sourceMappingURL=listener.browser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"listener.browser.d.ts","sourceRoot":"","sources":["../../../src/private-to-public/listener.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAErD,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,8BAA8B;IAC7C,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,gBAAgB,CAAA;CACnC;AAED,MAAM,WAAW,wBAAwB;IACvC,kBAAkB,EAAE,eAAe,CAAA;CACpC;AAED,qBAAa,oBAAqB,SAAQ,iBAAiB,CAAC,cAAc,CAAE,YAAW,QAAQ;IACvF,MAAM,IAAK,OAAO,CAAC,IAAI,CAAC;IAI9B,QAAQ,IAAK,SAAS,EAAE;IAIlB,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;CAG9B"}
1
+ {"version":3,"file":"listener.browser.d.ts","sourceRoot":"","sources":["../../../src/private-to-public/listener.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAErD,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,8BAA8B;IAC7C,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE,gBAAgB,CAAA;CACnC;AAED,MAAM,WAAW,wBAAwB;IACvC,kBAAkB,EAAE,eAAe,CAAA;CACpC;AAED,qBAAa,oBAAqB,SAAQ,iBAAiB,CAAC,cAAc,CAAE,YAAW,QAAQ;IACvF,MAAM,IAAK,OAAO,CAAC,IAAI,CAAC;IAI9B,QAAQ,IAAK,SAAS,EAAE;IAIxB,mBAAmB,IAAK,IAAI;IAItB,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;CAG9B"}
@@ -7,6 +7,8 @@ export class WebRTCDirectListener extends TypedEventEmitter {
7
7
  getAddrs() {
8
8
  return [];
9
9
  }
10
+ updateAnnounceAddrs() {
11
+ }
10
12
  async close() {
11
13
  }
12
14
  }
@@ -1 +1 @@
1
- {"version":3,"file":"listener.browser.js","sourceRoot":"","sources":["../../../src/private-to-public/listener.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAchD,MAAM,OAAO,oBAAqB,SAAQ,iBAAiC;IACzE,KAAK,CAAC,MAAM;QACV,MAAM,IAAI,kBAAkB,CAAC,gCAAgC,CAAC,CAAA;IAChE,CAAC;IAED,QAAQ;QACN,OAAO,EAAE,CAAA;IACX,CAAC;IAED,KAAK,CAAC,KAAK;IAEX,CAAC;CACF"}
1
+ {"version":3,"file":"listener.browser.js","sourceRoot":"","sources":["../../../src/private-to-public/listener.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAchD,MAAM,OAAO,oBAAqB,SAAQ,iBAAiC;IACzE,KAAK,CAAC,MAAM;QACV,MAAM,IAAI,kBAAkB,CAAC,gCAAgC,CAAC,CAAA;IAChE,CAAC;IAED,QAAQ;QACN,OAAO,EAAE,CAAA;IACX,CAAC;IAED,mBAAmB;IAEnB,CAAC;IAED,KAAK,CAAC,KAAK;IAEX,CAAC;CACF"}
@@ -33,6 +33,7 @@ export declare class WebRTCDirectListener extends TypedEventEmitter<ListenerEven
33
33
  private startUDPMuxServer;
34
34
  private incomingConnection;
35
35
  getAddrs(): Multiaddr[];
36
+ updateAnnounceAddrs(multiaddrs: Multiaddr[]): void;
36
37
  close(): Promise<void>;
37
38
  }
38
39
  //# sourceMappingURL=listener.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"listener.d.ts","sourceRoot":"","sources":["../../../src/private-to-public/listener.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAUrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAG3E,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,QAAQ,EAAE,eAAe,EAAU,YAAY,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC/I,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AASxD,MAAM,WAAW,8BAA8B;IAC7C,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,QAAQ,CAAA;IAClB,YAAY,CAAC,EAAE,oBAAoB,EAAE,CAAA;IACrC,iBAAiB,CAAC,EAAE,MAAM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BAAiB;YAuCX,kBAAkB;IAgDhC,QAAQ,IAAK,SAAS,EAAE;IAIxB,mBAAmB,CAAE,UAAU,EAAE,SAAS,EAAE,GAAG,IAAI;IA4B7C,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;CAqB9B"}
@@ -1,8 +1,8 @@
1
1
  import { networkInterfaces } from 'node:os';
2
2
  import { isIPv4, isIPv6 } from '@chainsafe/is-ip';
3
3
  import { TypedEventEmitter } from '@libp2p/interface';
4
- import { multiaddr, protocols } from '@multiformats/multiaddr';
5
- import { IP4 } from '@multiformats/multiaddr-matcher';
4
+ import { multiaddr, protocols, fromStringTuples } from '@multiformats/multiaddr';
5
+ import { IP4, WebRTCDirect } from '@multiformats/multiaddr-matcher';
6
6
  import { Crypto } from '@peculiar/webcrypto';
7
7
  import getPort from 'get-port';
8
8
  import pWaitFor from 'p-wait-for';
@@ -15,6 +15,8 @@ const crypto = new Crypto();
15
15
  * The time to wait, in milliseconds, for the data channel handshake to complete
16
16
  */
17
17
  const HANDSHAKE_TIMEOUT_MS = 10_000;
18
+ const CODEC_WEBRTC_DIRECT = 0x0118;
19
+ const CODEC_CERTHASH = 0x01d2;
18
20
  const UDP_PROTOCOL = protocols('udp');
19
21
  const IP4_PROTOCOL = protocols('ip4');
20
22
  const IP6_PROTOCOL = protocols('ip6');
@@ -32,7 +34,7 @@ export class WebRTCDirectListener extends TypedEventEmitter {
32
34
  this.init = init;
33
35
  this.components = components;
34
36
  this.multiaddrs = [];
35
- this.servers = {};
37
+ this.servers = [];
36
38
  this.connections = new Map();
37
39
  this.log = components.logger.forComponent('libp2p:webrtc-direct:listener');
38
40
  this.certificate = init.certificates?.[0];
@@ -67,38 +69,56 @@ export class WebRTCDirectListener extends TypedEventEmitter {
67
69
  // single mux listener. This is necessary because libjuice binds to all
68
70
  // interfaces for a given port so we we need to key on just the port number
69
71
  // not the host + the port number
70
- if (this.servers[port] == null) {
71
- this.servers[port] = this.startUDPMuxServer(host, port);
72
+ let existingServer = this.servers.find(s => s.port === port);
73
+ // if the server has not been started yet, or the port is a wildcard port
74
+ // and there is already a wildcard port for this address family, start a new
75
+ // UDP mux server
76
+ const wildcardPorts = port === 0 && existingServer?.port === 0;
77
+ const sameAddressFamily = (existingServer?.isIPv4 === true && isIPv4(host)) || (existingServer?.isIPv6 === true && isIPv6(host));
78
+ if (existingServer == null || (wildcardPorts && sameAddressFamily)) {
79
+ existingServer = this.startUDPMuxServer(host, port);
80
+ this.servers.push(existingServer);
72
81
  }
73
- const server = await this.servers[port];
82
+ const server = await existingServer.server;
74
83
  const address = server.address();
75
84
  getNetworkAddresses(host, address.port, ipVersion).forEach((ma) => {
76
85
  this.multiaddrs.push(multiaddr(`${ma}/webrtc-direct/certhash/${this.certificate?.certhash}`));
77
86
  });
78
87
  this.safeDispatchEvent('listening');
79
88
  }
80
- async startUDPMuxServer(host, port) {
81
- if (port === 0) {
82
- // libjuice doesn't map 0 to a random free port so we have to do it
83
- // ourselves
84
- port = await getPort();
85
- }
86
- // ensure we have a certificate
87
- if (this.certificate == null) {
88
- const keyPair = await crypto.subtle.generateKey({
89
- name: 'ECDSA',
90
- namedCurve: 'P-256'
91
- }, true, ['sign', 'verify']);
92
- this.certificate = await generateTransportCertificate(keyPair, {
93
- days: 365 * 10
94
- });
95
- }
96
- return stunListener(host, port, this.log, (ufrag, remoteHost, remotePort) => {
97
- this.incomingConnection(ufrag, remoteHost, remotePort)
98
- .catch(err => {
99
- this.log.error('error processing incoming STUN request', err);
100
- });
101
- });
89
+ startUDPMuxServer(host, port) {
90
+ return {
91
+ port,
92
+ isIPv4: isIPv4(host),
93
+ isIPv6: isIPv6(host),
94
+ server: Promise.resolve()
95
+ .then(async () => {
96
+ if (port === 0) {
97
+ // libjuice doesn't map 0 to a random free port so we have to do it
98
+ // ourselves
99
+ port = await getPort();
100
+ }
101
+ // ensure we have a certificate
102
+ if (this.certificate == null) {
103
+ const keyPair = await crypto.subtle.generateKey({
104
+ name: 'ECDSA',
105
+ namedCurve: 'P-256'
106
+ }, true, ['sign', 'verify']);
107
+ const certificate = await generateTransportCertificate(keyPair, {
108
+ days: 365 * 10
109
+ });
110
+ if (this.certificate == null) {
111
+ this.certificate = certificate;
112
+ }
113
+ }
114
+ return stunListener(host, port, this.log, (ufrag, remoteHost, remotePort) => {
115
+ this.incomingConnection(ufrag, remoteHost, remotePort)
116
+ .catch(err => {
117
+ this.log.error('error processing incoming STUN request', err);
118
+ });
119
+ });
120
+ })
121
+ };
102
122
  }
103
123
  async incomingConnection(ufrag, remoteHost, remotePort) {
104
124
  const key = `${remoteHost}:${remotePort}:${ufrag}`;
@@ -145,13 +165,34 @@ export class WebRTCDirectListener extends TypedEventEmitter {
145
165
  getAddrs() {
146
166
  return this.multiaddrs;
147
167
  }
168
+ updateAnnounceAddrs(multiaddrs) {
169
+ for (let i = 0; i < multiaddrs.length; i++) {
170
+ let ma = multiaddrs[i];
171
+ if (!WebRTCDirect.exactMatch(ma)) {
172
+ continue;
173
+ }
174
+ // add the certhash if it is missing
175
+ const tuples = ma.stringTuples();
176
+ for (let j = 0; j < tuples.length; j++) {
177
+ if (tuples[j][0] !== CODEC_WEBRTC_DIRECT) {
178
+ continue;
179
+ }
180
+ const certhashIndex = j + 1;
181
+ if (tuples[certhashIndex] == null || tuples[certhashIndex][0] !== CODEC_CERTHASH) {
182
+ tuples.splice(certhashIndex, 0, [CODEC_CERTHASH, this.certificate?.certhash]);
183
+ ma = fromStringTuples(tuples);
184
+ multiaddrs[i] = ma;
185
+ }
186
+ }
187
+ }
188
+ }
148
189
  async close() {
149
190
  for (const connection of this.connections.values()) {
150
191
  connection.close();
151
192
  }
152
193
  // stop all UDP mux listeners
153
- await Promise.all(Object.values(this.servers).map(async (p) => {
154
- const server = await p;
194
+ await Promise.all(this.servers.map(async (p) => {
195
+ const server = await p.server;
155
196
  await server.close();
156
197
  }));
157
198
  // RTCPeerConnections will be removed from the connections map when their
@@ -1 +1 @@
1
- {"version":3,"file":"listener.js","sourceRoot":"","sources":["../../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AAO,CAAC,EAAE,CAAC,MAAM,OAAO,IAAI,OAAO,QAAQ,IAAI,EAAE,CAAC,CAAA;IAC3D,CAAC;IAED,OAAO;QACL,MAAM,OAAO,IAAI,IAAI,QAAQ,IAAI,EAAE;KACpC,CAAA;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ {
2
+ "DataChannelOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.DataChannelOptions.html",
3
+ ".:DataChannelOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.DataChannelOptions.html",
4
+ "TransportCertificate": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.TransportCertificate.html",
5
+ ".:TransportCertificate": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.TransportCertificate.html",
6
+ "WebRTCDirectTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCDirectTransportComponents.html",
7
+ "WebRTCTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCTransportComponents.html",
8
+ "WebRTCTransportDirectInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCTransportDirectInit.html",
9
+ "WebRTCTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webrtc.WebRTCTransportInit.html",
10
+ "webRTC": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTC.html",
11
+ ".:webRTC": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTC.html",
12
+ "webRTCDirect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTCDirect.html",
13
+ ".:webRTCDirect": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webrtc.webRTCDirect.html"
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/webrtc",
3
- "version": "5.1.1-ff951f1a0",
3
+ "version": "5.2.0",
4
4
  "description": "A libp2p transport using WebRTC connections",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-webrtc#readme",
@@ -54,11 +54,11 @@
54
54
  "@chainsafe/is-ip": "^2.0.2",
55
55
  "@chainsafe/libp2p-noise": "^16.0.0",
56
56
  "@ipshipyard/node-datachannel": "^0.26.4",
57
- "@libp2p/interface": "2.5.0-ff951f1a0",
58
- "@libp2p/interface-internal": "2.3.1-ff951f1a0",
59
- "@libp2p/peer-id": "5.0.12-ff951f1a0",
60
- "@libp2p/utils": "6.5.1-ff951f1a0",
61
- "@multiformats/multiaddr": "^12.3.3",
57
+ "@libp2p/interface": "^2.6.0",
58
+ "@libp2p/interface-internal": "^2.3.2",
59
+ "@libp2p/peer-id": "^5.0.13",
60
+ "@libp2p/utils": "^6.5.2",
61
+ "@multiformats/multiaddr": "^12.4.0",
62
62
  "@multiformats/multiaddr-matcher": "^1.6.0",
63
63
  "@peculiar/webcrypto": "^1.5.0",
64
64
  "@peculiar/x509": "^1.11.0",
@@ -83,9 +83,9 @@
83
83
  "uint8arrays": "^5.1.0"
84
84
  },
85
85
  "devDependencies": {
86
- "@libp2p/crypto": "5.0.11-ff951f1a0",
87
- "@libp2p/interface-compliance-tests": "6.3.1-ff951f1a0",
88
- "@libp2p/logger": "5.1.8-ff951f1a0",
86
+ "@libp2p/crypto": "^5.0.12",
87
+ "@libp2p/interface-compliance-tests": "^6.3.2",
88
+ "@libp2p/logger": "^5.1.9",
89
89
  "@types/sinon": "^17.0.3",
90
90
  "aegir": "^45.0.5",
91
91
  "delay": "^6.0.0",
@@ -46,6 +46,10 @@ export class WebRTCPeerListener extends TypedEventEmitter<ListenerEvents> implem
46
46
  .flat()
47
47
  }
48
48
 
49
+ updateAnnounceAddrs (): void {
50
+
51
+ }
52
+
49
53
  async close (): Promise<void> {
50
54
  this.shutdownController.abort()
51
55
  queueMicrotask(() => {
@@ -22,6 +22,10 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
22
22
  return []
23
23
  }
24
24
 
25
+ updateAnnounceAddrs (): void {
26
+
27
+ }
28
+
25
29
  async close (): Promise<void> {
26
30
 
27
31
  }
@@ -1,8 +1,8 @@
1
1
  import { networkInterfaces } from 'node:os'
2
2
  import { isIPv4, isIPv6 } from '@chainsafe/is-ip'
3
3
  import { TypedEventEmitter } from '@libp2p/interface'
4
- import { multiaddr, protocols } from '@multiformats/multiaddr'
5
- import { IP4 } from '@multiformats/multiaddr-matcher'
4
+ import { multiaddr, protocols, fromStringTuples } from '@multiformats/multiaddr'
5
+ import { IP4, WebRTCDirect } from '@multiformats/multiaddr-matcher'
6
6
  import { Crypto } from '@peculiar/webcrypto'
7
7
  import getPort from 'get-port'
8
8
  import pWaitFor from 'p-wait-for'
@@ -22,6 +22,8 @@ const crypto = new Crypto()
22
22
  * The time to wait, in milliseconds, for the data channel handshake to complete
23
23
  */
24
24
  const HANDSHAKE_TIMEOUT_MS = 10_000
25
+ const CODEC_WEBRTC_DIRECT = 0x0118
26
+ const CODEC_CERTHASH = 0x01d2
25
27
 
26
28
  export interface WebRTCDirectListenerComponents {
27
29
  peerId: PeerId
@@ -47,8 +49,15 @@ const UDP_PROTOCOL = protocols('udp')
47
49
  const IP4_PROTOCOL = protocols('ip4')
48
50
  const IP6_PROTOCOL = protocols('ip6')
49
51
 
52
+ interface UDPMuxServer {
53
+ server: Promise<StunServer>
54
+ isIPv4: boolean
55
+ isIPv6: boolean
56
+ port: number
57
+ }
58
+
50
59
  export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> implements Listener {
51
- private readonly servers: Record<number, Promise<StunServer>>
60
+ private readonly servers: UDPMuxServer[]
52
61
  private readonly multiaddrs: Multiaddr[]
53
62
  private certificate?: TransportCertificate
54
63
  private readonly connections: Map<string, DirectRTCPeerConnection>
@@ -63,7 +72,7 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
63
72
  this.init = init
64
73
  this.components = components
65
74
  this.multiaddrs = []
66
- this.servers = {}
75
+ this.servers = []
67
76
  this.connections = new Map()
68
77
  this.log = components.logger.forComponent('libp2p:webrtc-direct:listener')
69
78
  this.certificate = init.certificates?.[0]
@@ -103,11 +112,20 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
103
112
  // single mux listener. This is necessary because libjuice binds to all
104
113
  // interfaces for a given port so we we need to key on just the port number
105
114
  // not the host + the port number
106
- if (this.servers[port] == null) {
107
- this.servers[port] = this.startUDPMuxServer(host, port)
115
+ let existingServer = this.servers.find(s => s.port === port)
116
+
117
+ // if the server has not been started yet, or the port is a wildcard port
118
+ // and there is already a wildcard port for this address family, start a new
119
+ // UDP mux server
120
+ const wildcardPorts = port === 0 && existingServer?.port === 0
121
+ const sameAddressFamily = (existingServer?.isIPv4 === true && isIPv4(host)) || (existingServer?.isIPv6 === true && isIPv6(host))
122
+
123
+ if (existingServer == null || (wildcardPorts && sameAddressFamily)) {
124
+ existingServer = this.startUDPMuxServer(host, port)
125
+ this.servers.push(existingServer)
108
126
  }
109
127
 
110
- const server = await this.servers[port]
128
+ const server = await existingServer.server
111
129
  const address = server.address()
112
130
 
113
131
  getNetworkAddresses(host, address.port, ipVersion).forEach((ma) => {
@@ -117,31 +135,43 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
117
135
  this.safeDispatchEvent('listening')
118
136
  }
119
137
 
120
- private async startUDPMuxServer (host: string, port: number): Promise<StunServer> {
121
- if (port === 0) {
122
- // libjuice doesn't map 0 to a random free port so we have to do it
123
- // ourselves
124
- port = await getPort()
125
- }
126
-
127
- // ensure we have a certificate
128
- if (this.certificate == null) {
129
- const keyPair = await crypto.subtle.generateKey({
130
- name: 'ECDSA',
131
- namedCurve: 'P-256'
132
- }, true, ['sign', 'verify'])
133
-
134
- this.certificate = await generateTransportCertificate(keyPair, {
135
- days: 365 * 10
136
- })
137
- }
138
-
139
- return stunListener(host, port, this.log, (ufrag, remoteHost, remotePort) => {
140
- this.incomingConnection(ufrag, remoteHost, remotePort)
141
- .catch(err => {
142
- this.log.error('error processing incoming STUN request', err)
138
+ private startUDPMuxServer (host: string, port: number): UDPMuxServer {
139
+ return {
140
+ port,
141
+ isIPv4: isIPv4(host),
142
+ isIPv6: isIPv6(host),
143
+ server: Promise.resolve()
144
+ .then(async (): Promise<StunServer> => {
145
+ if (port === 0) {
146
+ // libjuice doesn't map 0 to a random free port so we have to do it
147
+ // ourselves
148
+ port = await getPort()
149
+ }
150
+
151
+ // ensure we have a certificate
152
+ if (this.certificate == null) {
153
+ const keyPair = await crypto.subtle.generateKey({
154
+ name: 'ECDSA',
155
+ namedCurve: 'P-256'
156
+ }, true, ['sign', 'verify'])
157
+
158
+ const certificate = await generateTransportCertificate(keyPair, {
159
+ days: 365 * 10
160
+ })
161
+
162
+ if (this.certificate == null) {
163
+ this.certificate = certificate
164
+ }
165
+ }
166
+
167
+ return stunListener(host, port, this.log, (ufrag, remoteHost, remotePort) => {
168
+ this.incomingConnection(ufrag, remoteHost, remotePort)
169
+ .catch(err => {
170
+ this.log.error('error processing incoming STUN request', err)
171
+ })
172
+ })
143
173
  })
144
- })
174
+ }
145
175
  }
146
176
 
147
177
  private async incomingConnection (ufrag: string, remoteHost: string, remotePort: number): Promise<void> {
@@ -196,6 +226,34 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
196
226
  return this.multiaddrs
197
227
  }
198
228
 
229
+ updateAnnounceAddrs (multiaddrs: Multiaddr[]): void {
230
+ for (let i = 0; i < multiaddrs.length; i++) {
231
+ let ma = multiaddrs[i]
232
+
233
+ if (!WebRTCDirect.exactMatch(ma)) {
234
+ continue
235
+ }
236
+
237
+ // add the certhash if it is missing
238
+ const tuples = ma.stringTuples()
239
+
240
+ for (let j = 0; j < tuples.length; j++) {
241
+ if (tuples[j][0] !== CODEC_WEBRTC_DIRECT) {
242
+ continue
243
+ }
244
+
245
+ const certhashIndex = j + 1
246
+
247
+ if (tuples[certhashIndex] == null || tuples[certhashIndex][0] !== CODEC_CERTHASH) {
248
+ tuples.splice(certhashIndex, 0, [CODEC_CERTHASH, this.certificate?.certhash])
249
+
250
+ ma = fromStringTuples(tuples)
251
+ multiaddrs[i] = ma
252
+ }
253
+ }
254
+ }
255
+ }
256
+
199
257
  async close (): Promise<void> {
200
258
  for (const connection of this.connections.values()) {
201
259
  connection.close()
@@ -203,8 +261,8 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
203
261
 
204
262
  // stop all UDP mux listeners
205
263
  await Promise.all(
206
- Object.values(this.servers).map(async p => {
207
- const server = await p
264
+ this.servers.map(async p => {
265
+ const server = await p.server
208
266
  await server.close()
209
267
  })
210
268
  )