libp2p 0.46.20 → 0.46.21-0b4a2ee79

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 (278) hide show
  1. package/README.md +53 -40
  2. package/dist/index.min.js +40 -28
  3. package/dist/src/address-manager/index.d.ts +3 -1
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +3 -3
  6. package/dist/src/address-manager/index.js.map +1 -1
  7. package/dist/src/components.d.ts +3 -1
  8. package/dist/src/components.d.ts.map +1 -1
  9. package/dist/src/components.js.map +1 -1
  10. package/dist/src/connection/index.d.ts +2 -2
  11. package/dist/src/connection/index.d.ts.map +1 -1
  12. package/dist/src/connection/index.js +9 -9
  13. package/dist/src/connection/index.js.map +1 -1
  14. package/dist/src/connection-manager/auto-dial.d.ts +1 -1
  15. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  16. package/dist/src/connection-manager/auto-dial.js +21 -21
  17. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  18. package/dist/src/connection-manager/connection-pruner.d.ts +1 -1
  19. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  20. package/dist/src/connection-manager/connection-pruner.js +8 -8
  21. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  22. package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
  23. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  24. package/dist/src/connection-manager/constants.defaults.js +0 -4
  25. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  26. package/dist/src/connection-manager/dial-queue.d.ts +1 -3
  27. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  28. package/dist/src/connection-manager/dial-queue.js +30 -37
  29. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  30. package/dist/src/connection-manager/index.d.ts +1 -8
  31. package/dist/src/connection-manager/index.d.ts.map +1 -1
  32. package/dist/src/connection-manager/index.js +16 -17
  33. package/dist/src/connection-manager/index.js.map +1 -1
  34. package/dist/src/content-routing/index.js +1 -1
  35. package/dist/src/content-routing/index.js.map +1 -1
  36. package/dist/src/errors.d.ts +0 -2
  37. package/dist/src/errors.d.ts.map +1 -1
  38. package/dist/src/errors.js +0 -2
  39. package/dist/src/errors.js.map +1 -1
  40. package/dist/src/get-peer.d.ts.map +1 -1
  41. package/dist/src/get-peer.js +0 -3
  42. package/dist/src/get-peer.js.map +1 -1
  43. package/dist/src/index.d.ts +5 -6
  44. package/dist/src/index.d.ts.map +1 -1
  45. package/dist/src/index.js.map +1 -1
  46. package/dist/src/libp2p.d.ts +1 -2
  47. package/dist/src/libp2p.d.ts.map +1 -1
  48. package/dist/src/libp2p.js +23 -54
  49. package/dist/src/libp2p.js.map +1 -1
  50. package/dist/src/peer-routing.d.ts +5 -1
  51. package/dist/src/peer-routing.d.ts.map +1 -1
  52. package/dist/src/peer-routing.js +11 -8
  53. package/dist/src/peer-routing.js.map +1 -1
  54. package/dist/src/registrar.d.ts +3 -0
  55. package/dist/src/registrar.d.ts.map +1 -1
  56. package/dist/src/registrar.js +3 -3
  57. package/dist/src/registrar.js.map +1 -1
  58. package/dist/src/transport-manager.d.ts +3 -1
  59. package/dist/src/transport-manager.d.ts.map +1 -1
  60. package/dist/src/transport-manager.js +10 -10
  61. package/dist/src/transport-manager.js.map +1 -1
  62. package/dist/src/upgrader.d.ts +2 -2
  63. package/dist/src/upgrader.d.ts.map +1 -1
  64. package/dist/src/upgrader.js +75 -37
  65. package/dist/src/upgrader.js.map +1 -1
  66. package/dist/src/version.d.ts +1 -1
  67. package/dist/src/version.d.ts.map +1 -1
  68. package/dist/src/version.js +1 -1
  69. package/dist/src/version.js.map +1 -1
  70. package/package.json +34 -106
  71. package/src/address-manager/index.ts +5 -5
  72. package/src/components.ts +3 -1
  73. package/src/connection/index.ts +9 -10
  74. package/src/connection-manager/auto-dial.ts +21 -21
  75. package/src/connection-manager/connection-pruner.ts +8 -8
  76. package/src/connection-manager/constants.defaults.ts +0 -5
  77. package/src/connection-manager/dial-queue.ts +29 -38
  78. package/src/connection-manager/index.ts +16 -25
  79. package/src/content-routing/index.ts +1 -1
  80. package/src/errors.ts +0 -2
  81. package/src/get-peer.ts +0 -4
  82. package/src/index.ts +6 -7
  83. package/src/libp2p.ts +23 -61
  84. package/src/peer-routing.ts +15 -10
  85. package/src/registrar.ts +6 -5
  86. package/src/transport-manager.ts +12 -12
  87. package/src/upgrader.ts +100 -40
  88. package/src/version.ts +1 -1
  89. package/dist/src/autonat/constants.d.ts +0 -18
  90. package/dist/src/autonat/constants.d.ts.map +0 -1
  91. package/dist/src/autonat/constants.js +0 -18
  92. package/dist/src/autonat/constants.js.map +0 -1
  93. package/dist/src/autonat/index.d.ts +0 -62
  94. package/dist/src/autonat/index.d.ts.map +0 -1
  95. package/dist/src/autonat/index.js +0 -447
  96. package/dist/src/autonat/index.js.map +0 -1
  97. package/dist/src/autonat/pb/index.d.ts +0 -57
  98. package/dist/src/autonat/pb/index.d.ts.map +0 -1
  99. package/dist/src/autonat/pb/index.js +0 -250
  100. package/dist/src/autonat/pb/index.js.map +0 -1
  101. package/dist/src/circuit-relay/constants.d.ts +0 -50
  102. package/dist/src/circuit-relay/constants.d.ts.map +0 -1
  103. package/dist/src/circuit-relay/constants.js +0 -56
  104. package/dist/src/circuit-relay/constants.js.map +0 -1
  105. package/dist/src/circuit-relay/index.d.ts +0 -55
  106. package/dist/src/circuit-relay/index.d.ts.map +0 -1
  107. package/dist/src/circuit-relay/index.js +0 -38
  108. package/dist/src/circuit-relay/index.js.map +0 -1
  109. package/dist/src/circuit-relay/pb/index.d.ts +0 -93
  110. package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
  111. package/dist/src/circuit-relay/pb/index.js +0 -425
  112. package/dist/src/circuit-relay/pb/index.js.map +0 -1
  113. package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
  114. package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
  115. package/dist/src/circuit-relay/server/advert-service.js +0 -73
  116. package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
  117. package/dist/src/circuit-relay/server/index.d.ts +0 -65
  118. package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
  119. package/dist/src/circuit-relay/server/index.js +0 -312
  120. package/dist/src/circuit-relay/server/index.js.map +0 -1
  121. package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
  122. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
  123. package/dist/src/circuit-relay/server/reservation-store.js +0 -65
  124. package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
  125. package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
  126. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
  127. package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
  128. package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
  129. package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
  130. package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
  131. package/dist/src/circuit-relay/transport/discovery.js +0 -97
  132. package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
  133. package/dist/src/circuit-relay/transport/index.d.ts +0 -57
  134. package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
  135. package/dist/src/circuit-relay/transport/index.js +0 -276
  136. package/dist/src/circuit-relay/transport/index.js.map +0 -1
  137. package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
  138. package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
  139. package/dist/src/circuit-relay/transport/listener.js +0 -66
  140. package/dist/src/circuit-relay/transport/listener.js.map +0 -1
  141. package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
  142. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
  143. package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
  144. package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
  145. package/dist/src/circuit-relay/utils.d.ts +0 -13
  146. package/dist/src/circuit-relay/utils.d.ts.map +0 -1
  147. package/dist/src/circuit-relay/utils.js +0 -108
  148. package/dist/src/circuit-relay/utils.js.map +0 -1
  149. package/dist/src/dcutr/dcutr.d.ts +0 -43
  150. package/dist/src/dcutr/dcutr.d.ts.map +0 -1
  151. package/dist/src/dcutr/dcutr.js +0 -313
  152. package/dist/src/dcutr/dcutr.js.map +0 -1
  153. package/dist/src/dcutr/index.d.ts +0 -92
  154. package/dist/src/dcutr/index.d.ts.map +0 -1
  155. package/dist/src/dcutr/index.js +0 -63
  156. package/dist/src/dcutr/index.js.map +0 -1
  157. package/dist/src/dcutr/pb/message.d.ts +0 -20
  158. package/dist/src/dcutr/pb/message.d.ts.map +0 -1
  159. package/dist/src/dcutr/pb/message.js +0 -77
  160. package/dist/src/dcutr/pb/message.js.map +0 -1
  161. package/dist/src/dcutr/utils.d.ts +0 -8
  162. package/dist/src/dcutr/utils.d.ts.map +0 -1
  163. package/dist/src/dcutr/utils.js +0 -27
  164. package/dist/src/dcutr/utils.js.map +0 -1
  165. package/dist/src/fetch/constants.d.ts +0 -3
  166. package/dist/src/fetch/constants.d.ts.map +0 -1
  167. package/dist/src/fetch/constants.js +0 -4
  168. package/dist/src/fetch/constants.js.map +0 -1
  169. package/dist/src/fetch/index.d.ts +0 -61
  170. package/dist/src/fetch/index.d.ts.map +0 -1
  171. package/dist/src/fetch/index.js +0 -205
  172. package/dist/src/fetch/index.js.map +0 -1
  173. package/dist/src/fetch/pb/proto.d.ts +0 -28
  174. package/dist/src/fetch/pb/proto.d.ts.map +0 -1
  175. package/dist/src/fetch/pb/proto.js +0 -120
  176. package/dist/src/fetch/pb/proto.js.map +0 -1
  177. package/dist/src/identify/consts.d.ts +0 -10
  178. package/dist/src/identify/consts.d.ts.map +0 -1
  179. package/dist/src/identify/consts.js +0 -11
  180. package/dist/src/identify/consts.js.map +0 -1
  181. package/dist/src/identify/identify.d.ts +0 -54
  182. package/dist/src/identify/identify.d.ts.map +0 -1
  183. package/dist/src/identify/identify.js +0 -447
  184. package/dist/src/identify/identify.js.map +0 -1
  185. package/dist/src/identify/index.d.ts +0 -72
  186. package/dist/src/identify/index.d.ts.map +0 -1
  187. package/dist/src/identify/index.js +0 -15
  188. package/dist/src/identify/index.js.map +0 -1
  189. package/dist/src/identify/pb/message.d.ts +0 -17
  190. package/dist/src/identify/pb/message.d.ts.map +0 -1
  191. package/dist/src/identify/pb/message.js +0 -98
  192. package/dist/src/identify/pb/message.js.map +0 -1
  193. package/dist/src/insecure/index.d.ts +0 -26
  194. package/dist/src/insecure/index.d.ts.map +0 -1
  195. package/dist/src/insecure/index.js +0 -110
  196. package/dist/src/insecure/index.js.map +0 -1
  197. package/dist/src/insecure/pb/proto.d.ts +0 -30
  198. package/dist/src/insecure/pb/proto.d.ts.map +0 -1
  199. package/dist/src/insecure/pb/proto.js +0 -127
  200. package/dist/src/insecure/pb/proto.js.map +0 -1
  201. package/dist/src/ping/constants.d.ts +0 -9
  202. package/dist/src/ping/constants.d.ts.map +0 -1
  203. package/dist/src/ping/constants.js +0 -15
  204. package/dist/src/ping/constants.js.map +0 -1
  205. package/dist/src/ping/index.d.ts +0 -24
  206. package/dist/src/ping/index.d.ts.map +0 -1
  207. package/dist/src/ping/index.js +0 -115
  208. package/dist/src/ping/index.js.map +0 -1
  209. package/dist/src/pnet/crypto.d.ts +0 -18
  210. package/dist/src/pnet/crypto.d.ts.map +0 -1
  211. package/dist/src/pnet/crypto.js +0 -60
  212. package/dist/src/pnet/crypto.js.map +0 -1
  213. package/dist/src/pnet/errors.d.ts +0 -6
  214. package/dist/src/pnet/errors.d.ts.map +0 -1
  215. package/dist/src/pnet/errors.js +0 -6
  216. package/dist/src/pnet/errors.js.map +0 -1
  217. package/dist/src/pnet/index.d.ts +0 -31
  218. package/dist/src/pnet/index.d.ts.map +0 -1
  219. package/dist/src/pnet/index.js +0 -97
  220. package/dist/src/pnet/index.js.map +0 -1
  221. package/dist/src/pnet/key-generator.d.ts +0 -11
  222. package/dist/src/pnet/key-generator.d.ts.map +0 -1
  223. package/dist/src/pnet/key-generator.js +0 -22
  224. package/dist/src/pnet/key-generator.js.map +0 -1
  225. package/dist/src/upnp-nat/index.d.ts +0 -71
  226. package/dist/src/upnp-nat/index.d.ts.map +0 -1
  227. package/dist/src/upnp-nat/index.js +0 -136
  228. package/dist/src/upnp-nat/index.js.map +0 -1
  229. package/dist/src/utils/peer-job-queue.d.ts +0 -33
  230. package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
  231. package/dist/src/utils/peer-job-queue.js +0 -82
  232. package/dist/src/utils/peer-job-queue.js.map +0 -1
  233. package/dist/typedoc-urls.json +0 -71
  234. package/src/autonat/constants.ts +0 -19
  235. package/src/autonat/index.ts +0 -597
  236. package/src/autonat/pb/index.proto +0 -35
  237. package/src/autonat/pb/index.ts +0 -320
  238. package/src/circuit-relay/constants.ts +0 -72
  239. package/src/circuit-relay/index.ts +0 -59
  240. package/src/circuit-relay/pb/index.proto +0 -67
  241. package/src/circuit-relay/pb/index.ts +0 -539
  242. package/src/circuit-relay/server/advert-service.ts +0 -108
  243. package/src/circuit-relay/server/index.ts +0 -443
  244. package/src/circuit-relay/server/reservation-store.ts +0 -116
  245. package/src/circuit-relay/server/reservation-voucher.ts +0 -51
  246. package/src/circuit-relay/transport/discovery.ts +0 -136
  247. package/src/circuit-relay/transport/index.ts +0 -396
  248. package/src/circuit-relay/transport/listener.ts +0 -97
  249. package/src/circuit-relay/transport/reservation-store.ts +0 -312
  250. package/src/circuit-relay/utils.ts +0 -136
  251. package/src/dcutr/dcutr.ts +0 -378
  252. package/src/dcutr/index.ts +0 -102
  253. package/src/dcutr/pb/message.proto +0 -12
  254. package/src/dcutr/pb/message.ts +0 -96
  255. package/src/dcutr/utils.ts +0 -33
  256. package/src/fetch/README.md +0 -41
  257. package/src/fetch/constants.ts +0 -3
  258. package/src/fetch/index.ts +0 -315
  259. package/src/fetch/pb/proto.proto +0 -15
  260. package/src/fetch/pb/proto.ts +0 -153
  261. package/src/identify/README.md +0 -13
  262. package/src/identify/consts.ts +0 -12
  263. package/src/identify/identify.ts +0 -561
  264. package/src/identify/index.ts +0 -90
  265. package/src/identify/pb/message.proto +0 -30
  266. package/src/identify/pb/message.ts +0 -126
  267. package/src/insecure/index.ts +0 -138
  268. package/src/insecure/pb/proto.proto +0 -18
  269. package/src/insecure/pb/proto.ts +0 -161
  270. package/src/ping/constants.ts +0 -15
  271. package/src/ping/index.ts +0 -170
  272. package/src/pnet/README.md +0 -68
  273. package/src/pnet/crypto.ts +0 -67
  274. package/src/pnet/errors.ts +0 -5
  275. package/src/pnet/index.ts +0 -126
  276. package/src/pnet/key-generator.ts +0 -23
  277. package/src/upnp-nat/index.ts +0 -212
  278. package/src/utils/peer-job-queue.ts +0 -119
@@ -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
- }
@@ -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
@@ -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
- }