@libp2p/upnp-nat 3.0.5-bf0f74d66 → 3.0.5-c1d0b7fa4

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.
Files changed (41) hide show
  1. package/README.md +0 -40
  2. package/dist/src/check-external-address.d.ts.map +1 -1
  3. package/dist/src/check-external-address.js +2 -0
  4. package/dist/src/check-external-address.js.map +1 -1
  5. package/dist/src/constants.d.ts +0 -4
  6. package/dist/src/constants.d.ts.map +1 -1
  7. package/dist/src/constants.js +0 -4
  8. package/dist/src/constants.js.map +1 -1
  9. package/dist/src/gateway-finder.d.ts +26 -0
  10. package/dist/src/gateway-finder.d.ts.map +1 -0
  11. package/dist/src/gateway-finder.js +53 -0
  12. package/dist/src/gateway-finder.js.map +1 -0
  13. package/dist/src/index.d.ts +0 -125
  14. package/dist/src/index.d.ts.map +1 -1
  15. package/dist/src/index.js +0 -40
  16. package/dist/src/index.js.map +1 -1
  17. package/dist/src/upnp-nat.d.ts +1 -6
  18. package/dist/src/upnp-nat.d.ts.map +1 -1
  19. package/dist/src/upnp-nat.js +5 -20
  20. package/dist/src/upnp-nat.js.map +1 -1
  21. package/dist/src/upnp-port-mapper.d.ts +0 -1
  22. package/dist/src/upnp-port-mapper.d.ts.map +1 -1
  23. package/dist/src/upnp-port-mapper.js +10 -15
  24. package/dist/src/upnp-port-mapper.js.map +1 -1
  25. package/package.json +7 -7
  26. package/src/check-external-address.ts +2 -0
  27. package/src/constants.ts +0 -5
  28. package/src/gateway-finder.ts +75 -0
  29. package/src/index.ts +0 -133
  30. package/src/upnp-nat.ts +6 -28
  31. package/src/upnp-port-mapper.ts +10 -16
  32. package/dist/src/search-gateway-finder.d.ts +0 -27
  33. package/dist/src/search-gateway-finder.d.ts.map +0 -1
  34. package/dist/src/search-gateway-finder.js +0 -66
  35. package/dist/src/search-gateway-finder.js.map +0 -1
  36. package/dist/src/static-gateway-finder.d.ts +0 -23
  37. package/dist/src/static-gateway-finder.d.ts.map +0 -1
  38. package/dist/src/static-gateway-finder.js +0 -42
  39. package/dist/src/static-gateway-finder.js.map +0 -1
  40. package/src/search-gateway-finder.ts +0 -94
  41. package/src/static-gateway-finder.ts +0 -60
package/src/constants.ts CHANGED
@@ -1,7 +1,2 @@
1
- export const DEFAULT_INITIAL_GATEWAY_SEARCH_INTERVAL = 5_000
2
- export const DEFAULT_INITIAL_GATEWAY_SEARCH_TIMEOUT = 5_000
3
- export const DEFAULT_INITIAL_GATEWAY_SEARCH_MESSAGE_INTERVAL = 1_000
4
-
5
1
  export const DEFAULT_GATEWAY_SEARCH_TIMEOUT = 60_000
6
2
  export const DEFAULT_GATEWAY_SEARCH_INTERVAL = 300_000
7
- export const DEFAULT_GATEWAY_SEARCH_MESSAGE_INTERVAL = 10_000
@@ -0,0 +1,75 @@
1
+ import { TypedEventEmitter, start, stop } from '@libp2p/interface'
2
+ import { repeatingTask } from '@libp2p/utils/repeating-task'
3
+ import { DEFAULT_GATEWAY_SEARCH_INTERVAL, DEFAULT_GATEWAY_SEARCH_TIMEOUT } from './constants.js'
4
+ import type { Gateway, UPnPNAT } from '@achingbrain/nat-port-mapper'
5
+ import type { ComponentLogger, Logger } from '@libp2p/interface'
6
+ import type { RepeatingTask } from '@libp2p/utils/repeating-task'
7
+
8
+ export interface GatewayFinderComponents {
9
+ logger: ComponentLogger
10
+ }
11
+
12
+ export interface GatewayFinderInit {
13
+ portMappingClient: UPnPNAT
14
+ }
15
+
16
+ export interface GatewayFinderEvents {
17
+ 'gateway': CustomEvent<Gateway>
18
+ }
19
+
20
+ export class GatewayFinder extends TypedEventEmitter<GatewayFinderEvents> {
21
+ private readonly log: Logger
22
+ private readonly gateways: Gateway[]
23
+ private readonly findGateways: RepeatingTask
24
+ private readonly portMappingClient: UPnPNAT
25
+ private started: boolean
26
+
27
+ constructor (components: GatewayFinderComponents, init: GatewayFinderInit) {
28
+ super()
29
+
30
+ this.log = components.logger.forComponent('libp2p:upnp-nat')
31
+ this.portMappingClient = init.portMappingClient
32
+ this.started = false
33
+ this.gateways = []
34
+
35
+ // every five minutes, search for network gateways for one minute
36
+ this.findGateways = repeatingTask(async (options) => {
37
+ for await (const gateway of this.portMappingClient.findGateways({
38
+ ...options,
39
+ searchInterval: 10000
40
+ })) {
41
+ if (this.gateways.some(g => {
42
+ return g.id === gateway.id && g.family === gateway.family
43
+ })) {
44
+ // already seen this gateway
45
+ continue
46
+ }
47
+
48
+ this.gateways.push(gateway)
49
+ this.safeDispatchEvent('gateway', {
50
+ detail: gateway
51
+ })
52
+ }
53
+ }, DEFAULT_GATEWAY_SEARCH_INTERVAL, {
54
+ runImmediately: true,
55
+ timeout: DEFAULT_GATEWAY_SEARCH_TIMEOUT
56
+ })
57
+ }
58
+
59
+ async start (): Promise<void> {
60
+ if (this.started) {
61
+ return
62
+ }
63
+
64
+ this.started = true
65
+ await start(this.findGateways)
66
+ }
67
+
68
+ /**
69
+ * Stops the NAT manager
70
+ */
71
+ async stop (): Promise<void> {
72
+ await stop(this.findGateways)
73
+ this.started = false
74
+ }
75
+ }
package/src/index.ts CHANGED
@@ -33,46 +33,6 @@
33
33
  * }
34
34
  * })
35
35
  * ```
36
- *
37
- * @example Manually specifying gateways and external ports
38
- *
39
- * Some ISP-provided routers are underpowered and may require rebooting before
40
- * they will respond to SSDP M-SEARCH messages.
41
- *
42
- * You can manually specify your external address and/or gateways, though note
43
- * that those gateways will still need to have UPnP enabled in order for libp2p
44
- * to configure mapping of external ports (for IPv4) and/or opening pinholes in
45
- * the firewall (for IPv6).
46
- *
47
- * ```typescript
48
- * import { createLibp2p } from 'libp2p'
49
- * import { tcp } from '@libp2p/tcp'
50
- * import { uPnPNAT } from '@libp2p/upnp-nat'
51
- *
52
- * const node = await createLibp2p({
53
- * addresses: {
54
- * listen: [
55
- * '/ip4/0.0.0.0/tcp/0'
56
- * ]
57
- * },
58
- * transports: [
59
- * tcp()
60
- * ],
61
- * services: {
62
- * upnpNAT: uPnPNAT({
63
- * // manually specify external address - this will normally be an IPv4
64
- * // address that the router is performing NAT with
65
- * externalAddress: '92.137.164.96',
66
- * gateways: [
67
- * // an IPv4 gateway
68
- * 'http://192.168.1.1:8080/path/to/descriptor.xml',
69
- * // an IPv6 gateway
70
- * 'http://[xx:xx:xx:xx]:8080/path/to/descriptor.xml'
71
- * ]
72
- * })
73
- * }
74
- * })
75
- * ```
76
36
  */
77
37
 
78
38
  import { UPnPNAT as UPnPNATClass } from './upnp-nat.js'
@@ -90,14 +50,6 @@ export interface PMPOptions {
90
50
  }
91
51
 
92
52
  export interface UPnPNATInit {
93
- /**
94
- * By default we query discovered/configured gateways for their external
95
- * address. To specify it manually instead, pass a value here.
96
- *
97
- * Typically this would be an IPv4 address that the router performs NAT with.
98
- */
99
- externalAddress?: string
100
-
101
53
  /**
102
54
  * Check if the external address has changed this often in ms. Ignored if an
103
55
  * external address is specified.
@@ -157,91 +109,6 @@ export interface UPnPNATInit {
157
109
  * @default false
158
110
  */
159
111
  autoConfirmAddress?: boolean
160
-
161
- /**
162
- * By default we search for local gateways using SSDP M-SEARCH messages. To
163
- * manually specify a gateway instead, pass values here.
164
- *
165
- * A lot of ISP-provided gateway/routers are underpowered so may need
166
- * rebooting before they will respond to M-SEARCH messages.
167
- *
168
- * Each value is an IPv4 or IPv6 URL of the UPnP device descriptor document,
169
- * e.g. `http://192.168.1.1:8080/description.xml`. Please see the
170
- * documentation of your gateway to discover the URL.
171
- *
172
- * Note that some gateways will randomise the port/path the descriptor
173
- * document is served from and even change it over time so you may be forced
174
- * to use an SSDP search instead.
175
- */
176
- gateways?: string[]
177
-
178
- /**
179
- * How often to search for network gateways in ms.
180
- *
181
- * This interval is used before a gateway has been found on the network, after
182
- * that it switches to `gatewaySearchInterval` which lowers the frequency of
183
- * the search.
184
- *
185
- * @default 5_000
186
- */
187
- initialGatewaySearchInterval?: number
188
-
189
- /**
190
- * How often to send the `M-SEARCH` SSDP message during a gateway search in
191
- * ms.
192
- *
193
- * Some routers are flaky and may not respond to every query so decreasing
194
- * this will increase the number of search messages sent before the timeout.
195
- *
196
- * This interval is used before a gateway has been found on the network, after
197
- * that it switches to `gatewaySearchMessageInterval` which lowers the
198
- * frequency of search messages sent.
199
- *
200
- * @default 1_000
201
- */
202
- initialGatewaySearchMessageInterval?: number
203
-
204
- /**
205
- * How long to search for gateways for before giving up in ms.
206
- *
207
- * This timeout is used before a gateway has been found on the network, after
208
- * that it switches to `gatewaySearchTimeout` which increases the timeout to
209
- * give gateways more time to respond.
210
- *
211
- * @default 5_000
212
- */
213
- initialGatewaySearchTimeout?: number
214
-
215
- /**
216
- * How often to search for network gateways in ms.
217
- *
218
- * This interval is used after a gateway has been found on the network.
219
- *
220
- * @default 300_000
221
- */
222
- gatewaySearchInterval?: number
223
-
224
- /**
225
- * How often to send the `M-SEARCH` SSDP message during a gateway search in
226
- * ms.
227
- *
228
- * Some routers are flaky and may not respond to every query so decreasing
229
- * this will increase the number of search messages sent before the timeout.
230
- *
231
- * This interval is used after a gateway has been found on the network.
232
- *
233
- * @default 10_000
234
- */
235
- gatewaySearchMessageInterval?: number
236
-
237
- /**
238
- * How long to search for gateways for before giving up in ms.
239
- *
240
- * This timeout is used after a gateway has been found on the network.
241
- *
242
- * @default 60_000
243
- */
244
- gatewaySearchTimeout?: number
245
112
  }
246
113
 
247
114
  export interface UPnPNATComponents {
package/src/upnp-nat.ts CHANGED
@@ -1,22 +1,13 @@
1
1
  import { upnpNat } from '@achingbrain/nat-port-mapper'
2
2
  import { serviceCapabilities, serviceDependencies, setMaxListeners, start, stop } from '@libp2p/interface'
3
3
  import { debounce } from '@libp2p/utils/debounce'
4
- import { SearchGatewayFinder } from './search-gateway-finder.js'
5
- import { StaticGatewayFinder } from './static-gateway-finder.js'
4
+ import { GatewayFinder } from './gateway-finder.js'
6
5
  import { UPnPPortMapper } from './upnp-port-mapper.js'
7
6
  import type { UPnPNATComponents, UPnPNATInit, UPnPNAT as UPnPNATInterface } from './index.js'
8
7
  import type { Gateway, UPnPNAT as UPnPNATClient } from '@achingbrain/nat-port-mapper'
9
- import type { Logger, Startable, TypedEventTarget } from '@libp2p/interface'
8
+ import type { Logger, Startable } from '@libp2p/interface'
10
9
  import type { DebouncedFunction } from '@libp2p/utils/debounce'
11
10
 
12
- export interface GatewayFinderEvents {
13
- 'gateway': CustomEvent<Gateway>
14
- }
15
-
16
- export interface GatewayFinder extends TypedEventTarget<GatewayFinderEvents> {
17
-
18
- }
19
-
20
11
  export class UPnPNAT implements Startable, UPnPNATInterface {
21
12
  private readonly log: Logger
22
13
  private readonly components: UPnPNATComponents
@@ -53,23 +44,10 @@ export class UPnPNAT implements Startable, UPnPNATInterface {
53
44
  }
54
45
  }, 5_000)
55
46
 
56
- if (init.gateways != null) {
57
- this.gatewayFinder = new StaticGatewayFinder(components, {
58
- portMappingClient: this.portMappingClient,
59
- gateways: init.gateways
60
- })
61
- } else {
62
- // trigger update when we discovery gateways on the network
63
- this.gatewayFinder = new SearchGatewayFinder(components, {
64
- portMappingClient: this.portMappingClient,
65
- initialSearchInterval: init.initialGatewaySearchInterval,
66
- initialSearchTimeout: init.initialGatewaySearchTimeout,
67
- initialSearchMessageInterval: init.initialGatewaySearchMessageInterval,
68
- searchInterval: init.gatewaySearchInterval,
69
- searchTimeout: init.gatewaySearchTimeout,
70
- searchMessageInterval: init.gatewaySearchMessageInterval
71
- })
72
- }
47
+ // trigger update when we discovery gateways on the network
48
+ this.gatewayFinder = new GatewayFinder(components, {
49
+ portMappingClient: this.portMappingClient
50
+ })
73
51
 
74
52
  this.onGatewayDiscovered = this.onGatewayDiscovered.bind(this)
75
53
  }
@@ -6,7 +6,7 @@ import { isPrivate } from '@libp2p/utils/multiaddr/is-private'
6
6
  import { isPrivateIp } from '@libp2p/utils/private-ip'
7
7
  import { multiaddr } from '@multiformats/multiaddr'
8
8
  import { QUICV1, TCP, WebSockets, WebSocketsSecure, WebTransport } from '@multiformats/multiaddr-matcher'
9
- import { dynamicExternalAddress, staticExternalAddress } from './check-external-address.js'
9
+ import { dynamicExternalAddress } from './check-external-address.js'
10
10
  import { DoubleNATError } from './errors.js'
11
11
  import type { ExternalAddress } from './check-external-address.js'
12
12
  import type { Gateway } from '@achingbrain/nat-port-mapper'
@@ -18,7 +18,6 @@ const MAX_DATE = 8_640_000_000_000_000
18
18
 
19
19
  export interface UPnPPortMapperInit {
20
20
  gateway: Gateway
21
- externalAddress?: string
22
21
  externalAddressCheckInterval?: number
23
22
  externalAddressCheckTimeout?: number
24
23
  }
@@ -49,20 +48,15 @@ export class UPnPPortMapper {
49
48
  this.log = components.logger.forComponent(`libp2p:upnp-nat:gateway:${init.gateway.id}`)
50
49
  this.addressManager = components.addressManager
51
50
  this.gateway = init.gateway
52
-
53
- if (init.externalAddress != null) {
54
- this.externalAddress = staticExternalAddress(init.externalAddress)
55
- } else {
56
- this.externalAddress = dynamicExternalAddress({
57
- gateway: this.gateway,
58
- addressManager: this.addressManager,
59
- logger: components.logger
60
- }, {
61
- interval: init.externalAddressCheckInterval,
62
- timeout: init.externalAddressCheckTimeout,
63
- onExternalAddressChange: this.remapPorts.bind(this)
64
- })
65
- }
51
+ this.externalAddress = dynamicExternalAddress({
52
+ gateway: this.gateway,
53
+ addressManager: this.addressManager,
54
+ logger: components.logger
55
+ }, {
56
+ interval: init.externalAddressCheckInterval,
57
+ timeout: init.externalAddressCheckTimeout,
58
+ onExternalAddressChange: this.remapPorts.bind(this)
59
+ })
66
60
  this.gateway = init.gateway
67
61
  this.mappedPorts = new Map()
68
62
  this.started = false
@@ -1,27 +0,0 @@
1
- import { TypedEventEmitter } from '@libp2p/interface';
2
- import type { GatewayFinder, GatewayFinderEvents } from './upnp-nat.js';
3
- import type { UPnPNAT } from '@achingbrain/nat-port-mapper';
4
- import type { ComponentLogger } from '@libp2p/interface';
5
- export interface SearchGatewayFinderComponents {
6
- logger: ComponentLogger;
7
- }
8
- export interface SearchGatewayFinderInit {
9
- portMappingClient: UPnPNAT;
10
- initialSearchInterval?: number;
11
- initialSearchTimeout?: number;
12
- initialSearchMessageInterval?: number;
13
- searchInterval?: number;
14
- searchTimeout?: number;
15
- searchMessageInterval?: number;
16
- }
17
- export declare class SearchGatewayFinder extends TypedEventEmitter<GatewayFinderEvents> implements GatewayFinder {
18
- private readonly log;
19
- private readonly gateways;
20
- private readonly findGateways;
21
- private readonly portMappingClient;
22
- private started;
23
- constructor(components: SearchGatewayFinderComponents, init: SearchGatewayFinderInit);
24
- start(): Promise<void>;
25
- stop(): Promise<void>;
26
- }
27
- //# sourceMappingURL=search-gateway-finder.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"search-gateway-finder.d.ts","sourceRoot":"","sources":["../../src/search-gateway-finder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAe,MAAM,mBAAmB,CAAA;AAGlE,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACvE,OAAO,KAAK,EAAW,OAAO,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAGhE,MAAM,WAAW,6BAA6B;IAC5C,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,uBAAuB;IACtC,iBAAiB,EAAE,OAAO,CAAA;IAC1B,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAC7B,4BAA4B,CAAC,EAAE,MAAM,CAAA;IACrC,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,qBAAqB,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED,qBAAa,mBAAoB,SAAQ,iBAAiB,CAAC,mBAAmB,CAAE,YAAW,aAAa;IACtG,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,OAAO,CAAS;gBAEX,UAAU,EAAE,6BAA6B,EAAE,IAAI,EAAE,uBAAuB;IAmD/E,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IASvB,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;CAI7B"}
@@ -1,66 +0,0 @@
1
- import { TypedEventEmitter, start, stop } from '@libp2p/interface';
2
- import { repeatingTask } from '@libp2p/utils/repeating-task';
3
- import { DEFAULT_GATEWAY_SEARCH_INTERVAL, DEFAULT_GATEWAY_SEARCH_MESSAGE_INTERVAL, DEFAULT_GATEWAY_SEARCH_TIMEOUT, DEFAULT_INITIAL_GATEWAY_SEARCH_INTERVAL, DEFAULT_INITIAL_GATEWAY_SEARCH_MESSAGE_INTERVAL, DEFAULT_INITIAL_GATEWAY_SEARCH_TIMEOUT } from './constants.js';
4
- export class SearchGatewayFinder extends TypedEventEmitter {
5
- log;
6
- gateways;
7
- findGateways;
8
- portMappingClient;
9
- started;
10
- constructor(components, init) {
11
- super();
12
- this.log = components.logger.forComponent('libp2p:upnp-nat');
13
- this.portMappingClient = init.portMappingClient;
14
- this.started = false;
15
- this.gateways = [];
16
- // every five minutes, search for network gateways for one minute
17
- this.findGateways = repeatingTask(async (options) => {
18
- try {
19
- const searchMessageInterval = this.gateways.length > 0
20
- ? init.searchMessageInterval ?? DEFAULT_GATEWAY_SEARCH_MESSAGE_INTERVAL
21
- : init.initialSearchMessageInterval ?? DEFAULT_INITIAL_GATEWAY_SEARCH_MESSAGE_INTERVAL;
22
- this.log('begin gateway search, sending M-SEARCH every %dms', searchMessageInterval);
23
- for await (const gateway of this.portMappingClient.findGateways({
24
- ...options,
25
- searchInterval: searchMessageInterval
26
- })) {
27
- if (this.gateways.some(g => {
28
- return g.id === gateway.id && g.family === gateway.family;
29
- })) {
30
- // already seen this gateway
31
- continue;
32
- }
33
- this.gateways.push(gateway);
34
- this.safeDispatchEvent('gateway', {
35
- detail: gateway
36
- });
37
- // we've found a gateway, wait for longer before searching again
38
- const searchInterval = init.searchTimeout ?? DEFAULT_GATEWAY_SEARCH_INTERVAL;
39
- const searchTimeout = init.searchTimeout ?? DEFAULT_GATEWAY_SEARCH_TIMEOUT;
40
- this.log('switching gateway search to every %dms, timing out after %dms', searchInterval, searchTimeout);
41
- this.findGateways.setInterval(searchInterval);
42
- this.findGateways.setTimeout(searchTimeout);
43
- }
44
- this.log('gateway search finished, found %d gateways', this.gateways.length);
45
- }
46
- catch (err) {
47
- this.log.error('gateway search errored - %e', err);
48
- }
49
- }, init.initialSearchInterval ?? DEFAULT_INITIAL_GATEWAY_SEARCH_INTERVAL, {
50
- runImmediately: true,
51
- timeout: init.initialSearchTimeout ?? DEFAULT_INITIAL_GATEWAY_SEARCH_TIMEOUT
52
- });
53
- }
54
- async start() {
55
- if (this.started) {
56
- return;
57
- }
58
- this.started = true;
59
- await start(this.findGateways);
60
- }
61
- async stop() {
62
- await stop(this.findGateways);
63
- this.started = false;
64
- }
65
- }
66
- //# sourceMappingURL=search-gateway-finder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"search-gateway-finder.js","sourceRoot":"","sources":["../../src/search-gateway-finder.ts"],"names":[],"mappings":"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"}
@@ -1,23 +0,0 @@
1
- import { TypedEventEmitter } from '@libp2p/interface';
2
- import type { GatewayFinder, GatewayFinderEvents } from './upnp-nat.js';
3
- import type { UPnPNAT } from '@achingbrain/nat-port-mapper';
4
- import type { ComponentLogger } from '@libp2p/interface';
5
- export interface StaticGatewayFinderComponents {
6
- logger: ComponentLogger;
7
- }
8
- export interface StaticGatewayFinderInit {
9
- portMappingClient: UPnPNAT;
10
- gateways: string[];
11
- }
12
- export declare class StaticGatewayFinder extends TypedEventEmitter<GatewayFinderEvents> implements GatewayFinder {
13
- private readonly log;
14
- private readonly gatewayUrls;
15
- private readonly gateways;
16
- private readonly portMappingClient;
17
- private started;
18
- constructor(components: StaticGatewayFinderComponents, init: StaticGatewayFinderInit);
19
- start(): Promise<void>;
20
- afterStart(): Promise<void>;
21
- stop(): Promise<void>;
22
- }
23
- //# sourceMappingURL=static-gateway-finder.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"static-gateway-finder.d.ts","sourceRoot":"","sources":["../../src/static-gateway-finder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACvE,OAAO,KAAK,EAAW,OAAO,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAEhE,MAAM,WAAW,6BAA6B;IAC5C,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,uBAAuB;IACtC,iBAAiB,EAAE,OAAO,CAAA;IAC1B,QAAQ,EAAE,MAAM,EAAE,CAAA;CACnB;AAED,qBAAa,mBAAoB,SAAQ,iBAAiB,CAAC,mBAAmB,CAAE,YAAW,aAAa;IACtG,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAO;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,OAAO,CAAS;gBAEX,UAAU,EAAE,6BAA6B,EAAE,IAAI,EAAE,uBAAuB;IAU/E,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAIvB,UAAU,IAAK,OAAO,CAAC,IAAI,CAAC;IAqB5B,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;CAG7B"}
@@ -1,42 +0,0 @@
1
- import { TypedEventEmitter } from '@libp2p/interface';
2
- export class StaticGatewayFinder extends TypedEventEmitter {
3
- log;
4
- gatewayUrls;
5
- gateways;
6
- portMappingClient;
7
- started;
8
- constructor(components, init) {
9
- super();
10
- this.log = components.logger.forComponent('libp2p:upnp-nat:static-gateway-finder');
11
- this.portMappingClient = init.portMappingClient;
12
- this.started = false;
13
- this.gateways = [];
14
- this.gatewayUrls = init.gateways.map(url => new URL(url));
15
- }
16
- async start() {
17
- this.started = true;
18
- }
19
- async afterStart() {
20
- for (const url of this.gatewayUrls) {
21
- try {
22
- this.log('fetching gateway descriptor from %s', url);
23
- const gateway = await this.portMappingClient.getGateway(url);
24
- if (!this.started) {
25
- return;
26
- }
27
- this.log('found static gateway at %s', url);
28
- this.gateways.push(gateway);
29
- this.safeDispatchEvent('gateway', {
30
- detail: gateway
31
- });
32
- }
33
- catch (err) {
34
- this.log.error('could not contact static gateway at %s - %e', url, err);
35
- }
36
- }
37
- }
38
- async stop() {
39
- this.started = false;
40
- }
41
- }
42
- //# sourceMappingURL=static-gateway-finder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"static-gateway-finder.js","sourceRoot":"","sources":["../../src/static-gateway-finder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAcrD,MAAM,OAAO,mBAAoB,SAAQ,iBAAsC;IAC5D,GAAG,CAAQ;IACX,WAAW,CAAO;IAClB,QAAQ,CAAW;IACnB,iBAAiB,CAAS;IACnC,OAAO,CAAS;IAExB,YAAa,UAAyC,EAAE,IAA6B;QACnF,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,uCAAuC,CAAC,CAAA;QAClF,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,iBAAiB,CAAA;QAC/C,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAA;QAClB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3D,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;IACrB,CAAC;IAED,KAAK,CAAC,UAAU;QACd,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACnC,IAAI,CAAC;gBACH,IAAI,CAAC,GAAG,CAAC,qCAAqC,EAAE,GAAG,CAAC,CAAA;gBACpD,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;gBAE5D,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;oBAClB,OAAM;gBACR,CAAC;gBAED,IAAI,CAAC,GAAG,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAA;gBAC3C,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;gBAC3B,IAAI,CAAC,iBAAiB,CAAC,SAAS,EAAE;oBAChC,MAAM,EAAE,OAAO;iBAChB,CAAC,CAAA;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,6CAA6C,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;YACzE,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;IACtB,CAAC;CACF"}
@@ -1,94 +0,0 @@
1
- import { TypedEventEmitter, start, stop } from '@libp2p/interface'
2
- import { repeatingTask } from '@libp2p/utils/repeating-task'
3
- import { DEFAULT_GATEWAY_SEARCH_INTERVAL, DEFAULT_GATEWAY_SEARCH_MESSAGE_INTERVAL, DEFAULT_GATEWAY_SEARCH_TIMEOUT, DEFAULT_INITIAL_GATEWAY_SEARCH_INTERVAL, DEFAULT_INITIAL_GATEWAY_SEARCH_MESSAGE_INTERVAL, DEFAULT_INITIAL_GATEWAY_SEARCH_TIMEOUT } from './constants.js'
4
- import type { GatewayFinder, GatewayFinderEvents } from './upnp-nat.js'
5
- import type { Gateway, UPnPNAT } from '@achingbrain/nat-port-mapper'
6
- import type { ComponentLogger, Logger } from '@libp2p/interface'
7
- import type { RepeatingTask } from '@libp2p/utils/repeating-task'
8
-
9
- export interface SearchGatewayFinderComponents {
10
- logger: ComponentLogger
11
- }
12
-
13
- export interface SearchGatewayFinderInit {
14
- portMappingClient: UPnPNAT
15
- initialSearchInterval?: number
16
- initialSearchTimeout?: number
17
- initialSearchMessageInterval?: number
18
- searchInterval?: number
19
- searchTimeout?: number
20
- searchMessageInterval?: number
21
- }
22
-
23
- export class SearchGatewayFinder extends TypedEventEmitter<GatewayFinderEvents> implements GatewayFinder {
24
- private readonly log: Logger
25
- private readonly gateways: Gateway[]
26
- private readonly findGateways: RepeatingTask
27
- private readonly portMappingClient: UPnPNAT
28
- private started: boolean
29
-
30
- constructor (components: SearchGatewayFinderComponents, init: SearchGatewayFinderInit) {
31
- super()
32
-
33
- this.log = components.logger.forComponent('libp2p:upnp-nat')
34
- this.portMappingClient = init.portMappingClient
35
- this.started = false
36
- this.gateways = []
37
-
38
- // every five minutes, search for network gateways for one minute
39
- this.findGateways = repeatingTask(async (options) => {
40
- try {
41
- const searchMessageInterval = this.gateways.length > 0
42
- ? init.searchMessageInterval ?? DEFAULT_GATEWAY_SEARCH_MESSAGE_INTERVAL
43
- : init.initialSearchMessageInterval ?? DEFAULT_INITIAL_GATEWAY_SEARCH_MESSAGE_INTERVAL
44
-
45
- this.log('begin gateway search, sending M-SEARCH every %dms', searchMessageInterval)
46
-
47
- for await (const gateway of this.portMappingClient.findGateways({
48
- ...options,
49
- searchInterval: searchMessageInterval
50
- })) {
51
- if (this.gateways.some(g => {
52
- return g.id === gateway.id && g.family === gateway.family
53
- })) {
54
- // already seen this gateway
55
- continue
56
- }
57
-
58
- this.gateways.push(gateway)
59
- this.safeDispatchEvent('gateway', {
60
- detail: gateway
61
- })
62
-
63
- // we've found a gateway, wait for longer before searching again
64
- const searchInterval = init.searchTimeout ?? DEFAULT_GATEWAY_SEARCH_INTERVAL
65
- const searchTimeout = init.searchTimeout ?? DEFAULT_GATEWAY_SEARCH_TIMEOUT
66
- this.log('switching gateway search to every %dms, timing out after %dms', searchInterval, searchTimeout)
67
- this.findGateways.setInterval(searchInterval)
68
- this.findGateways.setTimeout(searchTimeout)
69
- }
70
-
71
- this.log('gateway search finished, found %d gateways', this.gateways.length)
72
- } catch (err) {
73
- this.log.error('gateway search errored - %e', err)
74
- }
75
- }, init.initialSearchInterval ?? DEFAULT_INITIAL_GATEWAY_SEARCH_INTERVAL, {
76
- runImmediately: true,
77
- timeout: init.initialSearchTimeout ?? DEFAULT_INITIAL_GATEWAY_SEARCH_TIMEOUT
78
- })
79
- }
80
-
81
- async start (): Promise<void> {
82
- if (this.started) {
83
- return
84
- }
85
-
86
- this.started = true
87
- await start(this.findGateways)
88
- }
89
-
90
- async stop (): Promise<void> {
91
- await stop(this.findGateways)
92
- this.started = false
93
- }
94
- }
@@ -1,60 +0,0 @@
1
- import { TypedEventEmitter } from '@libp2p/interface'
2
- import type { GatewayFinder, GatewayFinderEvents } from './upnp-nat.js'
3
- import type { Gateway, UPnPNAT } from '@achingbrain/nat-port-mapper'
4
- import type { ComponentLogger, Logger } from '@libp2p/interface'
5
-
6
- export interface StaticGatewayFinderComponents {
7
- logger: ComponentLogger
8
- }
9
-
10
- export interface StaticGatewayFinderInit {
11
- portMappingClient: UPnPNAT
12
- gateways: string[]
13
- }
14
-
15
- export class StaticGatewayFinder extends TypedEventEmitter<GatewayFinderEvents> implements GatewayFinder {
16
- private readonly log: Logger
17
- private readonly gatewayUrls: URL[]
18
- private readonly gateways: Gateway[]
19
- private readonly portMappingClient: UPnPNAT
20
- private started: boolean
21
-
22
- constructor (components: StaticGatewayFinderComponents, init: StaticGatewayFinderInit) {
23
- super()
24
-
25
- this.log = components.logger.forComponent('libp2p:upnp-nat:static-gateway-finder')
26
- this.portMappingClient = init.portMappingClient
27
- this.started = false
28
- this.gateways = []
29
- this.gatewayUrls = init.gateways.map(url => new URL(url))
30
- }
31
-
32
- async start (): Promise<void> {
33
- this.started = true
34
- }
35
-
36
- async afterStart (): Promise<void> {
37
- for (const url of this.gatewayUrls) {
38
- try {
39
- this.log('fetching gateway descriptor from %s', url)
40
- const gateway = await this.portMappingClient.getGateway(url)
41
-
42
- if (!this.started) {
43
- return
44
- }
45
-
46
- this.log('found static gateway at %s', url)
47
- this.gateways.push(gateway)
48
- this.safeDispatchEvent('gateway', {
49
- detail: gateway
50
- })
51
- } catch (err) {
52
- this.log.error('could not contact static gateway at %s - %e', url, err)
53
- }
54
- }
55
- }
56
-
57
- async stop (): Promise<void> {
58
- this.started = false
59
- }
60
- }