@armatofik/tgsnake-core 1.0.0

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 (171) hide show
  1. package/COPYING +674 -0
  2. package/COPYING.lesser +165 -0
  3. package/README.md +88 -0
  4. package/lib/package.json +46 -0
  5. package/lib/src/Logger.d.ts +3 -0
  6. package/lib/src/Logger.js +9 -0
  7. package/lib/src/Queue.d.ts +19 -0
  8. package/lib/src/Queue.js +52 -0
  9. package/lib/src/Timeout.d.ts +9 -0
  10. package/lib/src/Timeout.js +44 -0
  11. package/lib/src/Version.node.d.ts +4 -0
  12. package/lib/src/Version.node.js +26 -0
  13. package/lib/src/client/Auth.d.ts +40 -0
  14. package/lib/src/client/Auth.js +317 -0
  15. package/lib/src/client/Client.d.ts +83 -0
  16. package/lib/src/client/Client.js +461 -0
  17. package/lib/src/client/Session.d.ts +9 -0
  18. package/lib/src/client/Session.js +132 -0
  19. package/lib/src/client/index.d.ts +3 -0
  20. package/lib/src/client/index.js +39 -0
  21. package/lib/src/connection/TCP/TCPAbridged.d.ts +9 -0
  22. package/lib/src/connection/TCP/TCPAbridged.js +47 -0
  23. package/lib/src/connection/TCP/TCPAbridgedO.d.ts +12 -0
  24. package/lib/src/connection/TCP/TCPAbridgedO.js +140 -0
  25. package/lib/src/connection/TCP/TCPFull.d.ts +10 -0
  26. package/lib/src/connection/TCP/TCPFull.js +43 -0
  27. package/lib/src/connection/TCP/TCPIntermediate.d.ts +9 -0
  28. package/lib/src/connection/TCP/TCPIntermediate.js +31 -0
  29. package/lib/src/connection/TCP/TCPIntermediateO.d.ts +12 -0
  30. package/lib/src/connection/TCP/TCPIntermediateO.js +123 -0
  31. package/lib/src/connection/TCP/TCPPaddedIntermediate.d.ts +9 -0
  32. package/lib/src/connection/TCP/TCPPaddedIntermediate.js +38 -0
  33. package/lib/src/connection/TCP/index.d.ts +7 -0
  34. package/lib/src/connection/TCP/index.js +17 -0
  35. package/lib/src/connection/TCP/tcp.d.ts +17 -0
  36. package/lib/src/connection/TCP/tcp.js +98 -0
  37. package/lib/src/connection/WebSocket.d.ts +21 -0
  38. package/lib/src/connection/WebSocket.js +279 -0
  39. package/lib/src/connection/connection.d.ts +53 -0
  40. package/lib/src/connection/connection.js +189 -0
  41. package/lib/src/connection/index.d.ts +4 -0
  42. package/lib/src/connection/index.js +41 -0
  43. package/lib/src/crypto/Aes.d.ts +10 -0
  44. package/lib/src/crypto/Aes.js +135 -0
  45. package/lib/src/crypto/Mtproto.d.ts +5 -0
  46. package/lib/src/crypto/Mtproto.js +123 -0
  47. package/lib/src/crypto/Password.d.ts +5 -0
  48. package/lib/src/crypto/Password.js +92 -0
  49. package/lib/src/crypto/Prime.d.ts +4 -0
  50. package/lib/src/crypto/Prime.js +64 -0
  51. package/lib/src/crypto/RSA.d.ts +7 -0
  52. package/lib/src/crypto/RSA.js +99 -0
  53. package/lib/src/crypto/SecretChat.d.ts +5 -0
  54. package/lib/src/crypto/SecretChat.js +136 -0
  55. package/lib/src/crypto/index.d.ts +6 -0
  56. package/lib/src/crypto/index.js +42 -0
  57. package/lib/src/errors/Base.d.ts +9 -0
  58. package/lib/src/errors/Base.js +50 -0
  59. package/lib/src/errors/Client.d.ts +21 -0
  60. package/lib/src/errors/Client.js +29 -0
  61. package/lib/src/errors/File.d.ts +13 -0
  62. package/lib/src/errors/File.js +30 -0
  63. package/lib/src/errors/RpcError.d.ts +22 -0
  64. package/lib/src/errors/RpcError.js +120 -0
  65. package/lib/src/errors/SecretChat.d.ts +13 -0
  66. package/lib/src/errors/SecretChat.js +30 -0
  67. package/lib/src/errors/WebSocket.d.ts +13 -0
  68. package/lib/src/errors/WebSocket.js +30 -0
  69. package/lib/src/errors/exceptions/All.d.ts +774 -0
  70. package/lib/src/errors/exceptions/All.js +778 -0
  71. package/lib/src/errors/exceptions/BadRequest400.d.ts +2345 -0
  72. package/lib/src/errors/exceptions/BadRequest400.js +2945 -0
  73. package/lib/src/errors/exceptions/Flood420.d.ts +37 -0
  74. package/lib/src/errors/exceptions/Flood420.js +49 -0
  75. package/lib/src/errors/exceptions/Forbidden403.d.ts +205 -0
  76. package/lib/src/errors/exceptions/Forbidden403.js +260 -0
  77. package/lib/src/errors/exceptions/InternalServerError500.d.ts +221 -0
  78. package/lib/src/errors/exceptions/InternalServerError500.js +280 -0
  79. package/lib/src/errors/exceptions/NotAcceptable406.d.ts +117 -0
  80. package/lib/src/errors/exceptions/NotAcceptable406.js +149 -0
  81. package/lib/src/errors/exceptions/NotFound404.d.ts +9 -0
  82. package/lib/src/errors/exceptions/NotFound404.js +14 -0
  83. package/lib/src/errors/exceptions/SeeOther303.d.ts +25 -0
  84. package/lib/src/errors/exceptions/SeeOther303.js +34 -0
  85. package/lib/src/errors/exceptions/ServiceUnavailable503.d.ts +17 -0
  86. package/lib/src/errors/exceptions/ServiceUnavailable503.js +24 -0
  87. package/lib/src/errors/exceptions/Unauthorized401.d.ts +41 -0
  88. package/lib/src/errors/exceptions/Unauthorized401.js +54 -0
  89. package/lib/src/errors/exceptions/index.d.ts +10 -0
  90. package/lib/src/errors/exceptions/index.js +48 -0
  91. package/lib/src/errors/index.d.ts +31 -0
  92. package/lib/src/errors/index.js +111 -0
  93. package/lib/src/file/Download.d.ts +11 -0
  94. package/lib/src/file/Download.js +118 -0
  95. package/lib/src/file/File.d.ts +20 -0
  96. package/lib/src/file/File.js +82 -0
  97. package/lib/src/file/Upload.d.ts +19 -0
  98. package/lib/src/file/Upload.js +250 -0
  99. package/lib/src/file/index.d.ts +3 -0
  100. package/lib/src/file/index.js +11 -0
  101. package/lib/src/helpers.d.ts +34 -0
  102. package/lib/src/helpers.js +358 -0
  103. package/lib/src/index.d.ts +16 -0
  104. package/lib/src/index.js +56 -0
  105. package/lib/src/platform.node.d.ts +36 -0
  106. package/lib/src/platform.node.js +109 -0
  107. package/lib/src/raw/All.d.ts +2400 -0
  108. package/lib/src/raw/All.js +2403 -0
  109. package/lib/src/raw/Raw.d.ts +29195 -0
  110. package/lib/src/raw/Raw.js +97277 -0
  111. package/lib/src/raw/core/BytesIO.d.ts +33 -0
  112. package/lib/src/raw/core/BytesIO.js +145 -0
  113. package/lib/src/raw/core/GzipPacked.d.ts +10 -0
  114. package/lib/src/raw/core/GzipPacked.js +60 -0
  115. package/lib/src/raw/core/Message.d.ts +13 -0
  116. package/lib/src/raw/core/Message.js +81 -0
  117. package/lib/src/raw/core/MsgContainer.d.ts +11 -0
  118. package/lib/src/raw/core/MsgContainer.js +69 -0
  119. package/lib/src/raw/core/TLObject.d.ts +19 -0
  120. package/lib/src/raw/core/TLObject.js +108 -0
  121. package/lib/src/raw/core/UpdateSecretChat.d.ts +40 -0
  122. package/lib/src/raw/core/UpdateSecretChat.js +88 -0
  123. package/lib/src/raw/core/index.d.ts +8 -0
  124. package/lib/src/raw/core/index.js +52 -0
  125. package/lib/src/raw/core/primitive/Bool.d.ts +20 -0
  126. package/lib/src/raw/core/primitive/Bool.js +41 -0
  127. package/lib/src/raw/core/primitive/Bytes.d.ts +7 -0
  128. package/lib/src/raw/core/primitive/Bytes.js +41 -0
  129. package/lib/src/raw/core/primitive/Double.d.ts +7 -0
  130. package/lib/src/raw/core/primitive/Double.js +24 -0
  131. package/lib/src/raw/core/primitive/Float.d.ts +7 -0
  132. package/lib/src/raw/core/primitive/Float.js +24 -0
  133. package/lib/src/raw/core/primitive/Int.d.ts +23 -0
  134. package/lib/src/raw/core/primitive/Int.js +122 -0
  135. package/lib/src/raw/core/primitive/String.d.ts +7 -0
  136. package/lib/src/raw/core/primitive/String.js +15 -0
  137. package/lib/src/raw/core/primitive/Vector.d.ts +9 -0
  138. package/lib/src/raw/core/primitive/Vector.js +50 -0
  139. package/lib/src/raw/core/primitive/index.d.ts +7 -0
  140. package/lib/src/raw/core/primitive/index.js +22 -0
  141. package/lib/src/raw/index.d.ts +3 -0
  142. package/lib/src/raw/index.js +17 -0
  143. package/lib/src/session/Auth.d.ts +15 -0
  144. package/lib/src/session/Auth.js +254 -0
  145. package/lib/src/session/Session.d.ts +58 -0
  146. package/lib/src/session/Session.js +571 -0
  147. package/lib/src/session/index.d.ts +7 -0
  148. package/lib/src/session/index.js +49 -0
  149. package/lib/src/session/internals/DataCenter.d.ts +42 -0
  150. package/lib/src/session/internals/DataCenter.js +80 -0
  151. package/lib/src/session/internals/MsgFactory.d.ts +3 -0
  152. package/lib/src/session/internals/MsgFactory.js +23 -0
  153. package/lib/src/session/internals/MsgId.d.ts +9 -0
  154. package/lib/src/session/internals/MsgId.js +24 -0
  155. package/lib/src/session/internals/SeqNo.d.ts +6 -0
  156. package/lib/src/session/internals/SeqNo.js +18 -0
  157. package/lib/src/session/secretChats/SecretChat.d.ts +29 -0
  158. package/lib/src/session/secretChats/SecretChat.js +527 -0
  159. package/lib/src/session/secretChats/index.d.ts +1 -0
  160. package/lib/src/session/secretChats/index.js +5 -0
  161. package/lib/src/storage/Abstract.d.ts +68 -0
  162. package/lib/src/storage/Abstract.js +6 -0
  163. package/lib/src/storage/SecretChat.d.ts +42 -0
  164. package/lib/src/storage/SecretChat.js +99 -0
  165. package/lib/src/storage/Session.d.ts +63 -0
  166. package/lib/src/storage/Session.js +245 -0
  167. package/lib/src/storage/StringSession.d.ts +4 -0
  168. package/lib/src/storage/StringSession.js +77 -0
  169. package/lib/src/storage/index.d.ts +4 -0
  170. package/lib/src/storage/index.js +12 -0
  171. package/package.json +46 -0
@@ -0,0 +1,279 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Socket = void 0;
4
+ const platform_node_js_1 = require("../platform.node.js");
5
+ const Logger_js_1 = require("../Logger.js");
6
+ const index_js_1 = require("../errors/index.js");
7
+ const mutex = new platform_node_js_1.Mutex();
8
+ class Socket {
9
+ _client;
10
+ _data;
11
+ _read;
12
+ _promisedReading;
13
+ timeout;
14
+ _connectionClosed;
15
+ constructor(timeout) {
16
+ this._data = platform_node_js_1.Buffer.alloc(0);
17
+ this._connectionClosed = true;
18
+ this.timeout = timeout;
19
+ }
20
+ async connect(ip, port, proxy) {
21
+ if (platform_node_js_1.isBrowser) {
22
+ if (proxy && !('server' in proxy && 'port' in proxy && 'secret' in proxy)) {
23
+ throw new index_js_1.WSError.ProxyUnsupported();
24
+ }
25
+ if (port === 443) {
26
+ this._client = new WebSocket(`wss://${ip.replace('$PORT', String(port))}`, 'binary');
27
+ }
28
+ else {
29
+ this._client = new WebSocket(`ws://${ip.replace('$PORT', String(port))}`, 'binary');
30
+ }
31
+ this._connectionClosed = false;
32
+ this._read = new Promise((resolve) => {
33
+ this._promisedReading = resolve;
34
+ });
35
+ return new Promise((resolve, reject) => {
36
+ this._client.onopen = () => {
37
+ this.recv();
38
+ resolve(this);
39
+ };
40
+ this._client.onerror = (error) => {
41
+ return 'message' in error
42
+ ? reject(new index_js_1.WSError.WebSocketError(error.message))
43
+ : reject(error);
44
+ };
45
+ this._client.onclose = () => {
46
+ if (this._client.readyState >= 2) {
47
+ if (this._promisedReading)
48
+ this._promisedReading(false);
49
+ this._connectionClosed = true;
50
+ }
51
+ };
52
+ globalThis.addEventListener('offline', this.destroy);
53
+ });
54
+ }
55
+ else {
56
+ if (proxy &&
57
+ !('server' in proxy && 'port' in proxy && 'secret' in proxy) &&
58
+ 'hostname' in proxy &&
59
+ 'port' in proxy &&
60
+ 'socks' in proxy) {
61
+ const ws = await platform_node_js_1.SocksClient.createConnection({
62
+ proxy: {
63
+ host: proxy.hostname,
64
+ port: proxy.port,
65
+ type: proxy.socks < 4 || proxy.socks > 5 ? 5 : proxy.socks,
66
+ userId: proxy.username,
67
+ password: proxy.password,
68
+ },
69
+ command: 'connect',
70
+ timeout: this.timeout,
71
+ destination: {
72
+ host: ip,
73
+ port: port,
74
+ },
75
+ });
76
+ this._client = ws.socket;
77
+ this._client.setTimeout(this.timeout);
78
+ this._connectionClosed = false;
79
+ this._read = new Promise((resolve) => {
80
+ this._promisedReading = resolve;
81
+ });
82
+ return new Promise((resolve, reject) => {
83
+ this._client.on('error', (error) => {
84
+ return error.message
85
+ ? reject(new index_js_1.WSError.WebSocketError(error.message))
86
+ : reject(error);
87
+ });
88
+ this._client.on('close', () => {
89
+ if (this._client.destroyed) {
90
+ if (this._promisedReading)
91
+ this._promisedReading(false);
92
+ this._connectionClosed = true;
93
+ }
94
+ });
95
+ this.recv();
96
+ resolve(this);
97
+ });
98
+ }
99
+ else {
100
+ this._client = new platform_node_js_1.net.Socket();
101
+ this._client.setTimeout(this.timeout);
102
+ this._connectionClosed = false;
103
+ this._read = new Promise((resolve) => {
104
+ this._promisedReading = resolve;
105
+ });
106
+ return new Promise((resolve, reject) => {
107
+ this._client.connect(port, ip, () => {
108
+ this.recv();
109
+ resolve(this);
110
+ });
111
+ this._client.on('error', (error) => {
112
+ return error.message
113
+ ? reject(new index_js_1.WSError.WebSocketError(error.message))
114
+ : reject(error);
115
+ });
116
+ this._client.on('close', () => {
117
+ if (this._client.destroyed) {
118
+ if (this._promisedReading)
119
+ this._promisedReading(false);
120
+ this._connectionClosed = true;
121
+ }
122
+ });
123
+ });
124
+ }
125
+ }
126
+ }
127
+ async destroy() {
128
+ if (this._client && !this._connectionClosed) {
129
+ this._connectionClosed = true;
130
+ this._read = new Promise((resolve) => {
131
+ this._promisedReading = resolve;
132
+ });
133
+ if (platform_node_js_1.isBrowser) {
134
+ await this._client.close();
135
+ }
136
+ else {
137
+ await this._client.destroy();
138
+ await this._client.unref();
139
+ }
140
+ }
141
+ return this._connectionClosed;
142
+ }
143
+ recv() {
144
+ if (this._client && !this._connectionClosed) {
145
+ if (platform_node_js_1.isBrowser) {
146
+ this._client.onmessage = async (data) => {
147
+ const _data = platform_node_js_1.Buffer.from(await new Response(data.data).arrayBuffer());
148
+ const release = await mutex.acquire();
149
+ try {
150
+ Logger_js_1.Logger.debug(`[3] Receive ${platform_node_js_1.Buffer.byteLength(_data)} bytes data`);
151
+ this._data = platform_node_js_1.Buffer.concat([
152
+ this._data,
153
+ _data,
154
+ ]);
155
+ if (this._promisedReading)
156
+ this._promisedReading(true);
157
+ }
158
+ finally {
159
+ release();
160
+ }
161
+ };
162
+ }
163
+ else {
164
+ this._client.on('data', async (data) => {
165
+ const release = await mutex.acquire();
166
+ try {
167
+ Logger_js_1.Logger.debug(`[3] Receive ${platform_node_js_1.Buffer.byteLength(data)} bytes data`);
168
+ this._data = platform_node_js_1.Buffer.concat([
169
+ this._data,
170
+ data,
171
+ ]);
172
+ if (this._promisedReading)
173
+ this._promisedReading(true);
174
+ }
175
+ finally {
176
+ release();
177
+ }
178
+ });
179
+ }
180
+ }
181
+ else {
182
+ throw new index_js_1.WSError.Disconnected();
183
+ }
184
+ }
185
+ async send(data) {
186
+ if (this._client && !this._connectionClosed) {
187
+ const release = await mutex.acquire();
188
+ try {
189
+ if (platform_node_js_1.isBrowser) {
190
+ this._client.send(data);
191
+ }
192
+ else {
193
+ this._client.write(data);
194
+ }
195
+ }
196
+ finally {
197
+ release();
198
+ }
199
+ }
200
+ else {
201
+ throw new index_js_1.WSError.Disconnected();
202
+ }
203
+ }
204
+ async read(length) {
205
+ if (this._connectionClosed) {
206
+ throw new index_js_1.WSError.ReadClosed();
207
+ }
208
+ await this._read;
209
+ if (this._connectionClosed) {
210
+ throw new index_js_1.WSError.ReadClosed();
211
+ }
212
+ const toRead = this._data.subarray(0, length);
213
+ this._data = this._data.subarray(length);
214
+ if (platform_node_js_1.Buffer.byteLength(this._data) <= 0) {
215
+ this._read = new Promise((resolve) => {
216
+ this._promisedReading = resolve;
217
+ });
218
+ }
219
+ return toRead;
220
+ }
221
+ async reading(length) {
222
+ if (this._client && !this._connectionClosed) {
223
+ let data = platform_node_js_1.Buffer.alloc(0);
224
+ while (!this._connectionClosed) {
225
+ const readed = await this.read(length);
226
+ data = platform_node_js_1.Buffer.concat([data, readed]);
227
+ length = length - platform_node_js_1.Buffer.byteLength(readed);
228
+ if (!length)
229
+ return data;
230
+ }
231
+ }
232
+ else {
233
+ throw new index_js_1.WSError.ReadClosed();
234
+ }
235
+ }
236
+ [Symbol.for('nodejs.util.inspect.custom')]() {
237
+ const toPrint = {
238
+ _: this.constructor.name,
239
+ };
240
+ for (const key in this) {
241
+ if (Object.prototype.hasOwnProperty.call(this, key)) {
242
+ const value = this[key];
243
+ if (!key.startsWith('_') && value !== undefined && value !== null) {
244
+ toPrint[key] = value;
245
+ }
246
+ }
247
+ }
248
+ return toPrint;
249
+ }
250
+ [Symbol.for('Deno.customInspect')]() {
251
+ return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
252
+ }
253
+ toJSON() {
254
+ const toPrint = {
255
+ _: this.constructor.name,
256
+ };
257
+ for (const key in this) {
258
+ if (Object.prototype.hasOwnProperty.call(this, key)) {
259
+ const value = this[key];
260
+ if (!key.startsWith('_') && value !== undefined && value !== null) {
261
+ if (typeof value === 'bigint') {
262
+ toPrint[key] = String(value);
263
+ }
264
+ else if (Array.isArray(value)) {
265
+ toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
266
+ }
267
+ else {
268
+ toPrint[key] = value;
269
+ }
270
+ }
271
+ }
272
+ }
273
+ return toPrint;
274
+ }
275
+ toString() {
276
+ return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
277
+ }
278
+ }
279
+ exports.Socket = Socket;
@@ -0,0 +1,53 @@
1
+ import * as TCPs from './TCP/index.js';
2
+ import { Buffer } from '../platform.node.js';
3
+ export declare const TCPModes: {
4
+ 0: typeof TCPs.TCPFull;
5
+ 1: typeof TCPs.TCPAbridged;
6
+ 2: typeof TCPs.TCPIntermediate;
7
+ 3: typeof TCPs.TCPPaddedIntermediate;
8
+ 4: typeof TCPs.TCPAbridgedO;
9
+ 5: typeof TCPs.TCPIntermediateO;
10
+ };
11
+ export type TypeTCP = TCPs.TCPFull | TCPs.TCPAbridged | TCPs.TCPIntermediate | TCPs.TCPAbridgedO | TCPs.TCPIntermediateO | TCPs.TCPPaddedIntermediate;
12
+ export declare enum TCP {
13
+ TCPFull = 0,
14
+ TCPAbridged = 1,
15
+ TCPIntermediate = 2,
16
+ TCPPaddedIntermediate = 3,
17
+ TCPAbridgedO = 4,
18
+ TCPIntermediateO = 5
19
+ }
20
+ export interface SocksProxyInterface {
21
+ hostname: string;
22
+ port: number;
23
+ socks: 4 | 5;
24
+ username?: string;
25
+ password?: string;
26
+ }
27
+ export interface MtprotoProxyInterface {
28
+ server: string;
29
+ port: number;
30
+ secret: string | Buffer;
31
+ }
32
+ export type ProxyInterface = SocksProxyInterface | MtprotoProxyInterface;
33
+ export declare class Connection {
34
+ maxRetries: number;
35
+ private _dcId;
36
+ private _test;
37
+ private _proxy?;
38
+ private _media;
39
+ private _mode;
40
+ private _address;
41
+ private _protocol;
42
+ private _connected;
43
+ private _local;
44
+ constructor(dcId: number, test: boolean, ipv6: boolean, proxy?: ProxyInterface, media?: boolean, mode?: TCP, local?: boolean);
45
+ connect(): Promise<true>;
46
+ close(): Promise<void>;
47
+ send(data: Buffer): Promise<void>;
48
+ recv(): Promise<Buffer<ArrayBufferLike> | undefined>;
49
+ toJSON(): {
50
+ [key: string]: any;
51
+ };
52
+ toString(): string;
53
+ }
@@ -0,0 +1,189 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.Connection = exports.TCP = exports.TCPModes = void 0;
37
+ const TCPs = __importStar(require("./TCP/index.js"));
38
+ const index_js_1 = require("../session/index.js");
39
+ const helpers_js_1 = require("../helpers.js");
40
+ const Logger_js_1 = require("../Logger.js");
41
+ const platform_node_js_1 = require("../platform.node.js");
42
+ const index_js_2 = require("../errors/index.js");
43
+ exports.TCPModes = {
44
+ 0: TCPs.TCPFull,
45
+ 1: TCPs.TCPAbridged,
46
+ 2: TCPs.TCPIntermediate,
47
+ 3: TCPs.TCPPaddedIntermediate,
48
+ 4: TCPs.TCPAbridgedO,
49
+ 5: TCPs.TCPIntermediateO,
50
+ };
51
+ var TCP;
52
+ (function (TCP) {
53
+ TCP[TCP["TCPFull"] = 0] = "TCPFull";
54
+ TCP[TCP["TCPAbridged"] = 1] = "TCPAbridged";
55
+ TCP[TCP["TCPIntermediate"] = 2] = "TCPIntermediate";
56
+ TCP[TCP["TCPPaddedIntermediate"] = 3] = "TCPPaddedIntermediate";
57
+ TCP[TCP["TCPAbridgedO"] = 4] = "TCPAbridgedO";
58
+ TCP[TCP["TCPIntermediateO"] = 5] = "TCPIntermediateO";
59
+ })(TCP || (exports.TCP = TCP = {}));
60
+ class Connection {
61
+ maxRetries;
62
+ _dcId;
63
+ _test;
64
+ _proxy;
65
+ _media;
66
+ _mode;
67
+ _address;
68
+ _protocol;
69
+ _connected;
70
+ _local;
71
+ constructor(dcId, test, ipv6, proxy, media = false, mode = TCP.TCPFull, local = (platform_node_js_1.isBrowser && globalThis && globalThis.location.protocol !== 'https:') || true) {
72
+ this.maxRetries = 3;
73
+ this._dcId = dcId;
74
+ this._test = test;
75
+ this._proxy = proxy;
76
+ this._media = media;
77
+ this._mode = mode;
78
+ this._address = index_js_1.DataCenter.DataCenter(dcId, test, ipv6, media);
79
+ this._local = local;
80
+ this._connected = false;
81
+ }
82
+ async connect() {
83
+ if (this._protocol && this._connected) {
84
+ throw new index_js_2.ClientError.ClientReady();
85
+ }
86
+ for (let i = 0; i < this.maxRetries; i++) {
87
+ if (((this._proxy &&
88
+ 'server' in this._proxy &&
89
+ 'port' in this._proxy &&
90
+ 'secret' in this._proxy) ||
91
+ platform_node_js_1.isBrowser) &&
92
+ this._mode !== TCP.TCPAbridgedO &&
93
+ this._mode !== TCP.TCPIntermediateO) {
94
+ if (this._proxy &&
95
+ 'server' in this._proxy &&
96
+ 'port' in this._proxy &&
97
+ 'secret' in this._proxy) {
98
+ const secret = (0, helpers_js_1.normalizeSecretString)(this._proxy.secret);
99
+ if (secret[0] === 0xdd) {
100
+ this._mode = TCP.TCPIntermediateO;
101
+ }
102
+ else {
103
+ this._mode = TCP.TCPAbridgedO;
104
+ }
105
+ }
106
+ else {
107
+ this._mode = TCP.TCPAbridgedO;
108
+ }
109
+ }
110
+ this._protocol = new exports.TCPModes[this._mode]();
111
+ try {
112
+ Logger_js_1.Logger.debug(`[1] Connecting to DC${this._dcId} with ${this._protocol.constructor.name}`);
113
+ await this._protocol.connect(this._address[0], platform_node_js_1.isBrowser ? (this._local ? 80 : this._address[1]) : this._address[1], this._proxy, this._dcId + (this._test ? 10000 : 0) * (this._media ? -1 : 1));
114
+ this._connected = true;
115
+ break;
116
+ }
117
+ catch (error) {
118
+ Logger_js_1.Logger.error(`[106] Got error when trying connecting to telegram :`, error);
119
+ this._protocol.close();
120
+ await (0, helpers_js_1.sleep)(2000);
121
+ }
122
+ }
123
+ if (!this._connected) {
124
+ throw new index_js_2.ClientError.ClientFailed();
125
+ }
126
+ return this._connected;
127
+ }
128
+ async close() {
129
+ if (!this._protocol || !this._connected) {
130
+ throw new index_js_2.ClientError.ClientNotReady();
131
+ }
132
+ this._connected = false;
133
+ await (0, helpers_js_1.sleep)(10);
134
+ await this._protocol.close();
135
+ }
136
+ async send(data) {
137
+ Logger_js_1.Logger.debug(`[2] Sending ${platform_node_js_1.Buffer.byteLength(data)} bytes data.`);
138
+ await this._protocol.send(data);
139
+ }
140
+ async recv() {
141
+ if (!this._connected) {
142
+ throw new index_js_2.ClientError.ClientDisconnected();
143
+ }
144
+ return await this._protocol.recv();
145
+ }
146
+ [Symbol.for('nodejs.util.inspect.custom')]() {
147
+ const toPrint = {
148
+ _: this.constructor.name,
149
+ };
150
+ for (const key in this) {
151
+ if (Object.prototype.hasOwnProperty.call(this, key)) {
152
+ const value = this[key];
153
+ if (!key.startsWith('_') && value !== undefined && value !== null) {
154
+ toPrint[key] = value;
155
+ }
156
+ }
157
+ }
158
+ return toPrint;
159
+ }
160
+ [Symbol.for('Deno.customInspect')]() {
161
+ return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
162
+ }
163
+ toJSON() {
164
+ const toPrint = {
165
+ _: this.constructor.name,
166
+ };
167
+ for (const key in this) {
168
+ if (Object.prototype.hasOwnProperty.call(this, key)) {
169
+ const value = this[key];
170
+ if (!key.startsWith('_') && value !== undefined && value !== null) {
171
+ if (typeof value === 'bigint') {
172
+ toPrint[key] = String(value);
173
+ }
174
+ else if (Array.isArray(value)) {
175
+ toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
176
+ }
177
+ else {
178
+ toPrint[key] = value;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ return toPrint;
184
+ }
185
+ toString() {
186
+ return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
187
+ }
188
+ }
189
+ exports.Connection = Connection;
@@ -0,0 +1,4 @@
1
+ import * as TCP from './TCP/index.js';
2
+ export * as Connections from './connection.js';
3
+ export { Socket } from './WebSocket.js';
4
+ export { TCP };
@@ -0,0 +1,41 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.TCP = exports.Socket = exports.Connections = void 0;
37
+ const TCP = __importStar(require("./TCP/index.js"));
38
+ exports.TCP = TCP;
39
+ exports.Connections = __importStar(require("./connection.js"));
40
+ var WebSocket_js_1 = require("./WebSocket.js");
41
+ Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return WebSocket_js_1.Socket; } });
@@ -0,0 +1,10 @@
1
+ import { Buffer } from '../platform.node.js';
2
+ export declare function ige256Encrypt(data: Buffer, key: Buffer, iv: Buffer): Buffer;
3
+ export declare function ige256Decrypt(data: Buffer, key: Buffer, iv: Buffer): Buffer;
4
+ export type CtrCipherFn = (data: Buffer) => Buffer;
5
+ export declare function ctr256Cipher(key: Buffer, iv: Buffer): CtrCipherFn;
6
+ export declare function xor(a: Buffer, b: Buffer): Buffer<ArrayBuffer>;
7
+ export declare function AES(key: Buffer): {
8
+ encrypt(data: Buffer): Buffer;
9
+ decrypt(data: Buffer): Buffer;
10
+ };