Skip to content

Commit

Permalink
fix: increase max autonat streams and limit incoming message size (#2890
Browse files Browse the repository at this point in the history
)

Allows more outgoing streams to be open at the same time and limits
the allowed incoming message size.
  • Loading branch information
achingbrain authored Dec 12, 2024
1 parent 92f9acb commit d3e5a33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
11 changes: 6 additions & 5 deletions packages/protocol-autonat/src/autonat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { multiaddr, protocols } from '@multiformats/multiaddr'
import { anySignal } from 'any-signal'
import { pbStream } from 'it-protobuf-stream'
import * as Digest from 'multiformats/hashes/digest'
import {
DEFAULT_CONNECTION_THRESHOLD,
MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION, TIMEOUT
} from './constants.js'
import { DEFAULT_CONNECTION_THRESHOLD, MAX_INBOUND_STREAMS, MAX_MESSAGE_SIZE, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION, TIMEOUT } from './constants.js'
import { Message } from './pb/index.js'
import type { AutoNATComponents, AutoNATServiceInit } from './index.js'
import type { Logger, Connection, PeerId, Startable, AbortOptions } from '@libp2p/interface'
Expand Down Expand Up @@ -89,6 +86,7 @@ export class AutoNATService implements Startable {
private readonly timeout: number
private readonly maxInboundStreams: number
private readonly maxOutboundStreams: number
private readonly maxMessageSize: number
private started: boolean
private readonly log: Logger
private topologyId?: string
Expand All @@ -106,6 +104,7 @@ export class AutoNATService implements Startable {
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
this.connectionThreshold = init.connectionThreshold ?? DEFAULT_CONNECTION_THRESHOLD
this.maxMessageSize = init.maxMessageSize ?? MAX_MESSAGE_SIZE
this.dialResults = new Map()
this.findPeers = repeatingTask(this.findRandomPeers.bind(this), 60_000)
this.addressFilter = createScalableCuckooFilter(1024)
Expand Down Expand Up @@ -229,7 +228,9 @@ export class AutoNATService implements Startable {
const signal = AbortSignal.timeout(this.timeout)
setMaxListeners(Infinity, signal)

const messages = pbStream(data.stream).pb(Message)
const messages = pbStream(data.stream, {
maxDataLength: this.maxMessageSize
}).pb(Message)

try {
const request = await messages.read({
Expand Down
5 changes: 3 additions & 2 deletions packages/protocol-autonat/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const PROTOCOL_NAME = 'autonat'
*/
export const PROTOCOL_VERSION = '1.0.0'
export const TIMEOUT = 30000
export const MAX_INBOUND_STREAMS = 1
export const MAX_OUTBOUND_STREAMS = 1
export const MAX_INBOUND_STREAMS = 2
export const MAX_OUTBOUND_STREAMS = 20
export const DEFAULT_CONNECTION_THRESHOLD = 80
export const MAX_MESSAGE_SIZE = 8192
8 changes: 8 additions & 0 deletions packages/protocol-autonat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export interface AutoNATServiceInit {
* @default 80
*/
connectionThreshold?: number

/**
* How large incoming autonat messages are allowed to be in bytes. If messages
* larger than this are received the stream will be reset.
*
* @default 8192
*/
maxMessageSize?: number
}

export interface AutoNATComponents {
Expand Down

0 comments on commit d3e5a33

Please sign in to comment.