@libp2p/upnp-nat 1.0.22 → 1.1.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/dist/src/index.d.ts +6 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/upnp-nat.d.ts +5 -5
- package/dist/src/upnp-nat.d.ts.map +1 -1
- package/dist/src/upnp-nat.js +9 -17
- package/dist/src/upnp-nat.js.map +1 -1
- package/dist/typedoc-urls.json +4 -2
- package/package.json +7 -7
- package/src/index.ts +9 -3
- package/src/upnp-nat.ts +15 -23
package/dist/src/index.d.ts
CHANGED
|
@@ -34,8 +34,10 @@
|
|
|
34
34
|
* })
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
+
import { type NatAPI, type MapPortOptions } from './upnp-nat.js';
|
|
37
38
|
import type { ComponentLogger, NodeInfo, PeerId } from '@libp2p/interface';
|
|
38
39
|
import type { AddressManager, TransportManager } from '@libp2p/interface-internal';
|
|
40
|
+
export type { NatAPI, MapPortOptions };
|
|
39
41
|
export interface PMPOptions {
|
|
40
42
|
/**
|
|
41
43
|
* Whether to enable PMP as well as UPnP
|
|
@@ -75,5 +77,8 @@ export interface UPnPNATComponents {
|
|
|
75
77
|
transportManager: TransportManager;
|
|
76
78
|
addressManager: AddressManager;
|
|
77
79
|
}
|
|
78
|
-
export
|
|
80
|
+
export interface UPnPNAT {
|
|
81
|
+
client: NatAPI;
|
|
82
|
+
}
|
|
83
|
+
export declare function uPnPNAT(init?: UPnPNATInit): (components: UPnPNATComponents) => UPnPNAT;
|
|
79
84
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAA2B,KAAK,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAA;AACzF,OAAO,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAElF,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;AAEtC,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IAExB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IAErB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IAEnB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,QAAQ,CAAA;IAClB,MAAM,EAAE,eAAe,CAAA;IACvB,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,cAAc,EAAE,cAAc,CAAA;CAC/B;AAED,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAA;CACf;AAED,wBAAgB,OAAO,CAAE,IAAI,GAAE,WAAgB,GAAG,CAAC,UAAU,EAAE,iBAAiB,KAAK,OAAO,CAI3F"}
|
package/dist/src/index.js
CHANGED
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
* })
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
import { UPnPNAT } from './upnp-nat.js';
|
|
37
|
+
import { UPnPNAT as UPnPNATClass } from './upnp-nat.js';
|
|
38
38
|
export function uPnPNAT(init = {}) {
|
|
39
39
|
return (components) => {
|
|
40
|
-
return new
|
|
40
|
+
return new UPnPNATClass(components, init);
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
43
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,OAAO,IAAI,YAAY,EAAoC,MAAM,eAAe,CAAA;AAyDzF,MAAM,UAAU,OAAO,CAAE,OAAoB,EAAE;IAC7C,OAAO,CAAC,UAA6B,EAAE,EAAE;QACvC,OAAO,IAAI,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC,CAAA;AACH,CAAC"}
|
package/dist/src/upnp-nat.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { type NatAPI } from '@achingbrain/nat-port-mapper';
|
|
2
|
-
import type { UPnPNATComponents, UPnPNATInit } from './index.js';
|
|
1
|
+
import { type NatAPI, type MapPortOptions } from '@achingbrain/nat-port-mapper';
|
|
2
|
+
import type { UPnPNATComponents, UPnPNATInit, UPnPNAT as UPnPNATInterface } from './index.js';
|
|
3
3
|
import type { Startable } from '@libp2p/interface';
|
|
4
|
-
export
|
|
4
|
+
export type { NatAPI, MapPortOptions };
|
|
5
|
+
export declare class UPnPNAT implements Startable, UPnPNATInterface {
|
|
6
|
+
client: NatAPI;
|
|
5
7
|
private readonly components;
|
|
6
8
|
private readonly externalAddress?;
|
|
7
9
|
private readonly localAddress?;
|
|
@@ -10,7 +12,6 @@ export declare class UPnPNAT implements Startable {
|
|
|
10
12
|
private readonly keepAlive;
|
|
11
13
|
private readonly gateway?;
|
|
12
14
|
private started;
|
|
13
|
-
private client?;
|
|
14
15
|
private readonly log;
|
|
15
16
|
constructor(components: UPnPNATComponents, init: UPnPNATInit);
|
|
16
17
|
isStarted(): boolean;
|
|
@@ -22,7 +23,6 @@ export declare class UPnPNAT implements Startable {
|
|
|
22
23
|
*/
|
|
23
24
|
afterStart(): void;
|
|
24
25
|
mapIpAddresses(): Promise<void>;
|
|
25
|
-
_getClient(): NatAPI;
|
|
26
26
|
/**
|
|
27
27
|
* Stops the NAT manager
|
|
28
28
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upnp-nat.d.ts","sourceRoot":"","sources":["../../src/upnp-nat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,8BAA8B,CAAA;
|
|
1
|
+
{"version":3,"file":"upnp-nat.d.ts","sourceRoot":"","sources":["../../src/upnp-nat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAMxF,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7F,OAAO,KAAK,EAAU,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAI1D,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;AAMtC,qBAAa,OAAQ,YAAW,SAAS,EAAE,gBAAgB;IAClD,MAAM,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmB;IAC9C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAQ;IACzC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAQ;IACtC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;IACpC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAQ;IACjC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,UAAU,EAAE,iBAAiB,EAAE,IAAI,EAAE,WAAW;IAwB7D,SAAS,IAAK,OAAO;IAIrB,KAAK,IAAK,IAAI;IAId;;;;OAIG;IACH,UAAU,IAAK,IAAI;IAcb,cAAc,IAAK,OAAO,CAAC,IAAI,CAAC;IAsDtC;;OAEG;IACG,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;CAW7B"}
|
package/dist/src/upnp-nat.js
CHANGED
|
@@ -9,6 +9,7 @@ function highPort(min = 1024, max = 65535) {
|
|
|
9
9
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
10
10
|
}
|
|
11
11
|
export class UPnPNAT {
|
|
12
|
+
client;
|
|
12
13
|
components;
|
|
13
14
|
externalAddress;
|
|
14
15
|
localAddress;
|
|
@@ -17,7 +18,6 @@ export class UPnPNAT {
|
|
|
17
18
|
keepAlive;
|
|
18
19
|
gateway;
|
|
19
20
|
started;
|
|
20
|
-
client;
|
|
21
21
|
log;
|
|
22
22
|
constructor(components, init) {
|
|
23
23
|
this.components = components;
|
|
@@ -32,6 +32,12 @@ export class UPnPNAT {
|
|
|
32
32
|
if (this.ttl < DEFAULT_TTL) {
|
|
33
33
|
throw new CodeError(`NatManager ttl should be at least ${DEFAULT_TTL} seconds`, ERR_INVALID_PARAMETERS);
|
|
34
34
|
}
|
|
35
|
+
this.client = upnpNat({
|
|
36
|
+
description: this.description,
|
|
37
|
+
ttl: this.ttl,
|
|
38
|
+
keepAlive: this.keepAlive,
|
|
39
|
+
gateway: this.gateway
|
|
40
|
+
});
|
|
35
41
|
}
|
|
36
42
|
isStarted() {
|
|
37
43
|
return this.started;
|
|
@@ -74,8 +80,7 @@ export class UPnPNAT {
|
|
|
74
80
|
// eslint-disable-next-line no-continue
|
|
75
81
|
continue;
|
|
76
82
|
}
|
|
77
|
-
const
|
|
78
|
-
const publicIp = this.externalAddress ?? await client.externalIp();
|
|
83
|
+
const publicIp = this.externalAddress ?? await this.client.externalIp();
|
|
79
84
|
const isPrivate = isPrivateIp(publicIp);
|
|
80
85
|
if (isPrivate === true) {
|
|
81
86
|
throw new CodeError(`${publicIp} is private - please set config.nat.externalIp to an externally routable IP or ensure you are not behind a double NAT`, 'ERR_DOUBLE_NAT');
|
|
@@ -85,7 +90,7 @@ export class UPnPNAT {
|
|
|
85
90
|
}
|
|
86
91
|
const publicPort = highPort();
|
|
87
92
|
this.log(`opening uPnP connection from ${publicIp}:${publicPort} to ${host}:${port}`);
|
|
88
|
-
await client.map({
|
|
93
|
+
await this.client.map({
|
|
89
94
|
publicPort,
|
|
90
95
|
localPort: port,
|
|
91
96
|
localAddress: this.localAddress,
|
|
@@ -98,18 +103,6 @@ export class UPnPNAT {
|
|
|
98
103
|
}, transport));
|
|
99
104
|
}
|
|
100
105
|
}
|
|
101
|
-
_getClient() {
|
|
102
|
-
if (this.client != null) {
|
|
103
|
-
return this.client;
|
|
104
|
-
}
|
|
105
|
-
this.client = upnpNat({
|
|
106
|
-
description: this.description,
|
|
107
|
-
ttl: this.ttl,
|
|
108
|
-
keepAlive: this.keepAlive,
|
|
109
|
-
gateway: this.gateway
|
|
110
|
-
});
|
|
111
|
-
return this.client;
|
|
112
|
-
}
|
|
113
106
|
/**
|
|
114
107
|
* Stops the NAT manager
|
|
115
108
|
*/
|
|
@@ -119,7 +112,6 @@ export class UPnPNAT {
|
|
|
119
112
|
}
|
|
120
113
|
try {
|
|
121
114
|
await this.client.close();
|
|
122
|
-
this.client = undefined;
|
|
123
115
|
}
|
|
124
116
|
catch (err) {
|
|
125
117
|
this.log.error(err);
|
package/dist/src/upnp-nat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upnp-nat.js","sourceRoot":"","sources":["../../src/upnp-nat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"upnp-nat.js","sourceRoot":"","sources":["../../src/upnp-nat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAoC,MAAM,8BAA8B,CAAA;AACxF,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,qCAAqC,CAAA;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAItC,MAAM,WAAW,GAAG,IAAI,CAAA;AAIxB,SAAS,QAAQ,CAAE,GAAG,GAAG,IAAI,EAAE,GAAG,GAAG,KAAK;IACxC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAA;AAC1D,CAAC;AAED,MAAM,OAAO,OAAO;IACX,MAAM,CAAQ;IACJ,UAAU,CAAmB;IAC7B,eAAe,CAAS;IACxB,YAAY,CAAS;IACrB,WAAW,CAAQ;IACnB,GAAG,CAAQ;IACX,SAAS,CAAS;IAClB,OAAO,CAAS;IACzB,OAAO,CAAS;IACP,GAAG,CAAQ;IAE5B,YAAa,UAA6B,EAAE,IAAiB;QAC3D,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAE5B,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAA;QAC5D,IAAI,CAAC,OAAO,GAAG,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AErF,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;gBACpB,UAAU;gBACV,SAAS,EAAE,IAAI;gBACf,YAAY,EAAE,IAAI,CAAC,YAAY;gBAC/B,QAAQ,EAAE,SAAS,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK;aAC5D,CAAC,CAAA;YAEF,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,eAAe,CAAC,eAAe,CAAC;gBAC7D,MAAM,EAAE,CAAC;gBACT,OAAO,EAAE,QAAQ;gBACjB,IAAI,EAAE,UAAU;aACjB,EAAE,SAAS,CAAC,CAAC,CAAA;QAChB,CAAC;IACH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,IAAI,SAAS,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;YACrC,OAAM;QACR,CAAC;QAED,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAA;QAC3B,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;CACF"}
|
package/dist/typedoc-urls.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"PMPOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.PMPOptions.html",
|
|
3
3
|
".:PMPOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.PMPOptions.html",
|
|
4
|
+
"UPnPNAT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.UPnPNAT.html",
|
|
5
|
+
".:UPnPNAT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.UPnPNAT.html",
|
|
4
6
|
"UPnPNATComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.UPnPNATComponents.html",
|
|
5
7
|
".:UPnPNATComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.UPnPNATComponents.html",
|
|
6
8
|
"UPnPNATInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.UPnPNATInit.html",
|
|
7
9
|
".:UPnPNATInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_upnp_nat.UPnPNATInit.html",
|
|
8
|
-
"uPnPNAT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_upnp_nat.uPnPNAT.html",
|
|
9
|
-
".:uPnPNAT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_upnp_nat.uPnPNAT.html"
|
|
10
|
+
"uPnPNAT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_upnp_nat.uPnPNAT-1.html",
|
|
11
|
+
".:uPnPNAT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_upnp_nat.uPnPNAT-1.html"
|
|
10
12
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/upnp-nat",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "UPnP NAT hole punching",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/upnp-nat#readme",
|
|
@@ -51,16 +51,16 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@achingbrain/nat-port-mapper": "^1.0.13",
|
|
54
|
-
"@libp2p/interface": "^1.4.
|
|
55
|
-
"@libp2p/interface-internal": "^1.2.
|
|
56
|
-
"@libp2p/utils": "^5.4.
|
|
54
|
+
"@libp2p/interface": "^1.4.1",
|
|
55
|
+
"@libp2p/interface-internal": "^1.2.3",
|
|
56
|
+
"@libp2p/utils": "^5.4.3",
|
|
57
57
|
"@multiformats/multiaddr": "^12.2.3",
|
|
58
58
|
"wherearewe": "^2.0.1"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@libp2p/logger": "^4.0.
|
|
62
|
-
"@libp2p/peer-id-factory": "^4.1.
|
|
63
|
-
"aegir": "^
|
|
61
|
+
"@libp2p/logger": "^4.0.14",
|
|
62
|
+
"@libp2p/peer-id-factory": "^4.1.3",
|
|
63
|
+
"aegir": "^43.0.1",
|
|
64
64
|
"sinon-ts": "^2.0.0"
|
|
65
65
|
},
|
|
66
66
|
"sideEffects": false
|
package/src/index.ts
CHANGED
|
@@ -35,10 +35,12 @@
|
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
37
|
|
|
38
|
-
import { UPnPNAT } from './upnp-nat.js'
|
|
38
|
+
import { UPnPNAT as UPnPNATClass, type NatAPI, type MapPortOptions } from './upnp-nat.js'
|
|
39
39
|
import type { ComponentLogger, NodeInfo, PeerId } from '@libp2p/interface'
|
|
40
40
|
import type { AddressManager, TransportManager } from '@libp2p/interface-internal'
|
|
41
41
|
|
|
42
|
+
export type { NatAPI, MapPortOptions }
|
|
43
|
+
|
|
42
44
|
export interface PMPOptions {
|
|
43
45
|
/**
|
|
44
46
|
* Whether to enable PMP as well as UPnP
|
|
@@ -86,8 +88,12 @@ export interface UPnPNATComponents {
|
|
|
86
88
|
addressManager: AddressManager
|
|
87
89
|
}
|
|
88
90
|
|
|
89
|
-
export
|
|
91
|
+
export interface UPnPNAT {
|
|
92
|
+
client: NatAPI
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function uPnPNAT (init: UPnPNATInit = {}): (components: UPnPNATComponents) => UPnPNAT {
|
|
90
96
|
return (components: UPnPNATComponents) => {
|
|
91
|
-
return new
|
|
97
|
+
return new UPnPNATClass(components, init)
|
|
92
98
|
}
|
|
93
99
|
}
|
package/src/upnp-nat.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import { upnpNat, type NatAPI } from '@achingbrain/nat-port-mapper'
|
|
1
|
+
import { upnpNat, type NatAPI, type MapPortOptions } from '@achingbrain/nat-port-mapper'
|
|
2
2
|
import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface'
|
|
3
3
|
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
|
|
4
4
|
import { isPrivateIp } from '@libp2p/utils/private-ip'
|
|
5
5
|
import { fromNodeAddress } from '@multiformats/multiaddr'
|
|
6
6
|
import { isBrowser } from 'wherearewe'
|
|
7
|
-
import type { UPnPNATComponents, UPnPNATInit } from './index.js'
|
|
7
|
+
import type { UPnPNATComponents, UPnPNATInit, UPnPNAT as UPnPNATInterface } from './index.js'
|
|
8
8
|
import type { Logger, Startable } from '@libp2p/interface'
|
|
9
9
|
|
|
10
10
|
const DEFAULT_TTL = 7200
|
|
11
11
|
|
|
12
|
+
export type { NatAPI, MapPortOptions }
|
|
13
|
+
|
|
12
14
|
function highPort (min = 1024, max = 65535): number {
|
|
13
15
|
return Math.floor(Math.random() * (max - min + 1) + min)
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
export class UPnPNAT implements Startable {
|
|
18
|
+
export class UPnPNAT implements Startable, UPnPNATInterface {
|
|
19
|
+
public client: NatAPI
|
|
17
20
|
private readonly components: UPnPNATComponents
|
|
18
21
|
private readonly externalAddress?: string
|
|
19
22
|
private readonly localAddress?: string
|
|
@@ -22,7 +25,6 @@ export class UPnPNAT implements Startable {
|
|
|
22
25
|
private readonly keepAlive: boolean
|
|
23
26
|
private readonly gateway?: string
|
|
24
27
|
private started: boolean
|
|
25
|
-
private client?: NatAPI
|
|
26
28
|
private readonly log: Logger
|
|
27
29
|
|
|
28
30
|
constructor (components: UPnPNATComponents, init: UPnPNATInit) {
|
|
@@ -40,6 +42,13 @@ export class UPnPNAT implements Startable {
|
|
|
40
42
|
if (this.ttl < DEFAULT_TTL) {
|
|
41
43
|
throw new CodeError(`NatManager ttl should be at least ${DEFAULT_TTL} seconds`, ERR_INVALID_PARAMETERS)
|
|
42
44
|
}
|
|
45
|
+
|
|
46
|
+
this.client = upnpNat({
|
|
47
|
+
description: this.description,
|
|
48
|
+
ttl: this.ttl,
|
|
49
|
+
keepAlive: this.keepAlive,
|
|
50
|
+
gateway: this.gateway
|
|
51
|
+
})
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
isStarted (): boolean {
|
|
@@ -93,8 +102,7 @@ export class UPnPNAT implements Startable {
|
|
|
93
102
|
continue
|
|
94
103
|
}
|
|
95
104
|
|
|
96
|
-
const
|
|
97
|
-
const publicIp = this.externalAddress ?? await client.externalIp()
|
|
105
|
+
const publicIp = this.externalAddress ?? await this.client.externalIp()
|
|
98
106
|
const isPrivate = isPrivateIp(publicIp)
|
|
99
107
|
|
|
100
108
|
if (isPrivate === true) {
|
|
@@ -109,7 +117,7 @@ export class UPnPNAT implements Startable {
|
|
|
109
117
|
|
|
110
118
|
this.log(`opening uPnP connection from ${publicIp}:${publicPort} to ${host}:${port}`)
|
|
111
119
|
|
|
112
|
-
await client.map({
|
|
120
|
+
await this.client.map({
|
|
113
121
|
publicPort,
|
|
114
122
|
localPort: port,
|
|
115
123
|
localAddress: this.localAddress,
|
|
@@ -124,21 +132,6 @@ export class UPnPNAT implements Startable {
|
|
|
124
132
|
}
|
|
125
133
|
}
|
|
126
134
|
|
|
127
|
-
_getClient (): NatAPI {
|
|
128
|
-
if (this.client != null) {
|
|
129
|
-
return this.client
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
this.client = upnpNat({
|
|
133
|
-
description: this.description,
|
|
134
|
-
ttl: this.ttl,
|
|
135
|
-
keepAlive: this.keepAlive,
|
|
136
|
-
gateway: this.gateway
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
return this.client
|
|
140
|
-
}
|
|
141
|
-
|
|
142
135
|
/**
|
|
143
136
|
* Stops the NAT manager
|
|
144
137
|
*/
|
|
@@ -149,7 +142,6 @@ export class UPnPNAT implements Startable {
|
|
|
149
142
|
|
|
150
143
|
try {
|
|
151
144
|
await this.client.close()
|
|
152
|
-
this.client = undefined
|
|
153
145
|
} catch (err: any) {
|
|
154
146
|
this.log.error(err)
|
|
155
147
|
}
|