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,220 +0,0 @@
1
- import { logger } from '@libp2p/logger'
2
- import errCode from 'err-code'
3
- import { validateAddrs } from './utils.js'
4
- import { StreamHandler } from './stream-handler.js'
5
- import { CircuitRelay as CircuitPB } from '../pb/index.js'
6
- import { pipe } from 'it-pipe'
7
- import { codes as Errors } from '../../errors.js'
8
- import { stop } from './stop.js'
9
- import { RELAY_CODEC } from '../multicodec.js'
10
- import type { Connection } from '@libp2p/interface-connection'
11
- import { peerIdFromBytes } from '@libp2p/peer-id'
12
- import type { Duplex } from 'it-stream-types'
13
- import type { Circuit } from '../transport.js'
14
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
15
- import type { AbortOptions } from '@libp2p/interfaces'
16
- import type { Uint8ArrayList } from 'uint8arraylist'
17
-
18
- const log = logger('libp2p:circuit:hop')
19
-
20
- export interface HopRequest {
21
- connection: Connection
22
- request: CircuitPB
23
- streamHandler: StreamHandler
24
- circuit: Circuit
25
- connectionManager: ConnectionManager
26
- }
27
-
28
- export async function handleHop (hopRequest: HopRequest): Promise<void> {
29
- const {
30
- connection,
31
- request,
32
- streamHandler,
33
- circuit,
34
- connectionManager
35
- } = hopRequest
36
-
37
- // Ensure hop is enabled
38
- if (!circuit.hopEnabled()) {
39
- log('HOP request received but we are not acting as a relay')
40
- return streamHandler.end({
41
- type: CircuitPB.Type.STATUS,
42
- code: CircuitPB.Status.HOP_CANT_SPEAK_RELAY
43
- })
44
- }
45
-
46
- // Validate the HOP request has the required input
47
- try {
48
- validateAddrs(request, streamHandler)
49
- } catch (err: any) {
50
- log.error('invalid hop request via peer %p %o', connection.remotePeer, err)
51
-
52
- return
53
- }
54
-
55
- if (request.dstPeer == null) {
56
- log('HOP request received but we do not receive a dstPeer')
57
- return
58
- }
59
-
60
- // Get the connection to the destination (stop) peer
61
- const destinationPeer = peerIdFromBytes(request.dstPeer.id)
62
-
63
- const destinationConnections = connectionManager.getConnections(destinationPeer)
64
- if (destinationConnections.length === 0 && !circuit.hopActive()) {
65
- log('HOP request received but we are not connected to the destination peer')
66
- return streamHandler.end({
67
- type: CircuitPB.Type.STATUS,
68
- code: CircuitPB.Status.HOP_NO_CONN_TO_DST
69
- })
70
- }
71
-
72
- // TODO: Handle being an active relay
73
- if (destinationConnections.length === 0) {
74
- log('did not have connection to remote peer')
75
- return streamHandler.end({
76
- type: CircuitPB.Type.STATUS,
77
- code: CircuitPB.Status.HOP_NO_CONN_TO_DST
78
- })
79
- }
80
-
81
- // Handle the incoming HOP request by performing a STOP request
82
- const stopRequest = {
83
- type: CircuitPB.Type.STOP,
84
- dstPeer: request.dstPeer,
85
- srcPeer: request.srcPeer
86
- }
87
-
88
- let destinationStream: Duplex<Uint8ArrayList>
89
- try {
90
- log('performing STOP request')
91
- const result = await stop({
92
- connection: destinationConnections[0],
93
- request: stopRequest
94
- })
95
-
96
- if (result == null) {
97
- throw new Error('Could not stop')
98
- }
99
-
100
- destinationStream = result
101
- } catch (err: any) {
102
- log.error(err)
103
-
104
- return
105
- }
106
-
107
- log('hop request from %p is valid', connection.remotePeer)
108
- streamHandler.write({
109
- type: CircuitPB.Type.STATUS,
110
- code: CircuitPB.Status.SUCCESS
111
- })
112
- const sourceStream = streamHandler.rest()
113
-
114
- log('creating related connections')
115
- // Short circuit the two streams to create the relayed connection
116
- return await pipe(
117
- sourceStream,
118
- destinationStream,
119
- sourceStream
120
- )
121
- }
122
-
123
- export interface HopConfig extends AbortOptions {
124
- connection: Connection
125
- request: CircuitPB
126
- }
127
-
128
- /**
129
- * Performs a HOP request to a relay peer, to request a connection to another
130
- * peer. A new, virtual, connection will be created between the two via the relay.
131
- */
132
- export async function hop (options: HopConfig): Promise<Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>> {
133
- const {
134
- connection,
135
- request,
136
- signal
137
- } = options
138
-
139
- // Create a new stream to the relay
140
- const stream = await connection.newStream(RELAY_CODEC, {
141
- signal
142
- })
143
- // Send the HOP request
144
- const streamHandler = new StreamHandler({ stream })
145
- streamHandler.write(request)
146
-
147
- const response = await streamHandler.read()
148
-
149
- if (response == null) {
150
- throw errCode(new Error('HOP request had no response'), Errors.ERR_HOP_REQUEST_FAILED)
151
- }
152
-
153
- if (response.code === CircuitPB.Status.SUCCESS) {
154
- log('hop request was successful')
155
-
156
- return streamHandler.rest()
157
- }
158
-
159
- log('hop request failed with code %d, closing stream', response.code)
160
- streamHandler.close()
161
-
162
- throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED)
163
- }
164
-
165
- export interface CanHopOptions extends AbortOptions {
166
- connection: Connection
167
- }
168
-
169
- /**
170
- * Performs a CAN_HOP request to a relay peer, in order to understand its capabilities
171
- */
172
- export async function canHop (options: CanHopOptions) {
173
- const {
174
- connection,
175
- signal
176
- } = options
177
-
178
- // Create a new stream to the relay
179
- const stream = await connection.newStream(RELAY_CODEC, {
180
- signal
181
- })
182
-
183
- // Send the HOP request
184
- const streamHandler = new StreamHandler({ stream })
185
- streamHandler.write({
186
- type: CircuitPB.Type.CAN_HOP
187
- })
188
-
189
- const response = await streamHandler.read()
190
- await streamHandler.close()
191
-
192
- if (response == null || response.code !== CircuitPB.Status.SUCCESS) {
193
- return false
194
- }
195
-
196
- return true
197
- }
198
-
199
- export interface HandleCanHopOptions {
200
- connection: Connection
201
- streamHandler: StreamHandler
202
- circuit: Circuit
203
- }
204
-
205
- /**
206
- * Creates an unencoded CAN_HOP response based on the Circuits configuration
207
- */
208
- export function handleCanHop (options: HandleCanHopOptions) {
209
- const {
210
- connection,
211
- streamHandler,
212
- circuit
213
- } = options
214
- const canHop = circuit.hopEnabled()
215
- log('can hop (%s) request from %p', canHop, connection.remotePeer)
216
- streamHandler.end({
217
- type: CircuitPB.Type.STATUS,
218
- code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY
219
- })
220
- }
@@ -1,83 +0,0 @@
1
- import { logger } from '@libp2p/logger'
2
- import { CircuitRelay as CircuitPB } from '../pb/index.js'
3
- import { RELAY_CODEC } from '../multicodec.js'
4
- import { StreamHandler } from './stream-handler.js'
5
- import { validateAddrs } from './utils.js'
6
- import type { Connection } from '@libp2p/interface-connection'
7
- import type { Duplex } from 'it-stream-types'
8
- import type { AbortOptions } from '@libp2p/interfaces'
9
- import type { Uint8ArrayList } from 'uint8arraylist'
10
-
11
- const log = logger('libp2p:circuit:stop')
12
-
13
- export interface HandleStopOptions {
14
- connection: Connection
15
- request: CircuitPB
16
- streamHandler: StreamHandler
17
- }
18
-
19
- /**
20
- * Handles incoming STOP requests
21
- */
22
- export function handleStop (options: HandleStopOptions): Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> | undefined {
23
- const {
24
- connection,
25
- request,
26
- streamHandler
27
- } = options
28
-
29
- // Validate the STOP request has the required input
30
- try {
31
- validateAddrs(request, streamHandler)
32
- } catch (err: any) {
33
- log.error('invalid stop request via peer %p %o', connection.remotePeer, err)
34
- return
35
- }
36
-
37
- // The request is valid
38
- log('stop request is valid')
39
- streamHandler.write({
40
- type: CircuitPB.Type.STATUS,
41
- code: CircuitPB.Status.SUCCESS
42
- })
43
-
44
- return streamHandler.rest()
45
- }
46
-
47
- export interface StopOptions extends AbortOptions {
48
- connection: Connection
49
- request: CircuitPB
50
- }
51
-
52
- /**
53
- * Creates a STOP request
54
- */
55
- export async function stop (options: StopOptions) {
56
- const {
57
- connection,
58
- request,
59
- signal
60
- } = options
61
-
62
- const stream = await connection.newStream(RELAY_CODEC, {
63
- signal
64
- })
65
- log('starting stop request to %p', connection.remotePeer)
66
- const streamHandler = new StreamHandler({ stream })
67
-
68
- streamHandler.write(request)
69
- const response = await streamHandler.read()
70
-
71
- if (response == null) {
72
- streamHandler.close()
73
- return
74
- }
75
-
76
- if (response.code === CircuitPB.Status.SUCCESS) {
77
- log('stop request to %p was successful', connection.remotePeer)
78
- return streamHandler.rest()
79
- }
80
-
81
- log('stop request failed with code %d', response.code)
82
- streamHandler.close()
83
- }
@@ -1,87 +0,0 @@
1
- import { logger } from '@libp2p/logger'
2
- import * as lp from 'it-length-prefixed'
3
- import { Handshake, handshake } from 'it-handshake'
4
- import { CircuitRelay } from '../pb/index.js'
5
- import type { Stream } from '@libp2p/interface-connection'
6
- import type { Source } from 'it-stream-types'
7
- import type { Uint8ArrayList } from 'uint8arraylist'
8
-
9
- const log = logger('libp2p:circuit:stream-handler')
10
-
11
- export interface StreamHandlerOptions {
12
- /**
13
- * A duplex iterable
14
- */
15
- stream: Stream
16
-
17
- /**
18
- * max bytes length of message
19
- */
20
- maxLength?: number
21
- }
22
-
23
- export class StreamHandler {
24
- private readonly stream: Stream
25
- private readonly shake: Handshake<Uint8ArrayList | Uint8Array>
26
- private readonly decoder: Source<Uint8ArrayList>
27
-
28
- constructor (options: StreamHandlerOptions) {
29
- const { stream, maxLength = 4096 } = options
30
-
31
- this.stream = stream
32
- this.shake = handshake(this.stream)
33
- this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength })
34
- }
35
-
36
- /**
37
- * Read and decode message
38
- */
39
- async read () {
40
- // @ts-expect-error FIXME is a source, needs to be a generator
41
- const msg = await this.decoder.next()
42
-
43
- if (msg.value != null) {
44
- const value = CircuitRelay.decode(msg.value)
45
- log('read message type', value.type)
46
- return value
47
- }
48
-
49
- log('read received no value, closing stream')
50
- // End the stream, we didn't get data
51
- this.close()
52
- }
53
-
54
- /**
55
- * Encode and write array of buffers
56
- */
57
- write (msg: CircuitRelay) {
58
- log('write message type %s', msg.type)
59
- this.shake.write(lp.encode.single(CircuitRelay.encode(msg)))
60
- }
61
-
62
- /**
63
- * Return the handshake rest stream and invalidate handler
64
- */
65
- rest () {
66
- this.shake.rest()
67
- return this.shake.stream
68
- }
69
-
70
- /**
71
- * @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
72
- */
73
- end (msg: CircuitRelay) {
74
- this.write(msg)
75
- this.close()
76
- }
77
-
78
- /**
79
- * Close the stream
80
- */
81
- close () {
82
- log('closing the stream')
83
- void this.rest().sink([]).catch(err => {
84
- log.error(err)
85
- })
86
- }
87
- }
@@ -1,44 +0,0 @@
1
- import { multiaddr } from '@multiformats/multiaddr'
2
- import { CircuitRelay } from '../pb/index.js'
3
- import type { StreamHandler } from './stream-handler.js'
4
-
5
- /**
6
- * Write a response
7
- */
8
- function writeResponse (streamHandler: StreamHandler, status: CircuitRelay.Status) {
9
- streamHandler.write({
10
- type: CircuitRelay.Type.STATUS,
11
- code: status
12
- })
13
- }
14
-
15
- /**
16
- * Validate incomming HOP/STOP message
17
- */
18
- export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler) {
19
- try {
20
- if (msg.dstPeer?.addrs != null) {
21
- msg.dstPeer.addrs.forEach((addr) => {
22
- return multiaddr(addr)
23
- })
24
- }
25
- } catch (err: any) {
26
- writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
27
- ? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
28
- : CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID)
29
- throw err
30
- }
31
-
32
- try {
33
- if (msg.srcPeer?.addrs != null) {
34
- msg.srcPeer.addrs.forEach((addr) => {
35
- return multiaddr(addr)
36
- })
37
- }
38
- } catch (err: any) {
39
- writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
40
- ? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
41
- : CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID)
42
- throw err
43
- }
44
- }
@@ -1,83 +0,0 @@
1
- import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
2
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
3
- import type { PeerStore } from '@libp2p/interface-peer-store'
4
- import type { Listener } from '@libp2p/interface-transport'
5
- import { peerIdFromString } from '@libp2p/peer-id'
6
- import type { Multiaddr } from '@multiformats/multiaddr'
7
- import { multiaddr } from '@multiformats/multiaddr'
8
-
9
- export interface ListenerOptions {
10
- peerStore: PeerStore
11
- connectionManager: ConnectionManager
12
- }
13
-
14
- export function createListener (options: ListenerOptions): Listener {
15
- const listeningAddrs = new Map()
16
-
17
- /**
18
- * Add swarm handler and listen for incoming connections
19
- */
20
- async function listen (addr: Multiaddr): Promise<void> {
21
- const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
22
- const ma = multiaddr(addrString)
23
-
24
- const relayPeerStr = ma.getPeerId()
25
-
26
- if (relayPeerStr == null) {
27
- throw new Error('Could not determine relay peer from multiaddr')
28
- }
29
-
30
- const relayPeerId = peerIdFromString(relayPeerStr)
31
-
32
- await options.peerStore.addressBook.add(relayPeerId, [ma])
33
-
34
- const relayConn = await options.connectionManager.openConnection(relayPeerId)
35
- const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
36
-
37
- listeningAddrs.set(relayConn.remotePeer.toString(), relayedAddr)
38
- listener.dispatchEvent(new CustomEvent('listening'))
39
- }
40
-
41
- /**
42
- * Get fixed up multiaddrs
43
- *
44
- * NOTE: This method will grab the peers multiaddrs and expand them such that:
45
- *
46
- * a) If it's an existing /p2p-circuit address for a specific relay i.e.
47
- * `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
48
- * address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
49
- * `QmPeer` is this peers id
50
- * b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
51
- * addr, such when dialing over a relay with this address, it will create the circuit using
52
- * the encapsulated transport address. This is useful when for example, a peer should only
53
- * be dialed over TCP rather than any other transport
54
- *
55
- * @returns {Multiaddr[]}
56
- */
57
- function getAddrs () {
58
- const addrs = []
59
- for (const addr of listeningAddrs.values()) {
60
- addrs.push(addr)
61
- }
62
- return addrs
63
- }
64
-
65
- const listener: Listener = Object.assign(new EventEmitter(), {
66
- close: async () => await Promise.resolve(),
67
- listen,
68
- getAddrs
69
- })
70
-
71
- // Remove listeningAddrs when a peer disconnects
72
- options.connectionManager.addEventListener('peer:disconnect', (evt) => {
73
- const { detail: connection } = evt
74
- const deleted = listeningAddrs.delete(connection.remotePeer.toString())
75
-
76
- if (deleted) {
77
- // Announce listen addresses change
78
- listener.dispatchEvent(new CustomEvent('close'))
79
- }
80
- })
81
-
82
- return listener
83
- }
@@ -1,2 +0,0 @@
1
-
2
- export const RELAY_CODEC = '/libp2p/circuit/relay/0.1.0'