sroosh 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/CryptoFile-BROWSER.d.ts +2 -0
- package/CryptoFile-BROWSER.js +23 -0
- package/CryptoFile.d.ts +2 -0
- package/CryptoFile.js +23 -0
- package/Helpers.d.ts +151 -0
- package/Helpers.js +480 -0
- package/Password.d.ts +15 -0
- package/Password.js +271 -0
- package/README.md +105 -0
- package/Utils.d.ts +228 -0
- package/Utils.js +1248 -0
- package/Version.d.ts +1 -0
- package/Version.js +4 -0
- package/client/2fa.d.ts +48 -0
- package/client/2fa.js +109 -0
- package/client/TelegramClient.d.ts +1042 -0
- package/client/TelegramClient.js +1244 -0
- package/client/account.d.ts +0 -0
- package/client/account.js +1 -0
- package/client/auth.d.ts +93 -0
- package/client/auth.js +363 -0
- package/client/bots.d.ts +6 -0
- package/client/bots.js +24 -0
- package/client/buttons.d.ts +4 -0
- package/client/buttons.js +87 -0
- package/client/chats.d.ts +46 -0
- package/client/chats.js +350 -0
- package/client/dialogs.d.ts +52 -0
- package/client/dialogs.js +174 -0
- package/client/downloads.d.ts +157 -0
- package/client/downloads.js +598 -0
- package/client/fs-BROWSER.d.ts +10 -0
- package/client/fs-BROWSER.js +13 -0
- package/client/fs.d.ts +1 -0
- package/client/fs.js +13 -0
- package/client/index.d.ts +15 -0
- package/client/index.js +50 -0
- package/client/messageParse.d.ts +17 -0
- package/client/messageParse.js +191 -0
- package/client/messages.d.ts +259 -0
- package/client/messages.js +804 -0
- package/client/os-BROWSER.d.ts +5 -0
- package/client/os-BROWSER.js +10 -0
- package/client/os.d.ts +2 -0
- package/client/os.js +23 -0
- package/client/path-BROWSER.d.ts +7 -0
- package/client/path-BROWSER.js +8 -0
- package/client/path.d.ts +2 -0
- package/client/path.js +7 -0
- package/client/telegramBaseClient.d.ts +239 -0
- package/client/telegramBaseClient.js +369 -0
- package/client/updates.d.ts +31 -0
- package/client/updates.js +254 -0
- package/client/uploads.d.ts +137 -0
- package/client/uploads.js +514 -0
- package/client/users.d.ts +29 -0
- package/client/users.js +491 -0
- package/crypto/AuthKey.d.ts +20 -0
- package/crypto/AuthKey.js +76 -0
- package/crypto/CTR.d.ts +6 -0
- package/crypto/CTR.js +35 -0
- package/crypto/Factorizator.d.ts +19 -0
- package/crypto/Factorizator.js +74 -0
- package/crypto/IGE.d.ts +18 -0
- package/crypto/IGE.js +34 -0
- package/crypto/RSA.d.ts +15 -0
- package/crypto/RSA.js +150 -0
- package/crypto/converters.d.ts +19 -0
- package/crypto/converters.js +52 -0
- package/crypto/crypto.d.ts +27 -0
- package/crypto/crypto.js +117 -0
- package/crypto/words.d.ts +6 -0
- package/crypto/words.js +48 -0
- package/entityCache.d.ts +7 -0
- package/entityCache.js +79 -0
- package/errors/Common.d.ts +72 -0
- package/errors/Common.js +122 -0
- package/errors/RPCBaseErrors.d.ts +84 -0
- package/errors/RPCBaseErrors.js +134 -0
- package/errors/RPCErrorList.d.ts +37 -0
- package/errors/RPCErrorList.js +117 -0
- package/errors/index.d.ts +11 -0
- package/errors/index.js +29 -0
- package/events/Album.d.ts +36 -0
- package/events/Album.js +104 -0
- package/events/CallbackQuery.d.ts +74 -0
- package/events/CallbackQuery.js +193 -0
- package/events/DeletedMessage.d.ts +45 -0
- package/events/DeletedMessage.js +63 -0
- package/events/EditedMessage.d.ts +30 -0
- package/events/EditedMessage.js +41 -0
- package/events/NewMessage.d.ts +89 -0
- package/events/NewMessage.js +182 -0
- package/events/Raw.d.ts +29 -0
- package/events/Raw.js +43 -0
- package/events/common.d.ts +66 -0
- package/events/common.js +141 -0
- package/events/index.d.ts +2 -0
- package/events/index.js +8 -0
- package/extensions/AsyncQueue.d.ts +10 -0
- package/extensions/AsyncQueue.js +32 -0
- package/extensions/BinaryReader.d.ts +100 -0
- package/extensions/BinaryReader.js +242 -0
- package/extensions/BinaryWriter.d.ts +7 -0
- package/extensions/BinaryWriter.js +15 -0
- package/extensions/Deferred.d.ts +8 -0
- package/extensions/Deferred.js +16 -0
- package/extensions/Logger.d.ts +58 -0
- package/extensions/Logger.js +127 -0
- package/extensions/MessagePacker.d.ts +22 -0
- package/extensions/MessagePacker.js +154 -0
- package/extensions/PendingState.d.ts +12 -0
- package/extensions/PendingState.js +29 -0
- package/extensions/PromisedNetSockets-BROWSER.d.ts +8 -0
- package/extensions/PromisedNetSockets-BROWSER.js +12 -0
- package/extensions/PromisedNetSockets.d.ts +25 -0
- package/extensions/PromisedNetSockets.js +178 -0
- package/extensions/PromisedWebSockets.d.ts +19 -0
- package/extensions/PromisedWebSockets.js +134 -0
- package/extensions/html.d.ts +5 -0
- package/extensions/html.js +213 -0
- package/extensions/index.d.ts +7 -0
- package/extensions/index.js +17 -0
- package/extensions/markdown.d.ts +5 -0
- package/extensions/markdown.js +76 -0
- package/extensions/markdownv2.d.ts +5 -0
- package/extensions/markdownv2.js +51 -0
- package/extensions/net-BROWSER.d.ts +8 -0
- package/extensions/net-BROWSER.js +11 -0
- package/extensions/net.d.ts +1 -0
- package/extensions/net.js +13 -0
- package/extensions/socks-BROWSER.d.ts +3 -0
- package/extensions/socks-BROWSER.js +6 -0
- package/extensions/socks.d.ts +1 -0
- package/extensions/socks.js +13 -0
- package/index.d.ts +14 -0
- package/index.js +48 -0
- package/inspect-BROWSER.d.ts +3 -0
- package/inspect-BROWSER.js +6 -0
- package/inspect.d.ts +1 -0
- package/inspect.js +5 -0
- package/network/Authenticator.d.ts +12 -0
- package/network/Authenticator.js +193 -0
- package/network/MTProtoPlainSender.d.ts +19 -0
- package/network/MTProtoPlainSender.js +74 -0
- package/network/MTProtoSender.d.ts +291 -0
- package/network/MTProtoSender.js +854 -0
- package/network/MTProtoState.d.ts +103 -0
- package/network/MTProtoState.js +267 -0
- package/network/RequestState.d.ts +19 -0
- package/network/RequestState.js +35 -0
- package/network/connection/Connection.d.ts +71 -0
- package/network/connection/Connection.js +157 -0
- package/network/connection/ConnectionWebSocket.d.ts +19 -0
- package/network/connection/ConnectionWebSocket.js +77 -0
- package/network/connection/CustomIntermediatePacketCodec.d.ts +8 -0
- package/network/connection/CustomIntermediatePacketCodec.js +25 -0
- package/network/connection/CustomPromisedWebSockets.d.ts +22 -0
- package/network/connection/CustomPromisedWebSockets.js +170 -0
- package/network/connection/ObfuscatedIO.d.ts +12 -0
- package/network/connection/ObfuscatedIO.js +64 -0
- package/network/connection/TCPAbridged.d.ts +20 -0
- package/network/connection/TCPAbridged.js +58 -0
- package/network/connection/TCPFull.d.ts +17 -0
- package/network/connection/TCPFull.js +61 -0
- package/network/connection/TCPMTProxy.d.ts +50 -0
- package/network/connection/TCPMTProxy.js +121 -0
- package/network/connection/TCPObfuscated.d.ts +18 -0
- package/network/connection/TCPObfuscated.js +79 -0
- package/network/connection/index.d.ts +5 -0
- package/network/connection/index.js +13 -0
- package/network/index.d.ts +11 -0
- package/network/index.js +24 -0
- package/package.json +73 -0
- package/platform.d.ts +3 -0
- package/platform.js +6 -0
- package/requestIter.d.ts +24 -0
- package/requestIter.js +107 -0
- package/sessions/Abstract.d.ts +103 -0
- package/sessions/Abstract.js +6 -0
- package/sessions/CacheApiSession.d.ts +18 -0
- package/sessions/CacheApiSession.js +99 -0
- package/sessions/Memory.d.ts +38 -0
- package/sessions/Memory.js +272 -0
- package/sessions/StoreSession.d.ts +14 -0
- package/sessions/StoreSession.js +77 -0
- package/sessions/StringSession.d.ts +33 -0
- package/sessions/StringSession.js +116 -0
- package/sessions/index.d.ts +4 -0
- package/sessions/index.js +13 -0
- package/sessions/localStorage-BROWSER.d.ts +3 -0
- package/sessions/localStorage-BROWSER.js +9 -0
- package/sessions/localStorage.d.ts +1 -0
- package/sessions/localStorage.js +4 -0
- package/tl/AllTLObjects.d.ts +3 -0
- package/tl/AllTLObjects.js +17 -0
- package/tl/MTProtoRequest.d.ts +19 -0
- package/tl/MTProtoRequest.js +38 -0
- package/tl/api.d.ts +2 -0
- package/tl/api.js +507 -0
- package/tl/apiTl.d.ts +2 -0
- package/tl/apiTl.js +1638 -0
- package/tl/core/GZIPPacked.d.ts +16 -0
- package/tl/core/GZIPPacked.js +51 -0
- package/tl/core/MessageContainer.d.ts +12 -0
- package/tl/core/MessageContainer.js +42 -0
- package/tl/core/RPCResult.d.ts +15 -0
- package/tl/core/RPCResult.js +32 -0
- package/tl/core/TLMessage.d.ts +10 -0
- package/tl/core/TLMessage.js +14 -0
- package/tl/core/index.d.ts +6 -0
- package/tl/core/index.js +16 -0
- package/tl/custom/button.d.ts +25 -0
- package/tl/custom/button.js +78 -0
- package/tl/custom/chatGetter.d.ts +30 -0
- package/tl/custom/chatGetter.js +114 -0
- package/tl/custom/dialog.d.ts +31 -0
- package/tl/custom/dialog.js +40 -0
- package/tl/custom/draft.d.ts +22 -0
- package/tl/custom/draft.js +48 -0
- package/tl/custom/file.d.ts +22 -0
- package/tl/custom/file.js +68 -0
- package/tl/custom/forward.d.ts +16 -0
- package/tl/custom/forward.js +47 -0
- package/tl/custom/index.d.ts +1 -0
- package/tl/custom/index.js +5 -0
- package/tl/custom/inlineResult.d.ts +33 -0
- package/tl/custom/inlineResult.js +87 -0
- package/tl/custom/inlineResults.d.ts +21 -0
- package/tl/custom/inlineResults.js +26 -0
- package/tl/custom/message.d.ts +428 -0
- package/tl/custom/message.js +702 -0
- package/tl/custom/messageButton.d.ts +55 -0
- package/tl/custom/messageButton.js +152 -0
- package/tl/custom/senderGetter.d.ts +29 -0
- package/tl/custom/senderGetter.js +55 -0
- package/tl/generateModule.d.ts +1 -0
- package/tl/generateModule.js +17 -0
- package/tl/generationHelpers.d.ts +12 -0
- package/tl/generationHelpers.js +289 -0
- package/tl/index.d.ts +3 -0
- package/tl/index.js +10 -0
- package/tl/patched/index.d.ts +2 -0
- package/tl/patched/index.js +77 -0
- package/tl/schemaTl.d.ts +2 -0
- package/tl/schemaTl.js +64 -0
- package/tl/types-generator/generate.d.ts +1 -0
- package/tl/types-generator/generate.js +84 -0
- package/tl/types-generator/template.d.ts +6 -0
- package/tl/types-generator/template.js +257 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const MemorySession = require("./Memory");
|
|
3
|
+
const AuthKey = require("../crypto/AuthKey");
|
|
4
|
+
const utils = require("../Utils");
|
|
5
|
+
const CACHE_NAME = "GramJs";
|
|
6
|
+
const STORAGE_KEY_BASE = "GramJs-session-";
|
|
7
|
+
class CacheApi extends MemorySession {
|
|
8
|
+
constructor(sessionId) {
|
|
9
|
+
super();
|
|
10
|
+
this._storageKey = sessionId;
|
|
11
|
+
this._authKeys = {};
|
|
12
|
+
}
|
|
13
|
+
async load() {
|
|
14
|
+
if (!this._storageKey) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const json = await fetchFromCache(this._storageKey);
|
|
19
|
+
const { mainDcId, keys, hashes } = JSON.parse(json);
|
|
20
|
+
const { ipAddress, port } = await utils.getDC(mainDcId);
|
|
21
|
+
this.setDC(mainDcId, ipAddress, port, true);
|
|
22
|
+
Object.keys(keys).forEach((dcId) => {
|
|
23
|
+
if (keys[dcId] && hashes[dcId]) {
|
|
24
|
+
this._authKeys[dcId] = new AuthKey(Buffer.from(keys[dcId].data), Buffer.from(hashes[dcId].data));
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
throw new Error(`Failed to retrieve or parse JSON from Cache for key ${this._storageKey}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
setDC(dcId, serverAddress, port, skipUpdateStorage = false) {
|
|
33
|
+
this._dcId = dcId;
|
|
34
|
+
this._serverAddress = serverAddress;
|
|
35
|
+
this._port = port;
|
|
36
|
+
delete this._authKeys[dcId];
|
|
37
|
+
if (!skipUpdateStorage) {
|
|
38
|
+
void this._updateStorage();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async save() {
|
|
42
|
+
if (!this._storageKey) {
|
|
43
|
+
this._storageKey = generateStorageKey();
|
|
44
|
+
}
|
|
45
|
+
await this._updateStorage();
|
|
46
|
+
return this._storageKey;
|
|
47
|
+
}
|
|
48
|
+
get authKey() {
|
|
49
|
+
throw new Error("Not supported");
|
|
50
|
+
}
|
|
51
|
+
set authKey(value) {
|
|
52
|
+
throw new Error("Not supported");
|
|
53
|
+
}
|
|
54
|
+
getAuthKey(dcId = this._dcId) {
|
|
55
|
+
return this._authKeys[dcId];
|
|
56
|
+
}
|
|
57
|
+
setAuthKey(authKey, dcId = this._dcId) {
|
|
58
|
+
this._authKeys[dcId] = authKey;
|
|
59
|
+
void this._updateStorage();
|
|
60
|
+
}
|
|
61
|
+
async _updateStorage() {
|
|
62
|
+
if (!this._storageKey) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const sessionData = {
|
|
66
|
+
mainDcId: this._dcId,
|
|
67
|
+
keys: {},
|
|
68
|
+
hashes: {},
|
|
69
|
+
};
|
|
70
|
+
Object.keys(this._authKeys).map((dcId) => {
|
|
71
|
+
const authKey = this._authKeys[dcId];
|
|
72
|
+
sessionData.keys[dcId] = authKey._key;
|
|
73
|
+
sessionData.hashes[dcId] = authKey._hash;
|
|
74
|
+
});
|
|
75
|
+
await saveToCache(this._storageKey, JSON.stringify(sessionData));
|
|
76
|
+
}
|
|
77
|
+
async delete() {
|
|
78
|
+
const request = new Request(this._storageKey);
|
|
79
|
+
const cache = await self.caches.open(CACHE_NAME);
|
|
80
|
+
await cache.delete(request);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function generateStorageKey() {
|
|
84
|
+
// Creating two sessions at the same moment is not expected nor supported.
|
|
85
|
+
return `${STORAGE_KEY_BASE}${Date.now()}`;
|
|
86
|
+
}
|
|
87
|
+
async function fetchFromCache(key) {
|
|
88
|
+
const request = new Request(key);
|
|
89
|
+
const cache = await self.caches.open(CACHE_NAME);
|
|
90
|
+
const cached = await cache.match(request);
|
|
91
|
+
return cached ? cached.text() : null;
|
|
92
|
+
}
|
|
93
|
+
async function saveToCache(key, data) {
|
|
94
|
+
const request = new Request(key);
|
|
95
|
+
const response = new Response(data);
|
|
96
|
+
const cache = await self.caches.open(CACHE_NAME);
|
|
97
|
+
return cache.put(request, response);
|
|
98
|
+
}
|
|
99
|
+
module.exports = CacheApi;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Session } from "./Abstract";
|
|
2
|
+
import type { AuthKey } from "../crypto/AuthKey";
|
|
3
|
+
import { Api } from "../tl";
|
|
4
|
+
import bigInt from "big-integer";
|
|
5
|
+
import type { EntityLike } from "../define";
|
|
6
|
+
export declare class MemorySession extends Session {
|
|
7
|
+
protected _serverAddress?: string;
|
|
8
|
+
protected _dcId: number;
|
|
9
|
+
protected _port?: number;
|
|
10
|
+
protected _takeoutId: undefined;
|
|
11
|
+
protected _entities: Set<any>;
|
|
12
|
+
protected _updateStates: {};
|
|
13
|
+
protected _authKey?: AuthKey;
|
|
14
|
+
constructor();
|
|
15
|
+
setDC(dcId: number, serverAddress: string, port: number): void;
|
|
16
|
+
get dcId(): number;
|
|
17
|
+
get serverAddress(): string;
|
|
18
|
+
get port(): number;
|
|
19
|
+
get authKey(): AuthKey | undefined;
|
|
20
|
+
set authKey(value: AuthKey | undefined);
|
|
21
|
+
get takeoutId(): undefined;
|
|
22
|
+
set takeoutId(value: undefined);
|
|
23
|
+
getAuthKey(dcId?: number): AuthKey | undefined;
|
|
24
|
+
setAuthKey(authKey?: AuthKey, dcId?: number): undefined;
|
|
25
|
+
close(): void;
|
|
26
|
+
save(): void;
|
|
27
|
+
load(): Promise<void>;
|
|
28
|
+
delete(): void;
|
|
29
|
+
_entityValuesToRow(id: bigInt.BigInteger | string, hash: bigInt.BigInteger | string, username: string, phone: string, name: string): (string | bigInt.BigInteger)[];
|
|
30
|
+
_entityToRow(e: any): (string | bigInt.BigInteger)[] | undefined;
|
|
31
|
+
_entitiesToRows(tlo: any): (string | bigInt.BigInteger)[][];
|
|
32
|
+
processEntities(tlo: any): void;
|
|
33
|
+
getEntityRowsByPhone(phone: string): any[] | undefined;
|
|
34
|
+
getEntityRowsByUsername(username: string): any[] | undefined;
|
|
35
|
+
getEntityRowsByName(name: string): any[] | undefined;
|
|
36
|
+
getEntityRowsById(id: string | bigInt.BigInteger, exact?: boolean): any[] | undefined;
|
|
37
|
+
getInputEntity(key: EntityLike): Api.TypeInputPeer;
|
|
38
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MemorySession = void 0;
|
|
7
|
+
const Abstract_1 = require("./Abstract");
|
|
8
|
+
const tl_1 = require("../tl");
|
|
9
|
+
const big_integer_1 = __importDefault(require("big-integer"));
|
|
10
|
+
const Utils_1 = require("../Utils");
|
|
11
|
+
const Helpers_1 = require("../Helpers");
|
|
12
|
+
const __1 = require("../");
|
|
13
|
+
class MemorySession extends Abstract_1.Session {
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
this._serverAddress = undefined;
|
|
17
|
+
this._dcId = 0;
|
|
18
|
+
this._port = undefined;
|
|
19
|
+
this._takeoutId = undefined;
|
|
20
|
+
this._entities = new Set();
|
|
21
|
+
this._updateStates = {};
|
|
22
|
+
}
|
|
23
|
+
setDC(dcId, serverAddress, port) {
|
|
24
|
+
this._dcId = dcId | 0;
|
|
25
|
+
this._serverAddress = serverAddress;
|
|
26
|
+
this._port = port;
|
|
27
|
+
}
|
|
28
|
+
get dcId() {
|
|
29
|
+
return this._dcId;
|
|
30
|
+
}
|
|
31
|
+
get serverAddress() {
|
|
32
|
+
return this._serverAddress;
|
|
33
|
+
}
|
|
34
|
+
get port() {
|
|
35
|
+
return this._port;
|
|
36
|
+
}
|
|
37
|
+
get authKey() {
|
|
38
|
+
return this._authKey;
|
|
39
|
+
}
|
|
40
|
+
set authKey(value) {
|
|
41
|
+
this._authKey = value;
|
|
42
|
+
}
|
|
43
|
+
get takeoutId() {
|
|
44
|
+
return this._takeoutId;
|
|
45
|
+
}
|
|
46
|
+
set takeoutId(value) {
|
|
47
|
+
this._takeoutId = value;
|
|
48
|
+
}
|
|
49
|
+
getAuthKey(dcId) {
|
|
50
|
+
if (dcId && dcId !== this.dcId) {
|
|
51
|
+
// Not supported.
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return this.authKey;
|
|
55
|
+
}
|
|
56
|
+
setAuthKey(authKey, dcId) {
|
|
57
|
+
if (dcId && dcId !== this.dcId) {
|
|
58
|
+
// Not supported.
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
this.authKey = authKey;
|
|
62
|
+
}
|
|
63
|
+
close() { }
|
|
64
|
+
save() { }
|
|
65
|
+
async load() { }
|
|
66
|
+
delete() { }
|
|
67
|
+
_entityValuesToRow(id, hash, username, phone, name) {
|
|
68
|
+
// While this is a simple implementation it might be overrode by,
|
|
69
|
+
// other classes so they don't need to implement the plural form
|
|
70
|
+
// of the method. Don't remove.
|
|
71
|
+
return [id, hash, username, phone, name];
|
|
72
|
+
}
|
|
73
|
+
_entityToRow(e) {
|
|
74
|
+
if (!(e.classType === "constructor")) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
let p;
|
|
78
|
+
let markedId;
|
|
79
|
+
try {
|
|
80
|
+
p = (0, Utils_1.getInputPeer)(e, false);
|
|
81
|
+
markedId = (0, Utils_1.getPeerId)(p);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
let pHash;
|
|
87
|
+
if (p instanceof tl_1.Api.InputPeerUser ||
|
|
88
|
+
p instanceof tl_1.Api.InputPeerChannel) {
|
|
89
|
+
pHash = p.accessHash;
|
|
90
|
+
}
|
|
91
|
+
else if (p instanceof tl_1.Api.InputPeerChat) {
|
|
92
|
+
pHash = big_integer_1.default.zero;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
let username = e.username;
|
|
98
|
+
if (username) {
|
|
99
|
+
username = username.toLowerCase();
|
|
100
|
+
}
|
|
101
|
+
const phone = e.phone;
|
|
102
|
+
const name = (0, Utils_1.getDisplayName)(e);
|
|
103
|
+
return this._entityValuesToRow(markedId, pHash, username, phone, name);
|
|
104
|
+
}
|
|
105
|
+
_entitiesToRows(tlo) {
|
|
106
|
+
let entities = [];
|
|
107
|
+
if (!(tlo.classType === "constructor") && (0, Helpers_1.isArrayLike)(tlo)) {
|
|
108
|
+
// This may be a list of users already for instance
|
|
109
|
+
entities = tlo;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
if (typeof tlo === "object") {
|
|
113
|
+
if ("user" in tlo) {
|
|
114
|
+
entities.push(tlo.user);
|
|
115
|
+
}
|
|
116
|
+
if ("chat" in tlo) {
|
|
117
|
+
entities.push(tlo.chat);
|
|
118
|
+
}
|
|
119
|
+
if ("channel" in tlo) {
|
|
120
|
+
entities.push(tlo.channel);
|
|
121
|
+
}
|
|
122
|
+
if ("chats" in tlo && (0, Helpers_1.isArrayLike)(tlo.chats)) {
|
|
123
|
+
entities = entities.concat(tlo.chats);
|
|
124
|
+
}
|
|
125
|
+
if ("users" in tlo && (0, Helpers_1.isArrayLike)(tlo.users)) {
|
|
126
|
+
entities = entities.concat(tlo.users);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const rows = []; // Rows to add (id, hash, username, phone, name)
|
|
131
|
+
for (const e of entities) {
|
|
132
|
+
const row = this._entityToRow(e);
|
|
133
|
+
if (row) {
|
|
134
|
+
rows.push(row);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return rows;
|
|
138
|
+
}
|
|
139
|
+
processEntities(tlo) {
|
|
140
|
+
const entitiesSet = this._entitiesToRows(tlo);
|
|
141
|
+
for (const e of entitiesSet) {
|
|
142
|
+
this._entities.add(e);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
getEntityRowsByPhone(phone) {
|
|
146
|
+
for (const e of this._entities) {
|
|
147
|
+
// id, hash, username, phone, name
|
|
148
|
+
if (e[3] === phone) {
|
|
149
|
+
return [e[0], e[1]];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
getEntityRowsByUsername(username) {
|
|
154
|
+
for (const e of this._entities) {
|
|
155
|
+
// id, hash, username, phone, name
|
|
156
|
+
if (e[2] === username) {
|
|
157
|
+
return [e[0], e[1]];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
getEntityRowsByName(name) {
|
|
162
|
+
for (const e of this._entities) {
|
|
163
|
+
// id, hash, username, phone, name
|
|
164
|
+
if (e[4] === name) {
|
|
165
|
+
return [e[0], e[1]];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
getEntityRowsById(id, exact = true) {
|
|
170
|
+
if (exact) {
|
|
171
|
+
for (const e of this._entities) {
|
|
172
|
+
// id, hash, username, phone, name
|
|
173
|
+
if (e[0] === id) {
|
|
174
|
+
return [e[0], e[1]];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const ids = [
|
|
180
|
+
__1.utils.getPeerId(new tl_1.Api.PeerUser({ userId: (0, Helpers_1.returnBigInt)(id) })),
|
|
181
|
+
__1.utils.getPeerId(new tl_1.Api.PeerChat({ chatId: (0, Helpers_1.returnBigInt)(id) })),
|
|
182
|
+
__1.utils.getPeerId(new tl_1.Api.PeerChannel({ channelId: (0, Helpers_1.returnBigInt)(id) })),
|
|
183
|
+
];
|
|
184
|
+
for (const e of this._entities) {
|
|
185
|
+
// id, hash, username, phone, name
|
|
186
|
+
if (ids.includes(e[0])) {
|
|
187
|
+
return [e[0], e[1]];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
getInputEntity(key) {
|
|
193
|
+
let exact;
|
|
194
|
+
if (typeof key === "object" &&
|
|
195
|
+
!big_integer_1.default.isInstance(key) &&
|
|
196
|
+
key.SUBCLASS_OF_ID) {
|
|
197
|
+
if (key.SUBCLASS_OF_ID == 0xc91c90b6 ||
|
|
198
|
+
key.SUBCLASS_OF_ID == 0xe669bf46 ||
|
|
199
|
+
key.SUBCLASS_OF_ID == 0x40f202fd) {
|
|
200
|
+
// @ts-ignore
|
|
201
|
+
return key;
|
|
202
|
+
}
|
|
203
|
+
// Try to early return if this key can be casted as input peer
|
|
204
|
+
return __1.utils.getInputPeer(key);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
// Not a TLObject or can't be cast into InputPeer
|
|
208
|
+
if (typeof key === "object") {
|
|
209
|
+
key = __1.utils.getPeerId(key);
|
|
210
|
+
exact = true;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
exact = false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (big_integer_1.default.isInstance(key) ||
|
|
217
|
+
typeof key == "bigint" ||
|
|
218
|
+
typeof key == "number") {
|
|
219
|
+
key = key.toString();
|
|
220
|
+
}
|
|
221
|
+
let result = undefined;
|
|
222
|
+
if (typeof key === "string") {
|
|
223
|
+
const phone = __1.utils.parsePhone(key);
|
|
224
|
+
if (phone) {
|
|
225
|
+
result = this.getEntityRowsByPhone(phone);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
const { username, isInvite } = __1.utils.parseUsername(key);
|
|
229
|
+
if (username && !isInvite) {
|
|
230
|
+
result = this.getEntityRowsByUsername(username);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (!result) {
|
|
234
|
+
const id = __1.utils.parseID(key);
|
|
235
|
+
if (id) {
|
|
236
|
+
result = this.getEntityRowsById(id, exact);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!result) {
|
|
240
|
+
result = this.getEntityRowsByName(key);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (result) {
|
|
244
|
+
let entityId = result[0]; // unpack resulting tuple
|
|
245
|
+
const entityHash = (0, big_integer_1.default)(result[1]);
|
|
246
|
+
const resolved = __1.utils.resolveId((0, Helpers_1.returnBigInt)(entityId));
|
|
247
|
+
entityId = resolved[0];
|
|
248
|
+
const kind = resolved[1];
|
|
249
|
+
// removes the mark and returns type of entity
|
|
250
|
+
if (kind === tl_1.Api.PeerUser) {
|
|
251
|
+
return new tl_1.Api.InputPeerUser({
|
|
252
|
+
userId: entityId,
|
|
253
|
+
accessHash: entityHash,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
else if (kind === tl_1.Api.PeerChat) {
|
|
257
|
+
return new tl_1.Api.InputPeerChat({ chatId: entityId });
|
|
258
|
+
}
|
|
259
|
+
else if (kind === tl_1.Api.PeerChannel) {
|
|
260
|
+
return new tl_1.Api.InputPeerChannel({
|
|
261
|
+
channelId: entityId,
|
|
262
|
+
accessHash: entityHash,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
throw new Error("Could not find input entity with key " + key);
|
|
268
|
+
}
|
|
269
|
+
throw new Error("Could not find input entity with key " + key);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
exports.MemorySession = MemorySession;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MemorySession } from "./Memory";
|
|
2
|
+
import { AuthKey } from "../crypto/AuthKey";
|
|
3
|
+
import bigInt from "big-integer";
|
|
4
|
+
export declare class StoreSession extends MemorySession {
|
|
5
|
+
private readonly sessionName;
|
|
6
|
+
private store;
|
|
7
|
+
constructor(sessionName: string, divider?: string);
|
|
8
|
+
load(): Promise<void>;
|
|
9
|
+
setDC(dcId: number, serverAddress: string, port: number): void;
|
|
10
|
+
set authKey(value: AuthKey | undefined);
|
|
11
|
+
get authKey(): AuthKey | undefined;
|
|
12
|
+
processEntities(tlo: any): void;
|
|
13
|
+
getEntityRowsById(id: string | bigInt.BigInteger, exact?: boolean): any;
|
|
14
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.StoreSession = void 0;
|
|
7
|
+
const Memory_1 = require("./Memory");
|
|
8
|
+
const store2_1 = __importDefault(require("store2"));
|
|
9
|
+
const AuthKey_1 = require("../crypto/AuthKey");
|
|
10
|
+
class StoreSession extends Memory_1.MemorySession {
|
|
11
|
+
constructor(sessionName, divider = ":") {
|
|
12
|
+
super();
|
|
13
|
+
if (sessionName === "session") {
|
|
14
|
+
throw new Error("Session name can't be 'session'. Please use a different name.");
|
|
15
|
+
}
|
|
16
|
+
if (typeof localStorage === "undefined" || localStorage === null) {
|
|
17
|
+
const LocalStorage = require("./localStorage").LocalStorage;
|
|
18
|
+
this.store = store2_1.default.area(sessionName, new LocalStorage("./" + sessionName));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.store = store2_1.default.area(sessionName, localStorage);
|
|
22
|
+
}
|
|
23
|
+
if (divider == undefined) {
|
|
24
|
+
divider = ":";
|
|
25
|
+
}
|
|
26
|
+
this.sessionName = sessionName + divider;
|
|
27
|
+
}
|
|
28
|
+
async load() {
|
|
29
|
+
let authKey = this.store.get(this.sessionName + "authKey");
|
|
30
|
+
if (authKey && typeof authKey === "object") {
|
|
31
|
+
this._authKey = new AuthKey_1.AuthKey();
|
|
32
|
+
if ("data" in authKey) {
|
|
33
|
+
authKey = Buffer.from(authKey.data);
|
|
34
|
+
}
|
|
35
|
+
await this._authKey.setKey(authKey);
|
|
36
|
+
}
|
|
37
|
+
const dcId = this.store.get(this.sessionName + "dcId");
|
|
38
|
+
if (dcId) {
|
|
39
|
+
this._dcId = dcId;
|
|
40
|
+
}
|
|
41
|
+
const port = this.store.get(this.sessionName + "port");
|
|
42
|
+
if (port) {
|
|
43
|
+
this._port = port;
|
|
44
|
+
}
|
|
45
|
+
const serverAddress = this.store.get(this.sessionName + "serverAddress");
|
|
46
|
+
if (serverAddress) {
|
|
47
|
+
this._serverAddress = serverAddress;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
setDC(dcId, serverAddress, port) {
|
|
51
|
+
this.store.set(this.sessionName + "dcId", dcId);
|
|
52
|
+
this.store.set(this.sessionName + "port", port);
|
|
53
|
+
this.store.set(this.sessionName + "serverAddress", serverAddress);
|
|
54
|
+
super.setDC(dcId, serverAddress, port);
|
|
55
|
+
}
|
|
56
|
+
set authKey(value) {
|
|
57
|
+
this._authKey = value;
|
|
58
|
+
this.store.set(this.sessionName + "authKey", value === null || value === void 0 ? void 0 : value.getKey());
|
|
59
|
+
}
|
|
60
|
+
get authKey() {
|
|
61
|
+
return this._authKey;
|
|
62
|
+
}
|
|
63
|
+
processEntities(tlo) {
|
|
64
|
+
const rows = this._entitiesToRows(tlo);
|
|
65
|
+
if (!rows) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
for (const row of rows) {
|
|
69
|
+
row.push(new Date().getTime().toString());
|
|
70
|
+
this.store.set(this.sessionName + row[0], row);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
getEntityRowsById(id, exact = true) {
|
|
74
|
+
return this.store.get(this.sessionName + id.toString());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.StoreSession = StoreSession;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { MemorySession } from "./Memory";
|
|
3
|
+
export declare class StringSession extends MemorySession {
|
|
4
|
+
_key?: Buffer;
|
|
5
|
+
/**
|
|
6
|
+
* This session file can be easily saved and loaded as a string. According
|
|
7
|
+
* to the initial design, it contains only the data that is necessary for
|
|
8
|
+
* successful connection and authentication, so takeout ID is not stored.
|
|
9
|
+
|
|
10
|
+
* It is thought to be used where you don't want to create any on-disk
|
|
11
|
+
* files but would still like to be able to save and load existing sessions
|
|
12
|
+
* by other means.
|
|
13
|
+
|
|
14
|
+
* You can use custom `encode` and `decode` functions, if present:
|
|
15
|
+
|
|
16
|
+
* `encode` definition must be ``function encode(value: Buffer) -> string:``.
|
|
17
|
+
* `decode` definition must be ``function decode(value: string) -> Buffer:``.
|
|
18
|
+
* @param session {string|null}
|
|
19
|
+
*/
|
|
20
|
+
constructor(session?: string);
|
|
21
|
+
/**
|
|
22
|
+
* @param x {Buffer}
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
static encode(x: Buffer): string;
|
|
26
|
+
/**
|
|
27
|
+
* @param x {string}
|
|
28
|
+
* @returns {Buffer}
|
|
29
|
+
*/
|
|
30
|
+
static decode(x: string): Buffer;
|
|
31
|
+
load(): Promise<void>;
|
|
32
|
+
save(): string;
|
|
33
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringSession = void 0;
|
|
4
|
+
const Memory_1 = require("./Memory");
|
|
5
|
+
const extensions_1 = require("../extensions");
|
|
6
|
+
const AuthKey_1 = require("../crypto/AuthKey");
|
|
7
|
+
const CURRENT_VERSION = "1";
|
|
8
|
+
class StringSession extends Memory_1.MemorySession {
|
|
9
|
+
/**
|
|
10
|
+
* This session file can be easily saved and loaded as a string. According
|
|
11
|
+
* to the initial design, it contains only the data that is necessary for
|
|
12
|
+
* successful connection and authentication, so takeout ID is not stored.
|
|
13
|
+
|
|
14
|
+
* It is thought to be used where you don't want to create any on-disk
|
|
15
|
+
* files but would still like to be able to save and load existing sessions
|
|
16
|
+
* by other means.
|
|
17
|
+
|
|
18
|
+
* You can use custom `encode` and `decode` functions, if present:
|
|
19
|
+
|
|
20
|
+
* `encode` definition must be ``function encode(value: Buffer) -> string:``.
|
|
21
|
+
* `decode` definition must be ``function decode(value: string) -> Buffer:``.
|
|
22
|
+
* @param session {string|null}
|
|
23
|
+
*/
|
|
24
|
+
constructor(session) {
|
|
25
|
+
super();
|
|
26
|
+
if (session) {
|
|
27
|
+
if (session[0] !== CURRENT_VERSION) {
|
|
28
|
+
throw new Error("Not a valid string");
|
|
29
|
+
}
|
|
30
|
+
session = session.slice(1);
|
|
31
|
+
const r = StringSession.decode(session);
|
|
32
|
+
const reader = new extensions_1.BinaryReader(r);
|
|
33
|
+
this._dcId = reader.read(1).readUInt8(0);
|
|
34
|
+
if (session.length == 352) {
|
|
35
|
+
// Telethon session
|
|
36
|
+
const ip_v4 = reader.read(4);
|
|
37
|
+
// TODO looks ugly smh
|
|
38
|
+
this._serverAddress =
|
|
39
|
+
ip_v4[0].toString() +
|
|
40
|
+
"." +
|
|
41
|
+
ip_v4[1].toString() +
|
|
42
|
+
"." +
|
|
43
|
+
ip_v4[2].toString() +
|
|
44
|
+
"." +
|
|
45
|
+
ip_v4[3].toString();
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// TODO find a better of doing this
|
|
49
|
+
const serverAddressLen = reader.read(2).readInt16BE(0);
|
|
50
|
+
if (serverAddressLen > 100) {
|
|
51
|
+
reader.offset -= 2;
|
|
52
|
+
this._serverAddress = reader
|
|
53
|
+
.read(16)
|
|
54
|
+
.toString("hex")
|
|
55
|
+
.match(/.{1,4}/g)
|
|
56
|
+
.map((val) => val.replace(/^0+/, ""))
|
|
57
|
+
.join(":")
|
|
58
|
+
.replace(/0000\:/g, ":")
|
|
59
|
+
.replace(/:{2,}/g, "::");
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this._serverAddress = reader
|
|
63
|
+
.read(serverAddressLen)
|
|
64
|
+
.toString();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this._port = reader.read(2).readInt16BE(0);
|
|
68
|
+
this._key = reader.read(-1);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @param x {Buffer}
|
|
73
|
+
* @returns {string}
|
|
74
|
+
*/
|
|
75
|
+
static encode(x) {
|
|
76
|
+
return x.toString("base64");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @param x {string}
|
|
80
|
+
* @returns {Buffer}
|
|
81
|
+
*/
|
|
82
|
+
static decode(x) {
|
|
83
|
+
return Buffer.from(x, "base64");
|
|
84
|
+
}
|
|
85
|
+
async load() {
|
|
86
|
+
if (this._key) {
|
|
87
|
+
this._authKey = new AuthKey_1.AuthKey();
|
|
88
|
+
await this._authKey.setKey(this._key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
save() {
|
|
92
|
+
if (!this.authKey || !this.serverAddress || !this.port) {
|
|
93
|
+
return "";
|
|
94
|
+
}
|
|
95
|
+
// TS is weird
|
|
96
|
+
const key = this.authKey.getKey();
|
|
97
|
+
if (!key) {
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
const dcBuffer = Buffer.from([this.dcId]);
|
|
101
|
+
const addressBuffer = Buffer.from(this.serverAddress);
|
|
102
|
+
const addressLengthBuffer = Buffer.alloc(2);
|
|
103
|
+
addressLengthBuffer.writeInt16BE(addressBuffer.length, 0);
|
|
104
|
+
const portBuffer = Buffer.alloc(2);
|
|
105
|
+
portBuffer.writeInt16BE(this.port, 0);
|
|
106
|
+
return (CURRENT_VERSION +
|
|
107
|
+
StringSession.encode(Buffer.concat([
|
|
108
|
+
dcBuffer,
|
|
109
|
+
addressLengthBuffer,
|
|
110
|
+
addressBuffer,
|
|
111
|
+
portBuffer,
|
|
112
|
+
key,
|
|
113
|
+
])));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.StringSession = StringSession;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Session = exports.StoreSession = exports.StringSession = exports.MemorySession = void 0;
|
|
4
|
+
var Memory_1 = require("./Memory");
|
|
5
|
+
Object.defineProperty(exports, "MemorySession", { enumerable: true, get: function () { return Memory_1.MemorySession; } });
|
|
6
|
+
var StringSession_1 = require("./StringSession");
|
|
7
|
+
Object.defineProperty(exports, "StringSession", { enumerable: true, get: function () { return StringSession_1.StringSession; } });
|
|
8
|
+
var StoreSession_1 = require("./StoreSession");
|
|
9
|
+
Object.defineProperty(exports, "StoreSession", { enumerable: true, get: function () { return StoreSession_1.StoreSession; } });
|
|
10
|
+
var Abstract_1 = require("./Abstract");
|
|
11
|
+
Object.defineProperty(exports, "Session", { enumerable: true, get: function () { return Abstract_1.Session; } });
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
//export {CacheApiSession} from './CacheApiSession';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const LocalStorage: any;
|