@mtkruto/node 0.0.62 → 0.0.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/client/client.js +46 -21
- package/esm/client/client_abstract.js +2 -2
- package/esm/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/tgcrypto.js +7 -56
- package/esm/deps.js +1 -1
- package/esm/mod.js +17 -2
- package/esm/session/session.js +16 -0
- package/esm/session/session_local_storage.js +26 -0
- package/esm/session/session_memory.js +5 -0
- package/esm/transport/transport_abridged.js +69 -0
- package/esm/transport/transport_provider.js +4 -4
- package/esm/utilities/0_bigint.js +3 -0
- package/esm/utilities/1_password.js +140 -0
- package/package.json +1 -1
- package/script/client/client.js +68 -20
- package/script/client/client_abstract.js +2 -2
- package/script/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/tgcrypto.js +7 -79
- package/script/deps.js +1 -1
- package/script/mod.js +21 -5
- package/script/session/session.js +20 -0
- package/script/session/session_local_storage.js +30 -0
- package/script/session/session_memory.js +9 -0
- package/script/transport/transport_abridged.js +73 -0
- package/script/transport/transport_provider.js +4 -4
- package/script/utilities/0_bigint.js +5 -1
- package/script/utilities/1_password.js +174 -0
- package/types/client/client.d.ts +12 -6
- package/types/client/client_abstract.d.ts +4 -12
- package/types/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.d.ts +2 -0
- package/types/deps.d.ts +1 -1
- package/types/mod.d.ts +20 -2
- package/types/session/session.d.ts +8 -0
- package/types/session/session_local_storage.d.ts +7 -0
- package/types/session/session_memory.d.ts +5 -0
- package/types/transport/transport_abridged.d.ts +11 -0
- package/types/transport/transport_provider.d.ts +3 -2
- package/types/utilities/0_bigint.d.ts +1 -0
- package/types/utilities/1_password.d.ts +11 -0
- package/types/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.d.ts +0 -2
- /package/esm/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/mod.js +0 -0
- /package/script/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/mod.js +0 -0
- /package/types/deps/deno.land/x/{tgcrypto@0.1.1 → tgcrypto@0.1.3}/mod.d.ts +0 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
|
|
2
|
+
import { getObfuscationParameters } from "../utilities/1_obfuscation.js";
|
|
3
|
+
import { Transport } from "./transport.js";
|
|
4
|
+
export class TransportAbridged extends Transport {
|
|
5
|
+
constructor(connection, obfuscated = false) {
|
|
6
|
+
super();
|
|
7
|
+
Object.defineProperty(this, "connection", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: connection
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "obfuscated", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: obfuscated
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async initialize() {
|
|
21
|
+
if (!this.initialized) {
|
|
22
|
+
if (this.obfuscated) {
|
|
23
|
+
this.obfuscationParameters = await getObfuscationParameters(0xEFEFEFEF, this.connection);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
await this.connection.write(new Uint8Array([0xEF]));
|
|
27
|
+
}
|
|
28
|
+
this.initialized = true;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
throw new Error("Transport already initialized");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async receive() {
|
|
35
|
+
let length;
|
|
36
|
+
{
|
|
37
|
+
let buffer = new Uint8Array(1);
|
|
38
|
+
await this.connection.read(buffer);
|
|
39
|
+
buffer = this.decrypt(buffer);
|
|
40
|
+
if (buffer[0] < 0x7F) {
|
|
41
|
+
length = buffer[0];
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
let buffer = new Uint8Array(3);
|
|
45
|
+
await this.connection.read(buffer);
|
|
46
|
+
buffer = this.decrypt(buffer);
|
|
47
|
+
const dataView = new DataView(buffer.buffer);
|
|
48
|
+
length = dataView.getUint16(0, true);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
length *= 4;
|
|
52
|
+
let buffer = new Uint8Array(length);
|
|
53
|
+
await this.connection.read(buffer);
|
|
54
|
+
buffer = this.decrypt(buffer);
|
|
55
|
+
return buffer;
|
|
56
|
+
}
|
|
57
|
+
async send(buffer) {
|
|
58
|
+
if (!this.initialized) {
|
|
59
|
+
throw new Error("Transport not initialized");
|
|
60
|
+
}
|
|
61
|
+
const bufferLength = buffer.length / 4;
|
|
62
|
+
const header = new Uint8Array([bufferLength >= 0x7F ? 0x7F : bufferLength]);
|
|
63
|
+
const length = bufferLength >= 0x7F ? bufferFromBigInt(bufferLength, 3) : new Uint8Array();
|
|
64
|
+
await this.connection.write(this.encrypt(concat(header, length, buffer)));
|
|
65
|
+
}
|
|
66
|
+
deinitialize() {
|
|
67
|
+
this.initialized = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -11,13 +11,13 @@ const dcToNameMap = {
|
|
|
11
11
|
"4": "vesta",
|
|
12
12
|
"5": "flora",
|
|
13
13
|
};
|
|
14
|
-
export
|
|
15
|
-
return (cdn) => {
|
|
16
|
-
|
|
14
|
+
export function defaultTransportProvider(wss) {
|
|
15
|
+
return ({ dc, cdn }) => {
|
|
16
|
+
wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
|
|
17
17
|
const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
|
|
18
18
|
const connection = new ConnectionWebSocket(url);
|
|
19
19
|
const transport = new TransportIntermediate(connection, true);
|
|
20
20
|
const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
|
|
21
21
|
return { connection, transport, dcId };
|
|
22
22
|
};
|
|
23
|
-
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import * as dntShim from "../_dnt.shims.js";
|
|
2
|
+
import * as types from "../tl/2_types.js";
|
|
3
|
+
import { bufferFromBigInt, concat } from "./0_buffer.js";
|
|
4
|
+
import { sha256 } from "./0_hash.js";
|
|
5
|
+
import { bigIntFromBuffer, getRandomBigInt, mod, modExp } from "./0_bigint.js";
|
|
6
|
+
export function isSafePrime(primeBytes, g) {
|
|
7
|
+
// deno-fmt-ignore
|
|
8
|
+
const goodPrime = new Uint8Array([
|
|
9
|
+
0xC7, 0x1C, 0xAE, 0xB9, 0xC6, 0xB1, 0xC9, 0x04, 0x8E, 0x6C, 0x52, 0x2F,
|
|
10
|
+
0x70, 0xF1, 0x3F, 0x73, 0x98, 0x0D, 0x40, 0x23, 0x8E, 0x3E, 0x21, 0xC1,
|
|
11
|
+
0x49, 0x34, 0xD0, 0x37, 0x56, 0x3D, 0x93, 0x0F, 0x48, 0x19, 0x8A, 0x0A,
|
|
12
|
+
0xA7, 0xC1, 0x40, 0x58, 0x22, 0x94, 0x93, 0xD2, 0x25, 0x30, 0xF4, 0xDB,
|
|
13
|
+
0xFA, 0x33, 0x6F, 0x6E, 0x0A, 0xC9, 0x25, 0x13, 0x95, 0x43, 0xAE, 0xD4,
|
|
14
|
+
0x4C, 0xCE, 0x7C, 0x37, 0x20, 0xFD, 0x51, 0xF6, 0x94, 0x58, 0x70, 0x5A,
|
|
15
|
+
0xC6, 0x8C, 0xD4, 0xFE, 0x6B, 0x6B, 0x13, 0xAB, 0xDC, 0x97, 0x46, 0x51,
|
|
16
|
+
0x29, 0x69, 0x32, 0x84, 0x54, 0xF1, 0x8F, 0xAF, 0x8C, 0x59, 0x5F, 0x64,
|
|
17
|
+
0x24, 0x77, 0xFE, 0x96, 0xBB, 0x2A, 0x94, 0x1D, 0x5B, 0xCD, 0x1D, 0x4A,
|
|
18
|
+
0xC8, 0xCC, 0x49, 0x88, 0x07, 0x08, 0xFA, 0x9B, 0x37, 0x8E, 0x3C, 0x4F,
|
|
19
|
+
0x3A, 0x90, 0x60, 0xBE, 0xE6, 0x7C, 0xF9, 0xA4, 0xA4, 0xA6, 0x95, 0x81,
|
|
20
|
+
0x10, 0x51, 0x90, 0x7E, 0x16, 0x27, 0x53, 0xB5, 0x6B, 0x0F, 0x6B, 0x41,
|
|
21
|
+
0x0D, 0xBA, 0x74, 0xD8, 0xA8, 0x4B, 0x2A, 0x14, 0xB3, 0x14, 0x4E, 0x0E,
|
|
22
|
+
0xF1, 0x28, 0x47, 0x54, 0xFD, 0x17, 0xED, 0x95, 0x0D, 0x59, 0x65, 0xB4,
|
|
23
|
+
0xB9, 0xDD, 0x46, 0x58, 0x2D, 0xB1, 0x17, 0x8D, 0x16, 0x9C, 0x6B, 0xC4,
|
|
24
|
+
0x65, 0xB0, 0xD6, 0xFF, 0x9C, 0xA3, 0x92, 0x8F, 0xEF, 0x5B, 0x9A, 0xE4,
|
|
25
|
+
0xE4, 0x18, 0xFC, 0x15, 0xE8, 0x3E, 0xBE, 0xA0, 0xF8, 0x7F, 0xA9, 0xFF,
|
|
26
|
+
0x5E, 0xED, 0x70, 0x05, 0x0D, 0xED, 0x28, 0x49, 0xF4, 0x7B, 0xF9, 0x59,
|
|
27
|
+
0xD9, 0x56, 0x85, 0x0C, 0xE9, 0x29, 0x85, 0x1F, 0x0D, 0x81, 0x15, 0xF6,
|
|
28
|
+
0x35, 0xB1, 0x05, 0xEE, 0x2E, 0x4E, 0x15, 0xD0, 0x4B, 0x24, 0x54, 0xBF,
|
|
29
|
+
0x6F, 0x4F, 0xAD, 0xF0, 0x34, 0xB1, 0x04, 0x03, 0x11, 0x9C, 0xD8, 0xE3,
|
|
30
|
+
0xB9, 0x2F, 0xCC, 0x5B,
|
|
31
|
+
]);
|
|
32
|
+
if (goodPrime.every((v, i) => v == primeBytes[i])) {
|
|
33
|
+
if ([3, 4, 5, 7].includes(g)) { // It's good
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// H(data) := sha256(data)
|
|
40
|
+
export const h = sha256;
|
|
41
|
+
// SH(data, salt) := H(salt | data | salt)
|
|
42
|
+
export const sh = (data, salt) => h(concat(salt, data, salt));
|
|
43
|
+
// PH1(password, salt1, salt2) := SH(SH(password, salt1), salt2)
|
|
44
|
+
export const ph1 = async (password, salt1, salt2) => await sh(await sh(password, salt1), salt2);
|
|
45
|
+
export async function pbkdf2(password, salt, iterations) {
|
|
46
|
+
const key = await dntShim.dntGlobalThis.crypto.subtle.importKey("raw", password, "PBKDF2", false, ["deriveBits"]);
|
|
47
|
+
const buffer = await dntShim.dntGlobalThis.crypto.subtle.deriveBits({ name: "PBKDF2", salt, iterations, hash: "SHA-512" }, key, 512);
|
|
48
|
+
return new Uint8Array(buffer);
|
|
49
|
+
}
|
|
50
|
+
// PH2(password, salt1, salt2) := SH(pbkdf2(sha512, PH1(password, salt1, salt2), salt1, 100000), salt2)
|
|
51
|
+
export const ph2 = async (password, salt1, salt2) => await sh(await pbkdf2(await ph1(password, salt1, salt2), salt1, 100000), salt2);
|
|
52
|
+
export function isGoodModExpFirst(modexp, prime) {
|
|
53
|
+
const diff = prime - modexp;
|
|
54
|
+
const minDiffBitsCount = 2048 - 64;
|
|
55
|
+
const maxModExpSize = 256;
|
|
56
|
+
return !(diff < 0n ||
|
|
57
|
+
diff.toString(2).length < minDiffBitsCount ||
|
|
58
|
+
modexp.toString(2).length < minDiffBitsCount ||
|
|
59
|
+
Math.floor((modexp.toString(2).length + 7) / 8) > maxModExpSize);
|
|
60
|
+
}
|
|
61
|
+
export function pad(bigint) {
|
|
62
|
+
if (typeof bigint === "number") {
|
|
63
|
+
bigint = BigInt(bigint);
|
|
64
|
+
}
|
|
65
|
+
if (typeof bigint === "bigint") {
|
|
66
|
+
return bufferFromBigInt(bigint, 256, false);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
return concat(new Uint8Array(256 - bigint.length), bigint);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export async function checkPassword(password, ap) {
|
|
73
|
+
const algo = ap.currentAlgo;
|
|
74
|
+
if (!(algo instanceof
|
|
75
|
+
types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow)) {
|
|
76
|
+
throw new Error("Unexpected algorithm");
|
|
77
|
+
}
|
|
78
|
+
// g := algo.g
|
|
79
|
+
const g = algo.g;
|
|
80
|
+
// p := algo.p
|
|
81
|
+
const p = bigIntFromBuffer(algo.p, false);
|
|
82
|
+
if (!isSafePrime(algo.p, g)) {
|
|
83
|
+
throw new Error("Got unsafe prime");
|
|
84
|
+
}
|
|
85
|
+
const srpB = ap.srpB;
|
|
86
|
+
const srpId = ap.srpId;
|
|
87
|
+
{
|
|
88
|
+
if (!srpB) {
|
|
89
|
+
throw new Error("srbB is not set");
|
|
90
|
+
}
|
|
91
|
+
if (!srpId) {
|
|
92
|
+
throw new Error("srpId is not set");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// salt1 := algo.salt1
|
|
96
|
+
const salt1 = algo.salt1;
|
|
97
|
+
// salt2 := algo.salt2
|
|
98
|
+
const salt2 = algo.salt2;
|
|
99
|
+
// g_b := srp_B
|
|
100
|
+
const gB = bigIntFromBuffer(srpB, false);
|
|
101
|
+
// k := H(p | g)
|
|
102
|
+
const k = bigIntFromBuffer(await h(concat(pad(p), pad(g))), false);
|
|
103
|
+
let u = 0n;
|
|
104
|
+
let a = 0n;
|
|
105
|
+
let gA = 0n;
|
|
106
|
+
for (let i = 0; i < 1000; i++) {
|
|
107
|
+
a = getRandomBigInt(256, false);
|
|
108
|
+
// g_a := pow(g, a) mod p
|
|
109
|
+
gA = modExp(BigInt(g), a, p);
|
|
110
|
+
if (isGoodModExpFirst(gA, p)) {
|
|
111
|
+
u = bigIntFromBuffer(await sha256(concat(pad(gA), pad(gB))), false);
|
|
112
|
+
if (u > 0n) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!a || !u || !gA) {
|
|
118
|
+
throw new Error();
|
|
119
|
+
}
|
|
120
|
+
// x := PH2(password, salt1, salt2)
|
|
121
|
+
const x = bigIntFromBuffer(await ph2(password, salt1, salt2), false);
|
|
122
|
+
// v := pow(g, x) mod p
|
|
123
|
+
const v = modExp(BigInt(g), x, p);
|
|
124
|
+
// k_v := (k * v) mod p
|
|
125
|
+
const kV = mod(k * v, p);
|
|
126
|
+
// t := (g_b - k_v) mod p
|
|
127
|
+
const t = mod(gB - kV, p);
|
|
128
|
+
// s_a := pow(t, a + u * x) mod p
|
|
129
|
+
const sA = modExp(t, a + u * x, p);
|
|
130
|
+
// k_a := H(s_a)
|
|
131
|
+
const kA = await h(pad(sA));
|
|
132
|
+
// M1 := H(H(p) xor H(g) | H(salt1) | H(salt2) | g_a | g_b | k_a)
|
|
133
|
+
const hG = await h(pad(g));
|
|
134
|
+
const m1 = await h(concat((await h(pad(p))).map((v, i) => v ^ hG[i]), await h(salt1), await h(salt2), pad(gA), pad(gB), kA));
|
|
135
|
+
return new types.InputCheckPasswordSRP({
|
|
136
|
+
srpId: srpId,
|
|
137
|
+
A: pad(gA),
|
|
138
|
+
M1: m1,
|
|
139
|
+
});
|
|
140
|
+
}
|
package/package.json
CHANGED
package/script/client/client.js
CHANGED
|
@@ -1,21 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.Client = void 0;
|
|
4
27
|
const deps_js_1 = require("../deps.js");
|
|
5
28
|
const constants_js_1 = require("../constants.js");
|
|
6
29
|
const _0_bigint_js_1 = require("../utilities/0_bigint.js");
|
|
30
|
+
const _0_hash_js_1 = require("../utilities/0_hash.js");
|
|
7
31
|
const _1_message_js_1 = require("../utilities/1_message.js");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
32
|
+
const types = __importStar(require("../tl/2_types.js"));
|
|
33
|
+
const functions = __importStar(require("../tl/3_functions.js"));
|
|
10
34
|
const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
|
|
11
35
|
const _4_rpc_result_js_1 = require("../tl/4_rpc_result.js");
|
|
12
36
|
const _5_message_js_1 = require("../tl/5_message.js");
|
|
13
37
|
const _6_message_container_js_1 = require("../tl/6_message_container.js");
|
|
14
38
|
const client_abstract_js_1 = require("./client_abstract.js");
|
|
15
39
|
const client_plain_js_1 = require("./client_plain.js");
|
|
40
|
+
const session_memory_js_1 = require("../session/session_memory.js");
|
|
16
41
|
class Client extends client_abstract_js_1.ClientAbstract {
|
|
17
|
-
constructor() {
|
|
18
|
-
super(
|
|
42
|
+
constructor(session = new session_memory_js_1.SessionMemory(), params) {
|
|
43
|
+
super(params?.transportProvider);
|
|
44
|
+
Object.defineProperty(this, "session", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: session
|
|
49
|
+
});
|
|
19
50
|
Object.defineProperty(this, "sessionId", {
|
|
20
51
|
enumerable: true,
|
|
21
52
|
configurable: true,
|
|
@@ -54,12 +85,29 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
54
85
|
});
|
|
55
86
|
}
|
|
56
87
|
async connect() {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
88
|
+
await this.session.load();
|
|
89
|
+
let key;
|
|
90
|
+
let id;
|
|
91
|
+
if (this.session.authKey != null) {
|
|
92
|
+
key = this.session.authKey;
|
|
93
|
+
id = (0, _0_bigint_js_1.bigIntFromBuffer)((await (0, _0_hash_js_1.sha1)(key)).slice(-8), true, false);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
|
|
97
|
+
await plain.connect();
|
|
98
|
+
const { authKey, authKeyId, salt } = await plain.createAuthKey();
|
|
99
|
+
await plain.disconnect();
|
|
100
|
+
key = authKey;
|
|
101
|
+
id = authKeyId;
|
|
102
|
+
this.state.salt = salt;
|
|
103
|
+
}
|
|
61
104
|
this.auth = { key, id };
|
|
62
|
-
this.
|
|
105
|
+
if (this.session.dc != null) {
|
|
106
|
+
const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
|
|
107
|
+
this.connection = connection;
|
|
108
|
+
this.transport = transport;
|
|
109
|
+
this.dcId = dcId;
|
|
110
|
+
}
|
|
63
111
|
await super.connect();
|
|
64
112
|
// logger().debug("Client connected");
|
|
65
113
|
this.receiveLoop();
|
|
@@ -71,7 +119,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
71
119
|
}
|
|
72
120
|
while (this.connected) {
|
|
73
121
|
if (this.toAcknowledge.size >= constants_js_1.ackThreshold) {
|
|
74
|
-
await this.send(new
|
|
122
|
+
await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
|
|
75
123
|
this.toAcknowledge.clear();
|
|
76
124
|
}
|
|
77
125
|
const buffer = await this.transport.receive();
|
|
@@ -86,21 +134,21 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
86
134
|
const messages = decrypted instanceof _6_message_container_js_1.MessageContainer ? decrypted.messages : [decrypted];
|
|
87
135
|
for (const message of messages) {
|
|
88
136
|
let body = message.body;
|
|
89
|
-
if (body instanceof
|
|
137
|
+
if (body instanceof types.GZIPPacked) {
|
|
90
138
|
body = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(body.packedData)).readObject();
|
|
91
139
|
}
|
|
92
140
|
// logger().debug(`Received ${body.constructor.name}`);
|
|
93
|
-
if (body instanceof
|
|
141
|
+
if (body instanceof types.Updates) {
|
|
94
142
|
this.updatesHandler?.(this, body);
|
|
95
143
|
}
|
|
96
144
|
else if (message.body instanceof _4_rpc_result_js_1.RPCResult) {
|
|
97
145
|
let result = message.body.result;
|
|
98
|
-
if (result instanceof
|
|
146
|
+
if (result instanceof types.GZIPPacked) {
|
|
99
147
|
result = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(result.packedData)).readObject();
|
|
100
148
|
}
|
|
101
149
|
const promise = this.promises.get(message.body.messageId);
|
|
102
150
|
if (promise) {
|
|
103
|
-
if (result instanceof
|
|
151
|
+
if (result instanceof types.RPCError) {
|
|
104
152
|
promise.reject(result);
|
|
105
153
|
}
|
|
106
154
|
else {
|
|
@@ -109,15 +157,15 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
109
157
|
this.promises.delete(message.body.messageId);
|
|
110
158
|
}
|
|
111
159
|
}
|
|
112
|
-
else if (message.body instanceof
|
|
160
|
+
else if (message.body instanceof types.Pong) {
|
|
113
161
|
const promise = this.promises.get(message.body.msgId);
|
|
114
162
|
if (promise) {
|
|
115
163
|
promise.resolve(message.body);
|
|
116
164
|
this.promises.delete(message.body.msgId);
|
|
117
165
|
}
|
|
118
166
|
}
|
|
119
|
-
else if (message.body instanceof
|
|
120
|
-
if (message.body instanceof
|
|
167
|
+
else if (message.body instanceof types.BadMsgNotification || message.body instanceof types.BadServerSalt) {
|
|
168
|
+
if (message.body instanceof types.BadServerSalt) {
|
|
121
169
|
this.state.salt = message.body.newServerSalt;
|
|
122
170
|
}
|
|
123
171
|
const promise = this.promises.get(message.body.badMsgId);
|
|
@@ -133,7 +181,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
133
181
|
async pingLoop() {
|
|
134
182
|
while (this.connected) {
|
|
135
183
|
try {
|
|
136
|
-
await this.invoke(new
|
|
184
|
+
await this.invoke(new functions.Ping({ pingId: (0, _0_bigint_js_1.getRandomBigInt)(8, true, false) }));
|
|
137
185
|
}
|
|
138
186
|
catch (_err) {
|
|
139
187
|
// logger().error(`Failed to invoke ping: ${err}`);
|
|
@@ -146,7 +194,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
146
194
|
throw new Error("Not connected");
|
|
147
195
|
}
|
|
148
196
|
let seqNo = this.state.seqNo * 2;
|
|
149
|
-
if (!(function_ instanceof
|
|
197
|
+
if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
|
|
150
198
|
seqNo++;
|
|
151
199
|
this.state.seqNo++;
|
|
152
200
|
}
|
|
@@ -159,7 +207,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
159
207
|
const result = await new Promise((resolve, reject) => {
|
|
160
208
|
this.promises.set(message.id, { resolve, reject });
|
|
161
209
|
});
|
|
162
|
-
if (result instanceof
|
|
210
|
+
if (result instanceof types.BadServerSalt) {
|
|
163
211
|
return await this.invoke(function_);
|
|
164
212
|
}
|
|
165
213
|
else {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ClientAbstract = void 0;
|
|
4
|
-
const transport_provider_js_1 = require("../transport/transport_provider.js");
|
|
5
4
|
const deps_js_1 = require("../deps.js");
|
|
5
|
+
const transport_provider_js_1 = require("../transport/transport_provider.js");
|
|
6
6
|
class ClientAbstract {
|
|
7
7
|
constructor(transportProvider = (0, transport_provider_js_1.defaultTransportProvider)()) {
|
|
8
8
|
Object.defineProperty(this, "transportProvider", {
|
|
@@ -35,7 +35,7 @@ class ClientAbstract {
|
|
|
35
35
|
writable: true,
|
|
36
36
|
value: false
|
|
37
37
|
});
|
|
38
|
-
const { connection, transport, dcId } = transportProvider(false);
|
|
38
|
+
const { connection, transport, dcId } = transportProvider({ dc: transport_provider_js_1.defaultDc, cdn: false });
|
|
39
39
|
this.connection = connection;
|
|
40
40
|
this.transport = transport;
|
|
41
41
|
this.dcId = dcId;
|