libp2p 0.46.21 → 1.0.0-7682861f9

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 (301) hide show
  1. package/README.md +51 -47
  2. package/dist/index.min.js +40 -28
  3. package/dist/src/address-manager/index.d.ts +4 -5
  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 +4 -15
  8. package/dist/src/components.d.ts.map +1 -1
  9. package/dist/src/components.js +2 -2
  10. package/dist/src/components.js.map +1 -1
  11. package/dist/src/config/connection-gater.browser.d.ts +1 -1
  12. package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
  13. package/dist/src/config/connection-gater.browser.js.map +1 -1
  14. package/dist/src/config/connection-gater.d.ts +1 -1
  15. package/dist/src/config/connection-gater.d.ts.map +1 -1
  16. package/dist/src/config.d.ts.map +1 -1
  17. package/dist/src/config.js +1 -2
  18. package/dist/src/config.js.map +1 -1
  19. package/dist/src/connection/index.d.ts +4 -6
  20. package/dist/src/connection/index.d.ts.map +1 -1
  21. package/dist/src/connection/index.js +11 -13
  22. package/dist/src/connection/index.js.map +1 -1
  23. package/dist/src/connection-manager/auto-dial.d.ts +3 -6
  24. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  25. package/dist/src/connection-manager/auto-dial.js +21 -21
  26. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  27. package/dist/src/connection-manager/connection-pruner.d.ts +3 -5
  28. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  29. package/dist/src/connection-manager/connection-pruner.js +8 -8
  30. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  31. package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
  32. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  33. package/dist/src/connection-manager/constants.defaults.js +0 -4
  34. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  35. package/dist/src/connection-manager/dial-queue.d.ts +3 -10
  36. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  37. package/dist/src/connection-manager/dial-queue.js +30 -38
  38. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  39. package/dist/src/connection-manager/index.d.ts +3 -18
  40. package/dist/src/connection-manager/index.d.ts.map +1 -1
  41. package/dist/src/connection-manager/index.js +17 -18
  42. package/dist/src/connection-manager/index.js.map +1 -1
  43. package/dist/src/connection-manager/utils.js +1 -1
  44. package/dist/src/connection-manager/utils.js.map +1 -1
  45. package/dist/src/content-routing/index.d.ts +1 -5
  46. package/dist/src/content-routing/index.d.ts.map +1 -1
  47. package/dist/src/content-routing/index.js +2 -2
  48. package/dist/src/content-routing/index.js.map +1 -1
  49. package/dist/src/content-routing/utils.d.ts +1 -2
  50. package/dist/src/content-routing/utils.d.ts.map +1 -1
  51. package/dist/src/content-routing/utils.js +1 -1
  52. package/dist/src/content-routing/utils.js.map +1 -1
  53. package/dist/src/errors.d.ts +0 -2
  54. package/dist/src/errors.d.ts.map +1 -1
  55. package/dist/src/errors.js +0 -2
  56. package/dist/src/errors.js.map +1 -1
  57. package/dist/src/get-peer.d.ts +1 -1
  58. package/dist/src/get-peer.d.ts.map +1 -1
  59. package/dist/src/get-peer.js +1 -5
  60. package/dist/src/get-peer.js.map +1 -1
  61. package/dist/src/index.d.ts +5 -16
  62. package/dist/src/index.d.ts.map +1 -1
  63. package/dist/src/index.js.map +1 -1
  64. package/dist/src/libp2p.d.ts +4 -12
  65. package/dist/src/libp2p.d.ts.map +1 -1
  66. package/dist/src/libp2p.js +31 -65
  67. package/dist/src/libp2p.js.map +1 -1
  68. package/dist/src/peer-routing.d.ts +6 -6
  69. package/dist/src/peer-routing.d.ts.map +1 -1
  70. package/dist/src/peer-routing.js +12 -9
  71. package/dist/src/peer-routing.js.map +1 -1
  72. package/dist/src/registrar.d.ts +5 -7
  73. package/dist/src/registrar.d.ts.map +1 -1
  74. package/dist/src/registrar.js +4 -4
  75. package/dist/src/registrar.js.map +1 -1
  76. package/dist/src/transport-manager.d.ts +5 -9
  77. package/dist/src/transport-manager.d.ts.map +1 -1
  78. package/dist/src/transport-manager.js +12 -13
  79. package/dist/src/transport-manager.js.map +1 -1
  80. package/dist/src/upgrader.d.ts +8 -19
  81. package/dist/src/upgrader.d.ts.map +1 -1
  82. package/dist/src/upgrader.js +75 -38
  83. package/dist/src/upgrader.js.map +1 -1
  84. package/dist/src/version.d.ts +1 -1
  85. package/dist/src/version.d.ts.map +1 -1
  86. package/dist/src/version.js +1 -1
  87. package/dist/src/version.js.map +1 -1
  88. package/package.json +37 -106
  89. package/src/address-manager/index.ts +6 -9
  90. package/src/components.ts +5 -16
  91. package/src/config/connection-gater.browser.ts +1 -1
  92. package/src/config/connection-gater.ts +1 -1
  93. package/src/config.ts +1 -2
  94. package/src/connection/index.ts +12 -17
  95. package/src/connection-manager/auto-dial.ts +23 -26
  96. package/src/connection-manager/connection-pruner.ts +10 -12
  97. package/src/connection-manager/constants.defaults.ts +0 -5
  98. package/src/connection-manager/dial-queue.ts +31 -46
  99. package/src/connection-manager/index.ts +19 -36
  100. package/src/connection-manager/utils.ts +1 -1
  101. package/src/content-routing/index.ts +3 -7
  102. package/src/content-routing/utils.ts +2 -3
  103. package/src/errors.ts +0 -2
  104. package/src/get-peer.ts +2 -7
  105. package/src/index.ts +6 -17
  106. package/src/libp2p.ts +32 -80
  107. package/src/peer-routing.ts +16 -15
  108. package/src/registrar.ts +8 -12
  109. package/src/transport-manager.ts +15 -22
  110. package/src/upgrader.ts +110 -64
  111. package/src/version.ts +1 -1
  112. package/dist/src/autonat/constants.d.ts +0 -18
  113. package/dist/src/autonat/constants.d.ts.map +0 -1
  114. package/dist/src/autonat/constants.js +0 -18
  115. package/dist/src/autonat/constants.js.map +0 -1
  116. package/dist/src/autonat/index.d.ts +0 -62
  117. package/dist/src/autonat/index.d.ts.map +0 -1
  118. package/dist/src/autonat/index.js +0 -447
  119. package/dist/src/autonat/index.js.map +0 -1
  120. package/dist/src/autonat/pb/index.d.ts +0 -57
  121. package/dist/src/autonat/pb/index.d.ts.map +0 -1
  122. package/dist/src/autonat/pb/index.js +0 -250
  123. package/dist/src/autonat/pb/index.js.map +0 -1
  124. package/dist/src/circuit-relay/constants.d.ts +0 -50
  125. package/dist/src/circuit-relay/constants.d.ts.map +0 -1
  126. package/dist/src/circuit-relay/constants.js +0 -56
  127. package/dist/src/circuit-relay/constants.js.map +0 -1
  128. package/dist/src/circuit-relay/index.d.ts +0 -55
  129. package/dist/src/circuit-relay/index.d.ts.map +0 -1
  130. package/dist/src/circuit-relay/index.js +0 -38
  131. package/dist/src/circuit-relay/index.js.map +0 -1
  132. package/dist/src/circuit-relay/pb/index.d.ts +0 -93
  133. package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
  134. package/dist/src/circuit-relay/pb/index.js +0 -425
  135. package/dist/src/circuit-relay/pb/index.js.map +0 -1
  136. package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
  137. package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
  138. package/dist/src/circuit-relay/server/advert-service.js +0 -73
  139. package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
  140. package/dist/src/circuit-relay/server/index.d.ts +0 -65
  141. package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
  142. package/dist/src/circuit-relay/server/index.js +0 -312
  143. package/dist/src/circuit-relay/server/index.js.map +0 -1
  144. package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
  145. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
  146. package/dist/src/circuit-relay/server/reservation-store.js +0 -65
  147. package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
  148. package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
  149. package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
  150. package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
  151. package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
  152. package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
  153. package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
  154. package/dist/src/circuit-relay/transport/discovery.js +0 -97
  155. package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
  156. package/dist/src/circuit-relay/transport/index.d.ts +0 -57
  157. package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
  158. package/dist/src/circuit-relay/transport/index.js +0 -276
  159. package/dist/src/circuit-relay/transport/index.js.map +0 -1
  160. package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
  161. package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
  162. package/dist/src/circuit-relay/transport/listener.js +0 -66
  163. package/dist/src/circuit-relay/transport/listener.js.map +0 -1
  164. package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
  165. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
  166. package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
  167. package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
  168. package/dist/src/circuit-relay/utils.d.ts +0 -13
  169. package/dist/src/circuit-relay/utils.d.ts.map +0 -1
  170. package/dist/src/circuit-relay/utils.js +0 -108
  171. package/dist/src/circuit-relay/utils.js.map +0 -1
  172. package/dist/src/dcutr/dcutr.d.ts +0 -43
  173. package/dist/src/dcutr/dcutr.d.ts.map +0 -1
  174. package/dist/src/dcutr/dcutr.js +0 -313
  175. package/dist/src/dcutr/dcutr.js.map +0 -1
  176. package/dist/src/dcutr/index.d.ts +0 -92
  177. package/dist/src/dcutr/index.d.ts.map +0 -1
  178. package/dist/src/dcutr/index.js +0 -63
  179. package/dist/src/dcutr/index.js.map +0 -1
  180. package/dist/src/dcutr/pb/message.d.ts +0 -20
  181. package/dist/src/dcutr/pb/message.d.ts.map +0 -1
  182. package/dist/src/dcutr/pb/message.js +0 -77
  183. package/dist/src/dcutr/pb/message.js.map +0 -1
  184. package/dist/src/dcutr/utils.d.ts +0 -8
  185. package/dist/src/dcutr/utils.d.ts.map +0 -1
  186. package/dist/src/dcutr/utils.js +0 -27
  187. package/dist/src/dcutr/utils.js.map +0 -1
  188. package/dist/src/fetch/constants.d.ts +0 -3
  189. package/dist/src/fetch/constants.d.ts.map +0 -1
  190. package/dist/src/fetch/constants.js +0 -4
  191. package/dist/src/fetch/constants.js.map +0 -1
  192. package/dist/src/fetch/index.d.ts +0 -61
  193. package/dist/src/fetch/index.d.ts.map +0 -1
  194. package/dist/src/fetch/index.js +0 -205
  195. package/dist/src/fetch/index.js.map +0 -1
  196. package/dist/src/fetch/pb/proto.d.ts +0 -28
  197. package/dist/src/fetch/pb/proto.d.ts.map +0 -1
  198. package/dist/src/fetch/pb/proto.js +0 -120
  199. package/dist/src/fetch/pb/proto.js.map +0 -1
  200. package/dist/src/identify/consts.d.ts +0 -10
  201. package/dist/src/identify/consts.d.ts.map +0 -1
  202. package/dist/src/identify/consts.js +0 -11
  203. package/dist/src/identify/consts.js.map +0 -1
  204. package/dist/src/identify/identify.d.ts +0 -54
  205. package/dist/src/identify/identify.d.ts.map +0 -1
  206. package/dist/src/identify/identify.js +0 -447
  207. package/dist/src/identify/identify.js.map +0 -1
  208. package/dist/src/identify/index.d.ts +0 -72
  209. package/dist/src/identify/index.d.ts.map +0 -1
  210. package/dist/src/identify/index.js +0 -15
  211. package/dist/src/identify/index.js.map +0 -1
  212. package/dist/src/identify/pb/message.d.ts +0 -17
  213. package/dist/src/identify/pb/message.d.ts.map +0 -1
  214. package/dist/src/identify/pb/message.js +0 -98
  215. package/dist/src/identify/pb/message.js.map +0 -1
  216. package/dist/src/insecure/index.d.ts +0 -26
  217. package/dist/src/insecure/index.d.ts.map +0 -1
  218. package/dist/src/insecure/index.js +0 -110
  219. package/dist/src/insecure/index.js.map +0 -1
  220. package/dist/src/insecure/pb/proto.d.ts +0 -30
  221. package/dist/src/insecure/pb/proto.d.ts.map +0 -1
  222. package/dist/src/insecure/pb/proto.js +0 -127
  223. package/dist/src/insecure/pb/proto.js.map +0 -1
  224. package/dist/src/ping/constants.d.ts +0 -9
  225. package/dist/src/ping/constants.d.ts.map +0 -1
  226. package/dist/src/ping/constants.js +0 -15
  227. package/dist/src/ping/constants.js.map +0 -1
  228. package/dist/src/ping/index.d.ts +0 -24
  229. package/dist/src/ping/index.d.ts.map +0 -1
  230. package/dist/src/ping/index.js +0 -115
  231. package/dist/src/ping/index.js.map +0 -1
  232. package/dist/src/pnet/crypto.d.ts +0 -18
  233. package/dist/src/pnet/crypto.d.ts.map +0 -1
  234. package/dist/src/pnet/crypto.js +0 -60
  235. package/dist/src/pnet/crypto.js.map +0 -1
  236. package/dist/src/pnet/errors.d.ts +0 -6
  237. package/dist/src/pnet/errors.d.ts.map +0 -1
  238. package/dist/src/pnet/errors.js +0 -6
  239. package/dist/src/pnet/errors.js.map +0 -1
  240. package/dist/src/pnet/index.d.ts +0 -31
  241. package/dist/src/pnet/index.d.ts.map +0 -1
  242. package/dist/src/pnet/index.js +0 -97
  243. package/dist/src/pnet/index.js.map +0 -1
  244. package/dist/src/pnet/key-generator.d.ts +0 -11
  245. package/dist/src/pnet/key-generator.d.ts.map +0 -1
  246. package/dist/src/pnet/key-generator.js +0 -22
  247. package/dist/src/pnet/key-generator.js.map +0 -1
  248. package/dist/src/upnp-nat/index.d.ts +0 -71
  249. package/dist/src/upnp-nat/index.d.ts.map +0 -1
  250. package/dist/src/upnp-nat/index.js +0 -136
  251. package/dist/src/upnp-nat/index.js.map +0 -1
  252. package/dist/src/utils/peer-job-queue.d.ts +0 -33
  253. package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
  254. package/dist/src/utils/peer-job-queue.js +0 -82
  255. package/dist/src/utils/peer-job-queue.js.map +0 -1
  256. package/dist/typedoc-urls.json +0 -71
  257. package/src/autonat/constants.ts +0 -19
  258. package/src/autonat/index.ts +0 -597
  259. package/src/autonat/pb/index.proto +0 -35
  260. package/src/autonat/pb/index.ts +0 -320
  261. package/src/circuit-relay/constants.ts +0 -72
  262. package/src/circuit-relay/index.ts +0 -59
  263. package/src/circuit-relay/pb/index.proto +0 -67
  264. package/src/circuit-relay/pb/index.ts +0 -539
  265. package/src/circuit-relay/server/advert-service.ts +0 -108
  266. package/src/circuit-relay/server/index.ts +0 -443
  267. package/src/circuit-relay/server/reservation-store.ts +0 -116
  268. package/src/circuit-relay/server/reservation-voucher.ts +0 -51
  269. package/src/circuit-relay/transport/discovery.ts +0 -136
  270. package/src/circuit-relay/transport/index.ts +0 -396
  271. package/src/circuit-relay/transport/listener.ts +0 -97
  272. package/src/circuit-relay/transport/reservation-store.ts +0 -312
  273. package/src/circuit-relay/utils.ts +0 -136
  274. package/src/dcutr/dcutr.ts +0 -378
  275. package/src/dcutr/index.ts +0 -102
  276. package/src/dcutr/pb/message.proto +0 -12
  277. package/src/dcutr/pb/message.ts +0 -96
  278. package/src/dcutr/utils.ts +0 -33
  279. package/src/fetch/README.md +0 -41
  280. package/src/fetch/constants.ts +0 -3
  281. package/src/fetch/index.ts +0 -315
  282. package/src/fetch/pb/proto.proto +0 -15
  283. package/src/fetch/pb/proto.ts +0 -153
  284. package/src/identify/README.md +0 -13
  285. package/src/identify/consts.ts +0 -12
  286. package/src/identify/identify.ts +0 -561
  287. package/src/identify/index.ts +0 -90
  288. package/src/identify/pb/message.proto +0 -30
  289. package/src/identify/pb/message.ts +0 -126
  290. package/src/insecure/index.ts +0 -138
  291. package/src/insecure/pb/proto.proto +0 -18
  292. package/src/insecure/pb/proto.ts +0 -161
  293. package/src/ping/constants.ts +0 -15
  294. package/src/ping/index.ts +0 -170
  295. package/src/pnet/README.md +0 -68
  296. package/src/pnet/crypto.ts +0 -67
  297. package/src/pnet/errors.ts +0 -5
  298. package/src/pnet/index.ts +0 -126
  299. package/src/pnet/key-generator.ts +0 -23
  300. package/src/upnp-nat/index.ts +0 -212
  301. package/src/utils/peer-job-queue.ts +0 -119
@@ -1,378 +0,0 @@
1
- import { CodeError } from '@libp2p/interface/errors'
2
- import { logger } from '@libp2p/logger'
3
- import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
4
- import delay from 'delay'
5
- import { pbStream } from 'it-protobuf-stream'
6
- import { codes } from '../errors.js'
7
- import { HolePunch } from './pb/message.js'
8
- import { isPublicAndDialable } from './utils.js'
9
- import { multicodec } from './index.js'
10
- import type { DCUtRServiceComponents, DCUtRServiceInit } from './index.js'
11
- import type { Connection, Stream } from '@libp2p/interface/connection'
12
- import type { PeerStore } from '@libp2p/interface/peer-store'
13
- import type { Startable } from '@libp2p/interface/startable'
14
- import type { AddressManager } from '@libp2p/interface-internal/address-manager'
15
- import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
16
- import type { Registrar } from '@libp2p/interface-internal/registrar'
17
- import type { TransportManager } from '@libp2p/interface-internal/src/transport-manager/index.js'
18
-
19
- const log = logger('libp2p:dcutr')
20
-
21
- // https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#rpc-messages
22
- const MAX_DCUTR_MESSAGE_SIZE = 1024 * 4
23
- // ensure the dial has a high priority to jump to the head of the dial queue
24
- const DCUTR_DIAL_PRIORITY = 100
25
-
26
- const defaultValues = {
27
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L27
28
- timeout: 5000,
29
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L28
30
- retries: 3,
31
- maxInboundStreams: 1,
32
- maxOutboundStreams: 1
33
- }
34
-
35
- export class DefaultDCUtRService implements Startable {
36
- private started: boolean
37
- private readonly timeout: number
38
- private readonly retries: number
39
- private readonly maxInboundStreams: number
40
- private readonly maxOutboundStreams: number
41
- private readonly peerStore: PeerStore
42
- private readonly registrar: Registrar
43
- private readonly connectionManager: ConnectionManager
44
- private readonly addressManager: AddressManager
45
- private readonly transportManager: TransportManager
46
- private topologyId?: string
47
-
48
- constructor (components: DCUtRServiceComponents, init: DCUtRServiceInit) {
49
- this.started = false
50
- this.peerStore = components.peerStore
51
- this.registrar = components.registrar
52
- this.addressManager = components.addressManager
53
- this.connectionManager = components.connectionManager
54
- this.transportManager = components.transportManager
55
-
56
- this.timeout = init.timeout ?? defaultValues.timeout
57
- this.retries = init.retries ?? defaultValues.retries
58
- this.maxInboundStreams = init.maxInboundStreams ?? defaultValues.maxInboundStreams
59
- this.maxOutboundStreams = init.maxOutboundStreams ?? defaultValues.maxOutboundStreams
60
- }
61
-
62
- isStarted (): boolean {
63
- return this.started
64
- }
65
-
66
- async start (): Promise<void> {
67
- if (this.started) {
68
- return
69
- }
70
-
71
- // register for notifications of when peers that support DCUtR connect
72
- // nb. requires the identify service to be enabled
73
- this.topologyId = await this.registrar.register(multicodec, {
74
- notifyOnTransient: true,
75
- onConnect: (peerId, connection) => {
76
- if (!connection.transient) {
77
- // the connection is already direct, no upgrade is required
78
- return
79
- }
80
-
81
- // the inbound peer starts the connection upgrade
82
- if (connection.direction !== 'inbound') {
83
- return
84
- }
85
-
86
- this.upgradeInbound(connection)
87
- .catch(err => {
88
- log.error('error during outgoing DCUtR attempt', err)
89
- })
90
- }
91
- })
92
-
93
- await this.registrar.handle(multicodec, (data) => {
94
- void this.handleIncomingUpgrade(data.stream, data.connection).catch(err => {
95
- log.error('error during incoming DCUtR attempt', err)
96
- data.stream.abort(err)
97
- })
98
- }, {
99
- maxInboundStreams: this.maxInboundStreams,
100
- maxOutboundStreams: this.maxOutboundStreams,
101
- runOnTransientConnection: true
102
- })
103
-
104
- this.started = true
105
- }
106
-
107
- async stop (): Promise<void> {
108
- await this.registrar.unhandle(multicodec)
109
-
110
- if (this.topologyId != null) {
111
- this.registrar.unregister(this.topologyId)
112
- }
113
-
114
- this.started = false
115
- }
116
-
117
- /**
118
- * Perform the inbound connection upgrade as B
119
- *
120
- * @see https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol
121
- */
122
- async upgradeInbound (relayedConnection: Connection): Promise<void> {
123
- // Upon observing the new connection, the inbound peer (here B) checks the
124
- // addresses advertised by A via identify.
125
- //
126
- // If that set includes public addresses, then A may be reachable by a direct
127
- // connection, in which case B attempts a unilateral connection upgrade by
128
- // initiating a direct connection to A.
129
- if (await this.attemptUnilateralConnectionUpgrade(relayedConnection)) {
130
- return
131
- }
132
-
133
- let stream: Stream | undefined
134
-
135
- for (let i = 0; i < this.retries; i++) {
136
- const options = {
137
- signal: AbortSignal.timeout(this.timeout)
138
- }
139
-
140
- try {
141
- // 1. B opens a stream to A using the /libp2p/dcutr protocol.
142
- stream = await relayedConnection.newStream([multicodec], {
143
- signal: options.signal,
144
- runOnTransientConnection: true
145
- })
146
-
147
- const pb = pbStream(stream, {
148
- maxDataLength: MAX_DCUTR_MESSAGE_SIZE
149
- }).pb(HolePunch)
150
-
151
- // 2. B sends to A a Connect message containing its observed (and
152
- // possibly predicted) addresses from identify and starts a timer
153
- // to measure RTT of the relay connection.
154
- log('B sending connect to %p', relayedConnection.remotePeer)
155
- const connectTimer = Date.now()
156
- await pb.write({
157
- type: HolePunch.Type.CONNECT,
158
- observedAddresses: this.addressManager.getAddresses().map(ma => ma.bytes)
159
- }, options)
160
-
161
- log('B receiving connect from %p', relayedConnection.remotePeer)
162
- // 4. Upon receiving the Connect, B sends a Sync message
163
- const connect = await pb.read(options)
164
-
165
- if (connect.type !== HolePunch.Type.CONNECT) {
166
- log('A sent wrong message type')
167
- throw new CodeError('DCUtR message type was incorrect', codes.ERR_INVALID_MESSAGE)
168
- }
169
-
170
- const multiaddrs = this.getDialableMultiaddrs(connect.observedAddresses)
171
-
172
- if (multiaddrs.length === 0) {
173
- log('A did not have any dialable multiaddrs')
174
- throw new CodeError('DCUtR connect message had no multiaddrs', codes.ERR_INVALID_MESSAGE)
175
- }
176
-
177
- const rtt = Date.now() - connectTimer
178
-
179
- log('A sending sync, rtt %dms', rtt)
180
- await pb.write({
181
- type: HolePunch.Type.SYNC,
182
- observedAddresses: []
183
- }, options)
184
-
185
- log('A waiting for half RTT')
186
- // ..and starts a timer for half the RTT measured from the time between
187
- // sending the initial Connect and receiving the response
188
- await delay(rtt / 2)
189
-
190
- // TODO: when we have a QUIC transport, the dial step is different - for
191
- // now we only have tcp support
192
- // https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol
193
-
194
- log('B dialing', multiaddrs)
195
- // Upon expiry of the timer, B dials the address to A.
196
- const conn = await this.connectionManager.openConnection(multiaddrs, {
197
- signal: options.signal,
198
- priority: DCUTR_DIAL_PRIORITY
199
- })
200
-
201
- log('DCUtR to %p succeeded to address %a, closing relayed connection', relayedConnection.remotePeer, conn.remoteAddr)
202
- await relayedConnection.close(options)
203
-
204
- break
205
- } catch (err: any) {
206
- log.error('error while attempting DCUtR on attempt %d of %d', i + 1, this.retries, err)
207
- stream?.abort(err)
208
-
209
- if (i === this.retries) {
210
- throw err
211
- }
212
- } finally {
213
- if (stream != null) {
214
- await stream.close(options)
215
- }
216
- }
217
- }
218
- }
219
-
220
- /**
221
- * This is performed when A has dialed B via a relay but A also has a public
222
- * address that B can dial directly
223
- */
224
- async attemptUnilateralConnectionUpgrade (relayedConnection: Connection): Promise<boolean> {
225
- // Upon observing the new connection, the inbound peer (here B) checks the
226
- // addresses advertised by A via identify.
227
- const peerInfo = await this.peerStore.get(relayedConnection.remotePeer)
228
-
229
- // If that set includes public addresses, then A may be reachable by a direct
230
- // connection, in which case B attempts a unilateral connection upgrade by
231
- // initiating a direct connection to A.
232
- const publicAddresses = peerInfo.addresses
233
- .map(address => {
234
- const ma = address.multiaddr
235
-
236
- // ensure all multiaddrs have the peer id
237
- if (ma.getPeerId() == null) {
238
- return ma.encapsulate(`/p2p/${relayedConnection.remotePeer}`)
239
- }
240
-
241
- return ma
242
- })
243
- .filter(ma => {
244
- return isPublicAndDialable(ma, this.transportManager)
245
- })
246
-
247
- if (publicAddresses.length > 0) {
248
- const signal = AbortSignal.timeout(this.timeout)
249
-
250
- try {
251
- log('attempting unilateral connection upgrade to %a', publicAddresses)
252
-
253
- // force-dial the multiaddr(s), otherwise `connectionManager.openConnection`
254
- // will return the existing relayed connection
255
- const connection = await this.connectionManager.openConnection(publicAddresses, {
256
- signal,
257
- force: true
258
- })
259
-
260
- if (connection.transient) {
261
- throw new Error('Could not open a new, non-transient, connection')
262
- }
263
-
264
- log('unilateral connection upgrade to %p succeeded via %a, closing relayed connection', relayedConnection.remotePeer, connection.remoteAddr)
265
-
266
- await relayedConnection.close({
267
- signal
268
- })
269
-
270
- return true
271
- } catch (err) {
272
- log.error('unilateral connection upgrade to %p on addresses %a failed', relayedConnection.remotePeer, publicAddresses, err)
273
- }
274
- } else {
275
- log('peer %p has no public addresses, not attempting unilateral connection upgrade', relayedConnection.remotePeer)
276
- }
277
-
278
- // no public addresses or failed to dial public addresses
279
- return false
280
- }
281
-
282
- /**
283
- * Perform the connection upgrade as A
284
- *
285
- * @see https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol
286
- */
287
- async handleIncomingUpgrade (stream: Stream, relayedConnection: Connection): Promise<void> {
288
- const options = {
289
- signal: AbortSignal.timeout(this.timeout)
290
- }
291
-
292
- try {
293
- const pb = pbStream(stream, {
294
- maxDataLength: MAX_DCUTR_MESSAGE_SIZE
295
- }).pb(HolePunch)
296
-
297
- log('A receiving connect')
298
- // 3. Upon receiving the Connect, A responds back with a Connect message
299
- // containing its observed (and possibly predicted) addresses.
300
- const connect = await pb.read(options)
301
-
302
- if (connect.type !== HolePunch.Type.CONNECT) {
303
- log('B sent wrong message type')
304
- throw new CodeError('DCUtR message type was incorrect', codes.ERR_INVALID_MESSAGE)
305
- }
306
-
307
- if (connect.observedAddresses.length === 0) {
308
- log('B sent no multiaddrs')
309
- throw new CodeError('DCUtR connect message had no multiaddrs', codes.ERR_INVALID_MESSAGE)
310
- }
311
-
312
- const multiaddrs = this.getDialableMultiaddrs(connect.observedAddresses)
313
-
314
- if (multiaddrs.length === 0) {
315
- log('B had no dialable multiaddrs')
316
- throw new CodeError('DCUtR connect message had no dialable multiaddrs', codes.ERR_INVALID_MESSAGE)
317
- }
318
-
319
- log('A sending connect')
320
- await pb.write({
321
- type: HolePunch.Type.CONNECT,
322
- observedAddresses: this.addressManager.getAddresses().map(ma => ma.bytes)
323
- })
324
-
325
- log('A receiving sync')
326
- const sync = await pb.read(options)
327
-
328
- if (sync.type !== HolePunch.Type.SYNC) {
329
- throw new CodeError('DCUtR message type was incorrect', codes.ERR_INVALID_MESSAGE)
330
- }
331
-
332
- // TODO: when we have a QUIC transport, the dial step is different - for
333
- // now we only have tcp support
334
- // https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol
335
-
336
- // Upon receiving the Sync, A immediately dials the address to B
337
- log('A dialing', multiaddrs)
338
- const connection = await this.connectionManager.openConnection(multiaddrs, {
339
- signal: options.signal,
340
- priority: DCUTR_DIAL_PRIORITY,
341
- force: true
342
- })
343
-
344
- log('DCUtR to %p succeeded via %a, closing relayed connection', relayedConnection.remotePeer, connection.remoteAddr)
345
- await relayedConnection.close(options)
346
- } catch (err: any) {
347
- log.error('incoming DCUtR from %p failed', relayedConnection.remotePeer, err)
348
- stream.abort(err)
349
- } finally {
350
- await stream.close(options)
351
- }
352
- }
353
-
354
- /**
355
- * Takes the `addr` and converts it to a Multiaddr if possible
356
- */
357
- getDialableMultiaddrs (addrs: Array<Uint8Array | string | null | undefined>): Multiaddr[] {
358
- const output = []
359
-
360
- for (const addr of addrs) {
361
- if (addr == null || addr.length === 0) {
362
- continue
363
- }
364
-
365
- try {
366
- const ma = multiaddr(addr)
367
-
368
- if (!isPublicAndDialable(ma, this.transportManager)) {
369
- continue
370
- }
371
-
372
- output.push(ma)
373
- } catch {}
374
- }
375
-
376
- return output
377
- }
378
- }
@@ -1,102 +0,0 @@
1
- /**
2
- * @packageDocumentation
3
- *
4
- * Direct Connection Upgrade through Relay (DCUtR) is a protocol that allows two
5
- * nodes to connect to each other who would otherwise be prevented doing so due
6
- * to being behind NATed connections or firewalls.
7
- *
8
- * The protocol involves making a relayed connection between the two peers and
9
- * using the relay to synchronise connection timings so that they dial each other
10
- * at precisely the same moment.
11
- *
12
- * @example
13
- *
14
- * ```ts
15
- * import { createLibp2p } from 'libp2p'
16
- * import { circuitRelayTransport } from 'libp2p/circuit-relay'
17
- * import { tcp } from '@libp2p/tcp'
18
- * import { identifyService } from 'libp2p/identify'
19
- * import { dCUtRService } from 'libp2p/dcutr'
20
- *
21
- * const node = await createLibp2p({
22
- * transports: [
23
- * circuitRelayTransport(),
24
- * tcp()
25
- * ],
26
- * services: {
27
- * identify: identifyService(),
28
- * dcutr: dcutrService()
29
- * }
30
- * })
31
- *
32
- * // QmTarget is a peer that is behind a NAT, supports TCP and has a relay
33
- * // reservation
34
- * await node.dial('/ip4/.../p2p/QmRelay/p2p-circuit/p2p/QmTarget')
35
- *
36
- * // after a while the connection should automatically get upgraded to a
37
- * // direct connection (e.g. non-transient)
38
- * while (true) {
39
- * const connections = node.getConnections()
40
- *
41
- * if (connections.find(conn => conn.transient === false)) {
42
- * console.info('have direct connection')
43
- * break
44
- * } else {
45
- * console.info('have relayed connection')
46
- *
47
- * // wait a few seconds to see if it's succeeded yet
48
- * await new Promise((resolve) => {
49
- * setTimeout(() => resolve(), 5000)
50
- * })
51
- * }
52
- * }
53
- * ```
54
- */
55
-
56
- import { DefaultDCUtRService } from './dcutr.js'
57
- import type { PeerStore } from '@libp2p/interface/peer-store'
58
- import type { AddressManager } from '@libp2p/interface-internal/address-manager'
59
- import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
60
- import type { Registrar } from '@libp2p/interface-internal/registrar'
61
- import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
62
-
63
- export interface DCUtRServiceInit {
64
- /**
65
- * How long we should wait for the connection upgrade to complete (default: 5s)
66
- */
67
- timeout?: number
68
-
69
- /**
70
- * How many times to retry the connection upgrade (default: 3)
71
- */
72
- retries?: number
73
-
74
- /**
75
- * How many simultaneous inbound DCUtR protocol streams to allow on each
76
- * connection (default: 1)
77
- */
78
- maxInboundStreams?: number
79
-
80
- /**
81
- * How many simultaneous outbound DCUtR protocol streams to allow on each
82
- * connection (default: 1)
83
- */
84
- maxOutboundStreams?: number
85
- }
86
-
87
- export interface DCUtRServiceComponents {
88
- peerStore: PeerStore
89
- connectionManager: ConnectionManager
90
- registrar: Registrar
91
- addressManager: AddressManager
92
- transportManager: TransportManager
93
- }
94
-
95
- /**
96
- * The DCUtR protocol
97
- */
98
- export const multicodec = '/libp2p/dcutr'
99
-
100
- export function dcutrService (init: DCUtRServiceInit = {}): (components: DCUtRServiceComponents) => unknown {
101
- return (components) => new DefaultDCUtRService(components, init)
102
- }
@@ -1,12 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message HolePunch {
4
- enum Type {
5
- UNUSED = 0;
6
- CONNECT = 100;
7
- SYNC = 300;
8
- }
9
-
10
- optional Type type = 1;
11
- repeated bytes observed_addresses = 2;
12
- }
@@ -1,96 +0,0 @@
1
- /* eslint-disable import/export */
2
- /* eslint-disable complexity */
3
- /* eslint-disable @typescript-eslint/no-namespace */
4
- /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
- /* eslint-disable @typescript-eslint/no-empty-interface */
6
-
7
- import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime'
8
- import type { Codec } from 'protons-runtime'
9
- import type { Uint8ArrayList } from 'uint8arraylist'
10
-
11
- export interface HolePunch {
12
- type?: HolePunch.Type
13
- observedAddresses: Uint8Array[]
14
- }
15
-
16
- export namespace HolePunch {
17
- export enum Type {
18
- UNUSED = 'UNUSED',
19
- CONNECT = 'CONNECT',
20
- SYNC = 'SYNC'
21
- }
22
-
23
- enum __TypeValues {
24
- UNUSED = 0,
25
- CONNECT = 100,
26
- SYNC = 300
27
- }
28
-
29
- export namespace Type {
30
- export const codec = (): Codec<Type> => {
31
- return enumeration<Type>(__TypeValues)
32
- }
33
- }
34
-
35
- let _codec: Codec<HolePunch>
36
-
37
- export const codec = (): Codec<HolePunch> => {
38
- if (_codec == null) {
39
- _codec = message<HolePunch>((obj, w, opts = {}) => {
40
- if (opts.lengthDelimited !== false) {
41
- w.fork()
42
- }
43
-
44
- if (obj.type != null) {
45
- w.uint32(8)
46
- HolePunch.Type.codec().encode(obj.type, w)
47
- }
48
-
49
- if (obj.observedAddresses != null) {
50
- for (const value of obj.observedAddresses) {
51
- w.uint32(18)
52
- w.bytes(value)
53
- }
54
- }
55
-
56
- if (opts.lengthDelimited !== false) {
57
- w.ldelim()
58
- }
59
- }, (reader, length) => {
60
- const obj: any = {
61
- observedAddresses: []
62
- }
63
-
64
- const end = length == null ? reader.len : reader.pos + length
65
-
66
- while (reader.pos < end) {
67
- const tag = reader.uint32()
68
-
69
- switch (tag >>> 3) {
70
- case 1:
71
- obj.type = HolePunch.Type.codec().decode(reader)
72
- break
73
- case 2:
74
- obj.observedAddresses.push(reader.bytes())
75
- break
76
- default:
77
- reader.skipType(tag & 7)
78
- break
79
- }
80
- }
81
-
82
- return obj
83
- })
84
- }
85
-
86
- return _codec
87
- }
88
-
89
- export const encode = (obj: Partial<HolePunch>): Uint8Array => {
90
- return encodeMessage(obj, HolePunch.codec())
91
- }
92
-
93
- export const decode = (buf: Uint8Array | Uint8ArrayList): HolePunch => {
94
- return decodeMessage(buf, HolePunch.codec())
95
- }
96
- }
@@ -1,33 +0,0 @@
1
- import { type Multiaddr } from '@multiformats/multiaddr'
2
- import { Circuit, IP, DNS } from '@multiformats/multiaddr-matcher'
3
- import isPrivate from 'private-ip'
4
- import type { TransportManager } from '@libp2p/interface-internal/src/transport-manager'
5
-
6
- /**
7
- * Returns true if the passed multiaddr is public, not relayed and we have a
8
- * transport that can dial it
9
- */
10
- export function isPublicAndDialable (ma: Multiaddr, transportManager: TransportManager): boolean {
11
- // ignore circuit relay
12
- if (Circuit.matches(ma)) {
13
- return false
14
- }
15
-
16
- const transport = transportManager.transportForMultiaddr(ma)
17
-
18
- if (transport == null) {
19
- return false
20
- }
21
-
22
- // dns addresses are probably public?
23
- if (DNS.matches(ma)) {
24
- return true
25
- }
26
-
27
- // ensure we have only IPv4/IPv6 addresses
28
- if (!IP.matches(ma)) {
29
- return false
30
- }
31
-
32
- return isPrivate(ma.toOptions().host) === false
33
- }
@@ -1,41 +0,0 @@
1
- libp2p-fetch JavaScript Implementation <!-- omit in toc -->
2
- =====================================
3
-
4
- > Libp2p fetch protocol JavaScript implementation
5
-
6
- ## Table of contents <!-- omit in toc -->
7
-
8
- - [Overview](#overview)
9
- - [Usage](#usage)
10
-
11
- ## Overview
12
-
13
- An implementation of the Fetch protocol as described here: https://github.com/libp2p/specs/tree/master/fetch
14
-
15
- The fetch protocol is a simple protocol for requesting a value corresponding to a key from a peer.
16
-
17
- ## Usage
18
-
19
- ```javascript
20
- import { createLibp2p } from 'libp2p'
21
-
22
- /**
23
- * Given a key (as a string) returns a value (as a Uint8Array), or null if the key isn't found.
24
- * All keys must be prefixed my the same prefix, which will be used to find the appropriate key
25
- * lookup function.
26
- * @param key - a string
27
- * @returns value - a Uint8Array value that corresponds to the given key, or null if the key doesn't
28
- * have a corresponding value.
29
- */
30
- async function my_subsystem_key_lookup(key) {
31
- // app specific callback to lookup key-value pairs.
32
- }
33
-
34
- // Enable this peer to respond to fetch requests for keys that begin with '/my_subsystem_key_prefix/'
35
- const libp2p = createLibp2p()
36
- libp2p.fetchService.registerLookupFunction('/my_subsystem_key_prefix/', my_subsystem_key_lookup)
37
-
38
- const key = '/my_subsystem_key_prefix/{...}'
39
- const peerDst = PeerId.parse('Qmfoo...') // or Multiaddr instance
40
- const value = await libp2p.fetch(peerDst, key)
41
- ```
@@ -1,3 +0,0 @@
1
- // https://github.com/libp2p/specs/tree/master/fetch#wire-protocol
2
- export const PROTOCOL_VERSION = '0.0.1'
3
- export const PROTOCOL_NAME = 'fetch'