-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates nat-port-mapper to try to map ports on all available gateways instead of just the first one that is discovered on the network.
- Loading branch information
1 parent
bc90b4f
commit 26a3723
Showing
8 changed files
with
408 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const DEFAULT_PORT_MAPPING_TTL = 720_000 | ||
export const DEFAULT_GATEWAY_SEARCH_TIMEOUT = 60_000 | ||
export const DEFAULT_GATEWAY_SEARCH_INTERVAL = 300_000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { TypedEventEmitter, start, stop } from '@libp2p/interface' | ||
import { repeatingTask } from '@libp2p/utils/repeating-task' | ||
import { DEFAULT_GATEWAY_SEARCH_INTERVAL, DEFAULT_GATEWAY_SEARCH_TIMEOUT } from './constants.js' | ||
import type { Gateway, UPnPNAT } from '@achingbrain/nat-port-mapper' | ||
import type { ComponentLogger, Logger } from '@libp2p/interface' | ||
import type { RepeatingTask } from '@libp2p/utils/repeating-task' | ||
|
||
export interface GatewayFinderComponents { | ||
logger: ComponentLogger | ||
} | ||
|
||
export interface GatewayFinderInit { | ||
portMappingClient: UPnPNAT | ||
} | ||
|
||
export interface GatewayFinderEvents { | ||
'gateway': CustomEvent<Gateway> | ||
} | ||
|
||
export class GatewayFinder extends TypedEventEmitter<GatewayFinderEvents> { | ||
private readonly log: Logger | ||
private readonly gateways: Gateway[] | ||
private readonly findGateways: RepeatingTask | ||
private readonly portMappingClient: UPnPNAT | ||
private started: boolean | ||
|
||
constructor (components: GatewayFinderComponents, init: GatewayFinderInit) { | ||
super() | ||
|
||
this.log = components.logger.forComponent('libp2p:upnp-nat') | ||
this.portMappingClient = init.portMappingClient | ||
this.started = false | ||
this.gateways = [] | ||
|
||
// every five minutes, search for network gateways for one minute | ||
this.findGateways = repeatingTask(async (options) => { | ||
for await (const gateway of this.portMappingClient.findGateways(options)) { | ||
if (this.gateways.some(g => g.id === gateway.id)) { | ||
// already seen this gateway | ||
continue | ||
} | ||
|
||
this.gateways.push(gateway) | ||
this.safeDispatchEvent('gateway', { | ||
detail: gateway | ||
}) | ||
} | ||
}, DEFAULT_GATEWAY_SEARCH_INTERVAL, { | ||
runImmediately: true, | ||
timeout: DEFAULT_GATEWAY_SEARCH_TIMEOUT | ||
}) | ||
} | ||
|
||
async start (): Promise<void> { | ||
if (this.started) { | ||
return | ||
} | ||
|
||
this.started = true | ||
await start(this.findGateways) | ||
} | ||
|
||
/** | ||
* Stops the NAT manager | ||
*/ | ||
async stop (): Promise<void> { | ||
await stop(this.findGateways) | ||
this.started = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.