@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.
- package/COPYING +674 -0
- package/COPYING.lesser +165 -0
- package/README.md +88 -0
- package/lib/package.json +46 -0
- package/lib/src/Logger.d.ts +3 -0
- package/lib/src/Logger.js +9 -0
- package/lib/src/Queue.d.ts +19 -0
- package/lib/src/Queue.js +52 -0
- package/lib/src/Timeout.d.ts +9 -0
- package/lib/src/Timeout.js +44 -0
- package/lib/src/Version.node.d.ts +4 -0
- package/lib/src/Version.node.js +26 -0
- package/lib/src/client/Auth.d.ts +40 -0
- package/lib/src/client/Auth.js +317 -0
- package/lib/src/client/Client.d.ts +83 -0
- package/lib/src/client/Client.js +461 -0
- package/lib/src/client/Session.d.ts +9 -0
- package/lib/src/client/Session.js +132 -0
- package/lib/src/client/index.d.ts +3 -0
- package/lib/src/client/index.js +39 -0
- package/lib/src/connection/TCP/TCPAbridged.d.ts +9 -0
- package/lib/src/connection/TCP/TCPAbridged.js +47 -0
- package/lib/src/connection/TCP/TCPAbridgedO.d.ts +12 -0
- package/lib/src/connection/TCP/TCPAbridgedO.js +140 -0
- package/lib/src/connection/TCP/TCPFull.d.ts +10 -0
- package/lib/src/connection/TCP/TCPFull.js +43 -0
- package/lib/src/connection/TCP/TCPIntermediate.d.ts +9 -0
- package/lib/src/connection/TCP/TCPIntermediate.js +31 -0
- package/lib/src/connection/TCP/TCPIntermediateO.d.ts +12 -0
- package/lib/src/connection/TCP/TCPIntermediateO.js +123 -0
- package/lib/src/connection/TCP/TCPPaddedIntermediate.d.ts +9 -0
- package/lib/src/connection/TCP/TCPPaddedIntermediate.js +38 -0
- package/lib/src/connection/TCP/index.d.ts +7 -0
- package/lib/src/connection/TCP/index.js +17 -0
- package/lib/src/connection/TCP/tcp.d.ts +17 -0
- package/lib/src/connection/TCP/tcp.js +98 -0
- package/lib/src/connection/WebSocket.d.ts +21 -0
- package/lib/src/connection/WebSocket.js +279 -0
- package/lib/src/connection/connection.d.ts +53 -0
- package/lib/src/connection/connection.js +189 -0
- package/lib/src/connection/index.d.ts +4 -0
- package/lib/src/connection/index.js +41 -0
- package/lib/src/crypto/Aes.d.ts +10 -0
- package/lib/src/crypto/Aes.js +135 -0
- package/lib/src/crypto/Mtproto.d.ts +5 -0
- package/lib/src/crypto/Mtproto.js +123 -0
- package/lib/src/crypto/Password.d.ts +5 -0
- package/lib/src/crypto/Password.js +92 -0
- package/lib/src/crypto/Prime.d.ts +4 -0
- package/lib/src/crypto/Prime.js +64 -0
- package/lib/src/crypto/RSA.d.ts +7 -0
- package/lib/src/crypto/RSA.js +99 -0
- package/lib/src/crypto/SecretChat.d.ts +5 -0
- package/lib/src/crypto/SecretChat.js +136 -0
- package/lib/src/crypto/index.d.ts +6 -0
- package/lib/src/crypto/index.js +42 -0
- package/lib/src/errors/Base.d.ts +9 -0
- package/lib/src/errors/Base.js +50 -0
- package/lib/src/errors/Client.d.ts +21 -0
- package/lib/src/errors/Client.js +29 -0
- package/lib/src/errors/File.d.ts +13 -0
- package/lib/src/errors/File.js +30 -0
- package/lib/src/errors/RpcError.d.ts +22 -0
- package/lib/src/errors/RpcError.js +120 -0
- package/lib/src/errors/SecretChat.d.ts +13 -0
- package/lib/src/errors/SecretChat.js +30 -0
- package/lib/src/errors/WebSocket.d.ts +13 -0
- package/lib/src/errors/WebSocket.js +30 -0
- package/lib/src/errors/exceptions/All.d.ts +774 -0
- package/lib/src/errors/exceptions/All.js +778 -0
- package/lib/src/errors/exceptions/BadRequest400.d.ts +2345 -0
- package/lib/src/errors/exceptions/BadRequest400.js +2945 -0
- package/lib/src/errors/exceptions/Flood420.d.ts +37 -0
- package/lib/src/errors/exceptions/Flood420.js +49 -0
- package/lib/src/errors/exceptions/Forbidden403.d.ts +205 -0
- package/lib/src/errors/exceptions/Forbidden403.js +260 -0
- package/lib/src/errors/exceptions/InternalServerError500.d.ts +221 -0
- package/lib/src/errors/exceptions/InternalServerError500.js +280 -0
- package/lib/src/errors/exceptions/NotAcceptable406.d.ts +117 -0
- package/lib/src/errors/exceptions/NotAcceptable406.js +149 -0
- package/lib/src/errors/exceptions/NotFound404.d.ts +9 -0
- package/lib/src/errors/exceptions/NotFound404.js +14 -0
- package/lib/src/errors/exceptions/SeeOther303.d.ts +25 -0
- package/lib/src/errors/exceptions/SeeOther303.js +34 -0
- package/lib/src/errors/exceptions/ServiceUnavailable503.d.ts +17 -0
- package/lib/src/errors/exceptions/ServiceUnavailable503.js +24 -0
- package/lib/src/errors/exceptions/Unauthorized401.d.ts +41 -0
- package/lib/src/errors/exceptions/Unauthorized401.js +54 -0
- package/lib/src/errors/exceptions/index.d.ts +10 -0
- package/lib/src/errors/exceptions/index.js +48 -0
- package/lib/src/errors/index.d.ts +31 -0
- package/lib/src/errors/index.js +111 -0
- package/lib/src/file/Download.d.ts +11 -0
- package/lib/src/file/Download.js +118 -0
- package/lib/src/file/File.d.ts +20 -0
- package/lib/src/file/File.js +82 -0
- package/lib/src/file/Upload.d.ts +19 -0
- package/lib/src/file/Upload.js +250 -0
- package/lib/src/file/index.d.ts +3 -0
- package/lib/src/file/index.js +11 -0
- package/lib/src/helpers.d.ts +34 -0
- package/lib/src/helpers.js +358 -0
- package/lib/src/index.d.ts +16 -0
- package/lib/src/index.js +56 -0
- package/lib/src/platform.node.d.ts +36 -0
- package/lib/src/platform.node.js +109 -0
- package/lib/src/raw/All.d.ts +2400 -0
- package/lib/src/raw/All.js +2403 -0
- package/lib/src/raw/Raw.d.ts +29195 -0
- package/lib/src/raw/Raw.js +97277 -0
- package/lib/src/raw/core/BytesIO.d.ts +33 -0
- package/lib/src/raw/core/BytesIO.js +145 -0
- package/lib/src/raw/core/GzipPacked.d.ts +10 -0
- package/lib/src/raw/core/GzipPacked.js +60 -0
- package/lib/src/raw/core/Message.d.ts +13 -0
- package/lib/src/raw/core/Message.js +81 -0
- package/lib/src/raw/core/MsgContainer.d.ts +11 -0
- package/lib/src/raw/core/MsgContainer.js +69 -0
- package/lib/src/raw/core/TLObject.d.ts +19 -0
- package/lib/src/raw/core/TLObject.js +108 -0
- package/lib/src/raw/core/UpdateSecretChat.d.ts +40 -0
- package/lib/src/raw/core/UpdateSecretChat.js +88 -0
- package/lib/src/raw/core/index.d.ts +8 -0
- package/lib/src/raw/core/index.js +52 -0
- package/lib/src/raw/core/primitive/Bool.d.ts +20 -0
- package/lib/src/raw/core/primitive/Bool.js +41 -0
- package/lib/src/raw/core/primitive/Bytes.d.ts +7 -0
- package/lib/src/raw/core/primitive/Bytes.js +41 -0
- package/lib/src/raw/core/primitive/Double.d.ts +7 -0
- package/lib/src/raw/core/primitive/Double.js +24 -0
- package/lib/src/raw/core/primitive/Float.d.ts +7 -0
- package/lib/src/raw/core/primitive/Float.js +24 -0
- package/lib/src/raw/core/primitive/Int.d.ts +23 -0
- package/lib/src/raw/core/primitive/Int.js +122 -0
- package/lib/src/raw/core/primitive/String.d.ts +7 -0
- package/lib/src/raw/core/primitive/String.js +15 -0
- package/lib/src/raw/core/primitive/Vector.d.ts +9 -0
- package/lib/src/raw/core/primitive/Vector.js +50 -0
- package/lib/src/raw/core/primitive/index.d.ts +7 -0
- package/lib/src/raw/core/primitive/index.js +22 -0
- package/lib/src/raw/index.d.ts +3 -0
- package/lib/src/raw/index.js +17 -0
- package/lib/src/session/Auth.d.ts +15 -0
- package/lib/src/session/Auth.js +254 -0
- package/lib/src/session/Session.d.ts +58 -0
- package/lib/src/session/Session.js +571 -0
- package/lib/src/session/index.d.ts +7 -0
- package/lib/src/session/index.js +49 -0
- package/lib/src/session/internals/DataCenter.d.ts +42 -0
- package/lib/src/session/internals/DataCenter.js +80 -0
- package/lib/src/session/internals/MsgFactory.d.ts +3 -0
- package/lib/src/session/internals/MsgFactory.js +23 -0
- package/lib/src/session/internals/MsgId.d.ts +9 -0
- package/lib/src/session/internals/MsgId.js +24 -0
- package/lib/src/session/internals/SeqNo.d.ts +6 -0
- package/lib/src/session/internals/SeqNo.js +18 -0
- package/lib/src/session/secretChats/SecretChat.d.ts +29 -0
- package/lib/src/session/secretChats/SecretChat.js +527 -0
- package/lib/src/session/secretChats/index.d.ts +1 -0
- package/lib/src/session/secretChats/index.js +5 -0
- package/lib/src/storage/Abstract.d.ts +68 -0
- package/lib/src/storage/Abstract.js +6 -0
- package/lib/src/storage/SecretChat.d.ts +42 -0
- package/lib/src/storage/SecretChat.js +99 -0
- package/lib/src/storage/Session.d.ts +63 -0
- package/lib/src/storage/Session.js +245 -0
- package/lib/src/storage/StringSession.d.ts +4 -0
- package/lib/src/storage/StringSession.js +77 -0
- package/lib/src/storage/index.d.ts +4 -0
- package/lib/src/storage/index.js +12 -0
- package/package.json +46 -0
|
@@ -0,0 +1,461 @@
|
|
|
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.Client = void 0;
|
|
37
|
+
const platform_node_js_1 = require("../platform.node.js");
|
|
38
|
+
const Errors = __importStar(require("../errors/index.js"));
|
|
39
|
+
const index_js_1 = require("../raw/index.js");
|
|
40
|
+
const index_js_2 = require("../session/secretChats/index.js");
|
|
41
|
+
const connection_js_1 = require("../connection/connection.js");
|
|
42
|
+
const _Session = __importStar(require("./Session.js"));
|
|
43
|
+
const Version = __importStar(require("../Version.node.js"));
|
|
44
|
+
const helpers = __importStar(require("../helpers.js"));
|
|
45
|
+
const Files = __importStar(require("../file/index.js"));
|
|
46
|
+
class Client {
|
|
47
|
+
_apiId;
|
|
48
|
+
_apiHash;
|
|
49
|
+
_storage;
|
|
50
|
+
_testMode;
|
|
51
|
+
_proxy;
|
|
52
|
+
_ipv6;
|
|
53
|
+
_deviceModel;
|
|
54
|
+
_systemVersion;
|
|
55
|
+
_appVersion;
|
|
56
|
+
_systemLangCode;
|
|
57
|
+
_langCode;
|
|
58
|
+
_maxRetries;
|
|
59
|
+
_isCdn;
|
|
60
|
+
_sleepTreshold;
|
|
61
|
+
_takeout;
|
|
62
|
+
_noUpdates;
|
|
63
|
+
_takeoutId;
|
|
64
|
+
_dcId;
|
|
65
|
+
_defaultDcId;
|
|
66
|
+
_session;
|
|
67
|
+
_isConnected;
|
|
68
|
+
_connectionMode;
|
|
69
|
+
_local;
|
|
70
|
+
_secretChat;
|
|
71
|
+
_getFileSemaphore;
|
|
72
|
+
_saveFileSemaphore;
|
|
73
|
+
_maxReconnectRetries;
|
|
74
|
+
_me;
|
|
75
|
+
_handler = [];
|
|
76
|
+
constructor(session, apiHash, apiId, clientOptions) {
|
|
77
|
+
this._storage = session;
|
|
78
|
+
this._apiHash = apiHash;
|
|
79
|
+
this._apiId = apiId ?? session.apiId;
|
|
80
|
+
this._testMode = clientOptions?.testMode ?? false;
|
|
81
|
+
this._proxy = clientOptions?.proxy;
|
|
82
|
+
this._ipv6 = clientOptions?.ipv6 ?? false;
|
|
83
|
+
this._deviceModel = clientOptions?.deviceModel ?? platform_node_js_1.os.type().toString();
|
|
84
|
+
this._systemVersion =
|
|
85
|
+
clientOptions?.systemVersion ??
|
|
86
|
+
('Deno' in globalThis
|
|
87
|
+
?
|
|
88
|
+
Deno.version?.deno
|
|
89
|
+
?
|
|
90
|
+
`Deno ${Deno.version?.deno}`
|
|
91
|
+
: `Deno unknown`
|
|
92
|
+
: platform_node_js_1.os.release().toString());
|
|
93
|
+
this._appVersion = clientOptions?.appVersion ?? Version.version;
|
|
94
|
+
this._systemLangCode = clientOptions?.systemLangCode ?? 'en';
|
|
95
|
+
this._langCode = clientOptions?.langCode ?? this._systemLangCode;
|
|
96
|
+
this._sleepTreshold = clientOptions?.sleepTreshold ?? 10000;
|
|
97
|
+
this._maxRetries = clientOptions?.maxRetries ?? 5;
|
|
98
|
+
this._isCdn = clientOptions?.isCdn ?? false;
|
|
99
|
+
this._noUpdates = clientOptions?.noUpdates ?? false;
|
|
100
|
+
this._takeout = clientOptions?.takeout ?? false;
|
|
101
|
+
this._connectionMode = clientOptions?.tcp ?? connection_js_1.TCP.TCPFull;
|
|
102
|
+
this._local =
|
|
103
|
+
clientOptions?.local ??
|
|
104
|
+
((platform_node_js_1.isBrowser && globalThis && globalThis.location.protocol !== 'https:') || true);
|
|
105
|
+
this._secretChat = new index_js_2.SecretChat(session, this);
|
|
106
|
+
this._getFileSemaphore = new platform_node_js_1.Semaphore(clientOptions?.maxConcurrentTransmissions || 1);
|
|
107
|
+
this._saveFileSemaphore = new platform_node_js_1.Semaphore(clientOptions?.maxConcurrentTransmissions || 1);
|
|
108
|
+
this._maxReconnectRetries = clientOptions?.maxReconnectRetries || 3;
|
|
109
|
+
this._defaultDcId = clientOptions?.defaultDCId || 2;
|
|
110
|
+
}
|
|
111
|
+
exportSession() {
|
|
112
|
+
return _Session.exportSession.call(this);
|
|
113
|
+
}
|
|
114
|
+
invoke(query, retries = this._maxRetries, timeout = 15000, sleepTreshold = this._sleepTreshold) {
|
|
115
|
+
return _Session.invoke.call(this, query, retries, timeout, sleepTreshold);
|
|
116
|
+
}
|
|
117
|
+
logout() {
|
|
118
|
+
return _Session.logout.call(this);
|
|
119
|
+
}
|
|
120
|
+
start(auth) {
|
|
121
|
+
return _Session.start.call(this, auth);
|
|
122
|
+
}
|
|
123
|
+
connect() {
|
|
124
|
+
return _Session.connect.call(this);
|
|
125
|
+
}
|
|
126
|
+
async handleUpdate(update) {
|
|
127
|
+
if (!this._noUpdates) {
|
|
128
|
+
await this.fetchPeers('users' in update ? update.users : []);
|
|
129
|
+
await this.fetchPeers('chats' in update ? update.chats : []);
|
|
130
|
+
if (update instanceof index_js_1.Raw.Updates) {
|
|
131
|
+
const parsed = [];
|
|
132
|
+
for (const up of update.updates) {
|
|
133
|
+
if (up instanceof index_js_1.Raw.UpdateEncryption) {
|
|
134
|
+
if (up.chat instanceof index_js_1.Raw.EncryptedChat) {
|
|
135
|
+
await this._secretChat.finish(up.chat);
|
|
136
|
+
}
|
|
137
|
+
if (up.chat instanceof index_js_1.Raw.EncryptedChatDiscarded) {
|
|
138
|
+
await this._storage.removeSecretChatById(up.chat.id);
|
|
139
|
+
}
|
|
140
|
+
if (up.chat instanceof index_js_1.Raw.EncryptedChatRequested) {
|
|
141
|
+
await this._secretChat.accept(up.chat);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else if (up instanceof index_js_1.Raw.UpdateNewEncryptedMessage) {
|
|
145
|
+
const modUpdate = await this._handleSecretChatUpdate(up);
|
|
146
|
+
if (modUpdate) {
|
|
147
|
+
parsed.push(modUpdate);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
parsed.push(up);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
update.updates = parsed;
|
|
155
|
+
}
|
|
156
|
+
this._handler.forEach((callback) => {
|
|
157
|
+
return callback(update);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return update;
|
|
161
|
+
}
|
|
162
|
+
async _handleSecretChatUpdate(update) {
|
|
163
|
+
const modUpdate = await index_js_1.UpdateSecretChatMessage.generate(update, this._secretChat);
|
|
164
|
+
if (modUpdate.message instanceof index_js_1.SecretChatMessageService) {
|
|
165
|
+
const msg = modUpdate.message.message;
|
|
166
|
+
if (msg && 'action' in msg) {
|
|
167
|
+
const action = msg.action;
|
|
168
|
+
if (action instanceof index_js_1.Raw.DecryptedMessageActionRequestKey20) {
|
|
169
|
+
await this._secretChat.acceptRekeying(modUpdate.message.chatId, action);
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
if (action instanceof index_js_1.Raw.DecryptedMessageActionAcceptKey20) {
|
|
173
|
+
await this._secretChat.commitRekeying(modUpdate.message.chatId, action);
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (action instanceof index_js_1.Raw.DecryptedMessageActionCommitKey20) {
|
|
177
|
+
await this._secretChat.finalRekeying(modUpdate.message.chatId, action);
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (action instanceof index_js_1.Raw.DecryptedMessageActionNoop20) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (action instanceof index_js_1.Raw.DecryptedMessageActionNotifyLayer17) {
|
|
184
|
+
const peer = await this._storage.getSecretChatById(modUpdate.message.chatId);
|
|
185
|
+
if (peer) {
|
|
186
|
+
peer.layer = action.layer;
|
|
187
|
+
if (action.layer < 73) {
|
|
188
|
+
peer.mtproto = 1;
|
|
189
|
+
}
|
|
190
|
+
await peer.update(this._storage);
|
|
191
|
+
if (action.layer >= 17 &&
|
|
192
|
+
Date.now() / 1000 - peer.created > 15) {
|
|
193
|
+
await this._secretChat.notifyLayer(modUpdate.message.chatId);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
if (action instanceof index_js_1.Raw.DecryptedMessageActionSetMessageTTL8) {
|
|
199
|
+
const peer = await this._storage.getSecretChatById(modUpdate.message.chatId);
|
|
200
|
+
if (peer) {
|
|
201
|
+
peer.ttl = action.ttlSeconds;
|
|
202
|
+
await peer.update(this._storage);
|
|
203
|
+
}
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (modUpdate.message instanceof index_js_1.SecretChatMessage) {
|
|
209
|
+
const msg = modUpdate.message.message;
|
|
210
|
+
if (msg instanceof index_js_1.Raw.DecryptedMessageLayer17) {
|
|
211
|
+
const peer = await this._storage.getSecretChatById(modUpdate.message.chatId);
|
|
212
|
+
if (peer) {
|
|
213
|
+
peer.inSeqNo += 1;
|
|
214
|
+
if (msg.layer >= 17) {
|
|
215
|
+
peer.layer = msg.layer;
|
|
216
|
+
}
|
|
217
|
+
await peer.update(this._storage);
|
|
218
|
+
if (msg.layer >= 17 &&
|
|
219
|
+
Date.now() / 1000 - peer.created > 15) {
|
|
220
|
+
await this._secretChat.notifyLayer(modUpdate.message.chatId);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return modUpdate;
|
|
226
|
+
}
|
|
227
|
+
addHandler(callback) {
|
|
228
|
+
this._handler.push(callback);
|
|
229
|
+
}
|
|
230
|
+
async fetchPeers(peers) {
|
|
231
|
+
let isMin = false;
|
|
232
|
+
const parsedPeers = [];
|
|
233
|
+
for (const peer of peers) {
|
|
234
|
+
if (peer.min) {
|
|
235
|
+
isMin = true;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (peer instanceof index_js_1.Raw.User) {
|
|
239
|
+
peer;
|
|
240
|
+
parsedPeers.push([
|
|
241
|
+
peer.id,
|
|
242
|
+
peer.accessHash ?? BigInt(0),
|
|
243
|
+
peer.bot ? 'bot' : 'user',
|
|
244
|
+
peer.username
|
|
245
|
+
? [peer.username.toLowerCase()]
|
|
246
|
+
: peer.usernames && peer.usernames.length
|
|
247
|
+
? peer.usernames.map((username) => username.username.toLowerCase())
|
|
248
|
+
: undefined,
|
|
249
|
+
peer.phone ? peer.phone : undefined,
|
|
250
|
+
]);
|
|
251
|
+
}
|
|
252
|
+
else if (peer instanceof index_js_1.Raw.Chat || peer instanceof index_js_1.Raw.ChatForbidden) {
|
|
253
|
+
parsedPeers.push([BigInt(-peer.id), BigInt(0), 'group', undefined, undefined]);
|
|
254
|
+
}
|
|
255
|
+
else if (peer instanceof index_js_1.Raw.Channel || peer instanceof index_js_1.Raw.ChannelForbidden) {
|
|
256
|
+
parsedPeers.push([
|
|
257
|
+
helpers.getChannelId(peer.id),
|
|
258
|
+
peer.accessHash ?? BigInt(0),
|
|
259
|
+
peer.broadcast ? 'channel' : 'supergroup',
|
|
260
|
+
peer.username ? [peer.username.toLowerCase()] : undefined,
|
|
261
|
+
undefined,
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
await this._storage.updatePeers(parsedPeers);
|
|
267
|
+
return isMin;
|
|
268
|
+
}
|
|
269
|
+
async resolvePeer(peerId) {
|
|
270
|
+
if (!this._isConnected) {
|
|
271
|
+
throw new Errors.ClientError.ClientDisconnected();
|
|
272
|
+
}
|
|
273
|
+
if (typeof peerId === 'bigint') {
|
|
274
|
+
peerId;
|
|
275
|
+
let peer = await this._storage.getPeerById(peerId);
|
|
276
|
+
if (peer) {
|
|
277
|
+
return peer;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
const type = await helpers.getPeerType(peerId);
|
|
281
|
+
if (type === 'user') {
|
|
282
|
+
await this.fetchPeers(await this.invoke(new index_js_1.Raw.users.GetUsers({
|
|
283
|
+
id: [
|
|
284
|
+
new index_js_1.Raw.InputUser({
|
|
285
|
+
userId: peerId,
|
|
286
|
+
accessHash: BigInt(0),
|
|
287
|
+
}),
|
|
288
|
+
],
|
|
289
|
+
})));
|
|
290
|
+
}
|
|
291
|
+
else if (type === 'chat') {
|
|
292
|
+
await this.invoke(new index_js_1.Raw.messages.GetChats({
|
|
293
|
+
id: [-peerId],
|
|
294
|
+
}));
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
await this.invoke(new index_js_1.Raw.channels.GetChannels({
|
|
298
|
+
id: [
|
|
299
|
+
new index_js_1.Raw.InputChannel({
|
|
300
|
+
channelId: helpers.getChannelId(peerId),
|
|
301
|
+
accessHash: BigInt(0),
|
|
302
|
+
}),
|
|
303
|
+
],
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
peer = await this._storage.getPeerById(peerId);
|
|
307
|
+
if (!peer) {
|
|
308
|
+
throw new Errors.Exceptions.BadRequest.PeerIdInvalid();
|
|
309
|
+
}
|
|
310
|
+
return peer;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else if (typeof peerId === 'string') {
|
|
314
|
+
peerId;
|
|
315
|
+
if (peerId === 'self' || peerId === 'me') {
|
|
316
|
+
return new index_js_1.Raw.InputUserSelf();
|
|
317
|
+
}
|
|
318
|
+
let peer;
|
|
319
|
+
if (peerId.includes('@')) {
|
|
320
|
+
peer = await this._storage.getPeerByUsername(peerId.replace('@', '').trim());
|
|
321
|
+
if (peer) {
|
|
322
|
+
return peer;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
await this.invoke(new index_js_1.Raw.contacts.ResolveUsername({
|
|
326
|
+
username: peerId.replace('@', '').trim(),
|
|
327
|
+
}));
|
|
328
|
+
peer = await this._storage.getPeerByUsername(peerId.replace('@', '').trim());
|
|
329
|
+
if (peer) {
|
|
330
|
+
return peer;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
throw new Errors.Exceptions.BadRequest.PeerIdInvalid();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
else if (!Number.isNaN(peerId)) {
|
|
338
|
+
peer = await this._storage.getPeerById(BigInt(peerId));
|
|
339
|
+
if (peer) {
|
|
340
|
+
return peer;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
const type = await helpers.getPeerType(BigInt(peerId));
|
|
344
|
+
if (type === 'user') {
|
|
345
|
+
await this.fetchPeers(await this.invoke(new index_js_1.Raw.users.GetUsers({
|
|
346
|
+
id: [
|
|
347
|
+
new index_js_1.Raw.InputUser({
|
|
348
|
+
userId: BigInt(peerId),
|
|
349
|
+
accessHash: BigInt(0),
|
|
350
|
+
}),
|
|
351
|
+
],
|
|
352
|
+
})));
|
|
353
|
+
}
|
|
354
|
+
else if (type === 'chat') {
|
|
355
|
+
await this.invoke(new index_js_1.Raw.messages.GetChats({
|
|
356
|
+
id: [-BigInt(peerId)],
|
|
357
|
+
}));
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
await this.invoke(new index_js_1.Raw.channels.GetChannels({
|
|
361
|
+
id: [
|
|
362
|
+
new index_js_1.Raw.InputChannel({
|
|
363
|
+
channelId: helpers.getChannelId(BigInt(peerId)),
|
|
364
|
+
accessHash: BigInt(0),
|
|
365
|
+
}),
|
|
366
|
+
],
|
|
367
|
+
}));
|
|
368
|
+
}
|
|
369
|
+
peer = await this._storage.getPeerById(BigInt(peerId));
|
|
370
|
+
if (!peer) {
|
|
371
|
+
throw new Errors.Exceptions.BadRequest.PeerIdInvalid();
|
|
372
|
+
}
|
|
373
|
+
return peer;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
peer = await this._storage.getPeerByPhoneNumber(peerId);
|
|
378
|
+
if (peer) {
|
|
379
|
+
return peer;
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
throw new Errors.Exceptions.BadRequest.PeerIdInvalid();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
throw new Errors.Exceptions.BadRequest.PeerIdInvalid();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
startSecretChat(chatId) {
|
|
391
|
+
return this._secretChat.start(chatId);
|
|
392
|
+
}
|
|
393
|
+
destroySecretChat(chatId) {
|
|
394
|
+
return this._secretChat.destroy(chatId);
|
|
395
|
+
}
|
|
396
|
+
saveFile({ source, fileName, fileId, filePart, progress, }) {
|
|
397
|
+
return Files.upload(this, source, fileName, fileId, filePart, progress);
|
|
398
|
+
}
|
|
399
|
+
saveFileStream({ source, fileName, progress, }) {
|
|
400
|
+
return Files.uploadStream(this, source, fileName, progress);
|
|
401
|
+
}
|
|
402
|
+
downloadStream({ file, dcId, limit, offset }) {
|
|
403
|
+
return Files.downloadStream(this, file, dcId, limit || 0, offset || BigInt(0));
|
|
404
|
+
}
|
|
405
|
+
async download({ file, dcId, limit, offset }) {
|
|
406
|
+
const pipe = new Files.File();
|
|
407
|
+
const stream = await Files.downloadStream(this, file, dcId, limit || 0, offset || BigInt(0));
|
|
408
|
+
let resolve;
|
|
409
|
+
const promise = new Promise((res) => {
|
|
410
|
+
resolve = res;
|
|
411
|
+
});
|
|
412
|
+
pipe.on('finish', () => {
|
|
413
|
+
return resolve(pipe.bytes.buffer);
|
|
414
|
+
});
|
|
415
|
+
stream.pipe(pipe);
|
|
416
|
+
return promise;
|
|
417
|
+
}
|
|
418
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
419
|
+
const toPrint = {
|
|
420
|
+
_: this.constructor.name,
|
|
421
|
+
};
|
|
422
|
+
for (const key in this) {
|
|
423
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
424
|
+
const value = this[key];
|
|
425
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
426
|
+
toPrint[key] = value;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return toPrint;
|
|
431
|
+
}
|
|
432
|
+
[Symbol.for('Deno.customInspect')]() {
|
|
433
|
+
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
|
|
434
|
+
}
|
|
435
|
+
toJSON() {
|
|
436
|
+
const toPrint = {
|
|
437
|
+
_: this.constructor.name,
|
|
438
|
+
};
|
|
439
|
+
for (const key in this) {
|
|
440
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
441
|
+
const value = this[key];
|
|
442
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
443
|
+
if (typeof value === 'bigint') {
|
|
444
|
+
toPrint[key] = String(value);
|
|
445
|
+
}
|
|
446
|
+
else if (Array.isArray(value)) {
|
|
447
|
+
toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
toPrint[key] = value;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return toPrint;
|
|
456
|
+
}
|
|
457
|
+
toString() {
|
|
458
|
+
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
exports.Client = Client;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Client } from './Client.js';
|
|
2
|
+
import { Raw, TLObject } from '../raw/index.js';
|
|
3
|
+
import * as _Auth from './Auth.js';
|
|
4
|
+
export declare function loadSession(this: Client): Promise<void>;
|
|
5
|
+
export declare function connect(this: Client): Promise<void>;
|
|
6
|
+
export declare function start(this: Client, auth?: _Auth.SigInBot | _Auth.SigInUser): Promise<Raw.users.UserFull>;
|
|
7
|
+
export declare function logout(this: Client): Promise<any>;
|
|
8
|
+
export declare function exportSession(this: Client): Promise<string>;
|
|
9
|
+
export declare function invoke(this: Client, query: TLObject, retries: number, timeout: number, sleepTreshold: number): Promise<TLObject>;
|
|
@@ -0,0 +1,132 @@
|
|
|
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.loadSession = loadSession;
|
|
37
|
+
exports.connect = connect;
|
|
38
|
+
exports.start = start;
|
|
39
|
+
exports.logout = logout;
|
|
40
|
+
exports.exportSession = exportSession;
|
|
41
|
+
exports.invoke = invoke;
|
|
42
|
+
const index_js_1 = require("../raw/index.js");
|
|
43
|
+
const index_js_2 = require("../session/index.js");
|
|
44
|
+
const Logger_js_1 = require("../Logger.js");
|
|
45
|
+
const Errors = __importStar(require("../errors/index.js"));
|
|
46
|
+
const _Auth = __importStar(require("./Auth.js"));
|
|
47
|
+
const Version = __importStar(require("../Version.node.js"));
|
|
48
|
+
const platform_node_js_1 = require("../platform.node.js");
|
|
49
|
+
async function loadSession() {
|
|
50
|
+
await this._storage.load();
|
|
51
|
+
if (!this._storage.authKey) {
|
|
52
|
+
const [ip, port] = await index_js_2.DataCenter.DataCenter(this._defaultDcId, this._testMode, this._ipv6, false);
|
|
53
|
+
const auth = new index_js_2.Auth(this._defaultDcId, this._testMode, this._ipv6);
|
|
54
|
+
this._storage.setAddress(this._defaultDcId, ip, port, this._testMode);
|
|
55
|
+
this._storage.setApiId(this._apiId);
|
|
56
|
+
this._storage.setAuthKey(await auth.create(), this._defaultDcId);
|
|
57
|
+
}
|
|
58
|
+
if (!this._storage.apiId) {
|
|
59
|
+
this._storage.setApiId(this._apiId);
|
|
60
|
+
}
|
|
61
|
+
if (this._storage.testMode === undefined) {
|
|
62
|
+
this._storage.setAddress(this._storage.dcId, this._storage.ip, this._storage.port, this._testMode);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function connect() {
|
|
66
|
+
if (!this._isConnected) {
|
|
67
|
+
Logger_js_1.Logger.info(`[100] Using version: ${Version.version} - ${Version.getType()}`);
|
|
68
|
+
await loadSession.call(this);
|
|
69
|
+
this._session = new index_js_2.Session(this, this._storage.dcId, this._storage.authKey, this._storage.testMode, this._proxy, false, this._isCdn);
|
|
70
|
+
await this._session.start();
|
|
71
|
+
this._isConnected = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function start(auth) {
|
|
75
|
+
await connect.call(this);
|
|
76
|
+
if (this._storage.userId === undefined) {
|
|
77
|
+
if (auth) {
|
|
78
|
+
if (auth.botToken) {
|
|
79
|
+
await _Auth.siginBot.call(this, await auth.botToken);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
await _Auth.siginUser.call(this, { ...auth });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!this._storage.authKey) {
|
|
87
|
+
throw new Errors.ClientError.AuthKeyMissing();
|
|
88
|
+
}
|
|
89
|
+
if (!this._storage.isBot && this._takeout) {
|
|
90
|
+
const takeout = await this.invoke(new index_js_1.Raw.account.InitTakeoutSession({}));
|
|
91
|
+
this._takeoutId = takeout.id;
|
|
92
|
+
Logger_js_1.Logger.warning(`[104] Takeout session ${this._takeoutId} initiated.`);
|
|
93
|
+
}
|
|
94
|
+
await this.invoke(new index_js_1.Raw.updates.GetState());
|
|
95
|
+
const me = await _Auth.getMe.call(this);
|
|
96
|
+
this._me = me;
|
|
97
|
+
Logger_js_1.Logger.log(`[161] Logined as (${me.fullUser.id})`);
|
|
98
|
+
return me;
|
|
99
|
+
}
|
|
100
|
+
async function logout() {
|
|
101
|
+
await this.invoke(new index_js_1.Raw.auth.LogOut());
|
|
102
|
+
await this._storage.delete();
|
|
103
|
+
Logger_js_1.Logger.info(`[105] Logged out.`);
|
|
104
|
+
return platform_node_js_1.sysprc.exit(0);
|
|
105
|
+
}
|
|
106
|
+
async function exportSession() {
|
|
107
|
+
if (!this._storage.userId) {
|
|
108
|
+
const me = this._me ?? (await _Auth.getMe.call(this));
|
|
109
|
+
this._storage.setUserId(me.fullUser.id);
|
|
110
|
+
this._storage.setIsBot(Boolean(me.users[0].bot));
|
|
111
|
+
}
|
|
112
|
+
return this._storage.exportString();
|
|
113
|
+
}
|
|
114
|
+
async function invoke(query, retries, timeout, sleepTreshold) {
|
|
115
|
+
if (!this._isConnected) {
|
|
116
|
+
throw new Errors.ClientError.ClientDisconnected();
|
|
117
|
+
}
|
|
118
|
+
if (this._noUpdates) {
|
|
119
|
+
query = new index_js_1.Raw.InvokeWithoutUpdates({ query });
|
|
120
|
+
}
|
|
121
|
+
if (this._takeoutId) {
|
|
122
|
+
query = new index_js_1.Raw.InvokeWithTakeout({ query, takeoutId: this._takeoutId });
|
|
123
|
+
}
|
|
124
|
+
const r = await this._session.invoke(query, retries, timeout, sleepTreshold);
|
|
125
|
+
if (typeof r === 'object' && 'users' in r) {
|
|
126
|
+
await this.fetchPeers(r.users);
|
|
127
|
+
}
|
|
128
|
+
if (typeof r === 'object' && 'chats' in r) {
|
|
129
|
+
await this.fetchPeers(r.chats);
|
|
130
|
+
}
|
|
131
|
+
return r;
|
|
132
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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.Session = exports.Auth = exports.Client = void 0;
|
|
37
|
+
exports.Client = __importStar(require("./Client.js"));
|
|
38
|
+
exports.Auth = __importStar(require("./Auth.js"));
|
|
39
|
+
exports.Session = __importStar(require("./Session.js"));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Buffer } from '../../platform.node.js';
|
|
2
|
+
import { TCP } from './tcp.js';
|
|
3
|
+
import type { ProxyInterface } from '../connection.js';
|
|
4
|
+
export declare class TCPAbridged extends TCP {
|
|
5
|
+
constructor();
|
|
6
|
+
connect(ip: string, port: number, proxy?: ProxyInterface, dcId?: number): Promise<void>;
|
|
7
|
+
send(data: Buffer): Promise<void>;
|
|
8
|
+
recv(_length?: number): Promise<Buffer<ArrayBufferLike> | undefined>;
|
|
9
|
+
}
|