Skip to content

Commit 87dc7e9

Browse files
authored
feat(libp2p): direct connection through relay protocol (DCUtR) (#1928)
Implements the [DCUtR protocol](https://github.com/libp2p/specs/blob/master/relay/DCUtR.md). Only supports TCP Simultaneous Connect since there is no QUIC support yet
1 parent d994859 commit 87dc7e9

File tree

10 files changed

+844
-9
lines changed

10 files changed

+844
-9
lines changed

packages/interface-internal/src/connection-manager/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import type { PeerId } from '@libp2p/interface/peer-id'
44
import type { PeerMap } from '@libp2p/peer-collections'
55
import type { Multiaddr } from '@multiformats/multiaddr'
66

7+
export interface OpenConnectionOptions extends AbortOptions {
8+
/**
9+
* Connection requests with a higher priority will be executed before those
10+
* with a lower priority. (default: 50)
11+
*/
12+
priority?: number
13+
14+
/**
15+
* When opening a connection to a remote peer, if a connection already exists
16+
* it will be returned instead of creating a new connection. Pass true here
17+
* to override that and dial a new connection anyway. (default: false)
18+
*/
19+
force?: boolean
20+
}
21+
722
export interface ConnectionManager {
823
/**
924
* Return connections, optionally filtering by a PeerId
@@ -37,7 +52,7 @@ export interface ConnectionManager {
3752
* const connection = await libp2p.connectionManager.openConnection(peerId)
3853
* ```
3954
*/
40-
openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions) => Promise<Connection>
55+
openConnection: (peer: PeerId | Multiaddr | Multiaddr[], options?: OpenConnectionOptions) => Promise<Connection>
4156

4257
/**
4358
* Close our connections to a peer

packages/libp2p/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
"types": "./dist/src/circuit-relay/index.d.ts",
5757
"import": "./dist/src/circuit-relay/index.js"
5858
},
59+
"./dcutr": {
60+
"types": "./dist/src/dcutr/index.d.ts",
61+
"import": "./dist/src/dcutr/index.js"
62+
},
5963
"./fetch": {
6064
"types": "./dist/src/fetch/index.d.ts",
6165
"import": "./dist/src/fetch/index.js"
@@ -100,7 +104,8 @@
100104
"build": "aegir build",
101105
"generate": "run-s generate:proto:*",
102106
"generate:proto:autonat": "protons ./src/autonat/pb/index.proto",
103-
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
107+
"generate:proto:circuit-relay": "protons ./src/circuit-relay/pb/index.proto",
108+
"generate:proto:dcutr": "protons ./src/dcutr/pb/message.proto",
104109
"generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
105110
"generate:proto:identify": "protons ./src/identify/pb/message.proto",
106111
"generate:proto:plaintext": "protons ./src/insecure/pb/proto.proto",
@@ -129,6 +134,7 @@
129134
"@libp2p/utils": "^4.0.2",
130135
"@multiformats/mafmt": "^12.1.2",
131136
"@multiformats/multiaddr": "^12.1.5",
137+
"@multiformats/multiaddr-matcher": "^1.0.0",
132138
"abortable-iterator": "^5.0.1",
133139
"any-signal": "^4.1.1",
134140
"datastore-core": "^9.0.1",

packages/libp2p/src/connection-manager/auto-dial.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ export class AutoDial implements Startable {
222222

223223
log('connecting to a peerStore stored peer %p', peer.id)
224224
await this.connectionManager.openConnection(peer.id, {
225-
// @ts-expect-error needs adding to the ConnectionManager interface
226225
priority: this.autoDialPriority
227226
})
228227
}, {

packages/libp2p/src/connection-manager/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { Metrics } from '@libp2p/interface/metrics'
2020
import type { PeerId } from '@libp2p/interface/peer-id'
2121
import type { Peer, PeerStore } from '@libp2p/interface/peer-store'
2222
import type { Startable } from '@libp2p/interface/startable'
23-
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
23+
import type { ConnectionManager, OpenConnectionOptions } from '@libp2p/interface-internal/connection-manager'
2424
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
2525

2626
const log = logger('libp2p:connection-manager')
@@ -154,10 +154,6 @@ export interface DefaultConnectionManagerComponents {
154154
events: EventEmitter<Libp2pEvents>
155155
}
156156

157-
export interface OpenConnectionOptions extends AbortOptions {
158-
priority?: number
159-
}
160-
161157
/**
162158
* Responsible for managing known connections.
163159
*/
@@ -492,7 +488,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
492488

493489
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
494490

495-
if (peerId != null) {
491+
if (peerId != null && options.force !== true) {
496492
log('dial %p', peerId)
497493
const existingConnections = this.getConnections(peerId)
498494

0 commit comments

Comments
 (0)