Skip to content

Commit 6bd7bec

Browse files
committed
feat: updated config validation for identify and circuit relay (libp2p#1573)
1 parent be8bd3a commit 6bd7bec

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

packages/libp2p/src/circuit-relay/constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ export const DEFAULT_HOP_TIMEOUT = 30 * second
7070
* How long to wait before starting to advertise the relay service
7171
*/
7272
export const DEFAULT_ADVERT_BOOT_DELAY = 30 * second
73+
74+
/**
75+
* The default timeout for Incoming STOP requests from the relay
76+
*/
77+
export const DEFAULT_STOP_TIMEOUT = 30 * second

packages/libp2p/src/circuit-relay/transport/index.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { pbStream } from 'it-protobuf-stream'
99
import { number, object } from 'yup'
1010
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
1111
import { codes } from '../../errors.js'
12-
import { CIRCUIT_PROTO_CODE, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
12+
import { CIRCUIT_PROTO_CODE, DEFAULT_STOP_TIMEOUT, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
1313
import { StopMessage, HopMessage, Status } from '../pb/index.js'
1414
import { RelayDiscovery, type RelayDiscoveryComponents } from './discovery.js'
1515
import { createListener } from './listener.js'
@@ -92,7 +92,7 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
9292
* must finish the initial protocol negotiation within this timeout in ms
9393
* (default: 30000)
9494
*/
95-
stopTimeout: number
95+
stopTimeout?: number
9696

9797
/**
9898
* When creating a reservation it must complete within this number of ms
@@ -113,7 +113,7 @@ class CircuitRelayTransport implements Transport {
113113
private readonly reservationStore: ReservationStore
114114
private readonly maxInboundStopStreams?: number
115115
private readonly maxOutboundStopStreams?: number
116-
private readonly stopTimeout: number
116+
private readonly stopTimeout?: number
117117
private started: boolean
118118

119119
constructor (components: CircuitRelayTransportComponents, init: CircuitRelayTransportInit) {
@@ -256,7 +256,7 @@ class CircuitRelayTransport implements Transport {
256256
try {
257257
const pbstr = pbStream(stream)
258258
const hopstr = pbstr.pb(HopMessage)
259-
hopstr.write({
259+
await hopstr.write({
260260
type: HopMessage.Type.CONNECT,
261261
peer: {
262262
id: destinationPeer.toBytes(),
@@ -315,7 +315,7 @@ class CircuitRelayTransport implements Transport {
315315
* An incoming STOP request means a remote peer wants to dial us via a relay
316316
*/
317317
async onStop ({ connection, stream }: IncomingStreamData): Promise<void> {
318-
const signal = AbortSignal.timeout(this.stopTimeout)
318+
const signal = AbortSignal.timeout(this.stopTimeout ?? DEFAULT_STOP_TIMEOUT)
319319
const pbstr = pbStream(stream).pb(StopMessage)
320320
const request = await pbstr.read({
321321
signal
@@ -325,7 +325,7 @@ class CircuitRelayTransport implements Transport {
325325

326326
if (request?.type === undefined) {
327327
log.error('type was missing from circuit v2 stop protocol request from %s', connection.remotePeer)
328-
pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
328+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
329329
signal
330330
})
331331
await stream.close()
@@ -335,7 +335,7 @@ class CircuitRelayTransport implements Transport {
335335
// Validate the STOP request has the required input
336336
if (request.type !== StopMessage.Type.CONNECT) {
337337
log.error('invalid stop connect request via peer %p', connection.remotePeer)
338-
pbstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE }, {
338+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE }, {
339339
signal
340340
})
341341
await stream.close()
@@ -344,7 +344,7 @@ class CircuitRelayTransport implements Transport {
344344

345345
if (!isValidStop(request)) {
346346
log.error('invalid stop connect request via peer %p', connection.remotePeer)
347-
pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
347+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
348348
signal
349349
})
350350
await stream.close()
@@ -355,15 +355,15 @@ class CircuitRelayTransport implements Transport {
355355

356356
if ((await this.connectionGater.denyInboundRelayedConnection?.(connection.remotePeer, remotePeerId)) === true) {
357357
log.error('connection gater denied inbound relayed connection from %p', connection.remotePeer)
358-
pbstr.write({ type: StopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, {
358+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, {
359359
signal
360360
})
361361
await stream.close()
362362
return
363363
}
364364

365365
log.trace('sending success response to %p', connection.remotePeer)
366-
pbstr.write({ type: StopMessage.Type.STATUS, status: Status.OK }, {
366+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.OK }, {
367367
signal
368368
})
369369

@@ -386,7 +386,7 @@ export function circuitRelayTransport (init?: CircuitRelayTransportInit): (compo
386386
discoverRelays: number().min(0).integer().default(0),
387387
maxInboundStopStreams: number().min(0).integer().default(MAX_CONNECTIONS),
388388
maxOutboundStopStreams: number().min(0).integer().default(MAX_CONNECTIONS),
389-
stopTimeout: number().min(0).integer().default(30000)
389+
stopTimeout: number().min(0).integer().default(DEFAULT_STOP_TIMEOUT)
390390
}).validateSync(init)
391391

392392
return (components) => {

packages/libp2p/src/identify/identify.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
8686
agentVersion: init.agentVersion ?? AGENT_VERSION
8787
}
8888

89-
if (init.runOnConnectionOpen) {
89+
if (init.runOnConnectionOpen === true) {
9090
// When a new connection happens, trigger identify
9191
components.events.addEventListener('connection:open', (evt) => {
9292
const connection = evt.detail

packages/libp2p/src/identify/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { number, object, string } from 'yup'
1+
import { boolean, number, object, string } from 'yup'
22
import {
33
AGENT_VERSION,
44
MAX_IDENTIFY_MESSAGE_SIZE,
@@ -93,8 +93,9 @@ export function identifyService (init: IdentifyServiceInit = {}): (components: I
9393
timeout: number().integer().default(TIMEOUT),
9494
maxIdentifyMessageSize: number().integer().min(0).default(MAX_IDENTIFY_MESSAGE_SIZE),
9595
maxInboundStreams: number().integer().min(0).default(MAX_INBOUND_STREAMS),
96-
maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS)
96+
maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS),
97+
runOnConnectionOpen: boolean().default(true)
9798
}).validateSync(init)
9899

99100
return (components) => new DefaultIdentifyService(components, validatedConfig)
100-
}
101+
}

0 commit comments

Comments
 (0)