libp2p 0.42.2 → 0.43.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.
Files changed (295) hide show
  1. package/README.md +4 -4
  2. package/dist/index.min.js +29 -29
  3. package/dist/src/address-manager/index.d.ts +1 -10
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js +46 -29
  6. package/dist/src/address-manager/index.js.map +1 -1
  7. package/dist/src/autonat/constants.d.ts +4 -0
  8. package/dist/src/autonat/constants.d.ts.map +1 -0
  9. package/dist/src/autonat/constants.js +4 -0
  10. package/dist/src/autonat/constants.js.map +1 -0
  11. package/dist/src/autonat/index.d.ts +64 -0
  12. package/dist/src/autonat/index.d.ts.map +1 -0
  13. package/dist/src/autonat/index.js +417 -0
  14. package/dist/src/autonat/index.js.map +1 -0
  15. package/dist/src/autonat/pb/index.d.ts +57 -0
  16. package/dist/src/autonat/pb/index.d.ts.map +1 -0
  17. package/dist/src/autonat/pb/index.js +250 -0
  18. package/dist/src/autonat/pb/index.js.map +1 -0
  19. package/dist/src/circuit/constants.d.ts +31 -6
  20. package/dist/src/circuit/constants.d.ts.map +1 -1
  21. package/dist/src/circuit/constants.js +37 -7
  22. package/dist/src/circuit/constants.js.map +1 -1
  23. package/dist/src/circuit/index.d.ts +16 -61
  24. package/dist/src/circuit/index.d.ts.map +1 -1
  25. package/dist/src/circuit/index.js +2 -68
  26. package/dist/src/circuit/index.js.map +1 -1
  27. package/dist/src/circuit/pb/index.d.ts +83 -43
  28. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  29. package/dist/src/circuit/pb/index.js +363 -127
  30. package/dist/src/circuit/pb/index.js.map +1 -1
  31. package/dist/src/circuit/server/advert-service.d.ts +43 -0
  32. package/dist/src/circuit/server/advert-service.d.ts.map +1 -0
  33. package/dist/src/circuit/server/advert-service.js +69 -0
  34. package/dist/src/circuit/server/advert-service.js.map +1 -0
  35. package/dist/src/circuit/server/index.d.ts +60 -0
  36. package/dist/src/circuit/server/index.d.ts.map +1 -0
  37. package/dist/src/circuit/server/index.js +287 -0
  38. package/dist/src/circuit/server/index.js.map +1 -0
  39. package/dist/src/circuit/server/reservation-store.d.ts +49 -0
  40. package/dist/src/circuit/server/reservation-store.d.ts.map +1 -0
  41. package/dist/src/circuit/server/reservation-store.js +57 -0
  42. package/dist/src/circuit/server/reservation-store.js.map +1 -0
  43. package/dist/src/circuit/server/reservation-voucher.d.ts +18 -0
  44. package/dist/src/circuit/server/reservation-voucher.d.ts.map +1 -0
  45. package/dist/src/circuit/server/reservation-voucher.js +33 -0
  46. package/dist/src/circuit/server/reservation-voucher.js.map +1 -0
  47. package/dist/src/circuit/transport/discovery.d.ts +45 -0
  48. package/dist/src/circuit/transport/discovery.d.ts.map +1 -0
  49. package/dist/src/circuit/transport/discovery.js +82 -0
  50. package/dist/src/circuit/transport/discovery.js.map +1 -0
  51. package/dist/src/circuit/transport/index.d.ts +33 -0
  52. package/dist/src/circuit/transport/index.d.ts.map +1 -0
  53. package/dist/src/circuit/transport/index.js +225 -0
  54. package/dist/src/circuit/transport/index.js.map +1 -0
  55. package/dist/src/circuit/transport/listener.d.ts +9 -0
  56. package/dist/src/circuit/transport/listener.d.ts.map +1 -0
  57. package/dist/src/circuit/transport/listener.js +76 -0
  58. package/dist/src/circuit/transport/listener.js.map +1 -0
  59. package/dist/src/circuit/transport/reservation-store.d.ts +60 -0
  60. package/dist/src/circuit/transport/reservation-store.d.ts.map +1 -0
  61. package/dist/src/circuit/transport/reservation-store.js +165 -0
  62. package/dist/src/circuit/transport/reservation-store.js.map +1 -0
  63. package/dist/src/circuit/utils.d.ts +7 -0
  64. package/dist/src/circuit/utils.d.ts.map +1 -1
  65. package/dist/src/circuit/utils.js +87 -0
  66. package/dist/src/circuit/utils.js.map +1 -1
  67. package/dist/src/components.d.ts +2 -1
  68. package/dist/src/components.d.ts.map +1 -1
  69. package/dist/src/components.js +13 -13
  70. package/dist/src/components.js.map +1 -1
  71. package/dist/src/config.d.ts.map +1 -1
  72. package/dist/src/config.js +21 -25
  73. package/dist/src/config.js.map +1 -1
  74. package/dist/src/connection/index.d.ts +2 -2
  75. package/dist/src/connection/index.d.ts.map +1 -1
  76. package/dist/src/connection/index.js +4 -4
  77. package/dist/src/connection/index.js.map +1 -1
  78. package/dist/src/connection-manager/auto-dialler.d.ts +0 -4
  79. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  80. package/dist/src/connection-manager/auto-dialler.js +18 -18
  81. package/dist/src/connection-manager/auto-dialler.js.map +1 -1
  82. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  83. package/dist/src/connection-manager/dialer/dial-request.js +13 -6
  84. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
  85. package/dist/src/connection-manager/dialer/index.d.ts +10 -1
  86. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  87. package/dist/src/connection-manager/dialer/index.js +24 -9
  88. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  89. package/dist/src/connection-manager/index.d.ts +1 -4
  90. package/dist/src/connection-manager/index.d.ts.map +1 -1
  91. package/dist/src/connection-manager/index.js +8 -5
  92. package/dist/src/connection-manager/index.js.map +1 -1
  93. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  94. package/dist/src/connection-manager/latency-monitor.js +1 -1
  95. package/dist/src/connection-manager/latency-monitor.js.map +1 -1
  96. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  97. package/dist/src/content-routing/index.d.ts +6 -4
  98. package/dist/src/content-routing/index.d.ts.map +1 -1
  99. package/dist/src/content-routing/index.js +9 -9
  100. package/dist/src/content-routing/index.js.map +1 -1
  101. package/dist/src/content-routing/utils.d.ts +3 -3
  102. package/dist/src/content-routing/utils.d.ts.map +1 -1
  103. package/dist/src/content-routing/utils.js +2 -2
  104. package/dist/src/content-routing/utils.js.map +1 -1
  105. package/dist/src/dht/dht-content-routing.d.ts +2 -1
  106. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  107. package/dist/src/dht/dht-content-routing.js +2 -2
  108. package/dist/src/dht/dht-content-routing.js.map +1 -1
  109. package/dist/src/dht/dht-peer-routing.d.ts +3 -2
  110. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  111. package/dist/src/dht/dht-peer-routing.js +2 -2
  112. package/dist/src/dht/dht-peer-routing.js.map +1 -1
  113. package/dist/src/dht/dummy-dht.d.ts +1 -1
  114. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  115. package/dist/src/dht/dummy-dht.js +12 -12
  116. package/dist/src/dht/dummy-dht.js.map +1 -1
  117. package/dist/src/errors.d.ts +2 -1
  118. package/dist/src/errors.d.ts.map +1 -1
  119. package/dist/src/errors.js +1 -0
  120. package/dist/src/errors.js.map +1 -1
  121. package/dist/src/fetch/index.d.ts.map +1 -1
  122. package/dist/src/fetch/index.js +6 -6
  123. package/dist/src/fetch/index.js.map +1 -1
  124. package/dist/src/fetch/pb/proto.d.ts +3 -3
  125. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  126. package/dist/src/fetch/pb/proto.js +4 -3
  127. package/dist/src/fetch/pb/proto.js.map +1 -1
  128. package/dist/src/get-peer.js +2 -2
  129. package/dist/src/get-peer.js.map +1 -1
  130. package/dist/src/identify/index.d.ts +3 -1
  131. package/dist/src/identify/index.d.ts.map +1 -1
  132. package/dist/src/identify/index.js +19 -14
  133. package/dist/src/identify/index.js.map +1 -1
  134. package/dist/src/identify/pb/message.d.ts +2 -2
  135. package/dist/src/identify/pb/message.d.ts.map +1 -1
  136. package/dist/src/identify/pb/message.js +1 -0
  137. package/dist/src/identify/pb/message.js.map +1 -1
  138. package/dist/src/index.d.ts +14 -8
  139. package/dist/src/index.d.ts.map +1 -1
  140. package/dist/src/index.js.map +1 -1
  141. package/dist/src/insecure/index.d.ts.map +1 -1
  142. package/dist/src/insecure/index.js.map +1 -1
  143. package/dist/src/insecure/pb/proto.d.ts +3 -3
  144. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  145. package/dist/src/insecure/pb/proto.js +4 -5
  146. package/dist/src/insecure/pb/proto.js.map +1 -1
  147. package/dist/src/libp2p.d.ts +7 -3
  148. package/dist/src/libp2p.d.ts.map +1 -1
  149. package/dist/src/libp2p.js +35 -35
  150. package/dist/src/libp2p.js.map +1 -1
  151. package/dist/src/nat-manager.d.ts.map +1 -1
  152. package/dist/src/nat-manager.js +5 -3
  153. package/dist/src/nat-manager.js.map +1 -1
  154. package/dist/src/peer-record-updater.d.ts.map +1 -1
  155. package/dist/src/peer-routing.d.ts.map +1 -1
  156. package/dist/src/peer-routing.js +5 -5
  157. package/dist/src/peer-routing.js.map +1 -1
  158. package/dist/src/ping/index.d.ts.map +1 -1
  159. package/dist/src/ping/index.js +2 -2
  160. package/dist/src/ping/index.js.map +1 -1
  161. package/dist/src/pnet/crypto.d.ts +1 -1
  162. package/dist/src/pnet/crypto.d.ts.map +1 -1
  163. package/dist/src/pnet/crypto.js +1 -1
  164. package/dist/src/pnet/crypto.js.map +1 -1
  165. package/dist/src/pnet/index.js +3 -3
  166. package/dist/src/pnet/index.js.map +1 -1
  167. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  168. package/dist/src/pubsub/dummy-pubsub.js +9 -9
  169. package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
  170. package/dist/src/registrar.d.ts.map +1 -1
  171. package/dist/src/registrar.js +4 -4
  172. package/dist/src/registrar.js.map +1 -1
  173. package/dist/src/transport-manager.d.ts.map +1 -1
  174. package/dist/src/transport-manager.js +8 -9
  175. package/dist/src/transport-manager.js.map +1 -1
  176. package/dist/src/upgrader.d.ts +2 -1
  177. package/dist/src/upgrader.d.ts.map +1 -1
  178. package/dist/src/upgrader.js +38 -33
  179. package/dist/src/upgrader.js.map +1 -1
  180. package/dist/typedoc-urls.json +19 -6
  181. package/package.json +36 -33
  182. package/src/address-manager/index.ts +62 -35
  183. package/src/autonat/constants.ts +4 -0
  184. package/src/autonat/index.ts +567 -0
  185. package/src/autonat/pb/index.proto +35 -0
  186. package/src/autonat/pb/index.ts +320 -0
  187. package/src/circuit/constants.ts +48 -7
  188. package/src/circuit/index.ts +17 -129
  189. package/src/circuit/pb/index.proto +57 -32
  190. package/src/circuit/pb/index.ts +448 -138
  191. package/src/circuit/server/advert-service.ts +108 -0
  192. package/src/circuit/server/index.ts +421 -0
  193. package/src/circuit/server/reservation-store.ts +111 -0
  194. package/src/circuit/server/reservation-voucher.ts +51 -0
  195. package/src/circuit/transport/discovery.ts +127 -0
  196. package/src/circuit/transport/index.ts +325 -0
  197. package/src/circuit/transport/listener.ts +99 -0
  198. package/src/circuit/transport/reservation-store.ts +249 -0
  199. package/src/circuit/utils.ts +108 -0
  200. package/src/components.ts +22 -21
  201. package/src/config.ts +21 -25
  202. package/src/connection/index.ts +10 -10
  203. package/src/connection-manager/auto-dialler.ts +36 -40
  204. package/src/connection-manager/dialer/dial-request.ts +13 -6
  205. package/src/connection-manager/dialer/index.ts +34 -15
  206. package/src/connection-manager/index.ts +22 -23
  207. package/src/connection-manager/latency-monitor.ts +9 -9
  208. package/src/connection-manager/visibility-change-emitter.ts +4 -4
  209. package/src/content-routing/index.ts +18 -16
  210. package/src/content-routing/utils.ts +5 -5
  211. package/src/dht/dht-content-routing.ts +5 -4
  212. package/src/dht/dht-peer-routing.ts +5 -4
  213. package/src/dht/dummy-dht.ts +13 -13
  214. package/src/errors.ts +2 -1
  215. package/src/fetch/index.ts +13 -13
  216. package/src/fetch/pb/proto.ts +8 -7
  217. package/src/get-peer.ts +3 -3
  218. package/src/identify/index.ts +29 -24
  219. package/src/identify/pb/message.ts +3 -2
  220. package/src/index.ts +15 -8
  221. package/src/insecure/index.ts +2 -1
  222. package/src/insecure/pb/proto.ts +8 -9
  223. package/src/libp2p.ts +52 -46
  224. package/src/nat-manager.ts +11 -9
  225. package/src/peer-record-updater.ts +4 -4
  226. package/src/peer-routing.ts +9 -9
  227. package/src/ping/index.ts +6 -6
  228. package/src/pnet/crypto.ts +3 -3
  229. package/src/pnet/index.ts +3 -3
  230. package/src/pnet/key-generator.ts +1 -1
  231. package/src/pubsub/dummy-pubsub.ts +9 -9
  232. package/src/registrar.ts +12 -12
  233. package/src/transport-manager.ts +17 -18
  234. package/src/upgrader.ts +43 -42
  235. package/src/version.ts +1 -1
  236. package/dist/src/circuit/auto-relay.d.ts +0 -46
  237. package/dist/src/circuit/auto-relay.d.ts.map +0 -1
  238. package/dist/src/circuit/auto-relay.js +0 -220
  239. package/dist/src/circuit/auto-relay.js.map +0 -1
  240. package/dist/src/circuit/circuit/hop.d.ts +0 -42
  241. package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
  242. package/dist/src/circuit/circuit/hop.js +0 -142
  243. package/dist/src/circuit/circuit/hop.js.map +0 -1
  244. package/dist/src/circuit/circuit/stop.d.ts +0 -24
  245. package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
  246. package/dist/src/circuit/circuit/stop.js +0 -51
  247. package/dist/src/circuit/circuit/stop.js.map +0 -1
  248. package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
  249. package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
  250. package/dist/src/circuit/circuit/stream-handler.js +0 -59
  251. package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
  252. package/dist/src/circuit/circuit/utils.d.ts +0 -7
  253. package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
  254. package/dist/src/circuit/circuit/utils.js +0 -43
  255. package/dist/src/circuit/circuit/utils.js.map +0 -1
  256. package/dist/src/circuit/listener.d.ts +0 -9
  257. package/dist/src/circuit/listener.d.ts.map +0 -1
  258. package/dist/src/circuit/listener.js +0 -62
  259. package/dist/src/circuit/listener.js.map +0 -1
  260. package/dist/src/circuit/multicodec.d.ts +0 -2
  261. package/dist/src/circuit/multicodec.d.ts.map +0 -1
  262. package/dist/src/circuit/multicodec.js +0 -2
  263. package/dist/src/circuit/multicodec.js.map +0 -1
  264. package/dist/src/circuit/transport.d.ts +0 -51
  265. package/dist/src/circuit/transport.d.ts.map +0 -1
  266. package/dist/src/circuit/transport.js +0 -223
  267. package/dist/src/circuit/transport.js.map +0 -1
  268. package/dist/src/keychain/cms.d.ts +0 -33
  269. package/dist/src/keychain/cms.d.ts.map +0 -1
  270. package/dist/src/keychain/cms.js +0 -129
  271. package/dist/src/keychain/cms.js.map +0 -1
  272. package/dist/src/keychain/index.d.ts +0 -151
  273. package/dist/src/keychain/index.d.ts.map +0 -1
  274. package/dist/src/keychain/index.js +0 -519
  275. package/dist/src/keychain/index.js.map +0 -1
  276. package/dist/src/keychain/util.d.ts +0 -21
  277. package/dist/src/keychain/util.d.ts.map +0 -1
  278. package/dist/src/keychain/util.js +0 -79
  279. package/dist/src/keychain/util.js.map +0 -1
  280. package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
  281. package/src/circuit/README.md +0 -111
  282. package/src/circuit/auto-relay.ts +0 -292
  283. package/src/circuit/circuit/hop.ts +0 -220
  284. package/src/circuit/circuit/stop.ts +0 -83
  285. package/src/circuit/circuit/stream-handler.ts +0 -87
  286. package/src/circuit/circuit/utils.ts +0 -44
  287. package/src/circuit/listener.ts +0 -83
  288. package/src/circuit/multicodec.ts +0 -2
  289. package/src/circuit/transport.ts +0 -278
  290. package/src/keychain/README.md +0 -55
  291. package/src/keychain/cms.ts +0 -150
  292. package/src/keychain/doc/private-key.png +0 -0
  293. package/src/keychain/doc/private-key.xml +0 -1
  294. package/src/keychain/index.ts +0 -629
  295. package/src/keychain/util.ts +0 -82
@@ -1,128 +0,0 @@
1
- EDIT: This document is outdated and here only for historical purposes
2
-
3
- NOTE: This document is structured in an `if-then/else[if]-then` manner, each line is a precondition for following lines with a higher number of indentation
4
-
5
- Example:
6
-
7
- - if there are apples
8
- - eat them
9
- - if not, check for pears
10
- - then eat them
11
- - if not, check for cherries
12
- - then eat them
13
-
14
- Or,
15
-
16
- - if there are apples
17
- - eat them
18
- - if not
19
- - check for pears
20
- - then eat them
21
- - if not
22
- - check for cherries
23
- - then eat them
24
-
25
- In order to minimize nesting, the first example is preferred
26
-
27
- # Relay flow
28
-
29
- ## Relay transport (dialer/listener)
30
-
31
- - ### Dial over a relay
32
- - See if there is a relay that's already connected to the destination peer, if not
33
- - Ask all the peer's known relays to dial the destination peer until an active relay (one that can dial on behalf of other peers), or a relay that may have recently acquired a connection to the destination peer is successful.
34
- - If successful
35
- - Write the `/ipfs/relay/circuit/1.0.0` header to the relay, followed by the destination address
36
- - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmDest`.
37
- - If no relays could connect, fail the same way a regular transport would
38
- - Once the connection has been established, the swarm should treat it as a regular connection,
39
- - i.e. muxing, encrypt, etc should all be performed on the relayed connection
40
-
41
- - ### Listen for relayed connections
42
- - Peer mounts the `/ipfs/relay/circuit/1.0.0` proto and listens for relayed connections
43
- - A connection arrives
44
- - read the address of the source peer from the incoming connection stream
45
- - if valid, create a PeerInfo object for that peer and add the incoming address to its multiaddresses list
46
- - pass the connection to `protocolMuxer(swarm.protocols, conn)` to have it go through the regular muxing/encryption flow
47
-
48
- - ### Relay discovery and static relay addresses in swarm config
49
-
50
- - #### Relay address in swarm config
51
- - A peer has relay addresses in its swarm config section
52
- - On node startup, connect to the relays in swarm config
53
- - if successful add address to swarms PeerInfo's multiaddresses
54
- - `identify` should take care of announcing that the peer is reachable over the listed relays
55
-
56
- - #### Passive relay discovery
57
- - A peer that can dial over `/ipfs/relay/circuit/1.0.0` listens for the `peer-mux-established` swarm event, every time a new muxed connection arrives, it checks if the incoming peer is a relay. (How would this work? Some way of discovering if its a relay is required.)
58
- - *Useful in cases when the peer/node doesn't know of any relays on startup and also, to learn of as many additional relays in the network as possible*
59
- - *Useful during startup, when connecting to bootstrap nodes. It allows us to implicitly learn if its a relay without having to explicitly add `/p2p-circuit` addresses to the bootstrap list*
60
- - *Also useful if the relay communicates its capabilities upon connecting to it, as to avoid additional unnecessary requests/queries. I.e. if it supports weather its able to forward connections and weather it supports the `ls` or other commands.*
61
- - *Should it be possible to disable passive relay discovery?*
62
- - This could be useful when the peer wants to be reachable **only** over the listed relays
63
- - If the incoming peer is a relay, send an `ls` and record its peers
64
-
65
- ## Relay Nodes
66
-
67
- - ### Passive relay node
68
- - *A passive relay does not explicitly dial into any requested peer, only those that it's swarm already has connections to.*
69
- - When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
70
- - check its swarm's peerbook(?) see if its a known peer, if it is
71
- - use the swarms existing connection and
72
- - send the multistream header and the source peer address to the dest peer
73
- - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
74
- - circuit the source and dest connections
75
- - if couldn't dial, or the connection/stream to the dest peer closed prematurelly
76
- - close the src stream
77
-
78
-
79
- - ### Active relay node
80
- - *An active relay node can dial other peers even if its swarm doesnt know about those peers*
81
- - When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
82
- - use the swarm to dial to the dest node
83
- - send the multistream header and the source peer address to the dest peer
84
- - e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
85
- - circuit the source and dest connections
86
- - if couldn't dial, or the connection/stream to the dest peer closed prematurely
87
- - close the src stream
88
-
89
- - ### `ls` command
90
- - *A relay node can allow the peers known to it's swarm to be listed*
91
- - *this should be possible to enable/disable from the config*
92
- - when a relay gets the `ls` request
93
- - if enabled, get its swarm's peerbook's known peers and return their ids and multiaddrs
94
- - e.g `[{id: /ipfs/QmPeerId, addrs: ['ma1', 'ma2', 'ma3']}, ...]`
95
- - if disabled, respond with `na`
96
-
97
-
98
- ## Relay Implementation notes
99
-
100
- - ### Relay transport
101
- - Currently I've implemented the dialer and listener parts of the relay as a transport, meaning that it *tries* to implement the `interface-transport` interface as closely as possible. This seems to work pretty well and it's makes the dialer/listener parts really easy to plug in into the swarm. I think this is the cleanest solution.
102
-
103
- - ### `circuit-relay`
104
- - This is implemented as a separate piece (not a transport), and it can be enabled/disabled with a config. The transport listener however, will do the initial parsing of the incoming header and figure out weather it's a connection that's needs to be handled by the circuit-relay, or its a connection that is being relayed from a circuit-relay.
105
-
106
- ## Relay swarm integration
107
-
108
- - The relay transport is mounted explicitly by calling the `swarm.connection.relay(config.relay)` from libp2p
109
- - Swarm will register the dialer and listener using the swarm `transport.add` and `transport.listen` methods
110
-
111
- - ### Listener
112
- - the listener registers itself as a multistream handler on the `/ipfs/relay/circuit/1.0.0` proto
113
- - if `circuit-relay` is enabled, the listener will delegate connections to it if appropriate
114
- - when the listener receives a connection, it will read the multiaddr and determine if its a connection that needs to be relayed, or its a connection that is being relayed
115
-
116
- - ### Dialer
117
- - When the swarm attempts to dial to a peer, it will filter the protocols that the peer can be reached on
118
- - *The relay will be used in two cases*
119
- - If the peer has an explicit relay address that it can be reached on
120
- - no other transport is available
121
- - The relay will attempt to dial the peer over that relay
122
- - If no explicit relay address is provided
123
- - no other transport is available
124
- - A generic circuit address will be added to the peers multiaddr list
125
- - i.e. `/p2p-circuit/ipfs/QmDest`
126
- - If another transport is available, then use that instead of the relay
127
-
128
-
@@ -1,111 +0,0 @@
1
- # js-libp2p-circuit
2
-
3
- > Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/connection) interface for dial/listen.
4
-
5
- **Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-circuit.
6
-
7
- `libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node. You can read more about this in its [spec](https://github.com/libp2p/specs/tree/master/relay).
8
-
9
- ## Table of Contents
10
-
11
- - [js-libp2p-circuit](#js-libp2p-circuit)
12
- - [Table of Contents](#table-of-contents)
13
- - [Why?](#why)
14
- - [libp2p-circuit and IPFS](#libp2p-circuit-and-ipfs)
15
- - [Usage](#usage)
16
- - [API](#api)
17
- - [Implementation rational](#implementation-rational)
18
-
19
- ### Why?
20
-
21
- `circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
22
-
23
- The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
24
- - routing traffic between private nets and circumventing NAT layers
25
- - route mangling for better privacy (matreshka/shallot dialing).
26
-
27
- It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes.
28
-
29
- ### libp2p-circuit and IPFS
30
-
31
- Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
32
-
33
- ## Usage
34
-
35
- Libp2p circuit configuration can be seen at [Setup with Relay](../../doc/CONFIGURATION.md#setup-with-relay).
36
-
37
- Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows:
38
-
39
- ```js
40
- import { multiaddr } from '@multiformats/multiaddr'
41
- import Libp2p from 'libp2p'
42
- import { tcp } from '@libp2p/tcp'
43
- import { mplex } from '@libp2p/mplex'
44
- import { noise } from '@chainsafe/libp2p-noise'
45
-
46
- const relayAddr = ...
47
-
48
- const node = await createLibp2p({
49
- addresses: {
50
- listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
51
- },
52
- transports: [
53
- tcp()
54
- ],
55
- streamMuxers: [
56
- mplex()
57
- ],
58
- connectionEncryption: [
59
- noise()
60
- ]
61
- },
62
- config: {
63
- relay: { // Circuit Relay options (this config is part of libp2p core configurations)
64
- enabled: true // Allows you to dial and accept relayed connections. Does not make you a relay.
65
- }
66
- }
67
- })
68
- ```
69
-
70
- ## API
71
-
72
- [![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
73
-
74
- `libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e:
75
-
76
- `/p2p-circuit/ip4/127.0.0.1/tcp/4001/p2p/QmHash`
77
-
78
- Both for dialing and listening.
79
-
80
- ### Implementation rational
81
-
82
- This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening.
83
-
84
- ```
85
- libp2p libp2p-circuit (transport)
86
- +-------------------------------------------------+ +--------------------------+
87
- | +---------------------------------+ | | |
88
- | | | | | +------------------+ |
89
- | | | | circuit-relay listens for the HOP | | | |
90
- | | libp2p <------------------------------------------------| circuit-relay | |
91
- | | | | message to handle incomming relay | | | |
92
- | | | | requests from other nodes | +------------------+ |
93
- | +---------------------------------+ | | |
94
- | ^ ^ ^ ^ ^ ^ | | +------------------+ |
95
- | | | | | | | | | | +-------------+ | |
96
- | | | | | | | | dialer uses libp2p to dial | | | | | |
97
- | | | | +----------------------------------------------------------------------> dialer | | |
98
- | | | transports | | to a circuit-relay node using the | | | | | |
99
- | | | | | | | HOP message | | +-------------+ | |
100
- | | | | | | | | | | |
101
- | v v | v v | | | | |
102
- |+------------------|----------------------------+| | | +-------------+ | |
103
- || | | | | || | | | | | |
104
- ||libp2p-tcp |libp2p-ws | .... |libp2p-circuit || listener handles STOP messages from| | | listener | | |
105
- || | +--------------------------------------------------------------------------> | | |
106
- || | | |plugs in just || circuit-relay nodes | | +-------------+ | |
107
- || | | |as any other || | | | |
108
- || | | |transport || | +------------------+ |
109
- |+-----------------------------------------------+| | |
110
- +-------------------------------------------------+ +--------------------------+
111
- ```
@@ -1,292 +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 { RELAY_CODEC } from './multicodec.js'
5
- import { canHop } from './circuit/hop.js'
6
- import { namespaceToCid } from './utils.js'
7
- import {
8
- CIRCUIT_PROTO_CODE,
9
- HOP_METADATA_KEY,
10
- HOP_METADATA_VALUE,
11
- RELAY_RENDEZVOUS_NS
12
- } from './constants.js'
13
- import type { PeerId } from '@libp2p/interface-peer-id'
14
- import type { AddressSorter, PeerProtocolsChangeData } from '@libp2p/interface-peer-store'
15
- import type { Connection } from '@libp2p/interface-connection'
16
- import sort from 'it-sort'
17
- import all from 'it-all'
18
- import { pipe } from 'it-pipe'
19
- import { publicAddressesFirst } from '@libp2p/utils/address-sort'
20
- import type { RelayComponents } from './index.js'
21
-
22
- const log = logger('libp2p:auto-relay')
23
-
24
- const noop = () => {}
25
-
26
- export interface AutoRelayInit {
27
- addressSorter?: AddressSorter
28
- maxListeners?: number
29
- onError?: (error: Error, msg?: string) => void
30
- }
31
-
32
- export class AutoRelay {
33
- private readonly components: RelayComponents
34
- private readonly addressSorter: AddressSorter
35
- private readonly maxListeners: number
36
- private readonly listenRelays: Set<string>
37
- private readonly onError: (error: Error, msg?: string) => void
38
-
39
- constructor (components: RelayComponents, init: AutoRelayInit) {
40
- this.components = components
41
- this.addressSorter = init.addressSorter ?? publicAddressesFirst
42
- this.maxListeners = init.maxListeners ?? 1
43
- this.listenRelays = new Set()
44
- this.onError = init.onError ?? noop
45
-
46
- this._onProtocolChange = this._onProtocolChange.bind(this)
47
- this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
48
-
49
- this.components.peerStore.addEventListener('change:protocols', (evt) => {
50
- void this._onProtocolChange(evt).catch(err => {
51
- log.error(err)
52
- })
53
- })
54
- this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected)
55
- }
56
-
57
- /**
58
- * Check if a peer supports the relay protocol.
59
- * If the protocol is not supported, check if it was supported before and remove it as a listen relay.
60
- * If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
61
- * inside the threshold.
62
- */
63
- async _onProtocolChange (evt: CustomEvent<PeerProtocolsChangeData>) {
64
- const {
65
- peerId,
66
- protocols
67
- } = evt.detail
68
- const id = peerId.toString()
69
-
70
- // Check if it has the protocol
71
- const hasProtocol = protocols.find(protocol => protocol === RELAY_CODEC)
72
-
73
- // If no protocol, check if we were keeping the peer before as a listenRelay
74
- if (hasProtocol == null) {
75
- if (this.listenRelays.has(id)) {
76
- await this._removeListenRelay(id)
77
- }
78
-
79
- return
80
- }
81
-
82
- if (this.listenRelays.has(id)) {
83
- return
84
- }
85
-
86
- // If protocol, check if can hop, store info in the metadataBook and listen on it
87
- try {
88
- const connections = this.components.connectionManager.getConnections(peerId)
89
-
90
- if (connections.length === 0) {
91
- return
92
- }
93
-
94
- const connection = connections[0]
95
-
96
- // Do not hop on a relayed connection
97
- if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
98
- log(`relayed connection to ${id} will not be used to hop on`)
99
- return
100
- }
101
-
102
- const supportsHop = await canHop({ connection })
103
-
104
- if (supportsHop) {
105
- await this.components.peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
106
- await this._addListenRelay(connection, id)
107
- }
108
- } catch (err: any) {
109
- this.onError(err)
110
- }
111
- }
112
-
113
- /**
114
- * Peer disconnects
115
- */
116
- _onPeerDisconnected (evt: CustomEvent<Connection>) {
117
- const connection = evt.detail
118
- const peerId = connection.remotePeer
119
- const id = peerId.toString()
120
-
121
- // Not listening on this relay
122
- if (!this.listenRelays.has(id)) {
123
- return
124
- }
125
-
126
- this._removeListenRelay(id).catch(err => {
127
- log.error(err)
128
- })
129
- }
130
-
131
- /**
132
- * Attempt to listen on the given relay connection
133
- */
134
- async _addListenRelay (connection: Connection, id: string): Promise<void> {
135
- try {
136
- // Check if already listening on enough relays
137
- if (this.listenRelays.size >= this.maxListeners) {
138
- return
139
- }
140
-
141
- // Get peer known addresses and sort them with public addresses first
142
- const remoteAddrs = await pipe(
143
- await this.components.peerStore.addressBook.get(connection.remotePeer),
144
- (source) => sort(source, this.addressSorter),
145
- async (source) => await all(source)
146
- )
147
-
148
- // Attempt to listen on relay
149
- const result = await Promise.all(
150
- remoteAddrs.map(async addr => {
151
- try {
152
- let multiaddr = addr.multiaddr
153
-
154
- if (multiaddr.getPeerId() == null) {
155
- multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`)
156
- }
157
-
158
- multiaddr = multiaddr.encapsulate('/p2p-circuit')
159
-
160
- // Announce multiaddrs will update on listen success by TransportManager event being triggered
161
- await this.components.transportManager.listen([multiaddr])
162
- return true
163
- } catch (err: any) {
164
- log.error('error listening on circuit address', err)
165
- this.onError(err)
166
- }
167
-
168
- return false
169
- })
170
- )
171
-
172
- if (result.includes(true)) {
173
- this.listenRelays.add(id)
174
- }
175
- } catch (err: any) {
176
- this.onError(err)
177
- this.listenRelays.delete(id)
178
- }
179
- }
180
-
181
- /**
182
- * Remove listen relay
183
- */
184
- async _removeListenRelay (id: string) {
185
- if (this.listenRelays.delete(id)) {
186
- // TODO: this should be responsibility of the connMgr
187
- await this._listenOnAvailableHopRelays([id])
188
- }
189
- }
190
-
191
- /**
192
- * Try to listen on available hop relay connections.
193
- * The following order will happen while we do not have enough relays.
194
- * 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
195
- * 2. Dial and try to listen on the peers we know that support hop but are not connected.
196
- * 3. Search the network.
197
- */
198
- async _listenOnAvailableHopRelays (peersToIgnore: string[] = []) {
199
- // TODO: The peer redial issue on disconnect should be handled by connection gating
200
- // Check if already listening on enough relays
201
- if (this.listenRelays.size >= this.maxListeners) {
202
- return
203
- }
204
-
205
- const knownHopsToDial = []
206
- const peers = await this.components.peerStore.all()
207
-
208
- // Check if we have known hop peers to use and attempt to listen on the already connected
209
- for (const { id, metadata } of peers) {
210
- const idStr = id.toString()
211
-
212
- // Continue to next if listening on this or peer to ignore
213
- if (this.listenRelays.has(idStr)) {
214
- continue
215
- }
216
-
217
- if (peersToIgnore.includes(idStr)) {
218
- continue
219
- }
220
-
221
- const supportsHop = metadata.get(HOP_METADATA_KEY)
222
-
223
- // Continue to next if it does not support Hop
224
- if ((supportsHop == null) || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
225
- continue
226
- }
227
-
228
- const connections = this.components.connectionManager.getConnections(id)
229
-
230
- // If not connected, store for possible later use.
231
- if (connections.length === 0) {
232
- knownHopsToDial.push(id)
233
- continue
234
- }
235
-
236
- await this._addListenRelay(connections[0], idStr)
237
-
238
- // Check if already listening on enough relays
239
- if (this.listenRelays.size >= this.maxListeners) {
240
- return
241
- }
242
- }
243
-
244
- // Try to listen on known peers that are not connected
245
- for (const peerId of knownHopsToDial) {
246
- await this._tryToListenOnRelay(peerId)
247
-
248
- // Check if already listening on enough relays
249
- if (this.listenRelays.size >= this.maxListeners) {
250
- return
251
- }
252
- }
253
-
254
- // Try to find relays to hop on the network
255
- try {
256
- const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
257
- for await (const provider of this.components.contentRouting.findProviders(cid)) {
258
- if (provider.multiaddrs.length === 0) {
259
- continue
260
- }
261
-
262
- const peerId = provider.id
263
-
264
- if (peerId.equals(this.components.peerId)) {
265
- // Skip the provider if it's us as dialing will fail
266
- continue
267
- }
268
-
269
- await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs)
270
-
271
- await this._tryToListenOnRelay(peerId)
272
-
273
- // Check if already listening on enough relays
274
- if (this.listenRelays.size >= this.maxListeners) {
275
- return
276
- }
277
- }
278
- } catch (err: any) {
279
- this.onError(err)
280
- }
281
- }
282
-
283
- async _tryToListenOnRelay (peerId: PeerId) {
284
- try {
285
- const connection = await this.components.connectionManager.openConnection(peerId)
286
- await this._addListenRelay(connection, peerId.toString())
287
- } catch (err: any) {
288
- log.error('Could not use %p as relay', peerId, err)
289
- this.onError(err, `could not connect and listen on known hop relay ${peerId.toString()}`)
290
- }
291
- }
292
- }