libp2p 0.46.21 → 1.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 +51 -47
- package/dist/index.min.js +40 -28
- package/dist/src/address-manager/index.d.ts +4 -5
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +3 -3
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/components.d.ts +4 -15
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +2 -2
- package/dist/src/components.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +1 -1
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
- package/dist/src/config/connection-gater.browser.js.map +1 -1
- package/dist/src/config/connection-gater.d.ts +1 -1
- package/dist/src/config/connection-gater.d.ts.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +1 -2
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +4 -6
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +11 -13
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +3 -6
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +21 -21
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +3 -5
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +8 -8
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +0 -4
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +3 -10
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +30 -38
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +3 -18
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +17 -18
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.js +1 -1
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +1 -5
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +2 -2
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -2
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +1 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/errors.d.ts +0 -2
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -2
- package/dist/src/errors.js.map +1 -1
- package/dist/src/get-peer.d.ts +1 -1
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +1 -5
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +5 -16
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +4 -12
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +31 -65
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -6
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +12 -9
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -7
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +4 -4
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +5 -9
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +12 -13
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +8 -19
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +75 -38
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/dist/typedoc-urls.json +8 -69
- package/package.json +37 -106
- package/src/address-manager/index.ts +6 -9
- package/src/components.ts +5 -16
- package/src/config/connection-gater.browser.ts +1 -1
- package/src/config/connection-gater.ts +1 -1
- package/src/config.ts +1 -2
- package/src/connection/index.ts +12 -17
- package/src/connection-manager/auto-dial.ts +23 -26
- package/src/connection-manager/connection-pruner.ts +10 -12
- package/src/connection-manager/constants.defaults.ts +0 -5
- package/src/connection-manager/dial-queue.ts +31 -46
- package/src/connection-manager/index.ts +19 -36
- package/src/connection-manager/utils.ts +1 -1
- package/src/content-routing/index.ts +3 -7
- package/src/content-routing/utils.ts +2 -3
- package/src/errors.ts +0 -2
- package/src/get-peer.ts +2 -7
- package/src/index.ts +6 -17
- package/src/libp2p.ts +32 -80
- package/src/peer-routing.ts +16 -15
- package/src/registrar.ts +8 -12
- package/src/transport-manager.ts +15 -22
- package/src/upgrader.ts +110 -64
- package/src/version.ts +1 -1
- package/dist/src/autonat/constants.d.ts +0 -18
- package/dist/src/autonat/constants.d.ts.map +0 -1
- package/dist/src/autonat/constants.js +0 -18
- package/dist/src/autonat/constants.js.map +0 -1
- package/dist/src/autonat/index.d.ts +0 -62
- package/dist/src/autonat/index.d.ts.map +0 -1
- package/dist/src/autonat/index.js +0 -447
- package/dist/src/autonat/index.js.map +0 -1
- package/dist/src/autonat/pb/index.d.ts +0 -57
- package/dist/src/autonat/pb/index.d.ts.map +0 -1
- package/dist/src/autonat/pb/index.js +0 -250
- package/dist/src/autonat/pb/index.js.map +0 -1
- package/dist/src/circuit-relay/constants.d.ts +0 -50
- package/dist/src/circuit-relay/constants.d.ts.map +0 -1
- package/dist/src/circuit-relay/constants.js +0 -56
- package/dist/src/circuit-relay/constants.js.map +0 -1
- package/dist/src/circuit-relay/index.d.ts +0 -55
- package/dist/src/circuit-relay/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/index.js +0 -38
- package/dist/src/circuit-relay/index.js.map +0 -1
- package/dist/src/circuit-relay/pb/index.d.ts +0 -93
- package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/pb/index.js +0 -425
- package/dist/src/circuit-relay/pb/index.js.map +0 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/advert-service.js +0 -73
- package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
- package/dist/src/circuit-relay/server/index.d.ts +0 -65
- package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/index.js +0 -312
- package/dist/src/circuit-relay/server/index.js.map +0 -1
- package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
- package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/reservation-store.js +0 -65
- package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
- package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/discovery.js +0 -97
- package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
- package/dist/src/circuit-relay/transport/index.d.ts +0 -57
- package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/index.js +0 -276
- package/dist/src/circuit-relay/transport/index.js.map +0 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
- package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/listener.js +0 -66
- package/dist/src/circuit-relay/transport/listener.js.map +0 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
- package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
- package/dist/src/circuit-relay/utils.d.ts +0 -13
- package/dist/src/circuit-relay/utils.d.ts.map +0 -1
- package/dist/src/circuit-relay/utils.js +0 -108
- package/dist/src/circuit-relay/utils.js.map +0 -1
- package/dist/src/dcutr/dcutr.d.ts +0 -43
- package/dist/src/dcutr/dcutr.d.ts.map +0 -1
- package/dist/src/dcutr/dcutr.js +0 -313
- package/dist/src/dcutr/dcutr.js.map +0 -1
- package/dist/src/dcutr/index.d.ts +0 -92
- package/dist/src/dcutr/index.d.ts.map +0 -1
- package/dist/src/dcutr/index.js +0 -63
- package/dist/src/dcutr/index.js.map +0 -1
- package/dist/src/dcutr/pb/message.d.ts +0 -20
- package/dist/src/dcutr/pb/message.d.ts.map +0 -1
- package/dist/src/dcutr/pb/message.js +0 -77
- package/dist/src/dcutr/pb/message.js.map +0 -1
- package/dist/src/dcutr/utils.d.ts +0 -8
- package/dist/src/dcutr/utils.d.ts.map +0 -1
- package/dist/src/dcutr/utils.js +0 -27
- package/dist/src/dcutr/utils.js.map +0 -1
- package/dist/src/fetch/constants.d.ts +0 -3
- package/dist/src/fetch/constants.d.ts.map +0 -1
- package/dist/src/fetch/constants.js +0 -4
- package/dist/src/fetch/constants.js.map +0 -1
- package/dist/src/fetch/index.d.ts +0 -61
- package/dist/src/fetch/index.d.ts.map +0 -1
- package/dist/src/fetch/index.js +0 -205
- package/dist/src/fetch/index.js.map +0 -1
- package/dist/src/fetch/pb/proto.d.ts +0 -28
- package/dist/src/fetch/pb/proto.d.ts.map +0 -1
- package/dist/src/fetch/pb/proto.js +0 -120
- package/dist/src/fetch/pb/proto.js.map +0 -1
- package/dist/src/identify/consts.d.ts +0 -10
- package/dist/src/identify/consts.d.ts.map +0 -1
- package/dist/src/identify/consts.js +0 -11
- package/dist/src/identify/consts.js.map +0 -1
- package/dist/src/identify/identify.d.ts +0 -54
- package/dist/src/identify/identify.d.ts.map +0 -1
- package/dist/src/identify/identify.js +0 -447
- package/dist/src/identify/identify.js.map +0 -1
- package/dist/src/identify/index.d.ts +0 -72
- package/dist/src/identify/index.d.ts.map +0 -1
- package/dist/src/identify/index.js +0 -15
- package/dist/src/identify/index.js.map +0 -1
- package/dist/src/identify/pb/message.d.ts +0 -17
- package/dist/src/identify/pb/message.d.ts.map +0 -1
- package/dist/src/identify/pb/message.js +0 -98
- package/dist/src/identify/pb/message.js.map +0 -1
- package/dist/src/insecure/index.d.ts +0 -26
- package/dist/src/insecure/index.d.ts.map +0 -1
- package/dist/src/insecure/index.js +0 -110
- package/dist/src/insecure/index.js.map +0 -1
- package/dist/src/insecure/pb/proto.d.ts +0 -30
- package/dist/src/insecure/pb/proto.d.ts.map +0 -1
- package/dist/src/insecure/pb/proto.js +0 -127
- package/dist/src/insecure/pb/proto.js.map +0 -1
- package/dist/src/ping/constants.d.ts +0 -9
- package/dist/src/ping/constants.d.ts.map +0 -1
- package/dist/src/ping/constants.js +0 -15
- package/dist/src/ping/constants.js.map +0 -1
- package/dist/src/ping/index.d.ts +0 -24
- package/dist/src/ping/index.d.ts.map +0 -1
- package/dist/src/ping/index.js +0 -115
- package/dist/src/ping/index.js.map +0 -1
- package/dist/src/pnet/crypto.d.ts +0 -18
- package/dist/src/pnet/crypto.d.ts.map +0 -1
- package/dist/src/pnet/crypto.js +0 -60
- package/dist/src/pnet/crypto.js.map +0 -1
- package/dist/src/pnet/errors.d.ts +0 -6
- package/dist/src/pnet/errors.d.ts.map +0 -1
- package/dist/src/pnet/errors.js +0 -6
- package/dist/src/pnet/errors.js.map +0 -1
- package/dist/src/pnet/index.d.ts +0 -31
- package/dist/src/pnet/index.d.ts.map +0 -1
- package/dist/src/pnet/index.js +0 -97
- package/dist/src/pnet/index.js.map +0 -1
- package/dist/src/pnet/key-generator.d.ts +0 -11
- package/dist/src/pnet/key-generator.d.ts.map +0 -1
- package/dist/src/pnet/key-generator.js +0 -22
- package/dist/src/pnet/key-generator.js.map +0 -1
- package/dist/src/upnp-nat/index.d.ts +0 -71
- package/dist/src/upnp-nat/index.d.ts.map +0 -1
- package/dist/src/upnp-nat/index.js +0 -136
- package/dist/src/upnp-nat/index.js.map +0 -1
- package/dist/src/utils/peer-job-queue.d.ts +0 -33
- package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
- package/dist/src/utils/peer-job-queue.js +0 -82
- package/dist/src/utils/peer-job-queue.js.map +0 -1
- package/src/autonat/constants.ts +0 -19
- package/src/autonat/index.ts +0 -597
- package/src/autonat/pb/index.proto +0 -35
- package/src/autonat/pb/index.ts +0 -320
- package/src/circuit-relay/constants.ts +0 -72
- package/src/circuit-relay/index.ts +0 -59
- package/src/circuit-relay/pb/index.proto +0 -67
- package/src/circuit-relay/pb/index.ts +0 -539
- package/src/circuit-relay/server/advert-service.ts +0 -108
- package/src/circuit-relay/server/index.ts +0 -443
- package/src/circuit-relay/server/reservation-store.ts +0 -116
- package/src/circuit-relay/server/reservation-voucher.ts +0 -51
- package/src/circuit-relay/transport/discovery.ts +0 -136
- package/src/circuit-relay/transport/index.ts +0 -396
- package/src/circuit-relay/transport/listener.ts +0 -97
- package/src/circuit-relay/transport/reservation-store.ts +0 -312
- package/src/circuit-relay/utils.ts +0 -136
- package/src/dcutr/dcutr.ts +0 -378
- package/src/dcutr/index.ts +0 -102
- package/src/dcutr/pb/message.proto +0 -12
- package/src/dcutr/pb/message.ts +0 -96
- package/src/dcutr/utils.ts +0 -33
- package/src/fetch/README.md +0 -41
- package/src/fetch/constants.ts +0 -3
- package/src/fetch/index.ts +0 -315
- package/src/fetch/pb/proto.proto +0 -15
- package/src/fetch/pb/proto.ts +0 -153
- package/src/identify/README.md +0 -13
- package/src/identify/consts.ts +0 -12
- package/src/identify/identify.ts +0 -561
- package/src/identify/index.ts +0 -90
- package/src/identify/pb/message.proto +0 -30
- package/src/identify/pb/message.ts +0 -126
- package/src/insecure/index.ts +0 -138
- package/src/insecure/pb/proto.proto +0 -18
- package/src/insecure/pb/proto.ts +0 -161
- package/src/ping/constants.ts +0 -15
- package/src/ping/index.ts +0 -170
- package/src/pnet/README.md +0 -68
- package/src/pnet/crypto.ts +0 -67
- package/src/pnet/errors.ts +0 -5
- package/src/pnet/index.ts +0 -126
- package/src/pnet/key-generator.ts +0 -23
- package/src/upnp-nat/index.ts +0 -212
- package/src/utils/peer-job-queue.ts +0 -119
package/src/pnet/crypto.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
3
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
4
|
-
import xsalsa20 from 'xsalsa20'
|
|
5
|
-
import * as Errors from './errors.js'
|
|
6
|
-
import { KEY_LENGTH } from './key-generator.js'
|
|
7
|
-
import type { Source } from 'it-stream-types'
|
|
8
|
-
|
|
9
|
-
const log = logger('libp2p:pnet')
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Creates a stream iterable to encrypt messages in a private network
|
|
13
|
-
*/
|
|
14
|
-
export function createBoxStream (nonce: Uint8Array, psk: Uint8Array): (source: Source<Uint8Array>) => AsyncIterable<Uint8Array> {
|
|
15
|
-
const xor = xsalsa20(nonce, psk)
|
|
16
|
-
|
|
17
|
-
return (source: Source<Uint8Array>) => (async function * () {
|
|
18
|
-
for await (const chunk of source) {
|
|
19
|
-
yield Uint8Array.from(xor.update(chunk.slice()))
|
|
20
|
-
}
|
|
21
|
-
})()
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Creates a stream iterable to decrypt messages in a private network
|
|
26
|
-
*/
|
|
27
|
-
export function createUnboxStream (nonce: Uint8Array, psk: Uint8Array) {
|
|
28
|
-
return (source: Source<Uint8Array>) => (async function * () {
|
|
29
|
-
const xor = xsalsa20(nonce, psk)
|
|
30
|
-
log.trace('Decryption enabled')
|
|
31
|
-
|
|
32
|
-
for await (const chunk of source) {
|
|
33
|
-
yield Uint8Array.from(xor.update(chunk.slice()))
|
|
34
|
-
}
|
|
35
|
-
})()
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Decode the version 1 psk from the given Uint8Array
|
|
40
|
-
*/
|
|
41
|
-
export function decodeV1PSK (pskBuffer: Uint8Array): { tag: string | undefined, codecName: string | undefined, psk: Uint8Array } {
|
|
42
|
-
try {
|
|
43
|
-
// This should pull from multibase/multicodec to allow for
|
|
44
|
-
// more encoding flexibility. Ideally we'd consume the codecs
|
|
45
|
-
// from the buffer line by line to evaluate the next line
|
|
46
|
-
// programmatically instead of making assumptions about the
|
|
47
|
-
// encodings of each line.
|
|
48
|
-
const metadata = uint8ArrayToString(pskBuffer).split(/(?:\r\n|\r|\n)/g)
|
|
49
|
-
const pskTag = metadata.shift()
|
|
50
|
-
const codec = metadata.shift()
|
|
51
|
-
const pskString = metadata.shift()
|
|
52
|
-
const psk = uint8ArrayFromString(pskString ?? '', 'base16')
|
|
53
|
-
|
|
54
|
-
if (psk.byteLength !== KEY_LENGTH) {
|
|
55
|
-
throw new Error(Errors.INVALID_PSK)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
tag: pskTag,
|
|
60
|
-
codecName: codec,
|
|
61
|
-
psk
|
|
62
|
-
}
|
|
63
|
-
} catch (err: any) {
|
|
64
|
-
log.error(err)
|
|
65
|
-
throw new Error(Errors.INVALID_PSK)
|
|
66
|
-
}
|
|
67
|
-
}
|
package/src/pnet/errors.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export const INVALID_PEER = 'Not a valid peer connection'
|
|
2
|
-
export const INVALID_PSK = 'Your private shared key is invalid'
|
|
3
|
-
export const NO_LOCAL_ID = 'No local private key provided'
|
|
4
|
-
export const NO_HANDSHAKE_CONNECTION = 'No connection for the handshake provided'
|
|
5
|
-
export const STREAM_ENDED = 'Stream ended prematurely'
|
package/src/pnet/index.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @packageDocumentation
|
|
3
|
-
*
|
|
4
|
-
* Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
*
|
|
8
|
-
* ```typescript
|
|
9
|
-
* import { createLibp2p } from 'libp2p'
|
|
10
|
-
* import { preSharedKey, generateKey } from 'libp2p/pnet'
|
|
11
|
-
*
|
|
12
|
-
* // Create a Uint8Array and write the swarm key to it
|
|
13
|
-
* const swarmKey = new Uint8Array(95)
|
|
14
|
-
* generateKey(swarmKey)
|
|
15
|
-
*
|
|
16
|
-
* const node = await createLibp2p({
|
|
17
|
-
* // ...other options
|
|
18
|
-
* connectionProtector: preSharedKey({
|
|
19
|
-
* psk: swarmKey
|
|
20
|
-
* })
|
|
21
|
-
* })
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { randomBytes } from '@libp2p/crypto'
|
|
26
|
-
import { CodeError } from '@libp2p/interface/errors'
|
|
27
|
-
import { logger } from '@libp2p/logger'
|
|
28
|
-
import { handshake } from 'it-handshake'
|
|
29
|
-
import map from 'it-map'
|
|
30
|
-
import { duplexPair } from 'it-pair/duplex'
|
|
31
|
-
import { pipe } from 'it-pipe'
|
|
32
|
-
import { codes } from '../errors.js'
|
|
33
|
-
import {
|
|
34
|
-
createBoxStream,
|
|
35
|
-
createUnboxStream,
|
|
36
|
-
decodeV1PSK
|
|
37
|
-
} from './crypto.js'
|
|
38
|
-
import * as Errors from './errors.js'
|
|
39
|
-
import { NONCE_LENGTH } from './key-generator.js'
|
|
40
|
-
import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface/connection'
|
|
41
|
-
|
|
42
|
-
const log = logger('libp2p:pnet')
|
|
43
|
-
|
|
44
|
-
export { generateKey } from './key-generator.js'
|
|
45
|
-
|
|
46
|
-
export interface ProtectorInit {
|
|
47
|
-
enabled?: boolean
|
|
48
|
-
psk: Uint8Array
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
class PreSharedKeyConnectionProtector implements ConnectionProtector {
|
|
52
|
-
public tag: string
|
|
53
|
-
private readonly psk: Uint8Array
|
|
54
|
-
private readonly enabled: boolean
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Takes a Private Shared Key (psk) and provides a `protect` method
|
|
58
|
-
* for wrapping existing connections in a private encryption stream.
|
|
59
|
-
*/
|
|
60
|
-
constructor (init: ProtectorInit) {
|
|
61
|
-
this.enabled = init.enabled !== false
|
|
62
|
-
|
|
63
|
-
if (this.enabled) {
|
|
64
|
-
const decodedPSK = decodeV1PSK(init.psk)
|
|
65
|
-
this.psk = decodedPSK.psk
|
|
66
|
-
this.tag = decodedPSK.tag ?? ''
|
|
67
|
-
} else {
|
|
68
|
-
this.psk = new Uint8Array()
|
|
69
|
-
this.tag = ''
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Takes a given Connection and creates a private encryption stream
|
|
75
|
-
* between its two peers from the PSK the Protector instance was
|
|
76
|
-
* created with.
|
|
77
|
-
*/
|
|
78
|
-
async protect (connection: MultiaddrConnection): Promise<MultiaddrConnection> {
|
|
79
|
-
if (!this.enabled) {
|
|
80
|
-
return connection
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (connection == null) {
|
|
84
|
-
throw new CodeError(Errors.NO_HANDSHAKE_CONNECTION, codes.ERR_INVALID_PARAMETERS)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Exchange nonces
|
|
88
|
-
log('protecting the connection')
|
|
89
|
-
const localNonce = randomBytes(NONCE_LENGTH)
|
|
90
|
-
|
|
91
|
-
const shake = handshake(connection)
|
|
92
|
-
shake.write(localNonce)
|
|
93
|
-
|
|
94
|
-
const result = await shake.reader.next(NONCE_LENGTH)
|
|
95
|
-
|
|
96
|
-
if (result.value == null) {
|
|
97
|
-
throw new CodeError(Errors.STREAM_ENDED, codes.ERR_INVALID_PARAMETERS)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const remoteNonce = result.value.slice()
|
|
101
|
-
shake.rest()
|
|
102
|
-
|
|
103
|
-
// Create the boxing/unboxing pipe
|
|
104
|
-
log('exchanged nonces')
|
|
105
|
-
const [internal, external] = duplexPair<Uint8Array>()
|
|
106
|
-
pipe(
|
|
107
|
-
external,
|
|
108
|
-
// Encrypt all outbound traffic
|
|
109
|
-
createBoxStream(localNonce, this.psk),
|
|
110
|
-
shake.stream,
|
|
111
|
-
(source) => map(source, (buf) => buf.subarray()),
|
|
112
|
-
// Decrypt all inbound traffic
|
|
113
|
-
createUnboxStream(remoteNonce, this.psk),
|
|
114
|
-
external
|
|
115
|
-
).catch(log.error)
|
|
116
|
-
|
|
117
|
-
return {
|
|
118
|
-
...connection,
|
|
119
|
-
...internal
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function preSharedKey (init: ProtectorInit): () => ConnectionProtector {
|
|
125
|
-
return () => new PreSharedKeyConnectionProtector(init)
|
|
126
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from '@libp2p/crypto'
|
|
2
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
3
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generates a PSK that can be used in a libp2p-pnet private network
|
|
7
|
-
*
|
|
8
|
-
* @param {Uint8Array | NodeJS.WriteStream} bytes - An object to write the psk into
|
|
9
|
-
* @returns {void}
|
|
10
|
-
*/
|
|
11
|
-
export function generateKey (bytes: Uint8Array | NodeJS.WriteStream): void {
|
|
12
|
-
const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16')
|
|
13
|
-
const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
|
|
14
|
-
|
|
15
|
-
if (bytes instanceof Uint8Array) {
|
|
16
|
-
bytes.set(key)
|
|
17
|
-
} else {
|
|
18
|
-
bytes.write(key)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const NONCE_LENGTH = 24
|
|
23
|
-
export const KEY_LENGTH = 32
|
package/src/upnp-nat/index.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import { upnpNat, type NatAPI } from '@achingbrain/nat-port-mapper'
|
|
2
|
-
import { CodeError } from '@libp2p/interface/errors'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
|
-
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
|
|
5
|
-
import { fromNodeAddress } from '@multiformats/multiaddr'
|
|
6
|
-
import isPrivateIp from 'private-ip'
|
|
7
|
-
import { isBrowser } from 'wherearewe'
|
|
8
|
-
import { codes } from '../errors.js'
|
|
9
|
-
import * as pkg from '../version.js'
|
|
10
|
-
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
11
|
-
import type { Startable } from '@libp2p/interface/startable'
|
|
12
|
-
import type { AddressManager } from '@libp2p/interface-internal/address-manager'
|
|
13
|
-
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
14
|
-
|
|
15
|
-
const log = logger('libp2p:upnp-nat')
|
|
16
|
-
const DEFAULT_TTL = 7200
|
|
17
|
-
|
|
18
|
-
function highPort (min = 1024, max = 65535): number {
|
|
19
|
-
return Math.floor(Math.random() * (max - min + 1) + min)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface PMPOptions {
|
|
23
|
-
/**
|
|
24
|
-
* Whether to enable PMP as well as UPnP
|
|
25
|
-
*/
|
|
26
|
-
enabled?: boolean
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface UPnPNATInit {
|
|
30
|
-
/**
|
|
31
|
-
* Pass a value to use instead of auto-detection
|
|
32
|
-
*/
|
|
33
|
-
externalAddress?: string
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Pass a value to use instead of auto-detection
|
|
37
|
-
*/
|
|
38
|
-
localAddress?: string
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* A string value to use for the port mapping description on the gateway
|
|
42
|
-
*/
|
|
43
|
-
description?: string
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* How long UPnP port mappings should last for in seconds (minimum 1200)
|
|
47
|
-
*/
|
|
48
|
-
ttl?: number
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Whether to automatically refresh UPnP port mappings when their TTL is reached
|
|
52
|
-
*/
|
|
53
|
-
keepAlive?: boolean
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Pass a value to use instead of auto-detection
|
|
57
|
-
*/
|
|
58
|
-
gateway?: string
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export interface UPnPNATComponents {
|
|
62
|
-
peerId: PeerId
|
|
63
|
-
transportManager: TransportManager
|
|
64
|
-
addressManager: AddressManager
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
class UPnPNAT implements Startable {
|
|
68
|
-
private readonly components: UPnPNATComponents
|
|
69
|
-
private readonly externalAddress?: string
|
|
70
|
-
private readonly localAddress?: string
|
|
71
|
-
private readonly description: string
|
|
72
|
-
private readonly ttl: number
|
|
73
|
-
private readonly keepAlive: boolean
|
|
74
|
-
private readonly gateway?: string
|
|
75
|
-
private started: boolean
|
|
76
|
-
private client?: NatAPI
|
|
77
|
-
|
|
78
|
-
constructor (components: UPnPNATComponents, init: UPnPNATInit) {
|
|
79
|
-
this.components = components
|
|
80
|
-
|
|
81
|
-
this.started = false
|
|
82
|
-
this.externalAddress = init.externalAddress
|
|
83
|
-
this.localAddress = init.localAddress
|
|
84
|
-
this.description = init.description ?? `${pkg.name}@${pkg.version} ${this.components.peerId.toString()}`
|
|
85
|
-
this.ttl = init.ttl ?? DEFAULT_TTL
|
|
86
|
-
this.keepAlive = init.keepAlive ?? true
|
|
87
|
-
this.gateway = init.gateway
|
|
88
|
-
|
|
89
|
-
if (this.ttl < DEFAULT_TTL) {
|
|
90
|
-
throw new CodeError(`NatManager ttl should be at least ${DEFAULT_TTL} seconds`, codes.ERR_INVALID_PARAMETERS)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
isStarted (): boolean {
|
|
95
|
-
return this.started
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
start (): void {
|
|
99
|
-
// #TODO: is there a way to remove this? Seems like a hack
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Attempt to use uPnP to configure port mapping using the current gateway.
|
|
104
|
-
*
|
|
105
|
-
* Run after start to ensure the transport manager has all addresses configured.
|
|
106
|
-
*/
|
|
107
|
-
afterStart (): void {
|
|
108
|
-
if (isBrowser || this.started) {
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
this.started = true
|
|
113
|
-
|
|
114
|
-
// done async to not slow down startup
|
|
115
|
-
void this._start().catch((err) => {
|
|
116
|
-
// hole punching errors are non-fatal
|
|
117
|
-
log.error(err)
|
|
118
|
-
})
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async _start (): Promise<void> {
|
|
122
|
-
const addrs = this.components.transportManager.getAddrs()
|
|
123
|
-
|
|
124
|
-
for (const addr of addrs) {
|
|
125
|
-
// try to open uPnP ports for each thin waist address
|
|
126
|
-
const { family, host, port, transport } = addr.toOptions()
|
|
127
|
-
|
|
128
|
-
if (!addr.isThinWaistAddress() || transport !== 'tcp') {
|
|
129
|
-
// only bare tcp addresses
|
|
130
|
-
// eslint-disable-next-line no-continue
|
|
131
|
-
continue
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (isLoopback(addr)) {
|
|
135
|
-
// eslint-disable-next-line no-continue
|
|
136
|
-
continue
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (family !== 4) {
|
|
140
|
-
// ignore ipv6
|
|
141
|
-
// eslint-disable-next-line no-continue
|
|
142
|
-
continue
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const client = this._getClient()
|
|
146
|
-
const publicIp = this.externalAddress ?? await client.externalIp()
|
|
147
|
-
const isPrivate = isPrivateIp(publicIp)
|
|
148
|
-
|
|
149
|
-
if (isPrivate === true) {
|
|
150
|
-
throw new Error(`${publicIp} is private - please set config.nat.externalIp to an externally routable IP or ensure you are not behind a double NAT`)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (isPrivate == null) {
|
|
154
|
-
throw new Error(`${publicIp} is not an IP address`)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const publicPort = highPort()
|
|
158
|
-
|
|
159
|
-
log(`opening uPnP connection from ${publicIp}:${publicPort} to ${host}:${port}`)
|
|
160
|
-
|
|
161
|
-
await client.map({
|
|
162
|
-
publicPort,
|
|
163
|
-
localPort: port,
|
|
164
|
-
localAddress: this.localAddress,
|
|
165
|
-
protocol: transport.toUpperCase() === 'TCP' ? 'TCP' : 'UDP'
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
this.components.addressManager.addObservedAddr(fromNodeAddress({
|
|
169
|
-
family: 4,
|
|
170
|
-
address: publicIp,
|
|
171
|
-
port: publicPort
|
|
172
|
-
}, transport))
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
_getClient (): NatAPI {
|
|
177
|
-
if (this.client != null) {
|
|
178
|
-
return this.client
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
this.client = upnpNat({
|
|
182
|
-
description: this.description,
|
|
183
|
-
ttl: this.ttl,
|
|
184
|
-
keepAlive: this.keepAlive,
|
|
185
|
-
gateway: this.gateway
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
return this.client
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Stops the NAT manager
|
|
193
|
-
*/
|
|
194
|
-
async stop (): Promise<void> {
|
|
195
|
-
if (isBrowser || this.client == null) {
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
try {
|
|
200
|
-
await this.client.close()
|
|
201
|
-
this.client = undefined
|
|
202
|
-
} catch (err: any) {
|
|
203
|
-
log.error(err)
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export function uPnPNATService (init: UPnPNATInit = {}): (components: UPnPNATComponents) => UPnPNAT {
|
|
209
|
-
return (components: UPnPNATComponents) => {
|
|
210
|
-
return new UPnPNAT(components, init)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
|
|
3
|
-
import { CodeError } from '@libp2p/interface/errors'
|
|
4
|
-
import PQueue from 'p-queue'
|
|
5
|
-
import { codes } from '../errors.js'
|
|
6
|
-
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
7
|
-
import type { QueueAddOptions, Options, Queue } from 'p-queue'
|
|
8
|
-
|
|
9
|
-
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
10
|
-
// Used to compute insertion index to keep queue sorted after insertion
|
|
11
|
-
function lowerBound<T> (array: readonly T[], value: T, comparator: (a: T, b: T) => number): number {
|
|
12
|
-
let first = 0
|
|
13
|
-
let count = array.length
|
|
14
|
-
|
|
15
|
-
while (count > 0) {
|
|
16
|
-
const step = Math.trunc(count / 2)
|
|
17
|
-
let it = first + step
|
|
18
|
-
|
|
19
|
-
if (comparator(array[it]!, value) <= 0) {
|
|
20
|
-
first = ++it
|
|
21
|
-
count -= step + 1
|
|
22
|
-
} else {
|
|
23
|
-
count = step
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return first
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface RunFunction { (): Promise<unknown> }
|
|
31
|
-
|
|
32
|
-
export interface PeerPriorityQueueOptions extends QueueAddOptions {
|
|
33
|
-
peerId: PeerId
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface PeerJob {
|
|
37
|
-
priority: number
|
|
38
|
-
peerId: PeerId
|
|
39
|
-
run: RunFunction
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
44
|
-
* that adds support for filtering jobs by peer id
|
|
45
|
-
*/
|
|
46
|
-
class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
|
|
47
|
-
readonly #queue: PeerJob[] = []
|
|
48
|
-
|
|
49
|
-
enqueue (run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void {
|
|
50
|
-
const peerId = options?.peerId
|
|
51
|
-
const priority = options?.priority ?? 0
|
|
52
|
-
|
|
53
|
-
if (peerId == null) {
|
|
54
|
-
throw new CodeError('missing peer id', codes.ERR_INVALID_PARAMETERS)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const element: PeerJob = {
|
|
58
|
-
priority,
|
|
59
|
-
peerId,
|
|
60
|
-
run
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (this.size > 0 && this.#queue[this.size - 1]!.priority >= priority) {
|
|
64
|
-
this.#queue.push(element)
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const index = lowerBound(
|
|
69
|
-
this.#queue, element,
|
|
70
|
-
(a: Readonly<PeerPriorityQueueOptions>, b: Readonly<PeerPriorityQueueOptions>) => b.priority! - a.priority!
|
|
71
|
-
)
|
|
72
|
-
this.#queue.splice(index, 0, element)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
dequeue (): RunFunction | undefined {
|
|
76
|
-
const item = this.#queue.shift()
|
|
77
|
-
return item?.run
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
filter (options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[] {
|
|
81
|
-
if (options.peerId != null) {
|
|
82
|
-
const peerId = options.peerId
|
|
83
|
-
|
|
84
|
-
return this.#queue.filter(
|
|
85
|
-
(element: Readonly<PeerPriorityQueueOptions>) => peerId.equals(element.peerId)
|
|
86
|
-
).map((element: Readonly<{ run: RunFunction }>) => element.run)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return this.#queue.filter(
|
|
90
|
-
(element: Readonly<PeerPriorityQueueOptions>) => element.priority === options.priority
|
|
91
|
-
).map((element: Readonly<{ run: RunFunction }>) => element.run)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
get size (): number {
|
|
95
|
-
return this.#queue.length
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Extends PQueue to add support for querying queued jobs by peer id
|
|
101
|
-
*/
|
|
102
|
-
export class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
|
|
103
|
-
constructor (options: Options<PeerPriorityQueue, PeerPriorityQueueOptions> = {}) {
|
|
104
|
-
super({
|
|
105
|
-
...options,
|
|
106
|
-
queueClass: PeerPriorityQueue
|
|
107
|
-
})
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Returns true if this queue has a job for the passed peer id that has not yet
|
|
112
|
-
* started to run
|
|
113
|
-
*/
|
|
114
|
-
hasJob (peerId: PeerId): boolean {
|
|
115
|
-
return this.sizeBy({
|
|
116
|
-
peerId
|
|
117
|
-
}) > 0
|
|
118
|
-
}
|
|
119
|
-
}
|