-
Notifications
You must be signed in to change notification settings - Fork 443
/
index.ts
83 lines (74 loc) · 2.27 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
/**
* @packageDocumentation
*
* The AutoNAT service uses the [AutoNAT protocol](https://docs.libp2p.io/concepts/nat/autonat/).
* The service confirms addresses are dialable by remote peers, and updates the list it advertises.
*
* The service dials randomly selected peers with a request to verify it's external addresses.
* The request includes a list of public multiaddrs (addressManager.getObservedAddrs()).
* The remote peers dial that list and respond with the results.
*
* The AutoNAT service uses those responses to either:
* - addressManager.confirmObservedAddr(addr)
* - addressManager.removeObservedAddr(addr)
*
* The result list of candidates and confirmed addresses can be found
* at addressManager.getObservedAddrs()
*
* @example
*
* ```typescript
* import { createLibp2p } from 'libp2p'
* import { autoNAT } from '@libp2p/autonat'
*
* const node = await createLibp2p({
* // ...other options
* services: {
* autoNAT: autoNAT()
* }
* })
* ```
*/
import { AutoNATService } from './autonat.js'
import type { ComponentLogger, PeerId } from '@libp2p/interface'
import type { AddressManager, ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal'
export interface AutoNATServiceInit {
/**
* Allows overriding the protocol prefix used
*/
protocolPrefix?: string
/**
* How long we should wait for a remote peer to verify our external address
*/
timeout?: number
/**
* How long to wait after startup before trying to verify our external address
*/
startupDelay?: number
/**
* Verify our external addresses this often
*/
refreshInterval?: number
/**
* How many parallel inbound autoNAT streams we allow per-connection
*/
maxInboundStreams?: number
/**
* How many parallel outbound autoNAT streams we allow per-connection
*/
maxOutboundStreams?: number
}
export interface AutoNATComponents {
registrar: Registrar
addressManager: AddressManager
transportManager: TransportManager
peerId: PeerId
connectionManager: ConnectionManager
logger: ComponentLogger
randomWalk: RandomWalk
}
export function autoNAT (init: AutoNATServiceInit = {}): (components: AutoNATComponents) => unknown {
return (components) => {
return new AutoNATService(components, init)
}
}