-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
221 additions
and
424 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
export const RELAY_V1_CODEC = '/libp2p/circuit/relay/0.1.0' | ||
export const protocolIDv2Hop = '/libp2p/circuit/relay/0.2.0/hop' | ||
export const protocolIDv2Stop = '/libp2p/circuit/relay/0.2.0/stop' | ||
export const relayV1Codec = '/libp2p/circuit/relay/0.1.0' | ||
export const relayV2HopCodec = '/libp2p/circuit/relay/0.2.0/hop' | ||
export const relayV2StopCodec = '/libp2p/circuit/relay/0.2.0/stop' |
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,108 @@ | ||
import { logger } from '@libp2p/logger' | ||
import { codes } from '../errors.js' | ||
import { | ||
setDelayedInterval, | ||
clearDelayedInterval | ||
// @ts-expect-error set-delayed-interval does not export types | ||
} from 'set-delayed-interval' | ||
import { namespaceToCid } from './utils.js' | ||
import { | ||
RELAY_RENDEZVOUS_NS | ||
} from './constants.js' | ||
import type { AddressSorter } from '@libp2p/interfaces/peer-store' | ||
import type { Startable } from '@libp2p/interfaces/startable' | ||
import type { Components } from '@libp2p/interfaces/components' | ||
|
||
const log = logger('libp2p:relay') | ||
|
||
export interface RelayAdvertiseConfig { | ||
bootDelay?: number | ||
enabled?: boolean | ||
ttl?: number | ||
} | ||
|
||
export interface HopConfig { | ||
enabled?: boolean | ||
active?: boolean | ||
} | ||
|
||
export interface AutoRelayConfig { | ||
enabled?: boolean | ||
|
||
/** | ||
* maximum number of relays to listen | ||
*/ | ||
maxListeners: number | ||
} | ||
|
||
export interface RelayInit { | ||
addressSorter?: AddressSorter | ||
maxListeners?: number | ||
onError?: (error: Error, msg?: string) => void | ||
hop: HopConfig | ||
advertise: RelayAdvertiseConfig | ||
} | ||
|
||
export class Relay implements Startable { | ||
private readonly components: Components | ||
private readonly init: RelayInit | ||
private timeout?: any | ||
private started: boolean | ||
|
||
/** | ||
* Creates an instance of Relay | ||
*/ | ||
constructor (components: Components, init: RelayInit) { | ||
this.components = components | ||
this.started = false | ||
this.init = init | ||
this._advertiseService = this._advertiseService.bind(this) | ||
} | ||
|
||
isStarted () { | ||
return this.started | ||
} | ||
|
||
/** | ||
* Start Relay service | ||
*/ | ||
async start () { | ||
// Advertise service if HOP enabled | ||
if (this.init.hop.enabled !== false && this.init.advertise.enabled !== false) { | ||
this.timeout = setDelayedInterval( | ||
this._advertiseService, this.init.advertise.ttl, this.init.advertise.bootDelay | ||
) | ||
} | ||
|
||
this.started = true | ||
} | ||
|
||
/** | ||
* Stop Relay service | ||
*/ | ||
async stop () { | ||
if (this.timeout != null) { | ||
clearDelayedInterval(this.timeout) | ||
} | ||
|
||
this.started = false | ||
} | ||
|
||
/** | ||
* Advertise hop relay service in the network. | ||
*/ | ||
async _advertiseService () { | ||
try { | ||
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) | ||
await this.components.getContentRouting().provide(cid) | ||
} catch (err: any) { | ||
if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) { | ||
log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err) | ||
// Stop the advertise | ||
await this.stop() | ||
} else { | ||
log.error(err) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.