From cae7f90c574ce4f10d06886b4737c66db5bb9cbd Mon Sep 17 00:00:00 2001 From: teidesu Date: Sun, 9 May 2021 19:34:25 +0300 Subject: [PATCH] fix(core): payload size limit --- .../src/methods/messages/get-messages.ts | 22 +++++++++++-------- .../core/src/network/telegram-connection.ts | 17 +++++++++++++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/client/src/methods/messages/get-messages.ts b/packages/client/src/methods/messages/get-messages.ts index 9124ec6e..f8002735 100644 --- a/packages/client/src/methods/messages/get-messages.ts +++ b/packages/client/src/methods/messages/get-messages.ts @@ -1,6 +1,9 @@ import { TelegramClient } from '../../client' import { MaybeArray } from '@mtcute/core' -import { createUsersChatsIndex, normalizeToInputChannel } from '../../utils/peer-utils' +import { + createUsersChatsIndex, + normalizeToInputChannel, +} from '../../utils/peer-utils' import { tl } from '@mtcute/tl' import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' @@ -53,16 +56,17 @@ export async function getMessages( const isSingle = !Array.isArray(messageIds) if (isSingle) messageIds = [messageIds as number] - const ids = (messageIds as number[]).map( - (it) => - ({ - _: fromReply ? 'inputMessageReplyTo' : 'inputMessageID', - id: it, - } as tl.TypeInputMessage) - ) + const type = fromReply ? 'inputMessageReplyTo' : 'inputMessageID' + const ids: tl.TypeInputMessage[] = (messageIds as number[]).map((it) => ({ + _: type, + id: it, + })) const res = await this.call( - peer._ === 'inputPeerChannel' || peer._ === 'inputChannel' + peer._ === 'inputPeerChannel' || + peer._ === 'inputChannel' || + peer._ === 'inputPeerChannelFromMessage' || + peer._ === 'inputChannelFromMessage' ? { _: 'channels.getMessages', id: ids, diff --git a/packages/core/src/network/telegram-connection.ts b/packages/core/src/network/telegram-connection.ts index 6dcee851..3d6cd46a 100644 --- a/packages/core/src/network/telegram-connection.ts +++ b/packages/core/src/network/telegram-connection.ts @@ -551,6 +551,21 @@ export class TelegramConnection extends PersistentConnection { stack = new Error().stack } + const content = typeof method === 'string' ? message! : method.message + if (content.length > 1044404) { + // if you send larger payloads, telegram will just close connection, + // and since we resend them, it will get resent after reconnection and + // that will be an endless loop of reconnections. we don't want that, + // and payloads this large are usually a sign of an error in the code. + const err = new Error(`Payload is too big (${content.length} > 1044404)`) + if (typeof method === 'string') { + throw err + } else { + // shouldn't happen, but whatever + method.promise.reject(err) + } + } + const promise = typeof method === 'string' ? createControllablePromise() @@ -572,7 +587,7 @@ export class TelegramConnection extends PersistentConnection { this._pendingRpcCalls[messageId.toString(16)] = pending const encrypted = await this._mtproto.encryptMessage( - typeof method === 'string' ? message! : method.message, + content, messageId, seqNo )