@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
package/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ <https://github.com/MTKruto/MTKruto>
@@ -0,0 +1,68 @@
1
+ import { Deno } from "@deno/shim-deno";
2
+ export { Deno } from "@deno/shim-deno";
3
+ import { crypto } from "@deno/shim-crypto";
4
+ export { crypto } from "@deno/shim-crypto";
5
+ import { default as WebSocket } from "ws";
6
+ export { default as WebSocket } from "ws";
7
+ const dntGlobals = {
8
+ Deno,
9
+ crypto,
10
+ WebSocket,
11
+ };
12
+ export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
13
+ // deno-lint-ignore ban-types
14
+ function createMergeProxy(baseObj, extObj) {
15
+ return new Proxy(baseObj, {
16
+ get(_target, prop, _receiver) {
17
+ if (prop in extObj) {
18
+ return extObj[prop];
19
+ }
20
+ else {
21
+ return baseObj[prop];
22
+ }
23
+ },
24
+ set(_target, prop, value) {
25
+ if (prop in extObj) {
26
+ delete extObj[prop];
27
+ }
28
+ baseObj[prop] = value;
29
+ return true;
30
+ },
31
+ deleteProperty(_target, prop) {
32
+ let success = false;
33
+ if (prop in extObj) {
34
+ delete extObj[prop];
35
+ success = true;
36
+ }
37
+ if (prop in baseObj) {
38
+ delete baseObj[prop];
39
+ success = true;
40
+ }
41
+ return success;
42
+ },
43
+ ownKeys(_target) {
44
+ const baseKeys = Reflect.ownKeys(baseObj);
45
+ const extKeys = Reflect.ownKeys(extObj);
46
+ const extKeysSet = new Set(extKeys);
47
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
48
+ },
49
+ defineProperty(_target, prop, desc) {
50
+ if (prop in extObj) {
51
+ delete extObj[prop];
52
+ }
53
+ Reflect.defineProperty(baseObj, prop, desc);
54
+ return true;
55
+ },
56
+ getOwnPropertyDescriptor(_target, prop) {
57
+ if (prop in extObj) {
58
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
59
+ }
60
+ else {
61
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
62
+ }
63
+ },
64
+ has(_target, prop) {
65
+ return prop in extObj || prop in baseObj;
66
+ },
67
+ });
68
+ }
@@ -0,0 +1,169 @@
1
+ import { gunzip } from "../deps.js";
2
+ import { ackThreshold } from "../constants.js";
3
+ import { getRandomBigInt } from "../utilities/0_bigint.js";
4
+ import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
5
+ import { BadMsgNotification, BadServerSalt, GZIPPacked, MsgsAck, Pong, RPCError, Updates } from "../tl/2_types.js";
6
+ import { Ping } from "../tl/3_functions.js";
7
+ import { TLReader } from "../tl/3_tl_reader.js";
8
+ import { RPCResult } from "../tl/4_rpc_result.js";
9
+ import { Message } from "../tl/5_message.js";
10
+ import { MessageContainer } from "../tl/6_message_container.js";
11
+ import { ClientAbstract } from "./client_abstract.js";
12
+ import { ClientPlain } from "./client_plain.js";
13
+ export class Client extends ClientAbstract {
14
+ constructor() {
15
+ super(...arguments);
16
+ Object.defineProperty(this, "sessionId", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: getRandomBigInt(8, true, false)
21
+ });
22
+ Object.defineProperty(this, "auth", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ Object.defineProperty(this, "state", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: { salt: 0n, seqNo: 0 }
33
+ });
34
+ Object.defineProperty(this, "promises", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: new Map()
39
+ });
40
+ Object.defineProperty(this, "toAcknowledge", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: new Set()
45
+ });
46
+ Object.defineProperty(this, "updatesHandler", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: null
51
+ });
52
+ }
53
+ async connect() {
54
+ const plain = new ClientPlain(this.transportProvider);
55
+ await plain.connect();
56
+ const { authKey: key, authKeyId: id, salt } = await plain.createAuthKey();
57
+ await plain.disconnect();
58
+ this.auth = { key, id };
59
+ this.state.salt = salt;
60
+ await super.connect();
61
+ // logger().debug("Client connected");
62
+ this.receiveLoop();
63
+ this.pingLoop();
64
+ }
65
+ async receiveLoop() {
66
+ if (!this.auth) {
67
+ throw new Error("Not connected");
68
+ }
69
+ while (this.connected) {
70
+ if (this.toAcknowledge.size >= ackThreshold) {
71
+ await this.send(new MsgsAck({ msgIds: [...this.toAcknowledge] }));
72
+ this.toAcknowledge.clear();
73
+ }
74
+ const buffer = await this.transport.receive();
75
+ let decrypted;
76
+ try {
77
+ decrypted = await decryptMessage(buffer, this.auth.key, this.auth.id, this.sessionId);
78
+ }
79
+ catch (_err) {
80
+ // logger().error(`Failed to decrypt message: ${err}`);
81
+ continue;
82
+ }
83
+ const messages = decrypted instanceof MessageContainer ? decrypted.messages : [decrypted];
84
+ for (const message of messages) {
85
+ let body = message.body;
86
+ if (body instanceof GZIPPacked) {
87
+ body = new TLReader(gunzip(body.packedData)).readObject();
88
+ }
89
+ // logger().debug(`Received ${body.constructor.name}`);
90
+ if (body instanceof Updates) {
91
+ this.updatesHandler?.(this, body);
92
+ }
93
+ else if (message.body instanceof RPCResult) {
94
+ let result = message.body.result;
95
+ if (result instanceof GZIPPacked) {
96
+ result = new TLReader(gunzip(result.packedData)).readObject();
97
+ }
98
+ const promise = this.promises.get(message.body.messageId);
99
+ if (promise) {
100
+ if (result instanceof RPCError) {
101
+ promise.reject(result);
102
+ }
103
+ else {
104
+ promise.resolve(result);
105
+ }
106
+ this.promises.delete(message.body.messageId);
107
+ }
108
+ }
109
+ else if (message.body instanceof Pong) {
110
+ const promise = this.promises.get(message.body.msgId);
111
+ if (promise) {
112
+ promise.resolve(message.body);
113
+ this.promises.delete(message.body.msgId);
114
+ }
115
+ }
116
+ else if (message.body instanceof BadMsgNotification || message.body instanceof BadServerSalt) {
117
+ if (message.body instanceof BadServerSalt) {
118
+ this.state.salt = message.body.newServerSalt;
119
+ }
120
+ const promise = this.promises.get(message.body.badMsgId);
121
+ if (promise) {
122
+ promise.resolve(message.body);
123
+ this.promises.delete(message.body.badMsgId);
124
+ }
125
+ }
126
+ this.toAcknowledge.add(message.id);
127
+ }
128
+ }
129
+ }
130
+ async pingLoop() {
131
+ while (this.connected) {
132
+ try {
133
+ await this.invoke(new Ping({ pingId: getRandomBigInt(8, true, false) }));
134
+ }
135
+ catch (_err) {
136
+ // logger().error(`Failed to invoke ping: ${err}`);
137
+ }
138
+ await new Promise((r) => setTimeout(r, 60 * 1000));
139
+ }
140
+ }
141
+ async invoke(function_, noWait) {
142
+ if (!this.auth) {
143
+ throw new Error("Not connected");
144
+ }
145
+ let seqNo = this.state.seqNo * 2;
146
+ if (!(function_ instanceof Ping) && !(function_ instanceof MsgsAck)) {
147
+ seqNo++;
148
+ this.state.seqNo++;
149
+ }
150
+ const message = new Message(getMessageId(), seqNo, function_);
151
+ await this.transport.send(await encryptMessage(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
152
+ // logger().debug(`Invoked ${function_.constructor.name}`);
153
+ if (noWait) {
154
+ return;
155
+ }
156
+ const result = await new Promise((resolve, reject) => {
157
+ this.promises.set(message.id, { resolve, reject });
158
+ });
159
+ if (result instanceof BadServerSalt) {
160
+ return await this.invoke(function_);
161
+ }
162
+ else {
163
+ return result;
164
+ }
165
+ }
166
+ send(function_) {
167
+ return this.invoke(function_, true);
168
+ }
169
+ }
@@ -0,0 +1,51 @@
1
+ import { defaultTransportProvider } from "../transport/transport_provider.js";
2
+ import { initTgCrypto } from "../deps.js";
3
+ export class ClientAbstract {
4
+ constructor(transportProvider = defaultTransportProvider()) {
5
+ Object.defineProperty(this, "transportProvider", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: transportProvider
10
+ });
11
+ Object.defineProperty(this, "connection", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "transport", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "dcId", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "connected", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: false
34
+ });
35
+ const { connection, transport, dcId } = transportProvider(false);
36
+ this.connection = connection;
37
+ this.transport = transport;
38
+ this.dcId = dcId;
39
+ }
40
+ async connect() {
41
+ await initTgCrypto();
42
+ await this.connection.open();
43
+ await this.transport.initialize();
44
+ this.connected = true;
45
+ }
46
+ async disconnect() {
47
+ await this.transport.deinitialize();
48
+ await this.connection.close();
49
+ this.connected = false;
50
+ }
51
+ }
@@ -0,0 +1,112 @@
1
+ import { assertEquals, assertInstanceOf, factorize, ige256Decrypt, ige256Encrypt } from "../deps.js";
2
+ import { publicKeys } from "../constants.js";
3
+ import { bigIntFromBuffer, getRandomBigInt, modExp } from "../utilities/0_bigint.js";
4
+ import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
5
+ import { sha1 } from "../utilities/0_hash.js";
6
+ import { rsaPad } from "../utilities/1_auth.js";
7
+ import { packUnencryptedMessage, unpackUnencryptedMessage } from "../utilities/1_message.js";
8
+ import { ClientDHInnerData, DHGenOK, PQInnerDataDC, ResPQ, ServerDHInnerData, ServerDHParamsOK } from "../tl/2_types.js";
9
+ import { ReqDHParams, ReqPQMulti, SetClientDHParams } from "../tl/3_functions.js";
10
+ import { TLReader } from "../tl/3_tl_reader.js";
11
+ import { ClientAbstract } from "./client_abstract.js";
12
+ export class ClientPlain extends ClientAbstract {
13
+ async invoke(function_) {
14
+ await this.transport.send(packUnencryptedMessage(function_.serialize()));
15
+ const buffer = await this.transport.receive();
16
+ if (buffer.length == 4) {
17
+ const int = bigIntFromBuffer(buffer, true, true);
18
+ if (int == -404n) {
19
+ throw new Error("-404");
20
+ }
21
+ }
22
+ const { message } = unpackUnencryptedMessage(buffer);
23
+ const reader = new TLReader(message);
24
+ return reader.readObject();
25
+ }
26
+ async createAuthKey() {
27
+ const nonce = getRandomBigInt(16, false, true);
28
+ // logger().debug("Auth key creation started");
29
+ const resPq = await this.invoke(new ReqPQMulti({ nonce }));
30
+ assertInstanceOf(resPq, ResPQ);
31
+ assertEquals(resPq.nonce, nonce);
32
+ // logger().debug("Got res_pq");
33
+ const pq_ = bigIntFromBuffer(resPq.pq, false, false);
34
+ const [p_, q_] = factorize(pq_);
35
+ // logger().debug("Factorized pq");
36
+ const p = bufferFromBigInt(p_, 4, false, false);
37
+ const q = bufferFromBigInt(q_, 4, false, false);
38
+ let publicKeyFingerprint;
39
+ let publicKey;
40
+ for (const fingerprint of resPq.serverPublicKeyFingerprints) {
41
+ const maybePublicKey = publicKeys.get(fingerprint);
42
+ if (maybePublicKey) {
43
+ publicKeyFingerprint = fingerprint;
44
+ publicKey = maybePublicKey;
45
+ break;
46
+ }
47
+ }
48
+ if (!publicKeyFingerprint || !publicKey) {
49
+ throw new Error("No corresponding public key found");
50
+ }
51
+ const dc = this.dcId;
52
+ const pq = resPq.pq;
53
+ const serverNonce = resPq.serverNonce;
54
+ const newNonce = getRandomBigInt(32, false, true);
55
+ let encryptedData = await rsaPad(new PQInnerDataDC({
56
+ pq,
57
+ p,
58
+ q,
59
+ dc,
60
+ newNonce,
61
+ nonce,
62
+ serverNonce,
63
+ }).serialize(), publicKey);
64
+ const dhParams = await this.invoke(new ReqDHParams({
65
+ nonce,
66
+ serverNonce,
67
+ p,
68
+ q,
69
+ publicKeyFingerprint,
70
+ encryptedData,
71
+ }));
72
+ assertInstanceOf(dhParams, ServerDHParamsOK);
73
+ // logger().debug("Got server_DH_params_ok");
74
+ const newNonce_ = bufferFromBigInt(newNonce, 32, true, true);
75
+ const serverNonce_ = bufferFromBigInt(serverNonce, 16, true, true);
76
+ const tmpAesKey = concat(await sha1(concat(newNonce_, serverNonce_)), (await sha1(concat(serverNonce_, newNonce_))).slice(0, 0 + 12));
77
+ const tmpAesIv = concat((await sha1(concat(serverNonce_, newNonce_))).slice(12, 12 + 8), await sha1(concat(newNonce_, newNonce_)), newNonce_.slice(0, 0 + 4));
78
+ const answerWithHash = ige256Decrypt(dhParams.encryptedAnswer, tmpAesKey, tmpAesIv);
79
+ const dhInnerData = new TLReader(answerWithHash.slice(20)).readObject();
80
+ assertInstanceOf(dhInnerData, ServerDHInnerData);
81
+ const { g, gA: gA_, dhPrime: dhPrime_ } = dhInnerData;
82
+ const gA = bigIntFromBuffer(gA_, false, false);
83
+ const dhPrime = bigIntFromBuffer(dhPrime_, false, false);
84
+ const b = getRandomBigInt(256, false, false);
85
+ const gB = modExp(BigInt(g), b, dhPrime);
86
+ const data = new ClientDHInnerData({
87
+ nonce,
88
+ serverNonce,
89
+ retryId: 0n,
90
+ gB: bufferFromBigInt(gB, 256, false, false),
91
+ }).serialize();
92
+ let dataWithHash = concat(await sha1(data), data);
93
+ while (dataWithHash.length % 16 != 0) {
94
+ dataWithHash = concat(dataWithHash, new Uint8Array(1));
95
+ }
96
+ encryptedData = ige256Encrypt(dataWithHash, tmpAesKey, tmpAesIv);
97
+ const dhGenOk = await this.invoke(new SetClientDHParams({ nonce, serverNonce, encryptedData }));
98
+ assertInstanceOf(dhGenOk, DHGenOK);
99
+ // logger().debug("Got dh_gen_ok");
100
+ const serverNonceSlice = serverNonce_.slice(0, 8);
101
+ const salt = newNonce_.slice(0, 0 + 8).map((v, i) => v ^ serverNonceSlice[i]);
102
+ const authKey_ = modExp(gA, b, dhPrime);
103
+ const authKey = bufferFromBigInt(authKey_, 256, false, false);
104
+ const authKeyId = (await sha1(authKey)).slice(-8);
105
+ // logger().debug("Auth key created");
106
+ return {
107
+ authKey,
108
+ authKeyId: bigIntFromBuffer(authKeyId, true, false),
109
+ salt: bigIntFromBuffer(salt, true, false),
110
+ };
111
+ }
112
+ }
@@ -0,0 +1,2 @@
1
+ export class Connection {
2
+ }
@@ -0,0 +1,90 @@
1
+ import * as dntShim from "../_dnt.shims.js";
2
+ import { Mutex } from "../deps.js";
3
+ export class ConnectionWebSocket {
4
+ constructor(url) {
5
+ Object.defineProperty(this, "webSocket", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ Object.defineProperty(this, "rMutex", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: new Mutex()
16
+ });
17
+ Object.defineProperty(this, "wMutex", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: new Mutex()
22
+ });
23
+ Object.defineProperty(this, "buffer", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: new Array()
28
+ });
29
+ Object.defineProperty(this, "nextResolve", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: null
34
+ });
35
+ this.webSocket = new dntShim.WebSocket(url, "binary");
36
+ this.webSocket.onmessage = async (e) => {
37
+ // deno-lint-ignore no-explicit-any
38
+ const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
39
+ this.buffer.push(...Array.from(data));
40
+ if (this.nextResolve != null && this.buffer.length >= this.nextResolve[0]) {
41
+ this.nextResolve[1]();
42
+ this.nextResolve = null;
43
+ }
44
+ };
45
+ this.webSocket.onerror = console.error;
46
+ }
47
+ async open() {
48
+ while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
49
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
50
+ throw new Error("Connection was closed");
51
+ }
52
+ else {
53
+ await new Promise((r) => setTimeout(r, 5));
54
+ }
55
+ }
56
+ }
57
+ async read(p) {
58
+ if (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
59
+ throw new Error("Connection not open");
60
+ }
61
+ const release = await this.rMutex.acquire();
62
+ try {
63
+ if (this.buffer.length < p.length) {
64
+ await new Promise((r) => this.nextResolve = [p.length, r]);
65
+ }
66
+ p.set(this.buffer.splice(0, p.length));
67
+ }
68
+ finally {
69
+ release();
70
+ }
71
+ }
72
+ async write(p) {
73
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
74
+ throw new Error("Connection not open");
75
+ }
76
+ const release = await this.wMutex.acquire();
77
+ try {
78
+ this.webSocket.send(p);
79
+ }
80
+ finally {
81
+ release();
82
+ }
83
+ }
84
+ close() {
85
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
86
+ throw new Error("Connection not open");
87
+ }
88
+ this.webSocket.close();
89
+ }
90
+ }