Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: increase max autonat streams and limit incoming message size #2890

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading