Skip to content

Commit

Permalink
feat: added validation to dcutr
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Oct 5, 2023
1 parent e0080fb commit d45851d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/libp2p/src/circuit-relay/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL,
DEFAULT_MAX_RESERVATION_STORE_SIZE,
DEFAULT_MAX_RESERVATION_TTL,
RELAY_SOURCE_TAG,
RELAY_V2_HOP_CODEC,
RELAY_SOURCE_TAG,
RELAY_V2_HOP_CODEC,
RELAY_V2_STOP_CODEC
} from '../constants.js'
import { HopMessage, type Reservation, Status, StopMessage } from '../pb/index.js'
Expand Down
27 changes: 14 additions & 13 deletions packages/libp2p/src/dcutr/dcutr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Circuit, IP, DNS } from '@multiformats/multiaddr-matcher'
import delay from 'delay'
import { pbStream } from 'it-protobuf-stream'
import isPrivate from 'private-ip'
import { number, object } from 'yup'
import { codes } from '../errors.js'
import { HolePunch } from './pb/message.js'
import { multicodec } from './index.js'
Expand All @@ -24,15 +25,6 @@ const MAX_DCUTR_MESSAGE_SIZE = 1024 * 4
// ensure the dial has a high priority to jump to the head of the dial queue
const DCUTR_DIAL_PRIORITY = 100

const defaultValues = {
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L27
timeout: 5000,
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L28
retries: 3,
maxInboundStreams: 1,
maxOutboundStreams: 1
}

export class DefaultDCUtRService implements Startable {
private started: boolean
private readonly timeout: number
Expand All @@ -54,10 +46,19 @@ export class DefaultDCUtRService implements Startable {
this.connectionManager = components.connectionManager
this.transportManager = components.transportManager

this.timeout = init.timeout ?? defaultValues.timeout
this.retries = init.retries ?? defaultValues.retries
this.maxInboundStreams = init.maxInboundStreams ?? defaultValues.maxInboundStreams
this.maxOutboundStreams = init.maxOutboundStreams ?? defaultValues.maxOutboundStreams
const validatedConfig = object({
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L27
timeout: number().integer().default(5000).min(1),
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L28
retries: number().integer().default(3).min(1),
maxInboundStreams: number().integer().default(1).min(1),
maxOutboundStreams: number().integer().default(1).min(1)
}).validateSync(init)

this.timeout = validatedConfig.timeout
this.retries = validatedConfig.retries
this.maxInboundStreams = validatedConfig.maxInboundStreams
this.maxOutboundStreams = validatedConfig.maxOutboundStreams
}

isStarted (): boolean {
Expand Down

0 comments on commit d45851d

Please sign in to comment.