Skip to content

Commit d3e5a33

Browse files
authored
fix: increase max autonat streams and limit incoming message size (#2890)
Allows more outgoing streams to be open at the same time and limits the allowed incoming message size.
1 parent 92f9acb commit d3e5a33

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

packages/protocol-autonat/src/autonat.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import { multiaddr, protocols } from '@multiformats/multiaddr'
1010
import { anySignal } from 'any-signal'
1111
import { pbStream } from 'it-protobuf-stream'
1212
import * as Digest from 'multiformats/hashes/digest'
13-
import {
14-
DEFAULT_CONNECTION_THRESHOLD,
15-
MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION, TIMEOUT
16-
} from './constants.js'
13+
import { DEFAULT_CONNECTION_THRESHOLD, MAX_INBOUND_STREAMS, MAX_MESSAGE_SIZE, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION, TIMEOUT } from './constants.js'
1714
import { Message } from './pb/index.js'
1815
import type { AutoNATComponents, AutoNATServiceInit } from './index.js'
1916
import type { Logger, Connection, PeerId, Startable, AbortOptions } from '@libp2p/interface'
@@ -89,6 +86,7 @@ export class AutoNATService implements Startable {
8986
private readonly timeout: number
9087
private readonly maxInboundStreams: number
9188
private readonly maxOutboundStreams: number
89+
private readonly maxMessageSize: number
9290
private started: boolean
9391
private readonly log: Logger
9492
private topologyId?: string
@@ -106,6 +104,7 @@ export class AutoNATService implements Startable {
106104
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
107105
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
108106
this.connectionThreshold = init.connectionThreshold ?? DEFAULT_CONNECTION_THRESHOLD
107+
this.maxMessageSize = init.maxMessageSize ?? MAX_MESSAGE_SIZE
109108
this.dialResults = new Map()
110109
this.findPeers = repeatingTask(this.findRandomPeers.bind(this), 60_000)
111110
this.addressFilter = createScalableCuckooFilter(1024)
@@ -229,7 +228,9 @@ export class AutoNATService implements Startable {
229228
const signal = AbortSignal.timeout(this.timeout)
230229
setMaxListeners(Infinity, signal)
231230

232-
const messages = pbStream(data.stream).pb(Message)
231+
const messages = pbStream(data.stream, {
232+
maxDataLength: this.maxMessageSize
233+
}).pb(Message)
233234

234235
try {
235236
const request = await messages.read({

packages/protocol-autonat/src/constants.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const PROTOCOL_NAME = 'autonat'
1313
*/
1414
export const PROTOCOL_VERSION = '1.0.0'
1515
export const TIMEOUT = 30000
16-
export const MAX_INBOUND_STREAMS = 1
17-
export const MAX_OUTBOUND_STREAMS = 1
16+
export const MAX_INBOUND_STREAMS = 2
17+
export const MAX_OUTBOUND_STREAMS = 20
1818
export const DEFAULT_CONNECTION_THRESHOLD = 80
19+
export const MAX_MESSAGE_SIZE = 8192

packages/protocol-autonat/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ export interface AutoNATServiceInit {
7575
* @default 80
7676
*/
7777
connectionThreshold?: number
78+
79+
/**
80+
* How large incoming autonat messages are allowed to be in bytes. If messages
81+
* larger than this are received the stream will be reset.
82+
*
83+
* @default 8192
84+
*/
85+
maxMessageSize?: number
7886
}
7987

8088
export interface AutoNATComponents {

0 commit comments

Comments
 (0)