UNPKG

@tgsnake/core

Version:

Pure Telegram MTProto library for nodejs

78 lines (77 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringSession = void 0; const Logger_js_1 = require("../Logger.js"); const Session_js_1 = require("./Session.js"); const index_js_1 = require("../raw/index.js"); const helpers_js_1 = require("../helpers.js"); const platform_node_js_1 = require("../platform.node.js"); class StringSession extends Session_js_1.BaseSession { constructor(session) { super(); if (session.length) { Logger_js_1.Logger.debug(`[81] Starting parsing string session, length is: ${session.length}`); const start = Math.floor(Date.now() / 1000); if (session[0] === '1') { Logger_js_1.Logger.debug(`[82] The string session look like telethon or gramjs string session, start parsing.`); session = session.slice(1); const bytes = new index_js_1.BytesIO(platform_node_js_1.Buffer.from((0, helpers_js_1.base64urlTobase64)(session), 'base64')); this._dcId = bytes.read(1).readUInt8(); Logger_js_1.Logger.debug(`[84] Found dcId: ${this._dcId}.`); if (session.length === 352) { const ipv4 = bytes.read(4); this._ip = `${ipv4[0]}.${ipv4[1]}.${ipv4[2]}.${ipv4[3]}`; Logger_js_1.Logger.debug(`[85] Found ip: ${this._ip}.`); } else { const serverAddressLen = bytes.read(2).readInt16BE(); if (serverAddressLen > 1000) { bytes.seek(-2, 1); this._ip = bytes .read(16) .toString('hex') .match(/.{1,4}/g) .map((val) => val.replace(/^0+/, '')) .join(':') .replace(/0000\:/g, ':') .replace(/:{2,}/g, '::'); Logger_js_1.Logger.debug(`[86] Found ip: ${this._ip}.`); } else { this._ip = bytes.read(serverAddressLen).toString(); Logger_js_1.Logger.debug(`[87] Found ip: ${this._ip}.`); } } this._port = bytes.read(2).readInt16BE(); Logger_js_1.Logger.debug(`[88] Found port: ${this._port}.`); this._authKey = bytes.read(); Logger_js_1.Logger.debug(`[89] Found authKey: ${platform_node_js_1.Buffer.byteLength(this._authKey)} bytes.`); } else { const bytes = platform_node_js_1.Buffer.from((0, helpers_js_1.base64urlTobase64)(session), 'base64'); if (platform_node_js_1.Buffer.byteLength(bytes) === 271) { Logger_js_1.Logger.debug(`[90] The string session look like pyrogram or tgsnake string session, start parsing.`); Logger_js_1.Logger.debug(`[91] String session have a ${platform_node_js_1.Buffer.byteLength(bytes)} bytes`); this._dcId = bytes.readUInt8(0); Logger_js_1.Logger.debug(`[92] Found dcId: ${this._dcId}.`); this._apiId = bytes.readUInt32LE(1); Logger_js_1.Logger.debug(`[93] Found apiId: ${this._apiId}.`); this._testMode = bytes.readUInt8(5) ? true : false; Logger_js_1.Logger.debug(`[94] Found testMode: ${this._testMode}.`); this._authKey = bytes.subarray(6, 262); Logger_js_1.Logger.debug(`[95] Found authKey: ${platform_node_js_1.Buffer.byteLength(this._authKey)} bytes.`); this._userId = BigInt(`0x${bytes.subarray(262, 270).toString('hex')}`); Logger_js_1.Logger.debug(`[96] Found userId: ${this._userId}.`); this._isBot = bytes.readUInt8(270) ? true : false; Logger_js_1.Logger.debug(`[97] Found isBot: ${this._isBot}.`); Logger_js_1.Logger.debug(`[98] Done parsing string session (${Math.floor(Date.now() / 1000) - start}s)`); } else { Logger_js_1.Logger.error(`[99] Can't parsing ${platform_node_js_1.Buffer.byteLength(bytes)} bytes of string session, we only supported string session from telethon,gramjs,tgsnake, and pyrogram (latest version, old version doesn't supported)`); throw new Error(`Invalid String Session`); } } } } } exports.StringSession = StringSession;