@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
190 lines (189 loc) • 7.27 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connection = exports.TCP = exports.TCPModes = void 0;
const TCPs = __importStar(require("./TCP/index.js"));
const index_js_1 = require("../session/index.js");
const helpers_js_1 = require("../helpers.js");
const Logger_js_1 = require("../Logger.js");
const platform_node_js_1 = require("../platform.node.js");
const index_js_2 = require("../errors/index.js");
exports.TCPModes = {
0: TCPs.TCPFull,
1: TCPs.TCPAbridged,
2: TCPs.TCPIntermediate,
3: TCPs.TCPPaddedIntermediate,
4: TCPs.TCPAbridgedO,
5: TCPs.TCPIntermediateO,
};
var TCP;
(function (TCP) {
TCP[TCP["TCPFull"] = 0] = "TCPFull";
TCP[TCP["TCPAbridged"] = 1] = "TCPAbridged";
TCP[TCP["TCPIntermediate"] = 2] = "TCPIntermediate";
TCP[TCP["TCPPaddedIntermediate"] = 3] = "TCPPaddedIntermediate";
TCP[TCP["TCPAbridgedO"] = 4] = "TCPAbridgedO";
TCP[TCP["TCPIntermediateO"] = 5] = "TCPIntermediateO";
})(TCP || (exports.TCP = TCP = {}));
class Connection {
maxRetries;
_dcId;
_test;
_proxy;
_media;
_mode;
_address;
_protocol;
_connected;
_local;
constructor(dcId, test, ipv6, proxy, media = false, mode = TCP.TCPFull, local = (platform_node_js_1.isBrowser && globalThis && globalThis.location.protocol !== 'https:') || true) {
this.maxRetries = 3;
this._dcId = dcId;
this._test = test;
this._proxy = proxy;
this._media = media;
this._mode = mode;
this._address = index_js_1.DataCenter.DataCenter(dcId, test, ipv6, media);
this._local = local;
this._connected = false;
}
async connect() {
if (this._protocol && this._connected) {
throw new index_js_2.ClientError.ClientReady();
}
for (let i = 0; i < this.maxRetries; i++) {
if (((this._proxy &&
'server' in this._proxy &&
'port' in this._proxy &&
'secret' in this._proxy) ||
platform_node_js_1.isBrowser) &&
this._mode !== TCP.TCPAbridgedO &&
this._mode !== TCP.TCPIntermediateO) {
if (this._proxy &&
'server' in this._proxy &&
'port' in this._proxy &&
'secret' in this._proxy) {
const secret = (0, helpers_js_1.normalizeSecretString)(this._proxy.secret);
if (secret[0] === 0xdd) {
this._mode = TCP.TCPIntermediateO;
}
else {
this._mode = TCP.TCPAbridgedO;
}
}
else {
this._mode = TCP.TCPAbridgedO;
}
}
this._protocol = new exports.TCPModes[this._mode]();
try {
Logger_js_1.Logger.debug(`[1] Connecting to DC${this._dcId} with ${this._protocol.constructor.name}`);
await this._protocol.connect(this._address[0], platform_node_js_1.isBrowser ? (this._local ? 80 : this._address[1]) : this._address[1], this._proxy, this._dcId + (this._test ? 10000 : 0) * (this._media ? -1 : 1));
this._connected = true;
break;
}
catch (error) {
Logger_js_1.Logger.error(`[106] Got error when trying connecting to telegram :`, error);
this._protocol.close();
await (0, helpers_js_1.sleep)(2000);
}
}
if (!this._connected) {
throw new index_js_2.ClientError.ClientFailed();
}
return this._connected;
}
async close() {
if (!this._protocol || !this._connected) {
throw new index_js_2.ClientError.ClientNotReady();
}
this._connected = false;
await (0, helpers_js_1.sleep)(10);
await this._protocol.close();
}
async send(data) {
Logger_js_1.Logger.debug(`[2] Sending ${platform_node_js_1.Buffer.byteLength(data)} bytes data.`);
await this._protocol.send(data);
}
async recv() {
if (!this._connected) {
throw new index_js_2.ClientError.ClientDisconnected();
}
return await this._protocol.recv();
}
[Symbol.for('nodejs.util.inspect.custom')]() {
const toPrint = {
_: this.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_') && value !== undefined && value !== null) {
toPrint[key] = value;
}
}
}
return toPrint;
}
[Symbol.for('Deno.customInspect')]() {
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
}
toJSON() {
const toPrint = {
_: this.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_') && value !== undefined && value !== null) {
if (typeof value === 'bigint') {
toPrint[key] = String(value);
}
else if (Array.isArray(value)) {
toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
}
else {
toPrint[key] = value;
}
}
}
}
return toPrint;
}
toString() {
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
}
}
exports.Connection = Connection;