@mtkruto/browser 0.0.900 → 0.0.902

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.
Files changed (45) hide show
  1. package/esm/client/client.js +30 -27
  2. package/esm/client/client_abstract.d.ts +2 -0
  3. package/esm/client/client_abstract.js +14 -3
  4. package/esm/client/client_plain.js +20 -6
  5. package/{script/utilities/1_message.d.ts → esm/client/utilities/0_message.d.ts} +2 -2
  6. package/esm/{utilities/1_message.js → client/utilities/0_message.js} +10 -10
  7. package/esm/{utilities/1_password.d.ts → client/utilities/0_password.d.ts} +2 -2
  8. package/esm/{utilities/1_password.js → client/utilities/0_password.js} +4 -4
  9. package/esm/connection/connection_web_socket.js +5 -2
  10. package/esm/constants.d.ts +5 -5
  11. package/esm/constants.js +7 -6
  12. package/esm/deps.js +1 -1
  13. package/esm/mod.d.ts +1 -1
  14. package/esm/mod.js +1 -1
  15. package/esm/transport/transport_abridged.js +1 -1
  16. package/esm/transport/transport_intermediate.js +1 -1
  17. package/esm/transport/transport_provider.d.ts +7 -4
  18. package/esm/transport/transport_provider.js +11 -8
  19. package/esm/{utilities/1_obfuscation.d.ts → transport/utilities/0_obfuscation.d.ts} +2 -2
  20. package/esm/{utilities/1_obfuscation.js → transport/utilities/0_obfuscation.js} +2 -2
  21. package/esm/{utilities → types/utilities}/0_html.d.ts +1 -1
  22. package/esm/{utilities → types/utilities}/0_html.js +2 -2
  23. package/package.json +1 -1
  24. package/script/client/client.js +33 -30
  25. package/script/client/client_abstract.d.ts +2 -0
  26. package/script/client/client_abstract.js +14 -3
  27. package/script/client/client_plain.js +22 -8
  28. package/{esm/utilities/1_message.d.ts → script/client/utilities/0_message.d.ts} +2 -2
  29. package/script/{utilities/1_message.js → client/utilities/0_message.js} +10 -10
  30. package/script/{utilities/1_password.d.ts → client/utilities/0_password.d.ts} +2 -2
  31. package/script/{utilities/1_password.js → client/utilities/0_password.js} +4 -4
  32. package/script/connection/connection_web_socket.js +4 -1
  33. package/script/constants.d.ts +5 -5
  34. package/script/constants.js +7 -6
  35. package/script/deps.js +1 -1
  36. package/script/mod.d.ts +1 -1
  37. package/script/mod.js +2 -2
  38. package/script/transport/transport_abridged.js +2 -2
  39. package/script/transport/transport_intermediate.js +2 -2
  40. package/script/transport/transport_provider.d.ts +7 -4
  41. package/script/transport/transport_provider.js +11 -8
  42. package/script/{utilities/1_obfuscation.d.ts → transport/utilities/0_obfuscation.d.ts} +2 -2
  43. package/script/{utilities/1_obfuscation.js → transport/utilities/0_obfuscation.js} +2 -2
  44. package/script/{utilities → types/utilities}/0_html.d.ts +1 -1
  45. package/script/{utilities → types/utilities}/0_html.js +2 -2
@@ -1,11 +1,8 @@
1
1
  import { debug, gunzip, Mutex, queue } from "../deps.js";
2
- import { ackThreshold, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, USERNAME_TTL, ZERO_CHANNEL_ID } from "../constants.js";
2
+ import { ackThreshold, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, USERNAME_TTL, ZERO_CHANNEL_ID } from "../constants.js";
3
3
  import { bigIntFromBuffer, getRandomBigInt, getRandomId } from "../utilities/0_bigint.js";
4
4
  import { UNREACHABLE } from "../utilities/0_control.js";
5
5
  import { sha1 } from "../utilities/0_hash.js";
6
- import { parseHtml } from "../utilities/0_html.js";
7
- import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
8
- import { checkPassword } from "../utilities/1_password.js";
9
6
  import { as } from "../tl/1_tl_object.js";
10
7
  import * as types from "../tl/2_types.js";
11
8
  import * as functions from "../tl/3_functions.js";
@@ -16,15 +13,20 @@ import { MessageContainer } from "../tl/7_message_container.js";
16
13
  import { ClientAbstract } from "./client_abstract.js";
17
14
  import { ClientPlain } from "./client_plain.js";
18
15
  import { StorageMemory } from "../storage/storage_memory.js";
16
+ import { parseHtml } from "../types/utilities/0_html.js";
19
17
  import { messageEntityToTlObject } from "../types/0_message_entity.js";
20
18
  import { replyKeyboardRemoveToTlObject } from "../types/0_reply_keyboard_remove.js";
21
19
  import { forceReplyToTlObject } from "../types/0_force_reply.js";
22
20
  import { replyKeyboardMarkupToTlObject } from "../types/2_reply_keyboard_markup.js";
23
21
  import { inlineKeyboardMarkupToTlObject } from "../types/2_inline_keyboard_markup.js";
24
22
  import { constructMessage } from "../types/3_message.js"; // high-level wrapper for Telegram API's message
25
- const d = debug("client");
26
- const dRecoverUpdateGap = debug("client_recoverUpdateGap");
27
- const dRecoverChannelUpdateGap = debug("client_recoverChannelUpdateGap");
23
+ import { decryptMessage, encryptMessage, getMessageId } from "./utilities/0_message.js";
24
+ import { checkPassword } from "./utilities/0_password.js";
25
+ const d = debug("Client");
26
+ const dGap = debug("Client/recoverUpdateGap");
27
+ const dGapC = debug("Client/recoverChannelUpdateGap");
28
+ const dAuth = debug("Client/authorize");
29
+ const dRecv = debug("Client/receiveLoop");
28
30
  const UPDATE_GAP = Symbol();
29
31
  export const getEntity = Symbol();
30
32
  export const restartAuth = Symbol();
@@ -161,7 +163,7 @@ export class Client extends ClientAbstract {
161
163
  if (body instanceof types.GZIPPacked) {
162
164
  body = new TLReader(gunzip(body.packedData)).readObject();
163
165
  }
164
- d("received %s", body.constructor.name);
166
+ dRecv("received %s", body.constructor.name);
165
167
  if (body instanceof types.Updates || body instanceof types.TypeUpdate) {
166
168
  await this.processUpdates(body);
167
169
  }
@@ -171,10 +173,10 @@ export class Client extends ClientAbstract {
171
173
  result = new TLReader(gunzip(result.packedData)).readObject();
172
174
  }
173
175
  if (result instanceof types.RPCError) {
174
- d("RPCResult: %d %s", result.errorCode, result.errorMessage);
176
+ dRecv("RPCResult: %d %s", result.errorCode, result.errorMessage);
175
177
  }
176
178
  else {
177
- d("RPCResult: %s", result.constructor.name);
179
+ dRecv("RPCResult: %s", result.constructor.name);
178
180
  }
179
181
  if (result instanceof types.Updates || result instanceof types.TypeUpdate) {
180
182
  await this.processUpdates(result);
@@ -247,6 +249,7 @@ export class Client extends ClientAbstract {
247
249
  if (await this.storage.getDc() != dc) {
248
250
  await this.storage.setDc(dc);
249
251
  await this.storage.setAuthKey(null);
252
+ await this.storage.getAuthKey();
250
253
  }
251
254
  super.setDc(dc);
252
255
  }
@@ -288,7 +291,7 @@ export class Client extends ClientAbstract {
288
291
  }
289
292
  await super.connect();
290
293
  if (dc == null) {
291
- await this.storage.setDc(DEFAULT_INITIAL_DC);
294
+ await this.storage.setDc(this.transportProvider.initialDc);
292
295
  }
293
296
  d("enrypted client connected");
294
297
  this.receiveLoop();
@@ -323,7 +326,7 @@ export class Client extends ClientAbstract {
323
326
  if (!this.apiHash) {
324
327
  throw new Error("apiHash not set");
325
328
  }
326
- d("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
329
+ dAuth("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
327
330
  await this.invoke(new functions.InitConnection({
328
331
  apiId: this.apiId,
329
332
  appVersion: this.appVersion,
@@ -349,7 +352,7 @@ export class Client extends ClientAbstract {
349
352
  const input = await checkPassword(password, ap);
350
353
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
351
354
  await this.storage.setAccountType("user");
352
- d("authorized as user");
355
+ dAuth("authorized as user");
353
356
  break;
354
357
  }
355
358
  catch (err) {
@@ -385,7 +388,7 @@ export class Client extends ClientAbstract {
385
388
  try {
386
389
  if (params instanceof types.AuthExportedAuthorization) {
387
390
  await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
388
- d("authorization imported");
391
+ dAuth("authorization imported");
389
392
  }
390
393
  else if (typeof params == "object") {
391
394
  while (true) {
@@ -401,7 +404,7 @@ export class Client extends ClientAbstract {
401
404
  phoneNumber,
402
405
  settings: new types.CodeSettings(),
403
406
  }));
404
- d("verification code sent");
407
+ dAuth("verification code sent");
405
408
  if (sentCode instanceof types.AuthSentCode) {
406
409
  while (true) {
407
410
  const phoneCode = typeof params.code === "string" ? params.code : await params.code();
@@ -413,7 +416,7 @@ export class Client extends ClientAbstract {
413
416
  else {
414
417
  signedIn = true;
415
418
  await this.storage.setAccountType("user");
416
- d("authorized as user");
419
+ dAuth("authorized as user");
417
420
  break;
418
421
  }
419
422
  }
@@ -448,7 +451,7 @@ export class Client extends ClientAbstract {
448
451
  else {
449
452
  await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
450
453
  await this.storage.setAccountType("bot");
451
- d("authorized as bot");
454
+ dAuth("authorized as bot");
452
455
  }
453
456
  }
454
457
  catch (err) {
@@ -510,7 +513,7 @@ export class Client extends ClientAbstract {
510
513
  decrypted = await decryptMessage(buffer, this.auth.key, this.auth.id, this.sessionId);
511
514
  }
512
515
  catch (err) {
513
- d("failed to decrypt message: %o", err);
516
+ dRecv("failed to decrypt message: %o", err);
514
517
  continue;
515
518
  }
516
519
  const messages = decrypted instanceof MessageContainer ? decrypted.messages : [decrypted];
@@ -799,7 +802,7 @@ export class Client extends ClientAbstract {
799
802
  return localState;
800
803
  }
801
804
  async recoverUpdateGap(source) {
802
- dRecoverUpdateGap("recovering from update gap [%s]", source);
805
+ dGap("recovering from update gap [%s]", source);
803
806
  const release = await this.updateGapRecoveryMutex.acquire();
804
807
  try {
805
808
  let state = await this.getLocalState();
@@ -815,7 +818,7 @@ export class Client extends ClientAbstract {
815
818
  await this.applyUpdateNoGap(update);
816
819
  }
817
820
  if (difference instanceof types.UpdatesDifference) {
818
- dRecoverUpdateGap("recovered from update gap");
821
+ dGap("recovered from update gap");
819
822
  break;
820
823
  }
821
824
  else if (difference instanceof types.UpdatesDifferenceSlice) {
@@ -828,11 +831,11 @@ export class Client extends ClientAbstract {
828
831
  else if (difference instanceof types.UpdatesDifferenceTooLong) {
829
832
  // stored messages should be invalidated in case we store messages in the future
830
833
  state.pts = difference.pts;
831
- dRecoverUpdateGap("received differenceTooLong");
834
+ dGap("received differenceTooLong");
832
835
  }
833
836
  else if (difference instanceof types.UpdatesDifferenceEmpty) {
834
837
  await this.setUpdateStateDate(difference.date);
835
- dRecoverUpdateGap("there was no update gap");
838
+ dGap("there was no update gap");
836
839
  break;
837
840
  }
838
841
  else {
@@ -845,7 +848,7 @@ export class Client extends ClientAbstract {
845
848
  }
846
849
  }
847
850
  async recoverChannelUpdateGap(channelId, source) {
848
- dRecoverChannelUpdateGap("recovering channel update gap [%o, %s]", channelId, source);
851
+ dGapC("recovering channel update gap [%o, %s]", channelId, source);
849
852
  const release = await this.updateGapRecoveryMutex.acquire();
850
853
  try {
851
854
  const pts_ = await this.storage.getChannelPts(channelId);
@@ -868,12 +871,12 @@ export class Client extends ClientAbstract {
868
871
  await this.applyUpdateNoGap(update, false);
869
872
  }
870
873
  await this.storage.setChannelPts(channelId, difference.pts);
871
- dRecoverChannelUpdateGap("recovered from update gap");
874
+ dGapC("recovered from update gap [%o, %s]", channelId, source);
872
875
  break;
873
876
  }
874
877
  else if (difference instanceof types.UpdatesChannelDifferenceTooLong) {
875
878
  // invalidate messages
876
- dRecoverChannelUpdateGap("received channelDifferenceTooLong");
879
+ dGapC("received channelDifferenceTooLong");
877
880
  await this.processChats(difference.chats);
878
881
  await this.processUsers(difference.users);
879
882
  for (const message of difference.messages) {
@@ -886,10 +889,10 @@ export class Client extends ClientAbstract {
886
889
  else {
887
890
  UNREACHABLE();
888
891
  }
889
- dRecoverChannelUpdateGap("processed channelDifferenceTooLong");
892
+ dGapC("processed channelDifferenceTooLong");
890
893
  }
891
894
  else if (difference instanceof types.UpdatesChannelDifferenceEmpty) {
892
- dRecoverChannelUpdateGap("there was no update gap");
895
+ dGapC("there was no update gap");
893
896
  break;
894
897
  }
895
898
  }
@@ -7,6 +7,8 @@ export declare abstract class ClientAbstract {
7
7
  protected connection: Connection;
8
8
  protected transport: Transport;
9
9
  private _dcId;
10
+ private _initialDc;
11
+ get initialDc(): DC;
10
12
  constructor(transportProvider?: import("../transport/transport_provider.js").TransportProvider);
11
13
  get dcId(): number;
12
14
  setDc(dc: DC): MaybePromise<void>;
@@ -2,6 +2,9 @@ import { initTgCrypto } from "../deps.js";
2
2
  import { DEFAULT_INITIAL_DC } from "../constants.js";
3
3
  import { defaultTransportProvider } from "../transport/transport_provider.js";
4
4
  export class ClientAbstract {
5
+ get initialDc() {
6
+ return this._initialDc;
7
+ }
5
8
  constructor(transportProvider = defaultTransportProvider({ initialDc: DEFAULT_INITIAL_DC })) {
6
9
  Object.defineProperty(this, "transportProvider", {
7
10
  enumerable: true,
@@ -27,7 +30,15 @@ export class ClientAbstract {
27
30
  writable: true,
28
31
  value: void 0
29
32
  });
30
- const { connection, transport, dcId } = transportProvider({ cdn: false });
33
+ Object.defineProperty(this, "_initialDc", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ const { initialDc, createTransport } = transportProvider;
40
+ this._initialDc = initialDc;
41
+ const { connection, transport, dcId } = createTransport({ cdn: false });
31
42
  this.connection = connection;
32
43
  this.transport = transport;
33
44
  this._dcId = dcId;
@@ -37,7 +48,7 @@ export class ClientAbstract {
37
48
  }
38
49
  // MaybePromise since `Client` has to deal with `Storage.set()`
39
50
  setDc(dc) {
40
- const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
51
+ const { connection, transport, dcId } = this.transportProvider.createTransport({ dc, cdn: false });
41
52
  this.connection = connection;
42
53
  this.transport = transport;
43
54
  this._dcId = dcId;
@@ -53,7 +64,7 @@ export class ClientAbstract {
53
64
  async reconnect(dc) {
54
65
  await this.disconnect();
55
66
  if (dc) {
56
- this.setDc(dc);
67
+ await this.setDc(dc);
57
68
  }
58
69
  await this.connect();
59
70
  }
@@ -2,15 +2,16 @@ import { assertEquals, assertInstanceOf, debug, factorize, ige256Decrypt, ige256
2
2
  import { publicKeys } from "../constants.js";
3
3
  import { bigIntFromBuffer, getRandomBigInt, modExp } from "../utilities/0_bigint.js";
4
4
  import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
5
+ import { UNREACHABLE } from "../utilities/0_control.js";
5
6
  import { sha1 } from "../utilities/0_hash.js";
6
7
  import { rsaPad } from "../utilities/1_auth.js";
7
8
  import { serialize } from "../tl/1_tl_object.js";
8
- import { packUnencryptedMessage, unpackUnencryptedMessage } from "../utilities/1_message.js";
9
9
  import { ClientDHInnerData, DHGenOK, PQInnerDataDC, ResPQ, ServerDHInnerData, ServerDHParamsOK } from "../tl/2_types.js";
10
10
  import { ReqDHParams, ReqPQMulti, SetClientDHParams } from "../tl/3_functions.js";
11
11
  import { TLReader } from "../tl/3_tl_reader.js";
12
12
  import { ClientAbstract } from "./client_abstract.js";
13
- const d = debug("auth_key_creation");
13
+ import { packUnencryptedMessage, unpackUnencryptedMessage } from "./utilities/0_message.js";
14
+ const d = debug("ClientPlain/createAuthKey");
14
15
  export class ClientPlain extends ClientAbstract {
15
16
  async invoke(function_) {
16
17
  await this.transport.send(packUnencryptedMessage(function_[serialize]()));
@@ -28,10 +29,23 @@ export class ClientPlain extends ClientAbstract {
28
29
  async createAuthKey() {
29
30
  const nonce = getRandomBigInt(16, false, true);
30
31
  d("auth key creation started");
31
- const resPq = await this.invoke(new ReqPQMulti({ nonce }));
32
- assertInstanceOf(resPq, ResPQ);
33
- assertEquals(resPq.nonce, nonce);
34
- d("got res_pq");
32
+ let resPq = null;
33
+ for (let i = 0; i < 10; i++) {
34
+ try {
35
+ d("req_pq_multi [%d]", i + 1);
36
+ resPq = await this.invoke(new ReqPQMulti({ nonce }));
37
+ assertInstanceOf(resPq, ResPQ);
38
+ assertEquals(resPq.nonce, nonce);
39
+ d("got res_pq");
40
+ break;
41
+ }
42
+ catch (err) {
43
+ d("req_pq_multi error: %o", err);
44
+ }
45
+ }
46
+ if (!resPq) {
47
+ UNREACHABLE();
48
+ }
35
49
  const pq_ = bigIntFromBuffer(resPq.pq, false, false);
36
50
  const [p_, q_] = factorize(pq_);
37
51
  d("factorized pq");
@@ -1,5 +1,5 @@
1
- import { Message } from "../tl/6_message.js";
2
- import { MessageContainer } from "../tl/7_message_container.js";
1
+ import { Message } from "../../tl/6_message.js";
2
+ import { MessageContainer } from "../../tl/7_message_container.js";
3
3
  export declare function getMessageId(): bigint;
4
4
  export declare function packUnencryptedMessage(data: Uint8Array): Uint8Array;
5
5
  export declare function unpackUnencryptedMessage(buffer: Uint8Array): {
@@ -1,13 +1,13 @@
1
- import { assertEquals, ige256Decrypt, ige256Encrypt } from "../deps.js";
2
- import { TLRawReader } from "../tl/0_tl_raw_reader.js";
3
- import { TLRawWriter } from "../tl/0_tl_raw_writer.js";
4
- import { id, serialize } from "../tl/1_tl_object.js";
5
- import { TLReader } from "../tl/3_tl_reader.js";
6
- import { RPCResult } from "../tl/5_rpc_result.js";
7
- import { Message } from "../tl/6_message.js";
8
- import { MessageContainer } from "../tl/7_message_container.js";
9
- import { bufferFromBigInt, concat } from "./0_buffer.js";
10
- import { sha256 } from "./0_hash.js";
1
+ import { assertEquals, ige256Decrypt, ige256Encrypt } from "../../deps.js";
2
+ import { TLRawReader } from "../../tl/0_tl_raw_reader.js";
3
+ import { TLRawWriter } from "../../tl/0_tl_raw_writer.js";
4
+ import { id, serialize } from "../../tl/1_tl_object.js";
5
+ import { TLReader } from "../../tl/3_tl_reader.js";
6
+ import { RPCResult } from "../../tl/5_rpc_result.js";
7
+ import { Message } from "../../tl/6_message.js";
8
+ import { MessageContainer } from "../../tl/7_message_container.js";
9
+ import { bufferFromBigInt, concat } from "../../utilities/0_buffer.js";
10
+ import { sha256 } from "../../utilities/0_hash.js";
11
11
  let lastMsgId = 0n;
12
12
  export function getMessageId() {
13
13
  const now = new Date().getTime() / 1000 + 0;
@@ -1,5 +1,5 @@
1
- import * as types from "../tl/2_types.js";
2
- import { sha256 } from "./0_hash.js";
1
+ import * as types from "../../tl/2_types.js";
2
+ import { sha256 } from "../../utilities/0_hash.js";
3
3
  export declare function isSafePrime(primeBytes: Uint8Array, g: number): boolean;
4
4
  export declare const h: typeof sha256;
5
5
  export declare const sh: (data: Uint8Array, salt: Uint8Array) => Promise<Uint8Array>;
@@ -1,7 +1,7 @@
1
- import * as types from "../tl/2_types.js";
2
- import { bufferFromBigInt, concat } from "./0_buffer.js";
3
- import { sha256 } from "./0_hash.js";
4
- import { bigIntFromBuffer, getRandomBigInt, mod, modExp } from "./0_bigint.js";
1
+ import * as types from "../../tl/2_types.js";
2
+ import { bufferFromBigInt, concat } from "../../utilities/0_buffer.js";
3
+ import { sha256 } from "../../utilities/0_hash.js";
4
+ import { bigIntFromBuffer, getRandomBigInt, mod, modExp } from "../../utilities/0_bigint.js";
5
5
  export function isSafePrime(primeBytes, g) {
6
6
  // deno-fmt-ignore
7
7
  const goodPrime = new Uint8Array([
@@ -1,4 +1,5 @@
1
- import { Mutex } from "../deps.js";
1
+ import { debug, Mutex } from "../deps.js";
2
+ const d = debug("ConnectionWebSocket");
2
3
  export class ConnectionWebSocket {
3
4
  constructor(url) {
4
5
  Object.defineProperty(this, "webSocket", {
@@ -43,7 +44,9 @@ export class ConnectionWebSocket {
43
44
  this.nextResolve = null;
44
45
  }
45
46
  };
46
- this.webSocket.onerror = console.error;
47
+ this.webSocket.onerror = (err) => {
48
+ d("WebSocket error: %O", err);
49
+ };
47
50
  }
48
51
  get connected() {
49
52
  return this.webSocket.readyState == WebSocket.OPEN;
@@ -4,12 +4,12 @@ export declare const publicKeys: Map<bigint, [bigint, bigint]>;
4
4
  export declare const VECTOR_CONSTRUCTOR = 481674261;
5
5
  export declare const DEFAULT_INITIAL_DC: DC;
6
6
  export declare const LAYER = 158;
7
- export declare const DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
8
- export declare const DEFAULT_DEVICE_MODEL = "Krutaya Device";
9
- export declare const DEFAULT_LANG_CODE = "en";
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.902";
8
+ export declare const DEFAULT_DEVICE_MODEL: string;
9
+ export declare const DEFAULT_LANG_CODE: string;
10
10
  export declare const DEFAULT_LANG_PACK = "";
11
- export declare const DEFAULT_SYSTEM_LANG_CODE = "en";
12
- export declare const DEFAULT_SYSTEM_VERSION = "1.0";
11
+ export declare const DEFAULT_SYSTEM_LANG_CODE: string;
12
+ export declare const DEFAULT_SYSTEM_VERSION: string;
13
13
  export declare const USERNAME_TTL = 86400;
14
14
  export declare const MAX_CHAT_ID = 999999999999;
15
15
  export declare const MAX_CHANNEL_ID = 997852516352;
package/esm/constants.js CHANGED
@@ -61,13 +61,14 @@ export const publicKeys = new Map([
61
61
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
62
62
  export const DEFAULT_INITIAL_DC = "2-test";
63
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";
64
+ export const DEFAULT_APP_VERSION = "MTKruto 0.0.902";
65
+ // @ts-ignore: lib
66
+ export const DEFAULT_DEVICE_MODEL = typeof Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : Deno.build.os + "-" + Deno.build.arch;
67
+ export const DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
68
68
  export const DEFAULT_LANG_PACK = "";
69
- export const DEFAULT_SYSTEM_LANG_CODE = "en";
70
- export const DEFAULT_SYSTEM_VERSION = "1.0";
69
+ export const DEFAULT_SYSTEM_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
70
+ // @ts-ignore: lib
71
+ export const DEFAULT_SYSTEM_VERSION = typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : "Node.js/" + process.versions.node : navigator.userAgent;
71
72
  export const USERNAME_TTL = 86400;
72
73
  export const MAX_CHAT_ID = 999999999999;
73
74
  export const MAX_CHANNEL_ID = 997852516352;
package/esm/deps.js CHANGED
@@ -4,6 +4,6 @@ export { gunzip, gzip } from "./deps/raw.githubusercontent.com/MTKruto/compress/
4
4
  export { Mutex } from "async-mutex";
5
5
  export { Parser } from "./deps/deno.land/x/html_parser@v0.1.3/src/mod.js";
6
6
  import { debug as debug_ } from "./deps/raw.githubusercontent.com/MTKruto/debug/master/mod.js";
7
- export const debug = (v) => debug_(`mtkruto__${v}`);
7
+ export const debug = (v) => debug_(`mtkruto/${v}`);
8
8
  export { queue } from "./deps/deno.land/x/q@v0.0.1/mod.js";
9
9
  export { decode as base64Decode, encode as base64Encode } from "./deps/deno.land/std@0.190.0/encoding/base64.js";
package/esm/mod.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { checkPassword } from "./utilities/1_password.js";
2
1
  import { getRandomId } from "./utilities/0_bigint.js";
2
+ import { checkPassword } from "./client/utilities/0_password.js";
3
3
  export declare const utils: {
4
4
  checkPassword: typeof checkPassword;
5
5
  getRandomId: typeof getRandomId;
package/esm/mod.js CHANGED
@@ -1,5 +1,5 @@
1
- import { checkPassword } from "./utilities/1_password.js";
2
1
  import { getRandomId } from "./utilities/0_bigint.js";
2
+ import { checkPassword } from "./client/utilities/0_password.js";
3
3
  export const utils = { checkPassword, getRandomId };
4
4
  export { as } from "./tl/1_tl_object.js";
5
5
  export * as types from "./tl/2_types.js";
@@ -1,5 +1,5 @@
1
1
  import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
2
- import { getObfuscationParameters } from "../utilities/1_obfuscation.js";
2
+ import { getObfuscationParameters } from "./utilities/0_obfuscation.js";
3
3
  import { Transport } from "./transport.js";
4
4
  export class TransportAbridged extends Transport {
5
5
  constructor(connection, obfuscated = false) {
@@ -1,5 +1,5 @@
1
1
  import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
2
- import { getObfuscationParameters } from "../utilities/1_obfuscation.js";
2
+ import { getObfuscationParameters } from "./utilities/0_obfuscation.js";
3
3
  import { Transport } from "./transport.js";
4
4
  export class TransportIntermediate extends Transport {
5
5
  constructor(connection, obfuscated = false) {
@@ -5,10 +5,13 @@ export interface TransportProviderParams {
5
5
  dc?: DC;
6
6
  cdn: boolean;
7
7
  }
8
- export type TransportProvider = (params: TransportProviderParams) => {
9
- connection: Connection;
10
- transport: Transport;
11
- dcId: number;
8
+ export type TransportProvider = {
9
+ initialDc: DC;
10
+ createTransport: (params: TransportProviderParams) => {
11
+ connection: Connection;
12
+ transport: Transport;
13
+ dcId: number;
14
+ };
12
15
  };
13
16
  export interface TransportProviderCreatorParams {
14
17
  initialDc: DC;
@@ -11,13 +11,16 @@ const dcToNameMap = {
11
11
  "5": "flora",
12
12
  };
13
13
  export const defaultTransportProvider = ({ initialDc, wss }) => {
14
- return ({ dc, cdn }) => {
15
- dc ??= initialDc;
16
- wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
17
- const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
18
- const connection = new ConnectionWebSocket(url);
19
- const transport = new TransportIntermediate(connection, true);
20
- const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
21
- return { connection, transport, dcId };
14
+ return {
15
+ initialDc,
16
+ createTransport: ({ dc, cdn }) => {
17
+ dc ??= initialDc;
18
+ wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
19
+ const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
20
+ const connection = new ConnectionWebSocket(url);
21
+ const transport = new TransportIntermediate(connection, true);
22
+ const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
23
+ return { connection, transport, dcId };
24
+ },
22
25
  };
23
26
  };
@@ -1,5 +1,5 @@
1
- import { Connection } from "../connection/connection.js";
2
- import { CTR } from "./0_crypto.js";
1
+ import { CTR } from "../../utilities/0_crypto.js";
2
+ import { Connection } from "../../connection/connection.js";
3
3
  export declare function getObfuscationParameters(protocol: number, connection: Connection): Promise<{
4
4
  encryptionCTR: CTR;
5
5
  decryptionCTR: CTR;
@@ -1,5 +1,5 @@
1
- import { bufferFromBigInt, concat } from "./0_buffer.js";
2
- import { CTR } from "./0_crypto.js";
1
+ import { bufferFromBigInt, concat } from "../../utilities/0_buffer.js";
2
+ import { CTR } from "../../utilities/0_crypto.js";
3
3
  export async function getObfuscationParameters(protocol, connection) {
4
4
  const dc = 0xFCFF;
5
5
  let init;
@@ -1,2 +1,2 @@
1
- import { MessageEntity } from "../types/0_message_entity.js";
1
+ import { MessageEntity } from "../0_message_entity.js";
2
2
  export declare function parseHtml(html: string): readonly [string, MessageEntity[]];
@@ -1,5 +1,5 @@
1
- import { Parser } from "../deps.js";
2
- import { MessageEntityType } from "../types/0_message_entity.js";
1
+ import { Parser } from "../../deps.js";
2
+ import { MessageEntityType } from "../0_message_entity.js";
3
3
  export function parseHtml(html) {
4
4
  let text = "";
5
5
  const entities = new Array();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "@mtkruto/browser",
5
- "version": "0.0.900",
5
+ "version": "0.0.902",
6
6
  "description": "MTKruto for browsers",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -29,9 +29,6 @@ const constants_js_1 = require("../constants.js");
29
29
  const _0_bigint_js_1 = require("../utilities/0_bigint.js");
30
30
  const _0_control_js_1 = require("../utilities/0_control.js");
31
31
  const _0_hash_js_1 = require("../utilities/0_hash.js");
32
- const _0_html_js_1 = require("../utilities/0_html.js");
33
- const _1_message_js_1 = require("../utilities/1_message.js");
34
- const _1_password_js_1 = require("../utilities/1_password.js");
35
32
  const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
36
33
  const types = __importStar(require("../tl/2_types.js"));
37
34
  const functions = __importStar(require("../tl/3_functions.js"));
@@ -42,15 +39,20 @@ const _7_message_container_js_1 = require("../tl/7_message_container.js");
42
39
  const client_abstract_js_1 = require("./client_abstract.js");
43
40
  const client_plain_js_1 = require("./client_plain.js");
44
41
  const storage_memory_js_1 = require("../storage/storage_memory.js");
42
+ const _0_html_js_1 = require("../types/utilities/0_html.js");
45
43
  const _0_message_entity_js_1 = require("../types/0_message_entity.js");
46
44
  const _0_reply_keyboard_remove_js_1 = require("../types/0_reply_keyboard_remove.js");
47
45
  const _0_force_reply_js_1 = require("../types/0_force_reply.js");
48
46
  const _2_reply_keyboard_markup_js_1 = require("../types/2_reply_keyboard_markup.js");
49
47
  const _2_inline_keyboard_markup_js_1 = require("../types/2_inline_keyboard_markup.js");
50
48
  const _3_message_js_1 = require("../types/3_message.js"); // high-level wrapper for Telegram API's message
51
- const d = (0, deps_js_1.debug)("client");
52
- const dRecoverUpdateGap = (0, deps_js_1.debug)("client_recoverUpdateGap");
53
- const dRecoverChannelUpdateGap = (0, deps_js_1.debug)("client_recoverChannelUpdateGap");
49
+ const _0_message_js_1 = require("./utilities/0_message.js");
50
+ const _0_password_js_1 = require("./utilities/0_password.js");
51
+ const d = (0, deps_js_1.debug)("Client");
52
+ const dGap = (0, deps_js_1.debug)("Client/recoverUpdateGap");
53
+ const dGapC = (0, deps_js_1.debug)("Client/recoverChannelUpdateGap");
54
+ const dAuth = (0, deps_js_1.debug)("Client/authorize");
55
+ const dRecv = (0, deps_js_1.debug)("Client/receiveLoop");
54
56
  const UPDATE_GAP = Symbol();
55
57
  exports.getEntity = Symbol();
56
58
  exports.restartAuth = Symbol();
@@ -187,7 +189,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
187
189
  if (body instanceof types.GZIPPacked) {
188
190
  body = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(body.packedData)).readObject();
189
191
  }
190
- d("received %s", body.constructor.name);
192
+ dRecv("received %s", body.constructor.name);
191
193
  if (body instanceof types.Updates || body instanceof types.TypeUpdate) {
192
194
  await this.processUpdates(body);
193
195
  }
@@ -197,10 +199,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
197
199
  result = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(result.packedData)).readObject();
198
200
  }
199
201
  if (result instanceof types.RPCError) {
200
- d("RPCResult: %d %s", result.errorCode, result.errorMessage);
202
+ dRecv("RPCResult: %d %s", result.errorCode, result.errorMessage);
201
203
  }
202
204
  else {
203
- d("RPCResult: %s", result.constructor.name);
205
+ dRecv("RPCResult: %s", result.constructor.name);
204
206
  }
205
207
  if (result instanceof types.Updates || result instanceof types.TypeUpdate) {
206
208
  await this.processUpdates(result);
@@ -273,6 +275,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
273
275
  if (await this.storage.getDc() != dc) {
274
276
  await this.storage.setDc(dc);
275
277
  await this.storage.setAuthKey(null);
278
+ await this.storage.getAuthKey();
276
279
  }
277
280
  super.setDc(dc);
278
281
  }
@@ -314,7 +317,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
314
317
  }
315
318
  await super.connect();
316
319
  if (dc == null) {
317
- await this.storage.setDc(constants_js_1.DEFAULT_INITIAL_DC);
320
+ await this.storage.setDc(this.transportProvider.initialDc);
318
321
  }
319
322
  d("enrypted client connected");
320
323
  this.receiveLoop();
@@ -349,7 +352,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
349
352
  if (!this.apiHash) {
350
353
  throw new Error("apiHash not set");
351
354
  }
352
- d("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
355
+ dAuth("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
353
356
  await this.invoke(new functions.InitConnection({
354
357
  apiId: this.apiId,
355
358
  appVersion: this.appVersion,
@@ -372,10 +375,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
372
375
  if (ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
373
376
  try {
374
377
  const password = typeof params.password === "string" ? params.password : await params.password();
375
- const input = await (0, _1_password_js_1.checkPassword)(password, ap);
378
+ const input = await (0, _0_password_js_1.checkPassword)(password, ap);
376
379
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
377
380
  await this.storage.setAccountType("user");
378
- d("authorized as user");
381
+ dAuth("authorized as user");
379
382
  break;
380
383
  }
381
384
  catch (err) {
@@ -411,7 +414,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
411
414
  try {
412
415
  if (params instanceof types.AuthExportedAuthorization) {
413
416
  await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
414
- d("authorization imported");
417
+ dAuth("authorization imported");
415
418
  }
416
419
  else if (typeof params == "object") {
417
420
  while (true) {
@@ -427,7 +430,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
427
430
  phoneNumber,
428
431
  settings: new types.CodeSettings(),
429
432
  }));
430
- d("verification code sent");
433
+ dAuth("verification code sent");
431
434
  if (sentCode instanceof types.AuthSentCode) {
432
435
  while (true) {
433
436
  const phoneCode = typeof params.code === "string" ? params.code : await params.code();
@@ -439,7 +442,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
439
442
  else {
440
443
  signedIn = true;
441
444
  await this.storage.setAccountType("user");
442
- d("authorized as user");
445
+ dAuth("authorized as user");
443
446
  break;
444
447
  }
445
448
  }
@@ -474,7 +477,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
474
477
  else {
475
478
  await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
476
479
  await this.storage.setAccountType("bot");
477
- d("authorized as bot");
480
+ dAuth("authorized as bot");
478
481
  }
479
482
  }
480
483
  catch (err) {
@@ -533,10 +536,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
533
536
  }
534
537
  let decrypted;
535
538
  try {
536
- decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.auth.key, this.auth.id, this.sessionId);
539
+ decrypted = await (0, _0_message_js_1.decryptMessage)(buffer, this.auth.key, this.auth.id, this.sessionId);
537
540
  }
538
541
  catch (err) {
539
- d("failed to decrypt message: %o", err);
542
+ dRecv("failed to decrypt message: %o", err);
540
543
  continue;
541
544
  }
542
545
  const messages = decrypted instanceof _7_message_container_js_1.MessageContainer ? decrypted.messages : [decrypted];
@@ -565,8 +568,8 @@ class Client extends client_abstract_js_1.ClientAbstract {
565
568
  seqNo++;
566
569
  this.state.seqNo++;
567
570
  }
568
- const message = new _6_message_js_1.Message((0, _1_message_js_1.getMessageId)(), seqNo, function_);
569
- await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
571
+ const message = new _6_message_js_1.Message((0, _0_message_js_1.getMessageId)(), seqNo, function_);
572
+ await this.transport.send(await (0, _0_message_js_1.encryptMessage)(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
570
573
  d("invoked %s", function_.constructor.name);
571
574
  if (noWait) {
572
575
  return;
@@ -825,7 +828,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
825
828
  return localState;
826
829
  }
827
830
  async recoverUpdateGap(source) {
828
- dRecoverUpdateGap("recovering from update gap [%s]", source);
831
+ dGap("recovering from update gap [%s]", source);
829
832
  const release = await this.updateGapRecoveryMutex.acquire();
830
833
  try {
831
834
  let state = await this.getLocalState();
@@ -841,7 +844,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
841
844
  await this.applyUpdateNoGap(update);
842
845
  }
843
846
  if (difference instanceof types.UpdatesDifference) {
844
- dRecoverUpdateGap("recovered from update gap");
847
+ dGap("recovered from update gap");
845
848
  break;
846
849
  }
847
850
  else if (difference instanceof types.UpdatesDifferenceSlice) {
@@ -854,11 +857,11 @@ class Client extends client_abstract_js_1.ClientAbstract {
854
857
  else if (difference instanceof types.UpdatesDifferenceTooLong) {
855
858
  // stored messages should be invalidated in case we store messages in the future
856
859
  state.pts = difference.pts;
857
- dRecoverUpdateGap("received differenceTooLong");
860
+ dGap("received differenceTooLong");
858
861
  }
859
862
  else if (difference instanceof types.UpdatesDifferenceEmpty) {
860
863
  await this.setUpdateStateDate(difference.date);
861
- dRecoverUpdateGap("there was no update gap");
864
+ dGap("there was no update gap");
862
865
  break;
863
866
  }
864
867
  else {
@@ -871,7 +874,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
871
874
  }
872
875
  }
873
876
  async recoverChannelUpdateGap(channelId, source) {
874
- dRecoverChannelUpdateGap("recovering channel update gap [%o, %s]", channelId, source);
877
+ dGapC("recovering channel update gap [%o, %s]", channelId, source);
875
878
  const release = await this.updateGapRecoveryMutex.acquire();
876
879
  try {
877
880
  const pts_ = await this.storage.getChannelPts(channelId);
@@ -894,12 +897,12 @@ class Client extends client_abstract_js_1.ClientAbstract {
894
897
  await this.applyUpdateNoGap(update, false);
895
898
  }
896
899
  await this.storage.setChannelPts(channelId, difference.pts);
897
- dRecoverChannelUpdateGap("recovered from update gap");
900
+ dGapC("recovered from update gap [%o, %s]", channelId, source);
898
901
  break;
899
902
  }
900
903
  else if (difference instanceof types.UpdatesChannelDifferenceTooLong) {
901
904
  // invalidate messages
902
- dRecoverChannelUpdateGap("received channelDifferenceTooLong");
905
+ dGapC("received channelDifferenceTooLong");
903
906
  await this.processChats(difference.chats);
904
907
  await this.processUsers(difference.users);
905
908
  for (const message of difference.messages) {
@@ -912,10 +915,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
912
915
  else {
913
916
  (0, _0_control_js_1.UNREACHABLE)();
914
917
  }
915
- dRecoverChannelUpdateGap("processed channelDifferenceTooLong");
918
+ dGapC("processed channelDifferenceTooLong");
916
919
  }
917
920
  else if (difference instanceof types.UpdatesChannelDifferenceEmpty) {
918
- dRecoverChannelUpdateGap("there was no update gap");
921
+ dGapC("there was no update gap");
919
922
  break;
920
923
  }
921
924
  }
@@ -7,6 +7,8 @@ export declare abstract class ClientAbstract {
7
7
  protected connection: Connection;
8
8
  protected transport: Transport;
9
9
  private _dcId;
10
+ private _initialDc;
11
+ get initialDc(): DC;
10
12
  constructor(transportProvider?: import("../transport/transport_provider.js").TransportProvider);
11
13
  get dcId(): number;
12
14
  setDc(dc: DC): MaybePromise<void>;
@@ -5,6 +5,9 @@ const deps_js_1 = require("../deps.js");
5
5
  const constants_js_1 = require("../constants.js");
6
6
  const transport_provider_js_1 = require("../transport/transport_provider.js");
7
7
  class ClientAbstract {
8
+ get initialDc() {
9
+ return this._initialDc;
10
+ }
8
11
  constructor(transportProvider = (0, transport_provider_js_1.defaultTransportProvider)({ initialDc: constants_js_1.DEFAULT_INITIAL_DC })) {
9
12
  Object.defineProperty(this, "transportProvider", {
10
13
  enumerable: true,
@@ -30,7 +33,15 @@ class ClientAbstract {
30
33
  writable: true,
31
34
  value: void 0
32
35
  });
33
- const { connection, transport, dcId } = transportProvider({ cdn: false });
36
+ Object.defineProperty(this, "_initialDc", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ const { initialDc, createTransport } = transportProvider;
43
+ this._initialDc = initialDc;
44
+ const { connection, transport, dcId } = createTransport({ cdn: false });
34
45
  this.connection = connection;
35
46
  this.transport = transport;
36
47
  this._dcId = dcId;
@@ -40,7 +51,7 @@ class ClientAbstract {
40
51
  }
41
52
  // MaybePromise since `Client` has to deal with `Storage.set()`
42
53
  setDc(dc) {
43
- const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
54
+ const { connection, transport, dcId } = this.transportProvider.createTransport({ dc, cdn: false });
44
55
  this.connection = connection;
45
56
  this.transport = transport;
46
57
  this._dcId = dcId;
@@ -56,7 +67,7 @@ class ClientAbstract {
56
67
  async reconnect(dc) {
57
68
  await this.disconnect();
58
69
  if (dc) {
59
- this.setDc(dc);
70
+ await this.setDc(dc);
60
71
  }
61
72
  await this.connect();
62
73
  }
@@ -5,18 +5,19 @@ const deps_js_1 = require("../deps.js");
5
5
  const constants_js_1 = require("../constants.js");
6
6
  const _0_bigint_js_1 = require("../utilities/0_bigint.js");
7
7
  const _0_buffer_js_1 = require("../utilities/0_buffer.js");
8
+ const _0_control_js_1 = require("../utilities/0_control.js");
8
9
  const _0_hash_js_1 = require("../utilities/0_hash.js");
9
10
  const _1_auth_js_1 = require("../utilities/1_auth.js");
10
11
  const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
11
- const _1_message_js_1 = require("../utilities/1_message.js");
12
12
  const _2_types_js_1 = require("../tl/2_types.js");
13
13
  const _3_functions_js_1 = require("../tl/3_functions.js");
14
14
  const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
15
15
  const client_abstract_js_1 = require("./client_abstract.js");
16
- const d = (0, deps_js_1.debug)("auth_key_creation");
16
+ const _0_message_js_1 = require("./utilities/0_message.js");
17
+ const d = (0, deps_js_1.debug)("ClientPlain/createAuthKey");
17
18
  class ClientPlain extends client_abstract_js_1.ClientAbstract {
18
19
  async invoke(function_) {
19
- await this.transport.send((0, _1_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize]()));
20
+ await this.transport.send((0, _0_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize]()));
20
21
  const buffer = await this.transport.receive();
21
22
  if (buffer.length == 4) {
22
23
  const int = (0, _0_bigint_js_1.bigIntFromBuffer)(buffer, true, true);
@@ -24,17 +25,30 @@ class ClientPlain extends client_abstract_js_1.ClientAbstract {
24
25
  throw new Error("-404");
25
26
  }
26
27
  }
27
- const { message } = (0, _1_message_js_1.unpackUnencryptedMessage)(buffer);
28
+ const { message } = (0, _0_message_js_1.unpackUnencryptedMessage)(buffer);
28
29
  const reader = new _3_tl_reader_js_1.TLReader(message);
29
30
  return reader.readObject();
30
31
  }
31
32
  async createAuthKey() {
32
33
  const nonce = (0, _0_bigint_js_1.getRandomBigInt)(16, false, true);
33
34
  d("auth key creation started");
34
- const resPq = await this.invoke(new _3_functions_js_1.ReqPQMulti({ nonce }));
35
- (0, deps_js_1.assertInstanceOf)(resPq, _2_types_js_1.ResPQ);
36
- (0, deps_js_1.assertEquals)(resPq.nonce, nonce);
37
- d("got res_pq");
35
+ let resPq = null;
36
+ for (let i = 0; i < 10; i++) {
37
+ try {
38
+ d("req_pq_multi [%d]", i + 1);
39
+ resPq = await this.invoke(new _3_functions_js_1.ReqPQMulti({ nonce }));
40
+ (0, deps_js_1.assertInstanceOf)(resPq, _2_types_js_1.ResPQ);
41
+ (0, deps_js_1.assertEquals)(resPq.nonce, nonce);
42
+ d("got res_pq");
43
+ break;
44
+ }
45
+ catch (err) {
46
+ d("req_pq_multi error: %o", err);
47
+ }
48
+ }
49
+ if (!resPq) {
50
+ (0, _0_control_js_1.UNREACHABLE)();
51
+ }
38
52
  const pq_ = (0, _0_bigint_js_1.bigIntFromBuffer)(resPq.pq, false, false);
39
53
  const [p_, q_] = (0, deps_js_1.factorize)(pq_);
40
54
  d("factorized pq");
@@ -1,5 +1,5 @@
1
- import { Message } from "../tl/6_message.js";
2
- import { MessageContainer } from "../tl/7_message_container.js";
1
+ import { Message } from "../../tl/6_message.js";
2
+ import { MessageContainer } from "../../tl/7_message_container.js";
3
3
  export declare function getMessageId(): bigint;
4
4
  export declare function packUnencryptedMessage(data: Uint8Array): Uint8Array;
5
5
  export declare function unpackUnencryptedMessage(buffer: Uint8Array): {
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.decryptMessage = exports.encryptMessage = exports.unpackUnencryptedMessage = exports.packUnencryptedMessage = exports.getMessageId = void 0;
4
- const deps_js_1 = require("../deps.js");
5
- const _0_tl_raw_reader_js_1 = require("../tl/0_tl_raw_reader.js");
6
- const _0_tl_raw_writer_js_1 = require("../tl/0_tl_raw_writer.js");
7
- const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
8
- const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
9
- const _5_rpc_result_js_1 = require("../tl/5_rpc_result.js");
10
- const _6_message_js_1 = require("../tl/6_message.js");
11
- const _7_message_container_js_1 = require("../tl/7_message_container.js");
12
- const _0_buffer_js_1 = require("./0_buffer.js");
13
- const _0_hash_js_1 = require("./0_hash.js");
4
+ const deps_js_1 = require("../../deps.js");
5
+ const _0_tl_raw_reader_js_1 = require("../../tl/0_tl_raw_reader.js");
6
+ const _0_tl_raw_writer_js_1 = require("../../tl/0_tl_raw_writer.js");
7
+ const _1_tl_object_js_1 = require("../../tl/1_tl_object.js");
8
+ const _3_tl_reader_js_1 = require("../../tl/3_tl_reader.js");
9
+ const _5_rpc_result_js_1 = require("../../tl/5_rpc_result.js");
10
+ const _6_message_js_1 = require("../../tl/6_message.js");
11
+ const _7_message_container_js_1 = require("../../tl/7_message_container.js");
12
+ const _0_buffer_js_1 = require("../../utilities/0_buffer.js");
13
+ const _0_hash_js_1 = require("../../utilities/0_hash.js");
14
14
  let lastMsgId = 0n;
15
15
  function getMessageId() {
16
16
  const now = new Date().getTime() / 1000 + 0;
@@ -1,5 +1,5 @@
1
- import * as types from "../tl/2_types.js";
2
- import { sha256 } from "./0_hash.js";
1
+ import * as types from "../../tl/2_types.js";
2
+ import { sha256 } from "../../utilities/0_hash.js";
3
3
  export declare function isSafePrime(primeBytes: Uint8Array, g: number): boolean;
4
4
  export declare const h: typeof sha256;
5
5
  export declare const sh: (data: Uint8Array, salt: Uint8Array) => Promise<Uint8Array>;
@@ -24,10 +24,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.checkPassword = exports.pad = exports.isGoodModExpFirst = exports.ph2 = exports.pbkdf2 = exports.ph1 = exports.sh = exports.h = exports.isSafePrime = void 0;
27
- const types = __importStar(require("../tl/2_types.js"));
28
- const _0_buffer_js_1 = require("./0_buffer.js");
29
- const _0_hash_js_1 = require("./0_hash.js");
30
- const _0_bigint_js_1 = require("./0_bigint.js");
27
+ const types = __importStar(require("../../tl/2_types.js"));
28
+ const _0_buffer_js_1 = require("../../utilities/0_buffer.js");
29
+ const _0_hash_js_1 = require("../../utilities/0_hash.js");
30
+ const _0_bigint_js_1 = require("../../utilities/0_bigint.js");
31
31
  function isSafePrime(primeBytes, g) {
32
32
  // deno-fmt-ignore
33
33
  const goodPrime = new Uint8Array([
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConnectionWebSocket = void 0;
4
4
  const deps_js_1 = require("../deps.js");
5
+ const d = (0, deps_js_1.debug)("ConnectionWebSocket");
5
6
  class ConnectionWebSocket {
6
7
  constructor(url) {
7
8
  Object.defineProperty(this, "webSocket", {
@@ -46,7 +47,9 @@ class ConnectionWebSocket {
46
47
  this.nextResolve = null;
47
48
  }
48
49
  };
49
- this.webSocket.onerror = console.error;
50
+ this.webSocket.onerror = (err) => {
51
+ d("WebSocket error: %O", err);
52
+ };
50
53
  }
51
54
  get connected() {
52
55
  return this.webSocket.readyState == WebSocket.OPEN;
@@ -4,12 +4,12 @@ export declare const publicKeys: Map<bigint, [bigint, bigint]>;
4
4
  export declare const VECTOR_CONSTRUCTOR = 481674261;
5
5
  export declare const DEFAULT_INITIAL_DC: DC;
6
6
  export declare const LAYER = 158;
7
- export declare const DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
8
- export declare const DEFAULT_DEVICE_MODEL = "Krutaya Device";
9
- export declare const DEFAULT_LANG_CODE = "en";
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.902";
8
+ export declare const DEFAULT_DEVICE_MODEL: string;
9
+ export declare const DEFAULT_LANG_CODE: string;
10
10
  export declare const DEFAULT_LANG_PACK = "";
11
- export declare const DEFAULT_SYSTEM_LANG_CODE = "en";
12
- export declare const DEFAULT_SYSTEM_VERSION = "1.0";
11
+ export declare const DEFAULT_SYSTEM_LANG_CODE: string;
12
+ export declare const DEFAULT_SYSTEM_VERSION: string;
13
13
  export declare const USERNAME_TTL = 86400;
14
14
  export declare const MAX_CHAT_ID = 999999999999;
15
15
  export declare const MAX_CHANNEL_ID = 997852516352;
@@ -64,13 +64,14 @@ exports.publicKeys = new Map([
64
64
  exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
65
65
  exports.DEFAULT_INITIAL_DC = "2-test";
66
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";
67
+ exports.DEFAULT_APP_VERSION = "MTKruto 0.0.902";
68
+ // @ts-ignore: lib
69
+ exports.DEFAULT_DEVICE_MODEL = typeof Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : Deno.build.os + "-" + Deno.build.arch;
70
+ exports.DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
71
71
  exports.DEFAULT_LANG_PACK = "";
72
- exports.DEFAULT_SYSTEM_LANG_CODE = "en";
73
- exports.DEFAULT_SYSTEM_VERSION = "1.0";
72
+ exports.DEFAULT_SYSTEM_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
73
+ // @ts-ignore: lib
74
+ exports.DEFAULT_SYSTEM_VERSION = typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : "Node.js/" + process.versions.node : navigator.userAgent;
74
75
  exports.USERNAME_TTL = 86400;
75
76
  exports.MAX_CHAT_ID = 999999999999;
76
77
  exports.MAX_CHANNEL_ID = 997852516352;
package/script/deps.js CHANGED
@@ -31,7 +31,7 @@ Object.defineProperty(exports, "Mutex", { enumerable: true, get: function () { r
31
31
  var mod_js_2 = require("./deps/deno.land/x/html_parser@v0.1.3/src/mod.js");
32
32
  Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return mod_js_2.Parser; } });
33
33
  const mod_js_3 = require("./deps/raw.githubusercontent.com/MTKruto/debug/master/mod.js");
34
- const debug = (v) => (0, mod_js_3.debug)(`mtkruto__${v}`);
34
+ const debug = (v) => (0, mod_js_3.debug)(`mtkruto/${v}`);
35
35
  exports.debug = debug;
36
36
  var mod_js_4 = require("./deps/deno.land/x/q@v0.0.1/mod.js");
37
37
  Object.defineProperty(exports, "queue", { enumerable: true, get: function () { return mod_js_4.queue; } });
package/script/mod.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { checkPassword } from "./utilities/1_password.js";
2
1
  import { getRandomId } from "./utilities/0_bigint.js";
2
+ import { checkPassword } from "./client/utilities/0_password.js";
3
3
  export declare const utils: {
4
4
  checkPassword: typeof checkPassword;
5
5
  getRandomId: typeof getRandomId;
package/script/mod.js CHANGED
@@ -27,9 +27,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
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.as = exports.utils = void 0;
30
- const _1_password_js_1 = require("./utilities/1_password.js");
31
30
  const _0_bigint_js_1 = require("./utilities/0_bigint.js");
32
- exports.utils = { checkPassword: _1_password_js_1.checkPassword, getRandomId: _0_bigint_js_1.getRandomId };
31
+ const _0_password_js_1 = require("./client/utilities/0_password.js");
32
+ exports.utils = { checkPassword: _0_password_js_1.checkPassword, getRandomId: _0_bigint_js_1.getRandomId };
33
33
  var _1_tl_object_js_1 = require("./tl/1_tl_object.js");
34
34
  Object.defineProperty(exports, "as", { enumerable: true, get: function () { return _1_tl_object_js_1.as; } });
35
35
  exports.types = __importStar(require("./tl/2_types.js"));
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TransportAbridged = void 0;
4
4
  const _0_buffer_js_1 = require("../utilities/0_buffer.js");
5
- const _1_obfuscation_js_1 = require("../utilities/1_obfuscation.js");
5
+ const _0_obfuscation_js_1 = require("./utilities/0_obfuscation.js");
6
6
  const transport_js_1 = require("./transport.js");
7
7
  class TransportAbridged extends transport_js_1.Transport {
8
8
  constructor(connection, obfuscated = false) {
@@ -23,7 +23,7 @@ class TransportAbridged extends transport_js_1.Transport {
23
23
  async initialize() {
24
24
  if (!this.initialized) {
25
25
  if (this.obfuscated) {
26
- this.obfuscationParameters = await (0, _1_obfuscation_js_1.getObfuscationParameters)(0xEFEFEFEF, this.connection);
26
+ this.obfuscationParameters = await (0, _0_obfuscation_js_1.getObfuscationParameters)(0xEFEFEFEF, this.connection);
27
27
  }
28
28
  else {
29
29
  await this.connection.write(new Uint8Array([0xEF]));
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TransportIntermediate = void 0;
4
4
  const _0_buffer_js_1 = require("../utilities/0_buffer.js");
5
- const _1_obfuscation_js_1 = require("../utilities/1_obfuscation.js");
5
+ const _0_obfuscation_js_1 = require("./utilities/0_obfuscation.js");
6
6
  const transport_js_1 = require("./transport.js");
7
7
  class TransportIntermediate extends transport_js_1.Transport {
8
8
  constructor(connection, obfuscated = false) {
@@ -23,7 +23,7 @@ class TransportIntermediate extends transport_js_1.Transport {
23
23
  async initialize() {
24
24
  if (!this.initialized) {
25
25
  if (this.obfuscated) {
26
- this.obfuscationParameters = await (0, _1_obfuscation_js_1.getObfuscationParameters)(0xEEEEEEEE, this.connection);
26
+ this.obfuscationParameters = await (0, _0_obfuscation_js_1.getObfuscationParameters)(0xEEEEEEEE, this.connection);
27
27
  }
28
28
  else {
29
29
  await this.connection.write(new Uint8Array([0xEE, 0xEE, 0xEE, 0xEE]));
@@ -5,10 +5,13 @@ export interface TransportProviderParams {
5
5
  dc?: DC;
6
6
  cdn: boolean;
7
7
  }
8
- export type TransportProvider = (params: TransportProviderParams) => {
9
- connection: Connection;
10
- transport: Transport;
11
- dcId: number;
8
+ export type TransportProvider = {
9
+ initialDc: DC;
10
+ createTransport: (params: TransportProviderParams) => {
11
+ connection: Connection;
12
+ transport: Transport;
13
+ dcId: number;
14
+ };
12
15
  };
13
16
  export interface TransportProviderCreatorParams {
14
17
  initialDc: DC;
@@ -14,14 +14,17 @@ const dcToNameMap = {
14
14
  "5": "flora",
15
15
  };
16
16
  const defaultTransportProvider = ({ initialDc, wss }) => {
17
- return ({ dc, cdn }) => {
18
- dc ??= initialDc;
19
- wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
20
- const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
21
- const connection = new connection_web_socket_js_1.ConnectionWebSocket(url);
22
- const transport = new transport_intermediate_js_1.TransportIntermediate(connection, true);
23
- const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
24
- return { connection, transport, dcId };
17
+ return {
18
+ initialDc,
19
+ createTransport: ({ dc, cdn }) => {
20
+ dc ??= initialDc;
21
+ wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
22
+ const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
23
+ const connection = new connection_web_socket_js_1.ConnectionWebSocket(url);
24
+ const transport = new transport_intermediate_js_1.TransportIntermediate(connection, true);
25
+ const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
26
+ return { connection, transport, dcId };
27
+ },
25
28
  };
26
29
  };
27
30
  exports.defaultTransportProvider = defaultTransportProvider;
@@ -1,5 +1,5 @@
1
- import { Connection } from "../connection/connection.js";
2
- import { CTR } from "./0_crypto.js";
1
+ import { CTR } from "../../utilities/0_crypto.js";
2
+ import { Connection } from "../../connection/connection.js";
3
3
  export declare function getObfuscationParameters(protocol: number, connection: Connection): Promise<{
4
4
  encryptionCTR: CTR;
5
5
  decryptionCTR: CTR;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getObfuscationParameters = void 0;
4
- const _0_buffer_js_1 = require("./0_buffer.js");
5
- const _0_crypto_js_1 = require("./0_crypto.js");
4
+ const _0_buffer_js_1 = require("../../utilities/0_buffer.js");
5
+ const _0_crypto_js_1 = require("../../utilities/0_crypto.js");
6
6
  async function getObfuscationParameters(protocol, connection) {
7
7
  const dc = 0xFCFF;
8
8
  let init;
@@ -1,2 +1,2 @@
1
- import { MessageEntity } from "../types/0_message_entity.js";
1
+ import { MessageEntity } from "../0_message_entity.js";
2
2
  export declare function parseHtml(html: string): readonly [string, MessageEntity[]];
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseHtml = void 0;
4
- const deps_js_1 = require("../deps.js");
5
- const _0_message_entity_js_1 = require("../types/0_message_entity.js");
4
+ const deps_js_1 = require("../../deps.js");
5
+ const _0_message_entity_js_1 = require("../0_message_entity.js");
6
6
  function parseHtml(html) {
7
7
  let text = "";
8
8
  const entities = new Array();