@mtkruto/node 0.0.78 → 0.0.801
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 +155 -11
- package/esm/client/client_abstract.js +18 -2
- package/esm/constants.js +8 -0
- package/esm/mod.js +1 -1
- package/esm/session/session.js +21 -1
- package/esm/utilities/0_buffer.js +6 -1
- package/esm/utilities/1_message.js +3 -9
- package/package.json +1 -1
- package/script/client/client.js +154 -10
- package/script/client/client_abstract.js +18 -2
- package/script/constants.js +9 -1
- package/script/mod.js +8 -1
- package/script/session/session.js +21 -1
- package/script/utilities/0_buffer.js +6 -1
- package/script/utilities/1_message.js +2 -8
- package/types/client/client.d.ts +28 -2
- package/types/client/client_abstract.d.ts +5 -1
- package/types/constants.d.ts +7 -0
- package/types/mod.d.ts +1 -1
- package/types/session/session.d.ts +7 -3
- package/types/tl/3_functions.d.ts +216 -216
- package/types/utilities/1_message.d.ts +2 -2
package/esm/client/client.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { gunzip } from "../deps.js";
|
|
2
|
-
import { ackThreshold } from "../constants.js";
|
|
2
|
+
import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "../constants.js";
|
|
3
3
|
import { getRandomBigInt } from "../utilities/0_bigint.js";
|
|
4
4
|
import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
|
|
5
5
|
import * as types from "../tl/2_types.js";
|
|
@@ -12,7 +12,7 @@ import { ClientAbstract } from "./client_abstract.js";
|
|
|
12
12
|
import { ClientPlain } from "./client_plain.js";
|
|
13
13
|
import { SessionMemory } from "../session/session_memory.js";
|
|
14
14
|
export class Client extends ClientAbstract {
|
|
15
|
-
constructor(session = new SessionMemory(), params) {
|
|
15
|
+
constructor(session = new SessionMemory(), apiId = 0, apiHash = "", params) {
|
|
16
16
|
super(params?.transportProvider);
|
|
17
17
|
Object.defineProperty(this, "session", {
|
|
18
18
|
enumerable: true,
|
|
@@ -20,6 +20,18 @@ export class Client extends ClientAbstract {
|
|
|
20
20
|
writable: true,
|
|
21
21
|
value: session
|
|
22
22
|
});
|
|
23
|
+
Object.defineProperty(this, "apiId", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: apiId
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "apiHash", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: apiHash
|
|
34
|
+
});
|
|
23
35
|
Object.defineProperty(this, "sessionId", {
|
|
24
36
|
enumerable: true,
|
|
25
37
|
configurable: true,
|
|
@@ -50,29 +62,150 @@ export class Client extends ClientAbstract {
|
|
|
50
62
|
writable: true,
|
|
51
63
|
value: null
|
|
52
64
|
});
|
|
65
|
+
Object.defineProperty(this, "appVersion", {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
value: void 0
|
|
70
|
+
});
|
|
71
|
+
Object.defineProperty(this, "deviceModel", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: void 0
|
|
76
|
+
});
|
|
77
|
+
Object.defineProperty(this, "langCode", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: void 0
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "langPack", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "systemLangCode", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: void 0
|
|
94
|
+
});
|
|
95
|
+
Object.defineProperty(this, "systemVersion", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: void 0
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(this, "shouldLoadSession", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: true
|
|
106
|
+
});
|
|
107
|
+
this.appVersion = params?.appVersion ?? DEFAULT_APP_VERSION;
|
|
108
|
+
this.deviceModel = params?.deviceModel ?? DEFAULT_DEVICE_MODEL;
|
|
109
|
+
this.langCode = params?.langCode ?? DEFAULT_LANG_CODE;
|
|
110
|
+
this.langPack = params?.langPack ?? DEFAULT_LANG_PACK;
|
|
111
|
+
this.systemLangCode = params?.systemLangCode ?? DEFAULT_SYSTEM_LANG_CODE;
|
|
112
|
+
this.systemVersion = params?.systemVersion ?? DEFAULT_SYSTEM_VERSION;
|
|
113
|
+
}
|
|
114
|
+
setDc(dc) {
|
|
115
|
+
if (this.session.dc != dc) {
|
|
116
|
+
this.session.dc = dc;
|
|
117
|
+
this.session.authKey = null;
|
|
118
|
+
if (this.shouldLoadSession) {
|
|
119
|
+
this.shouldLoadSession = false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
super.setDc(dc);
|
|
53
123
|
}
|
|
54
124
|
async connect() {
|
|
55
|
-
|
|
125
|
+
if (this.shouldLoadSession) {
|
|
126
|
+
await this.session.load();
|
|
127
|
+
this.shouldLoadSession = false;
|
|
128
|
+
}
|
|
56
129
|
if (this.session.authKey == null) {
|
|
57
130
|
const plain = new ClientPlain(this.transportProvider);
|
|
131
|
+
if (this.session.dc != null) {
|
|
132
|
+
plain.setDc(this.session.dc);
|
|
133
|
+
}
|
|
58
134
|
await plain.connect();
|
|
59
135
|
const { authKey, salt } = await plain.createAuthKey();
|
|
60
136
|
await plain.disconnect();
|
|
61
137
|
this.state.salt = salt;
|
|
62
138
|
this.session.authKey = authKey;
|
|
63
|
-
await this.session.save();
|
|
64
139
|
}
|
|
65
140
|
if (this.session.dc != null) {
|
|
66
|
-
|
|
67
|
-
this.connection = connection;
|
|
68
|
-
this.transport = transport;
|
|
69
|
-
this.dcId = dcId;
|
|
141
|
+
this.setDc(this.session.dc);
|
|
70
142
|
}
|
|
143
|
+
await this.session.save();
|
|
71
144
|
await super.connect();
|
|
72
145
|
// logger().debug("Client connected");
|
|
73
146
|
this.receiveLoop();
|
|
74
147
|
this.pingLoop();
|
|
75
148
|
}
|
|
149
|
+
async authorize(params) {
|
|
150
|
+
if (!this.apiId) {
|
|
151
|
+
throw new Error("apiId not set");
|
|
152
|
+
}
|
|
153
|
+
if (!this.apiHash) {
|
|
154
|
+
throw new Error("apiHash not set");
|
|
155
|
+
}
|
|
156
|
+
await this.invoke(new functions.InitConnection({
|
|
157
|
+
apiId: this.apiId,
|
|
158
|
+
appVersion: this.appVersion,
|
|
159
|
+
deviceModel: this.deviceModel,
|
|
160
|
+
langCode: this.langCode,
|
|
161
|
+
langPack: this.langPack,
|
|
162
|
+
query: new functions.InvokeWithLayer({
|
|
163
|
+
layer: LAYER,
|
|
164
|
+
query: new functions.HelpGetConfig(),
|
|
165
|
+
}),
|
|
166
|
+
systemLangCode: this.systemLangCode,
|
|
167
|
+
systemVersion: this.systemVersion,
|
|
168
|
+
}));
|
|
169
|
+
try {
|
|
170
|
+
await this.invoke(new functions.UpdatesGetState());
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
catch (err) {
|
|
174
|
+
if (!(err instanceof types.RPCError) || err.errorMessage != "AUTH_KEY_UNREGISTERED") {
|
|
175
|
+
throw err;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
if (params instanceof types.AuthExportedAuthorization) {
|
|
180
|
+
await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
|
|
181
|
+
}
|
|
182
|
+
else if (typeof params == "object") {
|
|
183
|
+
throw new Error("Not implemented");
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
if (err instanceof types.RPCError) {
|
|
191
|
+
const match = err.errorMessage.match(/MIGRATE_(\d)$/);
|
|
192
|
+
if (match) {
|
|
193
|
+
let newDc = match[1];
|
|
194
|
+
if (Math.abs(this.dcId) >= 10000) {
|
|
195
|
+
newDc += "-test";
|
|
196
|
+
}
|
|
197
|
+
await this.reconnect(newDc);
|
|
198
|
+
await this.authorize(params);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
throw err;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
throw err;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
76
209
|
async receiveLoop() {
|
|
77
210
|
if (!this.session.authKey) {
|
|
78
211
|
throw new Error("Not connected");
|
|
@@ -82,10 +215,21 @@ export class Client extends ClientAbstract {
|
|
|
82
215
|
await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
|
|
83
216
|
this.toAcknowledge.clear();
|
|
84
217
|
}
|
|
85
|
-
|
|
218
|
+
let buffer;
|
|
219
|
+
try {
|
|
220
|
+
buffer = await this.transport.receive();
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
if (!this.connected) {
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw err;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
86
230
|
let decrypted;
|
|
87
231
|
try {
|
|
88
|
-
decrypted = await decryptMessage(buffer, this.session.authKey, this.sessionId);
|
|
232
|
+
decrypted = await decryptMessage(buffer, this.session.authKey, (await this.session.authKeyId), this.sessionId);
|
|
89
233
|
}
|
|
90
234
|
catch (_err) {
|
|
91
235
|
// logger().error(`Failed to decrypt message: ${err}`);
|
|
@@ -159,7 +303,7 @@ export class Client extends ClientAbstract {
|
|
|
159
303
|
this.state.seqNo++;
|
|
160
304
|
}
|
|
161
305
|
const message = new Message(getMessageId(), seqNo, function_);
|
|
162
|
-
await this.transport.send(await encryptMessage(message, this.session.authKey, this.state.salt, this.sessionId));
|
|
306
|
+
await this.transport.send(await encryptMessage(message, this.session.authKey, (await this.session.authKeyId), this.state.salt, this.sessionId));
|
|
163
307
|
// logger().debug(`Invoked ${function_.constructor.name}`);
|
|
164
308
|
if (noWait) {
|
|
165
309
|
return;
|
|
@@ -21,7 +21,7 @@ export class ClientAbstract {
|
|
|
21
21
|
writable: true,
|
|
22
22
|
value: void 0
|
|
23
23
|
});
|
|
24
|
-
Object.defineProperty(this, "
|
|
24
|
+
Object.defineProperty(this, "_dcId", {
|
|
25
25
|
enumerable: true,
|
|
26
26
|
configurable: true,
|
|
27
27
|
writable: true,
|
|
@@ -36,7 +36,16 @@ export class ClientAbstract {
|
|
|
36
36
|
const { connection, transport, dcId } = transportProvider({ cdn: false });
|
|
37
37
|
this.connection = connection;
|
|
38
38
|
this.transport = transport;
|
|
39
|
-
this.
|
|
39
|
+
this._dcId = dcId;
|
|
40
|
+
}
|
|
41
|
+
get dcId() {
|
|
42
|
+
return this._dcId;
|
|
43
|
+
}
|
|
44
|
+
setDc(dc) {
|
|
45
|
+
const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
|
|
46
|
+
this.connection = connection;
|
|
47
|
+
this.transport = transport;
|
|
48
|
+
this._dcId = dcId;
|
|
40
49
|
}
|
|
41
50
|
async connect() {
|
|
42
51
|
await initTgCrypto();
|
|
@@ -44,6 +53,13 @@ export class ClientAbstract {
|
|
|
44
53
|
await this.transport.initialize();
|
|
45
54
|
this.connected = true;
|
|
46
55
|
}
|
|
56
|
+
async reconnect(dc) {
|
|
57
|
+
await this.disconnect();
|
|
58
|
+
if (dc) {
|
|
59
|
+
this.setDc(dc);
|
|
60
|
+
}
|
|
61
|
+
await this.connect();
|
|
62
|
+
}
|
|
47
63
|
async disconnect() {
|
|
48
64
|
await this.transport.deinitialize();
|
|
49
65
|
await this.connection.close();
|
package/esm/constants.js
CHANGED
|
@@ -60,3 +60,11 @@ export const publicKeys = new Map([
|
|
|
60
60
|
]);
|
|
61
61
|
export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
|
|
62
62
|
export const DEFAULT_INITIAL_DC = "2-test";
|
|
63
|
+
export const LAYER = 158;
|
|
64
|
+
// TODO
|
|
65
|
+
export const DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
|
|
66
|
+
export const DEFAULT_DEVICE_MODEL = "Krutaya Device";
|
|
67
|
+
export const DEFAULT_LANG_CODE = "en";
|
|
68
|
+
export const DEFAULT_LANG_PACK = "";
|
|
69
|
+
export const DEFAULT_SYSTEM_LANG_CODE = "en";
|
|
70
|
+
export const DEFAULT_SYSTEM_VERSION = "1.0";
|
package/esm/mod.js
CHANGED
|
@@ -17,4 +17,4 @@ export * from "./transport/transport.js";
|
|
|
17
17
|
export * from "./transport/transport_provider.js";
|
|
18
18
|
export * from "./connection/connection.js";
|
|
19
19
|
export * from "./connection/connection_web_socket.js";
|
|
20
|
-
export { DEFAULT_INITIAL_DC } from "./constants.js";
|
|
20
|
+
export { DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "./constants.js";
|
package/esm/session/session.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { sha1 } from "../utilities/0_hash.js";
|
|
2
|
+
import { bigIntFromBuffer } from "../utilities/0_bigint.js";
|
|
1
3
|
export class Session {
|
|
2
4
|
constructor() {
|
|
3
5
|
Object.defineProperty(this, "dc", {
|
|
@@ -6,11 +8,29 @@ export class Session {
|
|
|
6
8
|
writable: true,
|
|
7
9
|
value: null
|
|
8
10
|
});
|
|
9
|
-
Object.defineProperty(this, "
|
|
11
|
+
Object.defineProperty(this, "_authKeyId", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: Promise.resolve(null)
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "_authKey", {
|
|
10
18
|
enumerable: true,
|
|
11
19
|
configurable: true,
|
|
12
20
|
writable: true,
|
|
13
21
|
value: null
|
|
14
22
|
});
|
|
15
23
|
}
|
|
24
|
+
get authKeyId() {
|
|
25
|
+
return this._authKeyId;
|
|
26
|
+
}
|
|
27
|
+
set authKey(authKey) {
|
|
28
|
+
this._authKey = authKey;
|
|
29
|
+
if (authKey != null) {
|
|
30
|
+
this._authKeyId = sha1(authKey).then((hash) => bigIntFromBuffer(hash.slice(-8), true, false));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
get authKey() {
|
|
34
|
+
return this._authKey;
|
|
35
|
+
}
|
|
16
36
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export function concat(...buffers) {
|
|
2
|
-
|
|
2
|
+
const bytes = new Array();
|
|
3
|
+
for (const buffer of buffers) {
|
|
4
|
+
bytes.push(...buffer);
|
|
5
|
+
}
|
|
6
|
+
const buffer = new Uint8Array(bytes);
|
|
7
|
+
return buffer;
|
|
3
8
|
}
|
|
4
9
|
const bufferFromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
5
10
|
export function bufferFromBigInt(bigIntVar, bytesNumber, little = true, signed = false) {
|
|
@@ -6,12 +6,8 @@ import { TLReader } from "../tl/3_tl_reader.js";
|
|
|
6
6
|
import { RPCResult } from "../tl/4_rpc_result.js";
|
|
7
7
|
import { Message } from "../tl/5_message.js";
|
|
8
8
|
import { MessageContainer } from "../tl/6_message_container.js";
|
|
9
|
-
import { bigIntFromBuffer } from "./0_bigint.js";
|
|
10
9
|
import { bufferFromBigInt, concat } from "./0_buffer.js";
|
|
11
|
-
import {
|
|
12
|
-
async function getAuthKeyId(authKey) {
|
|
13
|
-
return bigIntFromBuffer((await sha1(authKey)).slice(-8), true, false);
|
|
14
|
-
}
|
|
10
|
+
import { sha256 } from "./0_hash.js";
|
|
15
11
|
let lastMsgId = 0n;
|
|
16
12
|
export function getMessageId() {
|
|
17
13
|
const now = new Date().getTime() / 1000 + 0;
|
|
@@ -37,9 +33,8 @@ export function unpackUnencryptedMessage(buffer) {
|
|
|
37
33
|
const message = reader.read(messageLength);
|
|
38
34
|
return { messageId, message };
|
|
39
35
|
}
|
|
40
|
-
export async function encryptMessage(message, authKey, salt, sessionId) {
|
|
36
|
+
export async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
|
|
41
37
|
const encoded = message.body.serialize();
|
|
42
|
-
const authKeyId = await getAuthKeyId(authKey);
|
|
43
38
|
const payloadWriter = new TLRawWriter();
|
|
44
39
|
payloadWriter.writeInt64(salt);
|
|
45
40
|
payloadWriter.writeInt64(sessionId);
|
|
@@ -66,8 +61,7 @@ export async function encryptMessage(message, authKey, salt, sessionId) {
|
|
|
66
61
|
messageWriter.write(ige256Encrypt(payload, aesKey, aesIV));
|
|
67
62
|
return messageWriter.buffer;
|
|
68
63
|
}
|
|
69
|
-
export async function decryptMessage(buffer, authKey, _sessionId) {
|
|
70
|
-
const authKeyId = await getAuthKeyId(authKey);
|
|
64
|
+
export async function decryptMessage(buffer, authKey, authKeyId, _sessionId) {
|
|
71
65
|
const reader = new TLReader(buffer);
|
|
72
66
|
assertEquals(reader.readInt64(false), authKeyId);
|
|
73
67
|
const messageKey_ = reader.readInt128();
|
package/package.json
CHANGED
package/script/client/client.js
CHANGED
|
@@ -38,7 +38,7 @@ const client_abstract_js_1 = require("./client_abstract.js");
|
|
|
38
38
|
const client_plain_js_1 = require("./client_plain.js");
|
|
39
39
|
const session_memory_js_1 = require("../session/session_memory.js");
|
|
40
40
|
class Client extends client_abstract_js_1.ClientAbstract {
|
|
41
|
-
constructor(session = new session_memory_js_1.SessionMemory(), params) {
|
|
41
|
+
constructor(session = new session_memory_js_1.SessionMemory(), apiId = 0, apiHash = "", params) {
|
|
42
42
|
super(params?.transportProvider);
|
|
43
43
|
Object.defineProperty(this, "session", {
|
|
44
44
|
enumerable: true,
|
|
@@ -46,6 +46,18 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
46
46
|
writable: true,
|
|
47
47
|
value: session
|
|
48
48
|
});
|
|
49
|
+
Object.defineProperty(this, "apiId", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: apiId
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "apiHash", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: apiHash
|
|
60
|
+
});
|
|
49
61
|
Object.defineProperty(this, "sessionId", {
|
|
50
62
|
enumerable: true,
|
|
51
63
|
configurable: true,
|
|
@@ -76,29 +88,150 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
76
88
|
writable: true,
|
|
77
89
|
value: null
|
|
78
90
|
});
|
|
91
|
+
Object.defineProperty(this, "appVersion", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: void 0
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "deviceModel", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(this, "langCode", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(this, "langPack", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: void 0
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(this, "systemLangCode", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
configurable: true,
|
|
118
|
+
writable: true,
|
|
119
|
+
value: void 0
|
|
120
|
+
});
|
|
121
|
+
Object.defineProperty(this, "systemVersion", {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
configurable: true,
|
|
124
|
+
writable: true,
|
|
125
|
+
value: void 0
|
|
126
|
+
});
|
|
127
|
+
Object.defineProperty(this, "shouldLoadSession", {
|
|
128
|
+
enumerable: true,
|
|
129
|
+
configurable: true,
|
|
130
|
+
writable: true,
|
|
131
|
+
value: true
|
|
132
|
+
});
|
|
133
|
+
this.appVersion = params?.appVersion ?? constants_js_1.DEFAULT_APP_VERSION;
|
|
134
|
+
this.deviceModel = params?.deviceModel ?? constants_js_1.DEFAULT_DEVICE_MODEL;
|
|
135
|
+
this.langCode = params?.langCode ?? constants_js_1.DEFAULT_LANG_CODE;
|
|
136
|
+
this.langPack = params?.langPack ?? constants_js_1.DEFAULT_LANG_PACK;
|
|
137
|
+
this.systemLangCode = params?.systemLangCode ?? constants_js_1.DEFAULT_SYSTEM_LANG_CODE;
|
|
138
|
+
this.systemVersion = params?.systemVersion ?? constants_js_1.DEFAULT_SYSTEM_VERSION;
|
|
139
|
+
}
|
|
140
|
+
setDc(dc) {
|
|
141
|
+
if (this.session.dc != dc) {
|
|
142
|
+
this.session.dc = dc;
|
|
143
|
+
this.session.authKey = null;
|
|
144
|
+
if (this.shouldLoadSession) {
|
|
145
|
+
this.shouldLoadSession = false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
super.setDc(dc);
|
|
79
149
|
}
|
|
80
150
|
async connect() {
|
|
81
|
-
|
|
151
|
+
if (this.shouldLoadSession) {
|
|
152
|
+
await this.session.load();
|
|
153
|
+
this.shouldLoadSession = false;
|
|
154
|
+
}
|
|
82
155
|
if (this.session.authKey == null) {
|
|
83
156
|
const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
|
|
157
|
+
if (this.session.dc != null) {
|
|
158
|
+
plain.setDc(this.session.dc);
|
|
159
|
+
}
|
|
84
160
|
await plain.connect();
|
|
85
161
|
const { authKey, salt } = await plain.createAuthKey();
|
|
86
162
|
await plain.disconnect();
|
|
87
163
|
this.state.salt = salt;
|
|
88
164
|
this.session.authKey = authKey;
|
|
89
|
-
await this.session.save();
|
|
90
165
|
}
|
|
91
166
|
if (this.session.dc != null) {
|
|
92
|
-
|
|
93
|
-
this.connection = connection;
|
|
94
|
-
this.transport = transport;
|
|
95
|
-
this.dcId = dcId;
|
|
167
|
+
this.setDc(this.session.dc);
|
|
96
168
|
}
|
|
169
|
+
await this.session.save();
|
|
97
170
|
await super.connect();
|
|
98
171
|
// logger().debug("Client connected");
|
|
99
172
|
this.receiveLoop();
|
|
100
173
|
this.pingLoop();
|
|
101
174
|
}
|
|
175
|
+
async authorize(params) {
|
|
176
|
+
if (!this.apiId) {
|
|
177
|
+
throw new Error("apiId not set");
|
|
178
|
+
}
|
|
179
|
+
if (!this.apiHash) {
|
|
180
|
+
throw new Error("apiHash not set");
|
|
181
|
+
}
|
|
182
|
+
await this.invoke(new functions.InitConnection({
|
|
183
|
+
apiId: this.apiId,
|
|
184
|
+
appVersion: this.appVersion,
|
|
185
|
+
deviceModel: this.deviceModel,
|
|
186
|
+
langCode: this.langCode,
|
|
187
|
+
langPack: this.langPack,
|
|
188
|
+
query: new functions.InvokeWithLayer({
|
|
189
|
+
layer: constants_js_1.LAYER,
|
|
190
|
+
query: new functions.HelpGetConfig(),
|
|
191
|
+
}),
|
|
192
|
+
systemLangCode: this.systemLangCode,
|
|
193
|
+
systemVersion: this.systemVersion,
|
|
194
|
+
}));
|
|
195
|
+
try {
|
|
196
|
+
await this.invoke(new functions.UpdatesGetState());
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
if (!(err instanceof types.RPCError) || err.errorMessage != "AUTH_KEY_UNREGISTERED") {
|
|
201
|
+
throw err;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
if (params instanceof types.AuthExportedAuthorization) {
|
|
206
|
+
await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
|
|
207
|
+
}
|
|
208
|
+
else if (typeof params == "object") {
|
|
209
|
+
throw new Error("Not implemented");
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
if (err instanceof types.RPCError) {
|
|
217
|
+
const match = err.errorMessage.match(/MIGRATE_(\d)$/);
|
|
218
|
+
if (match) {
|
|
219
|
+
let newDc = match[1];
|
|
220
|
+
if (Math.abs(this.dcId) >= 10000) {
|
|
221
|
+
newDc += "-test";
|
|
222
|
+
}
|
|
223
|
+
await this.reconnect(newDc);
|
|
224
|
+
await this.authorize(params);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw err;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
throw err;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
102
235
|
async receiveLoop() {
|
|
103
236
|
if (!this.session.authKey) {
|
|
104
237
|
throw new Error("Not connected");
|
|
@@ -108,10 +241,21 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
108
241
|
await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
|
|
109
242
|
this.toAcknowledge.clear();
|
|
110
243
|
}
|
|
111
|
-
|
|
244
|
+
let buffer;
|
|
245
|
+
try {
|
|
246
|
+
buffer = await this.transport.receive();
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
if (!this.connected) {
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
throw err;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
112
256
|
let decrypted;
|
|
113
257
|
try {
|
|
114
|
-
decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, this.sessionId);
|
|
258
|
+
decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, (await this.session.authKeyId), this.sessionId);
|
|
115
259
|
}
|
|
116
260
|
catch (_err) {
|
|
117
261
|
// logger().error(`Failed to decrypt message: ${err}`);
|
|
@@ -185,7 +329,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
|
|
|
185
329
|
this.state.seqNo++;
|
|
186
330
|
}
|
|
187
331
|
const message = new _5_message_js_1.Message((0, _1_message_js_1.getMessageId)(), seqNo, function_);
|
|
188
|
-
await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, this.state.salt, this.sessionId));
|
|
332
|
+
await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, (await this.session.authKeyId), this.state.salt, this.sessionId));
|
|
189
333
|
// logger().debug(`Invoked ${function_.constructor.name}`);
|
|
190
334
|
if (noWait) {
|
|
191
335
|
return;
|
|
@@ -24,7 +24,7 @@ class ClientAbstract {
|
|
|
24
24
|
writable: true,
|
|
25
25
|
value: void 0
|
|
26
26
|
});
|
|
27
|
-
Object.defineProperty(this, "
|
|
27
|
+
Object.defineProperty(this, "_dcId", {
|
|
28
28
|
enumerable: true,
|
|
29
29
|
configurable: true,
|
|
30
30
|
writable: true,
|
|
@@ -39,7 +39,16 @@ class ClientAbstract {
|
|
|
39
39
|
const { connection, transport, dcId } = transportProvider({ cdn: false });
|
|
40
40
|
this.connection = connection;
|
|
41
41
|
this.transport = transport;
|
|
42
|
-
this.
|
|
42
|
+
this._dcId = dcId;
|
|
43
|
+
}
|
|
44
|
+
get dcId() {
|
|
45
|
+
return this._dcId;
|
|
46
|
+
}
|
|
47
|
+
setDc(dc) {
|
|
48
|
+
const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
|
|
49
|
+
this.connection = connection;
|
|
50
|
+
this.transport = transport;
|
|
51
|
+
this._dcId = dcId;
|
|
43
52
|
}
|
|
44
53
|
async connect() {
|
|
45
54
|
await (0, deps_js_1.initTgCrypto)();
|
|
@@ -47,6 +56,13 @@ class ClientAbstract {
|
|
|
47
56
|
await this.transport.initialize();
|
|
48
57
|
this.connected = true;
|
|
49
58
|
}
|
|
59
|
+
async reconnect(dc) {
|
|
60
|
+
await this.disconnect();
|
|
61
|
+
if (dc) {
|
|
62
|
+
this.setDc(dc);
|
|
63
|
+
}
|
|
64
|
+
await this.connect();
|
|
65
|
+
}
|
|
50
66
|
async disconnect() {
|
|
51
67
|
await this.transport.deinitialize();
|
|
52
68
|
await this.connection.close();
|
package/script/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_INITIAL_DC = exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
|
|
3
|
+
exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.LAYER = exports.DEFAULT_INITIAL_DC = exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
|
|
4
4
|
exports.ackThreshold = 10;
|
|
5
5
|
exports.publicKeys = new Map([
|
|
6
6
|
[
|
|
@@ -63,3 +63,11 @@ exports.publicKeys = new Map([
|
|
|
63
63
|
]);
|
|
64
64
|
exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
|
|
65
65
|
exports.DEFAULT_INITIAL_DC = "2-test";
|
|
66
|
+
exports.LAYER = 158;
|
|
67
|
+
// TODO
|
|
68
|
+
exports.DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
|
|
69
|
+
exports.DEFAULT_DEVICE_MODEL = "Krutaya Device";
|
|
70
|
+
exports.DEFAULT_LANG_CODE = "en";
|
|
71
|
+
exports.DEFAULT_LANG_PACK = "";
|
|
72
|
+
exports.DEFAULT_SYSTEM_LANG_CODE = "en";
|
|
73
|
+
exports.DEFAULT_SYSTEM_VERSION = "1.0";
|
package/script/mod.js
CHANGED
|
@@ -26,7 +26,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
26
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.DEFAULT_INITIAL_DC = exports.functions = exports.types = exports.utils = void 0;
|
|
29
|
+
exports.LAYER = exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_INITIAL_DC = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.functions = exports.types = exports.utils = void 0;
|
|
30
30
|
const _1_password_js_1 = require("./utilities/1_password.js");
|
|
31
31
|
const _0_bigint_js_1 = require("./utilities/0_bigint.js");
|
|
32
32
|
exports.utils = { checkPassword: _1_password_js_1.checkPassword, getRandomId: _0_bigint_js_1.getRandomId };
|
|
@@ -47,4 +47,11 @@ __exportStar(require("./transport/transport_provider.js"), exports);
|
|
|
47
47
|
__exportStar(require("./connection/connection.js"), exports);
|
|
48
48
|
__exportStar(require("./connection/connection_web_socket.js"), exports);
|
|
49
49
|
var constants_js_1 = require("./constants.js");
|
|
50
|
+
Object.defineProperty(exports, "DEFAULT_APP_VERSION", { enumerable: true, get: function () { return constants_js_1.DEFAULT_APP_VERSION; } });
|
|
51
|
+
Object.defineProperty(exports, "DEFAULT_DEVICE_MODEL", { enumerable: true, get: function () { return constants_js_1.DEFAULT_DEVICE_MODEL; } });
|
|
50
52
|
Object.defineProperty(exports, "DEFAULT_INITIAL_DC", { enumerable: true, get: function () { return constants_js_1.DEFAULT_INITIAL_DC; } });
|
|
53
|
+
Object.defineProperty(exports, "DEFAULT_LANG_CODE", { enumerable: true, get: function () { return constants_js_1.DEFAULT_LANG_CODE; } });
|
|
54
|
+
Object.defineProperty(exports, "DEFAULT_LANG_PACK", { enumerable: true, get: function () { return constants_js_1.DEFAULT_LANG_PACK; } });
|
|
55
|
+
Object.defineProperty(exports, "DEFAULT_SYSTEM_LANG_CODE", { enumerable: true, get: function () { return constants_js_1.DEFAULT_SYSTEM_LANG_CODE; } });
|
|
56
|
+
Object.defineProperty(exports, "DEFAULT_SYSTEM_VERSION", { enumerable: true, get: function () { return constants_js_1.DEFAULT_SYSTEM_VERSION; } });
|
|
57
|
+
Object.defineProperty(exports, "LAYER", { enumerable: true, get: function () { return constants_js_1.LAYER; } });
|