-
Notifications
You must be signed in to change notification settings - Fork 443
/
index.ts
102 lines (94 loc) · 2.88 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @packageDocumentation
*
* Direct Connection Upgrade through Relay (DCUtR) is a protocol that allows two
* nodes to connect to each other who would otherwise be prevented doing so due
* to being behind NATed connections or firewalls.
*
* The protocol involves making a relayed connection between the two peers and
* using the relay to synchronise connection timings so that they dial each other
* at precisely the same moment.
*
* @example
*
* ```TypeScript
* import { createLibp2p } from 'libp2p'
* import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
* import { tcp } from '@libp2p/tcp'
* import { identify } from '@libp2p/identify'
* import { dcutr } from '@libp2p/dcutr'
* import { multiaddr } from '@multiformats/multiaddr'
*
* const node = await createLibp2p({
* transports: [
* circuitRelayTransport(),
* tcp()
* ],
* services: {
* identify: identify(),
* dcutr: dcutr()
* }
* })
*
* // QmTarget is a peer that is behind a NAT, supports TCP and has a relay
* // reservation
* const ma = multiaddr('/ip4/.../p2p/QmRelay/p2p-circuit/p2p/QmTarget')
* await node.dial(ma)
*
* // after a while the connection should automatically get upgraded to a
* // direct connection (e.g. non-limited)
* while (true) {
* const connections = node.getConnections()
*
* if (connections.find(conn => conn.limits == null)) {
* console.info('have direct connection')
* break
* } else {
* console.info('have relayed connection')
*
* // wait a few seconds to see if it's succeeded yet
* await new Promise<void>((resolve) => {
* setTimeout(() => resolve(), 5000)
* })
* }
* }
* ```
*/
import { DefaultDCUtRService } from './dcutr.js'
import type { ComponentLogger, PeerStore } from '@libp2p/interface'
import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'
export interface DCUtRServiceInit {
/**
* How long we should wait for the connection upgrade to complete (default: 5s)
*/
timeout?: number
/**
* How many times to retry the connection upgrade (default: 3)
*/
retries?: number
/**
* How many simultaneous inbound DCUtR protocol streams to allow on each
* connection (default: 1)
*/
maxInboundStreams?: number
/**
* How many simultaneous outbound DCUtR protocol streams to allow on each
* connection (default: 1)
*/
maxOutboundStreams?: number
}
export interface DCUtRServiceComponents {
peerStore: PeerStore
connectionManager: ConnectionManager
registrar: Registrar
addressManager: AddressManager
transportManager: TransportManager
logger: ComponentLogger
}
/**
* The DCUtR protocol
*/
export const multicodec = '/libp2p/dcutr'
export function dcutr (init: DCUtRServiceInit = {}): (components: DCUtRServiceComponents) => unknown {
return (components) => new DefaultDCUtRService(components, init)
}