Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autonat support #1298

Merged
merged 12 commits into from
Mar 14, 2023
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"dep-check": "aegir dep-check -i protons",
"prepublishOnly": "node scripts/update-version.js",
"build": "aegir build",
"docs": "aegir docs",
"generate": "run-s generate:proto:*",
"generate:proto:autonat": "protons ./src/autonat/pb/index.proto",
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
"generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
"generate:proto:identify": "protons ./src/identify/pb/message.proto",
Expand Down Expand Up @@ -143,6 +144,7 @@
"it-drain": "^2.0.0",
"it-filter": "^2.0.0",
"it-first": "^2.0.0",
"it-parallel": "^3.0.0",
"it-handshake": "^4.1.2",
"it-length-prefixed": "^8.0.2",
"it-map": "^2.0.0",
Expand Down Expand Up @@ -201,6 +203,7 @@
"p-event": "^5.0.1",
"p-times": "^4.0.0",
"p-wait-for": "^5.0.0",
"protons": "^7.0.2",
"sinon": "^15.0.1",
"sinon-ts": "^1.0.0"
},
Expand Down
97 changes: 62 additions & 35 deletions src/address-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,35 @@ export interface AddressFilter {

const defaultAddressFilter = (addrs: Multiaddr[]): Multiaddr[] => addrs

interface ObservedAddressMetadata {
confident: boolean
}

/**
* If the passed multiaddr contains the passed peer id, remove it
*/
function stripPeerId (ma: Multiaddr, peerId: PeerId): Multiaddr {
const observedPeerIdStr = ma.getPeerId()

// strip our peer id if it has been passed
if (observedPeerIdStr != null) {
const observedPeerId = peerIdFromString(observedPeerIdStr)

// use same encoding for comparison
if (observedPeerId.equals(peerId)) {
ma = ma.decapsulate(multiaddr(`/p2p/${peerId.toString()}`))
}
}

return ma
}

export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
private readonly components: DefaultAddressManagerComponents
// this is an array to allow for duplicates, e.g. multiples of `/ip4/0.0.0.0/tcp/0`
private readonly listen: string[]
private readonly announce: Set<string>
private readonly observed: Set<string>
private readonly observed: Map<string, ObservedAddressMetadata>
private readonly announceFilter: AddressFilter

/**
Expand All @@ -66,7 +89,7 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
this.components = components
this.listen = listen.map(ma => ma.toString())
this.announce = new Set(announce.map(ma => ma.toString()))
this.observed = new Set()
this.observed = new Map()
this.announceFilter = init.announceFilter ?? defaultAddressFilter
}

Expand All @@ -88,52 +111,51 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
* Get observed multiaddrs
*/
getObservedAddrs (): Multiaddr[] {
return Array.from(this.observed).map((a) => multiaddr(a))
return Array.from(this.observed).map(([a]) => multiaddr(a))
}

/**
* Add peer observed addresses
* Signal that we have confidence an observed multiaddr is publicly dialable -
* this will make it appear in the output of getAddresses()
*/
confirmObservedAddr (addr: Multiaddr): void {

}
addObservedAddr (addr: Multiaddr): void {
addr = stripPeerId(addr, this.components.peerId)
const addrString = addr.toString()

/**
* Signal that we do not have confidence an observed multiaddr is publicly dialable -
* this will remove it from the output of getObservedAddrs()
*/
removeObservedAddr (addr: Multiaddr): void {
// do not trigger the change:addresses event if we already know about this address
if (this.observed.has(addrString)) {
return
}

this.observed.set(addrString, {
confident: false
})
}

/**
* Add peer observed addresses
*/
addObservedAddr (addr: string | Multiaddr): void {
let ma = multiaddr(addr)
const remotePeer = ma.getPeerId()

// strip our peer id if it has been passed
if (remotePeer != null) {
const remotePeerId = peerIdFromString(remotePeer)

// use same encoding for comparison
if (remotePeerId.equals(this.components.peerId)) {
ma = ma.decapsulate(multiaddr(`/p2p/${this.components.peerId.toString()}`))
}
confirmObservedAddr (addr: Multiaddr): void {
addr = stripPeerId(addr, this.components.peerId)
const addrString = addr.toString()

const metadata = this.observed.get(addrString) ?? {
confident: false
}

const addrString = ma.toString()
const startingConfidence = metadata.confident

// do not trigger the change:addresses event if we already know about this address
if (this.observed.has(addrString)) {
return
this.observed.set(addrString, {
confident: true
})

// only trigger the change:addresses event if our confidence in an address has changed
if (!startingConfidence) {
this.dispatchEvent(new CustomEvent('change:addresses'))
}
}

removeObservedAddr (addr: Multiaddr): void {
addr = stripPeerId(addr, this.components.peerId)
const addrString = addr.toString()

this.observed.add(addrString)
this.dispatchEvent(new CustomEvent('change:addresses'))
this.observed.delete(addrString)
}

getAddresses (): Multiaddr[] {
Expand All @@ -144,7 +166,12 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
addrs = this.components.transportManager.getAddrs().map(ma => ma.toString())
}

addrs = addrs.concat(this.getObservedAddrs().map(ma => ma.toString()))
// add observed addresses we are confident in
addrs = addrs.concat(
Array.from(this.observed)
.filter(([ma, metadata]) => metadata.confident)
.map(([ma]) => ma)
)

// dedupe multiaddrs
const addrSet = new Set(addrs)
Expand Down
4 changes: 4 additions & 0 deletions src/autonat/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const PROTOCOL = '/libp2p/autonat/1.0.0'
export const PROTOCOL_VERSION = '1.0.0'
export const PROTOCOL_NAME = 'autonat'
Loading