@mtkruto/node 0.0.6

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 (167) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +1 -0
  3. package/esm/_dnt.shims.js +68 -0
  4. package/esm/client/client.js +169 -0
  5. package/esm/client/client_abstract.js +51 -0
  6. package/esm/client/client_plain.js +112 -0
  7. package/esm/connection/connection.js +2 -0
  8. package/esm/connection/connection_web_socket.js +90 -0
  9. package/esm/constants.js +60 -0
  10. package/esm/deps/deno.land/std@0.186.0/fmt/colors.js +474 -0
  11. package/esm/deps/deno.land/std@0.186.0/testing/_diff.js +311 -0
  12. package/esm/deps/deno.land/std@0.186.0/testing/_format.js +23 -0
  13. package/esm/deps/deno.land/std@0.186.0/testing/asserts.js +633 -0
  14. package/esm/deps/deno.land/x/crc32@v0.2.0/mod.js +105 -0
  15. package/esm/deps/deno.land/x/tgcrypto@0.1.1/mod.js +68 -0
  16. package/esm/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.js +1127 -0
  17. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js +239 -0
  18. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.js +10 -0
  19. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.js +139 -0
  20. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.js +176 -0
  21. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.js +2 -0
  22. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.js +19 -0
  23. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.js +25 -0
  24. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.js +1941 -0
  25. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.js +85 -0
  26. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.js +321 -0
  27. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.js +1658 -0
  28. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.js +355 -0
  29. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.js +11 -0
  30. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.js +42 -0
  31. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.js +1107 -0
  32. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.js +86 -0
  33. package/esm/deps.js +4 -0
  34. package/esm/mod.js +4 -0
  35. package/esm/package.json +3 -0
  36. package/esm/tl/0_tl_raw_reader.js +62 -0
  37. package/esm/tl/0_tl_raw_writer.js +60 -0
  38. package/esm/tl/1_tl_object.js +149 -0
  39. package/esm/tl/2_types.js +48220 -0
  40. package/esm/tl/3_functions.js +19856 -0
  41. package/esm/tl/3_tl_object_deserializer.js +72 -0
  42. package/esm/tl/3_tl_reader.js +16 -0
  43. package/esm/tl/3_tl_writer.js +7 -0
  44. package/esm/tl/4_rpc_result.js +27 -0
  45. package/esm/tl/5_message.js +54 -0
  46. package/esm/tl/6_message_container.js +35 -0
  47. package/esm/transport/transport.js +32 -0
  48. package/esm/transport/transport_intermediate.js +56 -0
  49. package/esm/transport/transport_provider.js +23 -0
  50. package/esm/types.js +1 -0
  51. package/esm/utilities/0_bigint.js +37 -0
  52. package/esm/utilities/0_buffer.js +33 -0
  53. package/esm/utilities/0_crypto.js +35 -0
  54. package/esm/utilities/0_hash.js +7 -0
  55. package/esm/utilities/1_auth.js +29 -0
  56. package/esm/utilities/1_message.js +94 -0
  57. package/esm/utilities/1_obfuscation.js +33 -0
  58. package/package.json +40 -0
  59. package/script/_dnt.shims.js +77 -0
  60. package/script/client/client.js +173 -0
  61. package/script/client/client_abstract.js +55 -0
  62. package/script/client/client_plain.js +116 -0
  63. package/script/connection/connection.js +6 -0
  64. package/script/connection/connection_web_socket.js +117 -0
  65. package/script/constants.js +63 -0
  66. package/script/deps/deno.land/std@0.186.0/fmt/colors.js +548 -0
  67. package/script/deps/deno.land/std@0.186.0/testing/_diff.js +317 -0
  68. package/script/deps/deno.land/std@0.186.0/testing/_format.js +50 -0
  69. package/script/deps/deno.land/std@0.186.0/testing/asserts.js +659 -0
  70. package/script/deps/deno.land/x/crc32@v0.2.0/mod.js +113 -0
  71. package/script/deps/deno.land/x/tgcrypto@0.1.1/mod.js +82 -0
  72. package/script/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.js +1152 -0
  73. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js +271 -0
  74. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.js +14 -0
  75. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.js +172 -0
  76. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.js +208 -0
  77. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.js +18 -0
  78. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.js +22 -0
  79. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.js +30 -0
  80. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.js +1976 -0
  81. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.js +88 -0
  82. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.js +324 -0
  83. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.js +1674 -0
  84. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.js +358 -0
  85. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.js +14 -0
  86. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.js +44 -0
  87. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.js +1115 -0
  88. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.js +89 -0
  89. package/script/deps.js +30 -0
  90. package/script/mod.js +32 -0
  91. package/script/package.json +3 -0
  92. package/script/tl/0_tl_raw_reader.js +66 -0
  93. package/script/tl/0_tl_raw_writer.js +64 -0
  94. package/script/tl/1_tl_object.js +156 -0
  95. package/script/tl/2_types.js +49824 -0
  96. package/script/tl/3_functions.js +20413 -0
  97. package/script/tl/3_tl_object_deserializer.js +76 -0
  98. package/script/tl/3_tl_reader.js +20 -0
  99. package/script/tl/3_tl_writer.js +11 -0
  100. package/script/tl/4_rpc_result.js +31 -0
  101. package/script/tl/5_message.js +58 -0
  102. package/script/tl/6_message_container.js +39 -0
  103. package/script/transport/transport.js +36 -0
  104. package/script/transport/transport_intermediate.js +60 -0
  105. package/script/transport/transport_provider.js +27 -0
  106. package/script/types.js +2 -0
  107. package/script/utilities/0_bigint.js +67 -0
  108. package/script/utilities/0_buffer.js +38 -0
  109. package/script/utilities/0_crypto.js +39 -0
  110. package/script/utilities/0_hash.js +35 -0
  111. package/script/utilities/1_auth.js +56 -0
  112. package/script/utilities/1_message.js +102 -0
  113. package/script/utilities/1_obfuscation.js +60 -0
  114. package/types/_dnt.shims.d.ts +10 -0
  115. package/types/client/client.d.ts +19 -0
  116. package/types/client/client_abstract.d.ts +20 -0
  117. package/types/client/client_plain.d.ts +10 -0
  118. package/types/connection/connection.d.ts +7 -0
  119. package/types/connection/connection_web_socket.d.ts +13 -0
  120. package/types/constants.d.ts +2 -0
  121. package/types/deps/deno.land/std@0.186.0/fmt/colors.d.ts +270 -0
  122. package/types/deps/deno.land/std@0.186.0/testing/_diff.d.ts +26 -0
  123. package/types/deps/deno.land/std@0.186.0/testing/_format.d.ts +1 -0
  124. package/types/deps/deno.land/std@0.186.0/testing/asserts.d.ts +284 -0
  125. package/types/deps/deno.land/x/crc32@v0.2.0/mod.d.ts +15 -0
  126. package/types/deps/deno.land/x/tgcrypto@0.1.1/mod.d.ts +47 -0
  127. package/types/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.d.ts +2 -0
  128. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.d.ts +16 -0
  129. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.d.ts +1 -0
  130. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.d.ts +29 -0
  131. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.d.ts +23 -0
  132. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.d.ts +2 -0
  133. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.d.ts +1 -0
  134. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.d.ts +2 -0
  135. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.d.ts +78 -0
  136. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.d.ts +12 -0
  137. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.d.ts +1 -0
  138. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.d.ts +47 -0
  139. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.d.ts +1 -0
  140. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.d.ts +12 -0
  141. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.d.ts +30 -0
  142. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.d.ts +5 -0
  143. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.d.ts +14 -0
  144. package/types/deps.d.ts +4 -0
  145. package/types/mod.d.ts +4 -0
  146. package/types/tl/0_tl_raw_reader.d.ts +13 -0
  147. package/types/tl/0_tl_raw_writer.d.ts +13 -0
  148. package/types/tl/1_tl_object.d.ts +36 -0
  149. package/types/tl/2_types.d.ts +14815 -0
  150. package/types/tl/3_functions.d.ts +6122 -0
  151. package/types/tl/3_tl_object_deserializer.d.ts +3 -0
  152. package/types/tl/3_tl_reader.d.ts +4 -0
  153. package/types/tl/3_tl_writer.d.ts +5 -0
  154. package/types/tl/4_rpc_result.d.ts +8 -0
  155. package/types/tl/5_message.d.ts +11 -0
  156. package/types/tl/6_message_container.d.ts +9 -0
  157. package/types/transport/transport.d.ts +15 -0
  158. package/types/transport/transport_intermediate.d.ts +11 -0
  159. package/types/transport/transport_provider.d.ts +12 -0
  160. package/types/types.d.ts +1 -0
  161. package/types/utilities/0_bigint.d.ts +4 -0
  162. package/types/utilities/0_buffer.d.ts +2 -0
  163. package/types/utilities/0_crypto.d.ts +8 -0
  164. package/types/utilities/0_hash.d.ts +2 -0
  165. package/types/utilities/1_auth.d.ts +1 -0
  166. package/types/utilities/1_message.d.ts +10 -0
  167. package/types/utilities/1_obfuscation.d.ts +6 -0
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.dntGlobalThis = exports.WebSocket = exports.crypto = exports.Deno = void 0;
7
+ const shim_deno_1 = require("@deno/shim-deno");
8
+ var shim_deno_2 = require("@deno/shim-deno");
9
+ Object.defineProperty(exports, "Deno", { enumerable: true, get: function () { return shim_deno_2.Deno; } });
10
+ const shim_crypto_1 = require("@deno/shim-crypto");
11
+ var shim_crypto_2 = require("@deno/shim-crypto");
12
+ Object.defineProperty(exports, "crypto", { enumerable: true, get: function () { return shim_crypto_2.crypto; } });
13
+ const ws_1 = __importDefault(require("ws"));
14
+ var ws_2 = require("ws");
15
+ Object.defineProperty(exports, "WebSocket", { enumerable: true, get: function () { return __importDefault(ws_2).default; } });
16
+ const dntGlobals = {
17
+ Deno: shim_deno_1.Deno,
18
+ crypto: shim_crypto_1.crypto,
19
+ WebSocket: ws_1.default,
20
+ };
21
+ exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
22
+ // deno-lint-ignore ban-types
23
+ function createMergeProxy(baseObj, extObj) {
24
+ return new Proxy(baseObj, {
25
+ get(_target, prop, _receiver) {
26
+ if (prop in extObj) {
27
+ return extObj[prop];
28
+ }
29
+ else {
30
+ return baseObj[prop];
31
+ }
32
+ },
33
+ set(_target, prop, value) {
34
+ if (prop in extObj) {
35
+ delete extObj[prop];
36
+ }
37
+ baseObj[prop] = value;
38
+ return true;
39
+ },
40
+ deleteProperty(_target, prop) {
41
+ let success = false;
42
+ if (prop in extObj) {
43
+ delete extObj[prop];
44
+ success = true;
45
+ }
46
+ if (prop in baseObj) {
47
+ delete baseObj[prop];
48
+ success = true;
49
+ }
50
+ return success;
51
+ },
52
+ ownKeys(_target) {
53
+ const baseKeys = Reflect.ownKeys(baseObj);
54
+ const extKeys = Reflect.ownKeys(extObj);
55
+ const extKeysSet = new Set(extKeys);
56
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
57
+ },
58
+ defineProperty(_target, prop, desc) {
59
+ if (prop in extObj) {
60
+ delete extObj[prop];
61
+ }
62
+ Reflect.defineProperty(baseObj, prop, desc);
63
+ return true;
64
+ },
65
+ getOwnPropertyDescriptor(_target, prop) {
66
+ if (prop in extObj) {
67
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
68
+ }
69
+ else {
70
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
71
+ }
72
+ },
73
+ has(_target, prop) {
74
+ return prop in extObj || prop in baseObj;
75
+ },
76
+ });
77
+ }
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Client = void 0;
4
+ const deps_js_1 = require("../deps.js");
5
+ const constants_js_1 = require("../constants.js");
6
+ const _0_bigint_js_1 = require("../utilities/0_bigint.js");
7
+ const _1_message_js_1 = require("../utilities/1_message.js");
8
+ const _2_types_js_1 = require("../tl/2_types.js");
9
+ const _3_functions_js_1 = require("../tl/3_functions.js");
10
+ const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
11
+ const _4_rpc_result_js_1 = require("../tl/4_rpc_result.js");
12
+ const _5_message_js_1 = require("../tl/5_message.js");
13
+ const _6_message_container_js_1 = require("../tl/6_message_container.js");
14
+ const client_abstract_js_1 = require("./client_abstract.js");
15
+ const client_plain_js_1 = require("./client_plain.js");
16
+ class Client extends client_abstract_js_1.ClientAbstract {
17
+ constructor() {
18
+ super(...arguments);
19
+ Object.defineProperty(this, "sessionId", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: (0, _0_bigint_js_1.getRandomBigInt)(8, true, false)
24
+ });
25
+ Object.defineProperty(this, "auth", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ Object.defineProperty(this, "state", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: { salt: 0n, seqNo: 0 }
36
+ });
37
+ Object.defineProperty(this, "promises", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: new Map()
42
+ });
43
+ Object.defineProperty(this, "toAcknowledge", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: new Set()
48
+ });
49
+ Object.defineProperty(this, "updatesHandler", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: null
54
+ });
55
+ }
56
+ async connect() {
57
+ const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
58
+ await plain.connect();
59
+ const { authKey: key, authKeyId: id, salt } = await plain.createAuthKey();
60
+ await plain.disconnect();
61
+ this.auth = { key, id };
62
+ this.state.salt = salt;
63
+ await super.connect();
64
+ // logger().debug("Client connected");
65
+ this.receiveLoop();
66
+ this.pingLoop();
67
+ }
68
+ async receiveLoop() {
69
+ if (!this.auth) {
70
+ throw new Error("Not connected");
71
+ }
72
+ while (this.connected) {
73
+ if (this.toAcknowledge.size >= constants_js_1.ackThreshold) {
74
+ await this.send(new _2_types_js_1.MsgsAck({ msgIds: [...this.toAcknowledge] }));
75
+ this.toAcknowledge.clear();
76
+ }
77
+ const buffer = await this.transport.receive();
78
+ let decrypted;
79
+ try {
80
+ decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.auth.key, this.auth.id, this.sessionId);
81
+ }
82
+ catch (_err) {
83
+ // logger().error(`Failed to decrypt message: ${err}`);
84
+ continue;
85
+ }
86
+ const messages = decrypted instanceof _6_message_container_js_1.MessageContainer ? decrypted.messages : [decrypted];
87
+ for (const message of messages) {
88
+ let body = message.body;
89
+ if (body instanceof _2_types_js_1.GZIPPacked) {
90
+ body = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(body.packedData)).readObject();
91
+ }
92
+ // logger().debug(`Received ${body.constructor.name}`);
93
+ if (body instanceof _2_types_js_1.Updates) {
94
+ this.updatesHandler?.(this, body);
95
+ }
96
+ else if (message.body instanceof _4_rpc_result_js_1.RPCResult) {
97
+ let result = message.body.result;
98
+ if (result instanceof _2_types_js_1.GZIPPacked) {
99
+ result = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(result.packedData)).readObject();
100
+ }
101
+ const promise = this.promises.get(message.body.messageId);
102
+ if (promise) {
103
+ if (result instanceof _2_types_js_1.RPCError) {
104
+ promise.reject(result);
105
+ }
106
+ else {
107
+ promise.resolve(result);
108
+ }
109
+ this.promises.delete(message.body.messageId);
110
+ }
111
+ }
112
+ else if (message.body instanceof _2_types_js_1.Pong) {
113
+ const promise = this.promises.get(message.body.msgId);
114
+ if (promise) {
115
+ promise.resolve(message.body);
116
+ this.promises.delete(message.body.msgId);
117
+ }
118
+ }
119
+ else if (message.body instanceof _2_types_js_1.BadMsgNotification || message.body instanceof _2_types_js_1.BadServerSalt) {
120
+ if (message.body instanceof _2_types_js_1.BadServerSalt) {
121
+ this.state.salt = message.body.newServerSalt;
122
+ }
123
+ const promise = this.promises.get(message.body.badMsgId);
124
+ if (promise) {
125
+ promise.resolve(message.body);
126
+ this.promises.delete(message.body.badMsgId);
127
+ }
128
+ }
129
+ this.toAcknowledge.add(message.id);
130
+ }
131
+ }
132
+ }
133
+ async pingLoop() {
134
+ while (this.connected) {
135
+ try {
136
+ await this.invoke(new _3_functions_js_1.Ping({ pingId: (0, _0_bigint_js_1.getRandomBigInt)(8, true, false) }));
137
+ }
138
+ catch (_err) {
139
+ // logger().error(`Failed to invoke ping: ${err}`);
140
+ }
141
+ await new Promise((r) => setTimeout(r, 60 * 1000));
142
+ }
143
+ }
144
+ async invoke(function_, noWait) {
145
+ if (!this.auth) {
146
+ throw new Error("Not connected");
147
+ }
148
+ let seqNo = this.state.seqNo * 2;
149
+ if (!(function_ instanceof _3_functions_js_1.Ping) && !(function_ instanceof _2_types_js_1.MsgsAck)) {
150
+ seqNo++;
151
+ this.state.seqNo++;
152
+ }
153
+ const message = new _5_message_js_1.Message((0, _1_message_js_1.getMessageId)(), seqNo, function_);
154
+ await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
155
+ // logger().debug(`Invoked ${function_.constructor.name}`);
156
+ if (noWait) {
157
+ return;
158
+ }
159
+ const result = await new Promise((resolve, reject) => {
160
+ this.promises.set(message.id, { resolve, reject });
161
+ });
162
+ if (result instanceof _2_types_js_1.BadServerSalt) {
163
+ return await this.invoke(function_);
164
+ }
165
+ else {
166
+ return result;
167
+ }
168
+ }
169
+ send(function_) {
170
+ return this.invoke(function_, true);
171
+ }
172
+ }
173
+ exports.Client = Client;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientAbstract = void 0;
4
+ const transport_provider_js_1 = require("../transport/transport_provider.js");
5
+ const deps_js_1 = require("../deps.js");
6
+ class ClientAbstract {
7
+ constructor(transportProvider = (0, transport_provider_js_1.defaultTransportProvider)()) {
8
+ Object.defineProperty(this, "transportProvider", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: transportProvider
13
+ });
14
+ Object.defineProperty(this, "connection", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ Object.defineProperty(this, "transport", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "dcId", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "connected", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: false
37
+ });
38
+ const { connection, transport, dcId } = transportProvider(false);
39
+ this.connection = connection;
40
+ this.transport = transport;
41
+ this.dcId = dcId;
42
+ }
43
+ async connect() {
44
+ await (0, deps_js_1.initTgCrypto)();
45
+ await this.connection.open();
46
+ await this.transport.initialize();
47
+ this.connected = true;
48
+ }
49
+ async disconnect() {
50
+ await this.transport.deinitialize();
51
+ await this.connection.close();
52
+ this.connected = false;
53
+ }
54
+ }
55
+ exports.ClientAbstract = ClientAbstract;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientPlain = void 0;
4
+ const deps_js_1 = require("../deps.js");
5
+ const constants_js_1 = require("../constants.js");
6
+ const _0_bigint_js_1 = require("../utilities/0_bigint.js");
7
+ const _0_buffer_js_1 = require("../utilities/0_buffer.js");
8
+ const _0_hash_js_1 = require("../utilities/0_hash.js");
9
+ const _1_auth_js_1 = require("../utilities/1_auth.js");
10
+ const _1_message_js_1 = require("../utilities/1_message.js");
11
+ const _2_types_js_1 = require("../tl/2_types.js");
12
+ const _3_functions_js_1 = require("../tl/3_functions.js");
13
+ const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
14
+ const client_abstract_js_1 = require("./client_abstract.js");
15
+ class ClientPlain extends client_abstract_js_1.ClientAbstract {
16
+ async invoke(function_) {
17
+ await this.transport.send((0, _1_message_js_1.packUnencryptedMessage)(function_.serialize()));
18
+ const buffer = await this.transport.receive();
19
+ if (buffer.length == 4) {
20
+ const int = (0, _0_bigint_js_1.bigIntFromBuffer)(buffer, true, true);
21
+ if (int == -404n) {
22
+ throw new Error("-404");
23
+ }
24
+ }
25
+ const { message } = (0, _1_message_js_1.unpackUnencryptedMessage)(buffer);
26
+ const reader = new _3_tl_reader_js_1.TLReader(message);
27
+ return reader.readObject();
28
+ }
29
+ async createAuthKey() {
30
+ const nonce = (0, _0_bigint_js_1.getRandomBigInt)(16, false, true);
31
+ // logger().debug("Auth key creation started");
32
+ const resPq = await this.invoke(new _3_functions_js_1.ReqPQMulti({ nonce }));
33
+ (0, deps_js_1.assertInstanceOf)(resPq, _2_types_js_1.ResPQ);
34
+ (0, deps_js_1.assertEquals)(resPq.nonce, nonce);
35
+ // logger().debug("Got res_pq");
36
+ const pq_ = (0, _0_bigint_js_1.bigIntFromBuffer)(resPq.pq, false, false);
37
+ const [p_, q_] = (0, deps_js_1.factorize)(pq_);
38
+ // logger().debug("Factorized pq");
39
+ const p = (0, _0_buffer_js_1.bufferFromBigInt)(p_, 4, false, false);
40
+ const q = (0, _0_buffer_js_1.bufferFromBigInt)(q_, 4, false, false);
41
+ let publicKeyFingerprint;
42
+ let publicKey;
43
+ for (const fingerprint of resPq.serverPublicKeyFingerprints) {
44
+ const maybePublicKey = constants_js_1.publicKeys.get(fingerprint);
45
+ if (maybePublicKey) {
46
+ publicKeyFingerprint = fingerprint;
47
+ publicKey = maybePublicKey;
48
+ break;
49
+ }
50
+ }
51
+ if (!publicKeyFingerprint || !publicKey) {
52
+ throw new Error("No corresponding public key found");
53
+ }
54
+ const dc = this.dcId;
55
+ const pq = resPq.pq;
56
+ const serverNonce = resPq.serverNonce;
57
+ const newNonce = (0, _0_bigint_js_1.getRandomBigInt)(32, false, true);
58
+ let encryptedData = await (0, _1_auth_js_1.rsaPad)(new _2_types_js_1.PQInnerDataDC({
59
+ pq,
60
+ p,
61
+ q,
62
+ dc,
63
+ newNonce,
64
+ nonce,
65
+ serverNonce,
66
+ }).serialize(), publicKey);
67
+ const dhParams = await this.invoke(new _3_functions_js_1.ReqDHParams({
68
+ nonce,
69
+ serverNonce,
70
+ p,
71
+ q,
72
+ publicKeyFingerprint,
73
+ encryptedData,
74
+ }));
75
+ (0, deps_js_1.assertInstanceOf)(dhParams, _2_types_js_1.ServerDHParamsOK);
76
+ // logger().debug("Got server_DH_params_ok");
77
+ const newNonce_ = (0, _0_buffer_js_1.bufferFromBigInt)(newNonce, 32, true, true);
78
+ const serverNonce_ = (0, _0_buffer_js_1.bufferFromBigInt)(serverNonce, 16, true, true);
79
+ const tmpAesKey = (0, _0_buffer_js_1.concat)(await (0, _0_hash_js_1.sha1)((0, _0_buffer_js_1.concat)(newNonce_, serverNonce_)), (await (0, _0_hash_js_1.sha1)((0, _0_buffer_js_1.concat)(serverNonce_, newNonce_))).slice(0, 0 + 12));
80
+ const tmpAesIv = (0, _0_buffer_js_1.concat)((await (0, _0_hash_js_1.sha1)((0, _0_buffer_js_1.concat)(serverNonce_, newNonce_))).slice(12, 12 + 8), await (0, _0_hash_js_1.sha1)((0, _0_buffer_js_1.concat)(newNonce_, newNonce_)), newNonce_.slice(0, 0 + 4));
81
+ const answerWithHash = (0, deps_js_1.ige256Decrypt)(dhParams.encryptedAnswer, tmpAesKey, tmpAesIv);
82
+ const dhInnerData = new _3_tl_reader_js_1.TLReader(answerWithHash.slice(20)).readObject();
83
+ (0, deps_js_1.assertInstanceOf)(dhInnerData, _2_types_js_1.ServerDHInnerData);
84
+ const { g, gA: gA_, dhPrime: dhPrime_ } = dhInnerData;
85
+ const gA = (0, _0_bigint_js_1.bigIntFromBuffer)(gA_, false, false);
86
+ const dhPrime = (0, _0_bigint_js_1.bigIntFromBuffer)(dhPrime_, false, false);
87
+ const b = (0, _0_bigint_js_1.getRandomBigInt)(256, false, false);
88
+ const gB = (0, _0_bigint_js_1.modExp)(BigInt(g), b, dhPrime);
89
+ const data = new _2_types_js_1.ClientDHInnerData({
90
+ nonce,
91
+ serverNonce,
92
+ retryId: 0n,
93
+ gB: (0, _0_buffer_js_1.bufferFromBigInt)(gB, 256, false, false),
94
+ }).serialize();
95
+ let dataWithHash = (0, _0_buffer_js_1.concat)(await (0, _0_hash_js_1.sha1)(data), data);
96
+ while (dataWithHash.length % 16 != 0) {
97
+ dataWithHash = (0, _0_buffer_js_1.concat)(dataWithHash, new Uint8Array(1));
98
+ }
99
+ encryptedData = (0, deps_js_1.ige256Encrypt)(dataWithHash, tmpAesKey, tmpAesIv);
100
+ const dhGenOk = await this.invoke(new _3_functions_js_1.SetClientDHParams({ nonce, serverNonce, encryptedData }));
101
+ (0, deps_js_1.assertInstanceOf)(dhGenOk, _2_types_js_1.DHGenOK);
102
+ // logger().debug("Got dh_gen_ok");
103
+ const serverNonceSlice = serverNonce_.slice(0, 8);
104
+ const salt = newNonce_.slice(0, 0 + 8).map((v, i) => v ^ serverNonceSlice[i]);
105
+ const authKey_ = (0, _0_bigint_js_1.modExp)(gA, b, dhPrime);
106
+ const authKey = (0, _0_buffer_js_1.bufferFromBigInt)(authKey_, 256, false, false);
107
+ const authKeyId = (await (0, _0_hash_js_1.sha1)(authKey)).slice(-8);
108
+ // logger().debug("Auth key created");
109
+ return {
110
+ authKey,
111
+ authKeyId: (0, _0_bigint_js_1.bigIntFromBuffer)(authKeyId, true, false),
112
+ salt: (0, _0_bigint_js_1.bigIntFromBuffer)(salt, true, false),
113
+ };
114
+ }
115
+ }
116
+ exports.ClientPlain = ClientPlain;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Connection = void 0;
4
+ class Connection {
5
+ }
6
+ exports.Connection = Connection;
@@ -0,0 +1,117 @@
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
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ConnectionWebSocket = void 0;
27
+ const dntShim = __importStar(require("../_dnt.shims.js"));
28
+ const deps_js_1 = require("../deps.js");
29
+ class ConnectionWebSocket {
30
+ constructor(url) {
31
+ Object.defineProperty(this, "webSocket", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ Object.defineProperty(this, "rMutex", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: new deps_js_1.Mutex()
42
+ });
43
+ Object.defineProperty(this, "wMutex", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: new deps_js_1.Mutex()
48
+ });
49
+ Object.defineProperty(this, "buffer", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: new Array()
54
+ });
55
+ Object.defineProperty(this, "nextResolve", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: null
60
+ });
61
+ this.webSocket = new dntShim.WebSocket(url, "binary");
62
+ this.webSocket.onmessage = async (e) => {
63
+ // deno-lint-ignore no-explicit-any
64
+ const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
65
+ this.buffer.push(...Array.from(data));
66
+ if (this.nextResolve != null && this.buffer.length >= this.nextResolve[0]) {
67
+ this.nextResolve[1]();
68
+ this.nextResolve = null;
69
+ }
70
+ };
71
+ this.webSocket.onerror = console.error;
72
+ }
73
+ async open() {
74
+ while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
75
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
76
+ throw new Error("Connection was closed");
77
+ }
78
+ else {
79
+ await new Promise((r) => setTimeout(r, 5));
80
+ }
81
+ }
82
+ }
83
+ async read(p) {
84
+ if (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
85
+ throw new Error("Connection not open");
86
+ }
87
+ const release = await this.rMutex.acquire();
88
+ try {
89
+ if (this.buffer.length < p.length) {
90
+ await new Promise((r) => this.nextResolve = [p.length, r]);
91
+ }
92
+ p.set(this.buffer.splice(0, p.length));
93
+ }
94
+ finally {
95
+ release();
96
+ }
97
+ }
98
+ async write(p) {
99
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
100
+ throw new Error("Connection not open");
101
+ }
102
+ const release = await this.wMutex.acquire();
103
+ try {
104
+ this.webSocket.send(p);
105
+ }
106
+ finally {
107
+ release();
108
+ }
109
+ }
110
+ close() {
111
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
112
+ throw new Error("Connection not open");
113
+ }
114
+ this.webSocket.close();
115
+ }
116
+ }
117
+ exports.ConnectionWebSocket = ConnectionWebSocket;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.publicKeys = exports.ackThreshold = void 0;
4
+ exports.ackThreshold = 10;
5
+ exports.publicKeys = new Map([
6
+ [
7
+ 1562291298945373506n,
8
+ [
9
+ 23978758553106631992002580305620005835060400692492410830911253690968985161770919571023213268734637655796435779238577529598157303153929847488434262037216243092374262144086701552588446162198373312512977891135864544907383666560742498178155572733831904785232310227644261688873841336264291123806158164086416723396618993440700301670694812377102225720438542027067699276781356881649272759102712053106917756470596037969358935162126553921536961079884698448464480018715128825516337818216719699963463996161433765618041475321701550049005950467552064133935768219696743607832667385715968297285043180567281391541729832333512747963903n,
10
+ 0x010001n,
11
+ ],
12
+ ],
13
+ [
14
+ -5859577972006586033n,
15
+ [
16
+ 22718646979021445086805300267873836551952264292680929983215333222894263271262525404635917732844879510479026727119219632282263022986926715926905675829369119276087034208478103497496557160062032769614235480480336458978483235018994623019124956728706285653879392359295937777480998285327855536342942377483433941973435757959758939732133845114873967169906896837881767555178893700532356888631557478214225236142802178882405660867509208028117895779092487773043163348085906022471454630364430126878252139917614178636934412103623869072904053827933244809215364242885476208852061471203189128281292392955960922615335169478055469443233n,
17
+ 0x010001n,
18
+ ],
19
+ ],
20
+ [
21
+ 6491968696586960280n,
22
+ [
23
+ 24037766801008650742980770419085067708599000106468359115503808361335510549334399420739246345211161442047800836519033544747025851693968269285475039555231773313724462564908666239840898204833183290939296455776367417572678362602041185421910456164281750840651140599266716366431221860463163678044675384797103831824697137394559208723253047225996994374103488753637228569081911062604259973219466527532055001206549020539767836549715548081391829906556645384762696840019083743214331245456023666332360278739093925808884746079174665122518196162846505196334513910135812480878181576802670132412681595747104670774040613733524133809153n,
24
+ 0x010001n,
25
+ ],
26
+ ],
27
+ [
28
+ 6427105915145367799n,
29
+ [
30
+ 31397816550885835230852157218780061958032265523683168121843010509948800713659136033293079736773570890108359386900507157088426116094690624871733896824903852456886718992868563715191423124852392192384776940880374150924862556483265420429274105533416857995735561007585523057519126772910116492334365011698443015363661211091297809380358424492944309160255944656822953372259436558863685653753335752581293598201573897522965811840624726977678182734005734657974801081161883696567467154361452234673787603576616026939590191461977891577841590473181783600507799709098085844100324450292291634823637124474260102961720620172155279101593n,
31
+ 0x010001n,
32
+ ],
33
+ ],
34
+ [
35
+ 2685959930972952888n,
36
+ [
37
+ 26116452836398416979797305072689667731920792444719420894507233217583880016919839198933132831614577761055257601279314443643343829322673079507289288794037776440997114291901674159336092732642632335510805799979810490880123733875085953240308174829572850043815591814961322487356309436839345409265418743385218003844674882350344739294993993718594352763930783186948532591492541249018768829381368348025815240824308999288789287057821939532386904237315176663437974522079590346893012292633393394277518254001578790831900469743479052551168559741017618280316677467349819664126886746238706747395316681794438145749441351286324575707503n,
38
+ 0x010001n,
39
+ ],
40
+ ],
41
+ [
42
+ -3414540481677951611n,
43
+ [
44
+ 29379598170669337022986177149456128565388431120058863768162556424047512191330847455146576344487764408661701890505066208632169112269581063774293102577308490531282748465986139880977280302242772832972539403531316010870401287642763009136156734339538042419388722777357134487746169093539093850251243897188928735903389451772730245253062963384108812842079887538976360465290946139638691491496062099570836476454855996319192747663615955633778034897140982517446405334423701359108810182097749467210509584293428076654573384828809574217079944388301239431309115013843331317877374435868468779972014486325557807783825502498215169806323n,
45
+ 0x010001n,
46
+ ],
47
+ ],
48
+ [
49
+ -5595554452916591101n,
50
+ [
51
+ 25342889448840415564971689590713473206898847759084779052582026594546022463853940585885215951168491965708222649399180603818074200620463776135424884632162512403163793083921641631564740959529419359595852941166848940585952337613333022396096584117954892216031229237302943701877588456738335398602461675225081791820393153757504952636234951323237820036543581047826906120927972487366805292115792231423684261262330394324750785450942589751755390156647751460719351439969059949569615302809050721500330239005077889855323917509948255722081644689442127297605422579707142646660768825302832201908302295573257427896031830742328565032949n,
52
+ 0x010001n,
53
+ ],
54
+ ],
55
+ // Piltover
56
+ [
57
+ -5746138571559360724n,
58
+ [
59
+ 26007219673003768186863565706940772901187119797549016801229858995725553827566821957407312503173782965285118977548255274803204174826509270586421841734996746493071723679186583391927378886693846518017621303145594627156874011707147548520576401963675756312298478878474644101348427872255774246965916073553469761183250044599310582333625453447885100318336864215713703860033765477732305055537755094341310278183464296754000479758644167423888581130401586546401934096183449791147912201040435037926911688179080223967863256047196705714611239523210563418382771999824529581206920323901890386681875893623076449827387008128524246269437n,
60
+ 0x010001n,
61
+ ],
62
+ ],
63
+ ]);