diff --git a/packages/libp2p/src/address-manager/utils.ts b/packages/libp2p/src/address-manager/utils.ts index c0b53ec010..02d460bad3 100644 --- a/packages/libp2p/src/address-manager/utils.ts +++ b/packages/libp2p/src/address-manager/utils.ts @@ -1,6 +1,5 @@ import { type ObjectSchema, object, array, string, mixed } from 'yup' import { validateMultiaddr } from '../config/helpers.js' -import type { AddressManagerInit } from '.' import type { Multiaddr } from '@multiformats/multiaddr' export function debounce (func: () => void, wait: number): () => void { @@ -17,7 +16,7 @@ export function debounce (func: () => void, wait: number): () => void { } } -export function validateAddressManagerConfig (opts: AddressManagerInit): ObjectSchema> { +export function validateAddressManagerConfig (): ObjectSchema> { return object({ listen: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), announce: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), diff --git a/packages/libp2p/src/config/config.ts b/packages/libp2p/src/config/config.ts index 4f3799baf2..f761ad8410 100644 --- a/packages/libp2p/src/config/config.ts +++ b/packages/libp2p/src/config/config.ts @@ -5,12 +5,11 @@ import mergeOptions from 'merge-options' import { object } from 'yup' import { validateAddressManagerConfig } from '../address-manager/utils.js' import { validateConnectionManagerConfig } from '../connection-manager/utils.js' -import type { AddressManagerInit } from '../address-manager' import type { ConnectionManagerInit } from '../connection-manager/index.js' import type { Libp2pInit } from '../index.js' import type { ServiceMap, RecursivePartial } from '@libp2p/interface' -const DefaultConfig: Partial = { +const defaultConfig: Partial = { connectionManager: { resolvers: { dnsaddr: dnsaddrResolver @@ -24,13 +23,13 @@ const DefaultConfig: Partial = { export function validateConfig> (opts: RecursivePartial>): Libp2pInit { const libp2pConfig = object({ - addresses: validateAddressManagerConfig(opts?.addresses as AddressManagerInit), + addresses: validateAddressManagerConfig(), connectionManager: validateConnectionManagerConfig(opts?.connectionManager as ConnectionManagerInit) }) const parsedOpts = libp2pConfig.validateSync(opts) - const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, parsedOpts) + const resultingOptions: Libp2pInit = mergeOptions(defaultConfig, parsedOpts) return resultingOptions } diff --git a/packages/libp2p/src/config/helpers.ts b/packages/libp2p/src/config/helpers.ts index 0cf87f152e..fa0d99b274 100644 --- a/packages/libp2p/src/config/helpers.ts +++ b/packages/libp2p/src/config/helpers.ts @@ -3,6 +3,10 @@ import { multiaddr } from '@multiformats/multiaddr' import { codes } from '../errors.js' export const validateMultiaddr = (value: Array | undefined): boolean => { + if (value == null || value === undefined) { + return false + } + value?.forEach((addr) => { try { multiaddr(addr) @@ -10,5 +14,6 @@ export const validateMultiaddr = (value: Array | undefined): throw new CodeError(`invalid multiaddr: ${addr}`, codes.ERR_INVALID_MULTIADDR) } }) + return true } diff --git a/packages/libp2p/src/connection-manager/utils.ts b/packages/libp2p/src/connection-manager/utils.ts index bb95c73b9a..917dc3df0c 100644 --- a/packages/libp2p/src/connection-manager/utils.ts +++ b/packages/libp2p/src/connection-manager/utils.ts @@ -78,11 +78,11 @@ export const validateConnectionManagerConfig = (opts: ConnectionManagerInit): Ob autoDialConcurrency: number().integer().min(0).default(AUTO_DIAL_CONCURRENCY), autoDialPriority: number().integer().min(0).default(AUTO_DIAL_PRIORITY), maxParallelDials: number().integer().min(0).default(MAX_PARALLEL_DIALS), - maxPeerAddrsToDialed: number().min(0).integer().default(MAX_PEER_ADDRS_TO_DIAL), + maxPeerAddrsToDialed: number().integer().min(0).default(MAX_PEER_ADDRS_TO_DIAL), dialTimeout: number().integer().min(0).default(DIAL_TIMEOUT), inboundUpgradeTimeout: number().integer().min(0).default(INBOUND_UPGRADE_TIMEOUT), - allow: array().of(string()).test('is multiaddr', validateMultiaddr).optional(), - deny: array().of(string()).test('is multiaddr', validateMultiaddr).optional(), + allow: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), + deny: array().of(string()).test('is multiaddr', validateMultiaddr).default([]), inboundConnectionThreshold: number().integer().min(0).default(INBOUND_CONNECTION_THRESHOLD), maxIncomingPendingConnections: number().integer().min(0).default(MAX_INCOMING_PENDING_CONNECTIONS) }) diff --git a/packages/protocol-dcutr/src/constants.ts b/packages/protocol-dcutr/src/constants.ts new file mode 100644 index 0000000000..e5d4debba5 --- /dev/null +++ b/packages/protocol-dcutr/src/constants.ts @@ -0,0 +1,12 @@ +// https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#rpc-messages +export const MAX_DCUTR_MESSAGE_SIZE = 1024 * 4 +// ensure the dial has a high priority to jump to the head of the dial queue +export const DCUTR_DIAL_PRIORITY = 100 + +export const DEFAULT_MAX_INBOUND_STREAMS = 1 + +export const DEFAULT_MAX_OUTBOUND_STREAMS = 1 + +export const DEFAULT_TIMEOUT = 5000 + +export const DEFAULT_RETRIES = 3 diff --git a/packages/protocol-dcutr/src/dcutr.ts b/packages/protocol-dcutr/src/dcutr.ts index 6a44a7ed02..fad6358bb0 100644 --- a/packages/protocol-dcutr/src/dcutr.ts +++ b/packages/protocol-dcutr/src/dcutr.ts @@ -3,6 +3,7 @@ import { type Multiaddr, multiaddr } from '@multiformats/multiaddr' import delay from 'delay' import { pbStream } from 'it-protobuf-stream' import { number, object } from 'yup' +import { DCUTR_DIAL_PRIORITY, DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS, DEFAULT_RETRIES, DEFAULT_TIMEOUT, MAX_DCUTR_MESSAGE_SIZE } from './constants.js' import { HolePunch } from './pb/message.js' import { isPublicAndDialable } from './utils.js' import { multicodec } from './index.js' @@ -10,18 +11,13 @@ import type { DCUtRServiceComponents, DCUtRServiceInit } from './index.js' import type { Logger, Connection, Stream, PeerStore, Startable } from '@libp2p/interface' import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal' -// https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#rpc-messages -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 configValidator = object({ // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L27 - timeout: number().integer().default(5000).min(0), + timeout: number().integer().min(0).default(DEFAULT_TIMEOUT), // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/holepunch/holepuncher.go#L28 - retries: number().integer().default(3).min(0), - maxInboundStreams: number().integer().default(1).min(0), - maxOutboundStreams: number().integer().default(1).min(0) + retries: number().integer().min(0).default(DEFAULT_RETRIES), + maxInboundStreams: number().integer().min(0).default(DEFAULT_MAX_INBOUND_STREAMS), + maxOutboundStreams: number().integer().min(0).default(DEFAULT_MAX_OUTBOUND_STREAMS) }) export class DefaultDCUtRService implements Startable { diff --git a/packages/protocol-fetch/src/constants.ts b/packages/protocol-fetch/src/constants.ts index c55751f43c..ad6769121b 100644 --- a/packages/protocol-fetch/src/constants.ts +++ b/packages/protocol-fetch/src/constants.ts @@ -5,3 +5,4 @@ export const PROTOCOL_NAME = 'fetch' export const MAX_INBOUND_STREAMS = 1 export const MAX_OUTBOUND_STREAMS = 1 export const TIMEOUT = 10000 +export const PROTOCOL_PREFIX = 'libp2p' diff --git a/packages/protocol-fetch/src/fetch.ts b/packages/protocol-fetch/src/fetch.ts index 4eb4ca683c..f23ed8c6dc 100644 --- a/packages/protocol-fetch/src/fetch.ts +++ b/packages/protocol-fetch/src/fetch.ts @@ -2,8 +2,8 @@ import { CodeError, ERR_INVALID_MESSAGE, ERR_INVALID_PARAMETERS, ERR_TIMEOUT, se import { pbStream } from 'it-protobuf-stream' import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { toString as uint8arrayToString } from 'uint8arrays/to-string' -import { object, number } from 'yup' -import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_VERSION, TIMEOUT } from './constants.js' +import { object, number, string } from 'yup' +import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION, TIMEOUT } from './constants.js' import { FetchRequest, FetchResponse } from './pb/proto.js' import type { Fetch as FetchInterface, FetchComponents, FetchInit, LookupFunction } from './index.js' import type { AbortOptions, Logger, Stream, PeerId, Startable } from '@libp2p/interface' @@ -19,8 +19,10 @@ import type { IncomingStreamData } from '@libp2p/interface-internal' const configValidator = object({ timeout: number().integer().default(TIMEOUT), maxInboundStreams: number().integer().min(0).default(MAX_INBOUND_STREAMS), - maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS) + maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS), + protocolPrefix: string().default(PROTOCOL_PREFIX) }) + export class Fetch implements Startable, FetchInterface { public readonly protocol: string private readonly components: FetchComponents @@ -40,7 +42,7 @@ export class Fetch implements Startable, FetchInterface { const config = configValidator.validateSync(init) - this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}` + this.protocol = `/${config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}` this.timeout = config.timeout this.maxInboundStreams = config.maxInboundStreams this.maxOutboundStreams = config.maxOutboundStreams diff --git a/packages/protocol-identify/src/identify.ts b/packages/protocol-identify/src/identify.ts index a3934f0641..6370031011 100644 --- a/packages/protocol-identify/src/identify.ts +++ b/packages/protocol-identify/src/identify.ts @@ -20,9 +20,9 @@ import { MAX_OUTBOUND_STREAMS, MAX_IDENTIFY_MESSAGE_SIZE, TIMEOUT, - RUN_ON_CONNECTION_OPEN, + RUN_ON_CONNECTION_OPEN as DEFAULT_RUN_ON_CONNECTION_OPEN, PROTOCOL_PREFIX, - RUN_ON_TRANSIENT_CONNECTION, + RUN_ON_TRANSIENT_CONNECTION as DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS, MAX_PUSH_INCOMING_STREAMS, MAX_PUSH_OUTGOING_STREAMS, MAX_OBSERVED_ADDRESSES @@ -41,8 +41,8 @@ const configValidator = object({ maxPushOutgoingStreams: number().integer().min(0).default(MAX_PUSH_OUTGOING_STREAMS), maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS), maxObservedAddresses: number().integer().min(0).default(MAX_OBSERVED_ADDRESSES), - runOnConnectionOpen: boolean().default(RUN_ON_CONNECTION_OPEN), - runOnTransientConnection: boolean().default(RUN_ON_TRANSIENT_CONNECTION) + runOnConnectionOpen: boolean().default(DEFAULT_RUN_ON_CONNECTION_OPEN), + runOnTransientConnection: boolean().default(DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS) }) export class Identify implements Startable, IdentifyInterface { diff --git a/packages/protocol-ping/src/constants.ts b/packages/protocol-ping/src/constants.ts index 0e16b6b72b..6808799939 100644 --- a/packages/protocol-ping/src/constants.ts +++ b/packages/protocol-ping/src/constants.ts @@ -13,5 +13,6 @@ export const TIMEOUT = 10000 // opening stream A even though the dialing peer is opening stream B and closing stream A). export const MAX_INBOUND_STREAMS = 2 export const MAX_OUTBOUND_STREAMS = 1 +export const DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS = true export const ERR_WRONG_PING_ACK = 'ERR_WRONG_PING_ACK' diff --git a/packages/protocol-ping/src/ping.ts b/packages/protocol-ping/src/ping.ts index b1f69f2926..1f7a79b2fa 100644 --- a/packages/protocol-ping/src/ping.ts +++ b/packages/protocol-ping/src/ping.ts @@ -4,7 +4,7 @@ import first from 'it-first' import { pipe } from 'it-pipe' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' import { boolean, number, object, string } from 'yup' -import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, ERR_WRONG_PING_ACK } from './constants.js' +import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, ERR_WRONG_PING_ACK, DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS } from './constants.js' import type { PingServiceComponents, PingServiceInit, PingService as PingServiceInterface } from './index.js' import type { AbortOptions, Logger, Stream, PeerId, Startable } from '@libp2p/interface' import type { IncomingStreamData } from '@libp2p/interface-internal' @@ -15,7 +15,7 @@ const configValidator = object({ timeout: number().integer().min(0).default(TIMEOUT), maxInboundStreams: number().integer().min(0).default(MAX_INBOUND_STREAMS), maxOutboundStreams: number().integer().min(0).default(MAX_OUTBOUND_STREAMS), - runOnTransientConnection: boolean().default(true) + runOnTransientConnection: boolean().default(DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS) }) export class PingService implements Startable, PingServiceInterface { diff --git a/packages/transport-circuit-relay-v2/src/constants.ts b/packages/transport-circuit-relay-v2/src/constants.ts index 10526ae442..7d35a7d707 100644 --- a/packages/transport-circuit-relay-v2/src/constants.ts +++ b/packages/transport-circuit-relay-v2/src/constants.ts @@ -42,6 +42,14 @@ export const RELAY_SOURCE_TAG = 'circuit-relay-source' export const RELAY_TAG = 'circuit-relay-relay' +export const DEFAULT_DEFAULT_APPLY_LIMIT = true + +export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 10000 + +export const DEFAULT_MAX_RESERVATION_QUEUE_LENGTH = 100 + +export const DEFAULT_DISCOVER_RELAYS = 0 + // circuit v2 connection limits // https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/resources.go#L61-L66 diff --git a/packages/transport-circuit-relay-v2/src/server/index.ts b/packages/transport-circuit-relay-v2/src/server/index.ts index 3b25e7b8d5..109a5cd71c 100644 --- a/packages/transport-circuit-relay-v2/src/server/index.ts +++ b/packages/transport-circuit-relay-v2/src/server/index.ts @@ -4,16 +4,12 @@ import { RecordEnvelope } from '@libp2p/peer-record' import { type Multiaddr, multiaddr } from '@multiformats/multiaddr' import { pbStream, type ProtobufStream } from 'it-protobuf-stream' import pDefer from 'p-defer' -import { object, number, boolean } from 'yup' +import { object, number } from 'yup' import { CIRCUIT_PROTO_CODE, - DEFAULT_DURATION_LIMIT, DEFAULT_HOP_TIMEOUT, DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS, - DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, - DEFAULT_MAX_RESERVATION_STORE_SIZE, - DEFAULT_MAX_RESERVATION_TTL, MAX_CONNECTIONS, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, @@ -22,7 +18,7 @@ import { import { HopMessage, type Reservation, Status, StopMessage } from '../pb/index.js' import { createLimitedRelay } from '../utils.js' import { AdvertService, type AdvertServiceComponents, type AdvertServiceInit } from './advert-service.js' -import { ReservationStore, type ReservationStoreInit } from './reservation-store.js' +import { ReservationStore, reservationStoreConfigValidator, type ReservationStoreInit } from './reservation-store.js' import { ReservationVoucherRecord } from './reservation-voucher.js' import type { CircuitRelayService, RelayReservation } from '../index.js' import type { ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, Startable } from '@libp2p/interface' @@ -95,13 +91,7 @@ export interface RelayServerEvents { const configValidator = object({ hopTimeout: number().integer().min(0).default(DEFAULT_HOP_TIMEOUT), - reservations: object({ - maxReservations: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_STORE_SIZE), - reservationClearInterval: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL), - applyDefaultLimit: boolean().default(false), - reservationTtl: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_TTL), - defaultDurationLimit: number().integer().min(0).default(DEFAULT_DURATION_LIMIT) - }), + reservations: reservationStoreConfigValidator, maxInboundHopStreams: number().integer().min(0).default(DEFAULT_MAX_INBOUND_STREAMS), maxOutboundHopStreams: number().integer().min(0).default(DEFAULT_MAX_OUTBOUND_STREAMS), maxOutboundStopStreams: number().integer().min(0).default(MAX_CONNECTIONS) diff --git a/packages/transport-circuit-relay-v2/src/server/reservation-store.ts b/packages/transport-circuit-relay-v2/src/server/reservation-store.ts index 873c8a2972..293aa21bf8 100644 --- a/packages/transport-circuit-relay-v2/src/server/reservation-store.ts +++ b/packages/transport-circuit-relay-v2/src/server/reservation-store.ts @@ -1,6 +1,6 @@ import { PeerMap } from '@libp2p/peer-collections' import { object, mixed, number, boolean } from 'yup' -import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js' +import { DEFAULT_DATA_LIMIT, DEFAULT_DEFAULT_APPLY_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js' import { type Limit, Status } from '../pb/index.js' import type { RelayReservation } from '../index.js' import type { RecursivePartial, PeerId, Startable } from '@libp2p/interface' @@ -37,13 +37,13 @@ export interface ReservationStoreInit { export type ReservationStoreOptions = RecursivePartial -const configValidator = object({ +export const reservationStoreConfigValidator = object({ maxReservations: number().min(0).integer().default(DEFAULT_MAX_RESERVATION_STORE_SIZE), reservationClearInterval: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL), - applyDefaultLimit: boolean().default(true), + applyDefaultLimit: boolean().default(DEFAULT_DEFAULT_APPLY_LIMIT), reservationTtl: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_TTL), defaultDurationLimit: number().integer().min(0).default(DEFAULT_DURATION_LIMIT), - defaultDataLimit: mixed().test('is-bigint', 'Invalid bigint', value => typeof value === 'bigint').default(DEFAULT_DATA_LIMIT) + defaultDataLimit: mixed().test('is-bigint', 'Invalid bigint', value => typeof value === 'bigint').default(DEFAULT_DATA_LIMIT) }) export class ReservationStore implements Startable { public readonly reservations = new PeerMap() @@ -57,14 +57,14 @@ export class ReservationStore implements Startable { private readonly defaultDataLimit: bigint constructor (options: ReservationStoreOptions = {}) { - const config = configValidator.validateSync(options) + const config = reservationStoreConfigValidator.validateSync(options) this.maxReservations = config.maxReservations this.reservationClearInterval = config.reservationClearInterval this.applyDefaultLimit = config.applyDefaultLimit this.reservationTtl = config.reservationTtl this.defaultDurationLimit = config.defaultDurationLimit - this.defaultDataLimit = config.defaultDataLimit as bigint + this.defaultDataLimit = config.defaultDataLimit } isStarted (): boolean { diff --git a/packages/transport-circuit-relay-v2/src/transport/reservation-store.ts b/packages/transport-circuit-relay-v2/src/transport/reservation-store.ts index 2e56a142bf..45c83b5a29 100644 --- a/packages/transport-circuit-relay-v2/src/transport/reservation-store.ts +++ b/packages/transport-circuit-relay-v2/src/transport/reservation-store.ts @@ -5,7 +5,7 @@ import { multiaddr } from '@multiformats/multiaddr' import { pbStream } from 'it-protobuf-stream' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' import { number, object } from 'yup' -import { DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js' +import { DEFAULT_DISCOVER_RELAYS, DEFAULT_MAX_RESERVATION_QUEUE_LENGTH, DEFAULT_RESERVATION_COMPLETION_TIMEOUT, DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js' import { HopMessage, Status } from '../pb/index.js' import { getExpirationMilliseconds } from '../utils.js' import type { Reservation } from '../pb/index.js' @@ -71,9 +71,9 @@ export interface ReservationStoreEvents { } const configValidator = object({ - discoverRelays: number().integer().min(0).default(0), - maxReservationQueueLength: number().integer().min(0).default(100), - reservationCompletionTimeout: number().integer().min(0).default(10000), + discoverRelays: number().integer().min(0).default(DEFAULT_DISCOVER_RELAYS), + maxReservationQueueLength: number().integer().min(0).default(DEFAULT_MAX_RESERVATION_QUEUE_LENGTH), + reservationCompletionTimeout: number().integer().min(0).default(DEFAULT_RESERVATION_COMPLETION_TIMEOUT), reservationConcurrency: number().integer().min(0).default(DEFAULT_RESERVATION_CONCURRENCY) }) diff --git a/packages/transport-circuit-relay-v2/src/transport/transport.ts b/packages/transport-circuit-relay-v2/src/transport/transport.ts index c385cf5f9a..13972c194d 100644 --- a/packages/transport-circuit-relay-v2/src/transport/transport.ts +++ b/packages/transport-circuit-relay-v2/src/transport/transport.ts @@ -5,7 +5,7 @@ import * as mafmt from '@multiformats/mafmt' import { multiaddr } from '@multiformats/multiaddr' import { pbStream } from 'it-protobuf-stream' import { object, number } from 'yup' -import { CIRCUIT_PROTO_CODE, DEFAULT_STOP_TIMEOUT, ERR_HOP_REQUEST_FAILED, ERR_RELAYED_DIAL, MAX_CONNECTIONS, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js' +import { CIRCUIT_PROTO_CODE, DEFAULT_DISCOVER_RELAYS, DEFAULT_STOP_TIMEOUT, ERR_HOP_REQUEST_FAILED, ERR_RELAYED_DIAL, MAX_CONNECTIONS, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js' import { StopMessage, HopMessage, Status } from '../pb/index.js' import { RelayDiscovery } from './discovery.js' import { createListener } from './listener.js' @@ -40,7 +40,7 @@ interface ConnectOptions { } const configValidator = object({ - discoverRelays: number().min(0).integer().default(0), + discoverRelays: number().min(0).integer().default(DEFAULT_DISCOVER_RELAYS), maxInboundStopStreams: number().min(0).integer().default(MAX_CONNECTIONS), maxOutboundStopStreams: number().min(0).integer().default(MAX_CONNECTIONS), stopTimeout: number().min(0).integer().default(DEFAULT_STOP_TIMEOUT) diff --git a/packages/upnp-nat/src/upnp-nat.ts b/packages/upnp-nat/src/upnp-nat.ts index b03cd0335c..93ba2b96b5 100644 --- a/packages/upnp-nat/src/upnp-nat.ts +++ b/packages/upnp-nat/src/upnp-nat.ts @@ -10,6 +10,8 @@ import type { Logger, Startable } from '@libp2p/interface' const DEFAULT_TTL = 7200 +const DEFAULT_KEEP_ALIVE = true + function highPort (min = 1024, max = 65535): number { return Math.floor(Math.random() * (max - min + 1) + min) } @@ -21,7 +23,7 @@ const configValidator = object({ localAddress: string().matches(validIPRegex, 'Invalid IP address'), description: string(), ttl: number().integer().min(0).default(DEFAULT_TTL), - keepAlive: boolean().default(true), + keepAlive: boolean().default(DEFAULT_KEEP_ALIVE), gateway: string().optional() }) @@ -31,7 +33,7 @@ export class UPnPNAT implements Startable { private readonly localAddress?: string private readonly description: string private readonly ttl: number - private readonly keepAlive?: boolean + private readonly keepAlive: boolean private readonly gateway?: string private started: boolean private client?: NatAPI