@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,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SecretChat = void 0;
|
|
4
|
+
const platform_node_js_1 = require("../platform.node.js");
|
|
5
|
+
const index_js_1 = require("../raw/index.js");
|
|
6
|
+
class SecretChat {
|
|
7
|
+
id;
|
|
8
|
+
accessHash;
|
|
9
|
+
rekeyStep;
|
|
10
|
+
rekeyExchange;
|
|
11
|
+
created;
|
|
12
|
+
changed;
|
|
13
|
+
isAdmin;
|
|
14
|
+
authKey;
|
|
15
|
+
mtproto;
|
|
16
|
+
layer;
|
|
17
|
+
inSeqNo;
|
|
18
|
+
outSeqNo;
|
|
19
|
+
inSeqNoX;
|
|
20
|
+
outSeqNoX;
|
|
21
|
+
adminId;
|
|
22
|
+
timeRekey;
|
|
23
|
+
ttl;
|
|
24
|
+
_mutex;
|
|
25
|
+
constructor({ id, accessHash, isAdmin, authKey, }) {
|
|
26
|
+
this.id = id;
|
|
27
|
+
this.accessHash = accessHash;
|
|
28
|
+
this.isAdmin = isAdmin;
|
|
29
|
+
this.authKey = platform_node_js_1.Buffer.from(authKey);
|
|
30
|
+
this.created = Date.now() / 1000;
|
|
31
|
+
this.changed = 0;
|
|
32
|
+
this.mtproto = 2;
|
|
33
|
+
this.layer = index_js_1.Raw.Layer;
|
|
34
|
+
this.ttl = 0;
|
|
35
|
+
this.timeRekey = 100;
|
|
36
|
+
this.outSeqNoX = isAdmin ? 1 : 0;
|
|
37
|
+
this.inSeqNoX = isAdmin ? 0 : 1;
|
|
38
|
+
this._mutex = new platform_node_js_1.Mutex();
|
|
39
|
+
}
|
|
40
|
+
async update(storage) {
|
|
41
|
+
const release = await this._mutex.acquire();
|
|
42
|
+
try {
|
|
43
|
+
storage.updateSecretChats([this]);
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
release();
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
static save(storage, params) {
|
|
51
|
+
const tempChat = new SecretChat(params);
|
|
52
|
+
storage.updateSecretChats([tempChat]);
|
|
53
|
+
return tempChat;
|
|
54
|
+
}
|
|
55
|
+
static async remove(storage, id) {
|
|
56
|
+
return await storage.removeSecretChatById(id);
|
|
57
|
+
}
|
|
58
|
+
get input() {
|
|
59
|
+
return new index_js_1.Raw.InputEncryptedChat({
|
|
60
|
+
chatId: this.id,
|
|
61
|
+
accessHash: this.accessHash,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
65
|
+
const toPrint = {
|
|
66
|
+
_: this.constructor.name,
|
|
67
|
+
};
|
|
68
|
+
for (const key in this) {
|
|
69
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
70
|
+
const value = this[key];
|
|
71
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
72
|
+
toPrint[key] = value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return toPrint;
|
|
77
|
+
}
|
|
78
|
+
[Symbol.for('Deno.customInspect')]() {
|
|
79
|
+
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
|
|
80
|
+
}
|
|
81
|
+
toJSON() {
|
|
82
|
+
const toPrint = {
|
|
83
|
+
_: this.constructor.name,
|
|
84
|
+
};
|
|
85
|
+
for (const key in this) {
|
|
86
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
87
|
+
const value = this[key];
|
|
88
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
89
|
+
toPrint[key] = typeof value === 'bigint' ? String(value) : value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return toPrint;
|
|
94
|
+
}
|
|
95
|
+
toString() {
|
|
96
|
+
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.SecretChat = SecretChat;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { AbstractSession } from './Abstract.js';
|
|
2
|
+
import { Raw } from '../raw/index.js';
|
|
3
|
+
import { Buffer } from '../platform.node.js';
|
|
4
|
+
import type { SecretChat } from './SecretChat.js';
|
|
5
|
+
export declare function getInputPeer(id: bigint, accessHash: bigint, type: string): Raw.InputPeerUser | Raw.InputPeerChat | Raw.InputPeerChannel;
|
|
6
|
+
export declare class BaseSession extends AbstractSession {
|
|
7
|
+
protected _ip: string;
|
|
8
|
+
protected _dcId: number;
|
|
9
|
+
protected _port: number;
|
|
10
|
+
protected _peers: Map<bigint, [
|
|
11
|
+
id: bigint,
|
|
12
|
+
accessHash: bigint,
|
|
13
|
+
type: string,
|
|
14
|
+
username?: Array<string>,
|
|
15
|
+
phoneNumber?: string
|
|
16
|
+
]>;
|
|
17
|
+
protected _secretChats: Map<number, SecretChat>;
|
|
18
|
+
protected _authKey: Buffer;
|
|
19
|
+
protected _testMode: boolean;
|
|
20
|
+
protected _apiId: number;
|
|
21
|
+
protected _userId: bigint;
|
|
22
|
+
protected _isBot: boolean;
|
|
23
|
+
constructor();
|
|
24
|
+
setAddress(dcId: number, ip: string, port: number, testMode: boolean): void;
|
|
25
|
+
setAuthKey(authKey: Buffer, dcId: number): void;
|
|
26
|
+
setApiId(apiId: number): void;
|
|
27
|
+
setIsBot(isbot: boolean): void;
|
|
28
|
+
setUserId(userId: bigint): void;
|
|
29
|
+
get authKey(): Buffer<ArrayBufferLike>;
|
|
30
|
+
get isBot(): boolean;
|
|
31
|
+
get testMode(): boolean;
|
|
32
|
+
get userId(): bigint;
|
|
33
|
+
get apiId(): number;
|
|
34
|
+
get dcId(): number;
|
|
35
|
+
get port(): number;
|
|
36
|
+
get ip(): string;
|
|
37
|
+
get peers(): Map<bigint, [id: bigint, accessHash: bigint, type: string, username?: string[] | undefined, phoneNumber?: string | undefined]>;
|
|
38
|
+
get secretChats(): Map<number, SecretChat>;
|
|
39
|
+
load(): Promise<void>;
|
|
40
|
+
delete(): Promise<void>;
|
|
41
|
+
save(): Promise<void>;
|
|
42
|
+
updatePts(_pts: number, _date: number): Promise<void>;
|
|
43
|
+
getPts(): Promise<[pts: number, date: number]>;
|
|
44
|
+
move(session: AbstractSession): Promise<void>;
|
|
45
|
+
updatePeers(peers: Array<[
|
|
46
|
+
id: bigint,
|
|
47
|
+
accessHash: bigint,
|
|
48
|
+
type: string,
|
|
49
|
+
username?: Array<string>,
|
|
50
|
+
phoneNumber?: string
|
|
51
|
+
]>): Promise<void>;
|
|
52
|
+
updateSecretChats(chats: Array<SecretChat>): Promise<void>;
|
|
53
|
+
getSecretChatById(id: number): Promise<SecretChat | undefined>;
|
|
54
|
+
getPeerById(id: bigint): Promise<Raw.InputPeerUser | Raw.InputPeerChat | Raw.InputPeerChannel | undefined>;
|
|
55
|
+
getPeerByUsername(username: string): Promise<Raw.InputPeerUser | Raw.InputPeerChat | Raw.InputPeerChannel | undefined>;
|
|
56
|
+
getPeerByPhoneNumber(phoneNumber: string): Promise<Raw.InputPeerUser | Raw.InputPeerChat | Raw.InputPeerChannel | undefined>;
|
|
57
|
+
removeSecretChatById(id: number): Promise<boolean>;
|
|
58
|
+
exportString(): string;
|
|
59
|
+
toJSON(): {
|
|
60
|
+
[key: string]: any;
|
|
61
|
+
};
|
|
62
|
+
toString(): string;
|
|
63
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseSession = void 0;
|
|
4
|
+
exports.getInputPeer = getInputPeer;
|
|
5
|
+
const Logger_js_1 = require("../Logger.js");
|
|
6
|
+
const Abstract_js_1 = require("./Abstract.js");
|
|
7
|
+
const index_js_1 = require("../raw/index.js");
|
|
8
|
+
const helpers_js_1 = require("../helpers.js");
|
|
9
|
+
const platform_node_js_1 = require("../platform.node.js");
|
|
10
|
+
function getInputPeer(id, accessHash, type) {
|
|
11
|
+
if (type === 'bot' || type === 'user') {
|
|
12
|
+
return new index_js_1.Raw.InputPeerUser({
|
|
13
|
+
userId: id,
|
|
14
|
+
accessHash: accessHash,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
else if (type === 'group') {
|
|
18
|
+
return new index_js_1.Raw.InputPeerChat({
|
|
19
|
+
chatId: -id,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
else if (type === 'channel' || type === 'supergroup') {
|
|
23
|
+
return new index_js_1.Raw.InputPeerChannel({
|
|
24
|
+
channelId: (0, helpers_js_1.getChannelId)(id),
|
|
25
|
+
accessHash: accessHash,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
throw new Error(`Invalid peer type: ${type}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
class BaseSession extends Abstract_js_1.AbstractSession {
|
|
33
|
+
_ip;
|
|
34
|
+
_dcId = 2;
|
|
35
|
+
_port;
|
|
36
|
+
_peers = new Map();
|
|
37
|
+
_secretChats = new Map();
|
|
38
|
+
_authKey;
|
|
39
|
+
_testMode = false;
|
|
40
|
+
_apiId;
|
|
41
|
+
_userId;
|
|
42
|
+
_isBot;
|
|
43
|
+
constructor() {
|
|
44
|
+
super();
|
|
45
|
+
}
|
|
46
|
+
setAddress(dcId, ip, port, testMode) {
|
|
47
|
+
this._dcId = dcId ?? 2;
|
|
48
|
+
this._ip = ip;
|
|
49
|
+
this._port = port ?? 443;
|
|
50
|
+
this._testMode = testMode;
|
|
51
|
+
}
|
|
52
|
+
setAuthKey(authKey, dcId) {
|
|
53
|
+
if (dcId !== this._dcId)
|
|
54
|
+
return;
|
|
55
|
+
this._authKey = authKey;
|
|
56
|
+
}
|
|
57
|
+
setApiId(apiId) {
|
|
58
|
+
this._apiId = apiId;
|
|
59
|
+
}
|
|
60
|
+
setIsBot(isbot) {
|
|
61
|
+
this._isBot = isbot;
|
|
62
|
+
}
|
|
63
|
+
setUserId(userId) {
|
|
64
|
+
this._userId = userId;
|
|
65
|
+
}
|
|
66
|
+
get authKey() {
|
|
67
|
+
return this._authKey;
|
|
68
|
+
}
|
|
69
|
+
get isBot() {
|
|
70
|
+
return this._isBot;
|
|
71
|
+
}
|
|
72
|
+
get testMode() {
|
|
73
|
+
return this._testMode;
|
|
74
|
+
}
|
|
75
|
+
get userId() {
|
|
76
|
+
return this._userId;
|
|
77
|
+
}
|
|
78
|
+
get apiId() {
|
|
79
|
+
return this._apiId;
|
|
80
|
+
}
|
|
81
|
+
get dcId() {
|
|
82
|
+
return this._dcId;
|
|
83
|
+
}
|
|
84
|
+
get port() {
|
|
85
|
+
return this._port;
|
|
86
|
+
}
|
|
87
|
+
get ip() {
|
|
88
|
+
return this._ip;
|
|
89
|
+
}
|
|
90
|
+
get peers() {
|
|
91
|
+
return this._peers;
|
|
92
|
+
}
|
|
93
|
+
get secretChats() {
|
|
94
|
+
return this._secretChats;
|
|
95
|
+
}
|
|
96
|
+
async load() { }
|
|
97
|
+
async delete() { }
|
|
98
|
+
async save() { }
|
|
99
|
+
async updatePts(_pts, _date) { }
|
|
100
|
+
async getPts() {
|
|
101
|
+
const res = [0, 0];
|
|
102
|
+
return res;
|
|
103
|
+
}
|
|
104
|
+
async move(session) {
|
|
105
|
+
Logger_js_1.Logger.info(`[73] Moving session from ${this.constructor.name} to ${session.constructor.name}.`);
|
|
106
|
+
await session.setAddress(this._dcId, this._ip, this._port, this._testMode);
|
|
107
|
+
await session.setAuthKey(this._authKey, this._dcId);
|
|
108
|
+
await session.setApiId(this._apiId);
|
|
109
|
+
await session.setIsBot(this._isBot);
|
|
110
|
+
await session.setUserId(this._userId);
|
|
111
|
+
Logger_js_1.Logger.info(`[74] Successfully move session from ${this.constructor.name} to ${session.constructor.name}.`);
|
|
112
|
+
Logger_js_1.Logger.debug(`[75] Deleting current session, cause: moved to another instance (${session.constructor.name}).`);
|
|
113
|
+
await this.delete();
|
|
114
|
+
}
|
|
115
|
+
async updatePeers(peers) {
|
|
116
|
+
Logger_js_1.Logger.debug(`[76] Updating ${peers.length} peers`);
|
|
117
|
+
for (let peer of peers) {
|
|
118
|
+
this._peers.set(peer[0], peer);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async updateSecretChats(chats) {
|
|
122
|
+
Logger_js_1.Logger.debug(`[109] Updating ${chats.length} secret chats`);
|
|
123
|
+
for (let chat of chats) {
|
|
124
|
+
this._secretChats.set(chat.id, chat);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async getSecretChatById(id) {
|
|
128
|
+
Logger_js_1.Logger.debug(`[110] Getting secret chat by id: ${id}`);
|
|
129
|
+
let chat = this._secretChats.get(id);
|
|
130
|
+
if (chat) {
|
|
131
|
+
return chat;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async getPeerById(id) {
|
|
135
|
+
Logger_js_1.Logger.debug(`[77] Getting peer by id: ${id}`);
|
|
136
|
+
let peer = this._peers.get(id);
|
|
137
|
+
if (peer) {
|
|
138
|
+
return getInputPeer(peer[0], peer[1], peer[2]);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async getPeerByUsername(username) {
|
|
142
|
+
Logger_js_1.Logger.debug(`[78] Getting peer by username: ${username}`);
|
|
143
|
+
for (let [, peer] of this._peers) {
|
|
144
|
+
if (peer[3]) {
|
|
145
|
+
if (Array.isArray(peer[3]) && peer[3].includes(username.toLowerCase())) {
|
|
146
|
+
return getInputPeer(peer[0], peer[1], peer[2]);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async getPeerByPhoneNumber(phoneNumber) {
|
|
152
|
+
Logger_js_1.Logger.debug(`[79] Getting peer by phone number: ${phoneNumber}`);
|
|
153
|
+
for (let [, peer] of this._peers) {
|
|
154
|
+
if (peer[4] && peer[4] === phoneNumber) {
|
|
155
|
+
return getInputPeer(peer[0], peer[1], peer[2]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async removeSecretChatById(id) {
|
|
160
|
+
if (this._secretChats.has(id)) {
|
|
161
|
+
this._secretChats.delete(id);
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
exportString() {
|
|
166
|
+
let bytes = platform_node_js_1.Buffer.alloc(6);
|
|
167
|
+
bytes.writeUInt8(this._dcId, 0);
|
|
168
|
+
bytes.writeUInt32LE(this._apiId, 1);
|
|
169
|
+
bytes.writeUInt8(this._testMode ? 1 : 0, 5);
|
|
170
|
+
bytes = platform_node_js_1.Buffer.concat([bytes, this._authKey]);
|
|
171
|
+
bytes = platform_node_js_1.Buffer.concat([
|
|
172
|
+
bytes,
|
|
173
|
+
packLong(this._userId),
|
|
174
|
+
]);
|
|
175
|
+
bytes = platform_node_js_1.Buffer.concat([
|
|
176
|
+
bytes,
|
|
177
|
+
platform_node_js_1.Buffer.alloc(1),
|
|
178
|
+
]);
|
|
179
|
+
bytes.writeUInt8(this._isBot ? 1 : 0, 270);
|
|
180
|
+
Logger_js_1.Logger.debug(`[80] Exporting ${platform_node_js_1.Buffer.byteLength(bytes)} bytes of session`);
|
|
181
|
+
try {
|
|
182
|
+
return bytes.toString('base64url').replace(/=+$/g, '');
|
|
183
|
+
}
|
|
184
|
+
catch (_error) {
|
|
185
|
+
return bytes.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
189
|
+
const toPrint = {
|
|
190
|
+
_: this.constructor.name,
|
|
191
|
+
};
|
|
192
|
+
for (const key in this) {
|
|
193
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
194
|
+
const value = this[key];
|
|
195
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
196
|
+
toPrint[key] = value;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return toPrint;
|
|
201
|
+
}
|
|
202
|
+
[Symbol.for('Deno.customInspect')]() {
|
|
203
|
+
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
|
|
204
|
+
}
|
|
205
|
+
toJSON() {
|
|
206
|
+
const toPrint = {
|
|
207
|
+
_: this.constructor.name,
|
|
208
|
+
};
|
|
209
|
+
for (const key in this) {
|
|
210
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
211
|
+
const value = this[key];
|
|
212
|
+
if (!key.startsWith('_') && value !== undefined && value !== null) {
|
|
213
|
+
if (typeof value === 'bigint') {
|
|
214
|
+
toPrint[key] = String(value);
|
|
215
|
+
}
|
|
216
|
+
else if (Array.isArray(value)) {
|
|
217
|
+
toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
toPrint[key] = value;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return toPrint;
|
|
226
|
+
}
|
|
227
|
+
toString() {
|
|
228
|
+
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.BaseSession = BaseSession;
|
|
232
|
+
function packLong(long, little = true, signed = false) {
|
|
233
|
+
const bytes = platform_node_js_1.Buffer.alloc(8);
|
|
234
|
+
const shift = BigInt((1 << 16) * (1 << 16));
|
|
235
|
+
if (signed) {
|
|
236
|
+
bytes.writeInt32LE(Number(String(long % shift)), 0);
|
|
237
|
+
bytes.writeInt32LE(Number(String(long / shift)), 4);
|
|
238
|
+
return little ? bytes.reverse() : bytes;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
bytes.writeUInt32LE(Number(String(long % shift)), 0);
|
|
242
|
+
bytes.writeUInt32LE(Number(String(long / shift)), 4);
|
|
243
|
+
return little ? bytes.reverse() : bytes;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringSession = void 0;
|
|
4
|
+
const Logger_js_1 = require("../Logger.js");
|
|
5
|
+
const Session_js_1 = require("./Session.js");
|
|
6
|
+
const index_js_1 = require("../raw/index.js");
|
|
7
|
+
const helpers_js_1 = require("../helpers.js");
|
|
8
|
+
const platform_node_js_1 = require("../platform.node.js");
|
|
9
|
+
class StringSession extends Session_js_1.BaseSession {
|
|
10
|
+
constructor(session) {
|
|
11
|
+
super();
|
|
12
|
+
if (session.length) {
|
|
13
|
+
Logger_js_1.Logger.debug(`[81] Starting parsing string session, length is: ${session.length}`);
|
|
14
|
+
const start = Math.floor(Date.now() / 1000);
|
|
15
|
+
if (session[0] === '1') {
|
|
16
|
+
Logger_js_1.Logger.debug(`[82] The string session look like telethon or gramjs string session, start parsing.`);
|
|
17
|
+
session = session.slice(1);
|
|
18
|
+
const bytes = new index_js_1.BytesIO(platform_node_js_1.Buffer.from((0, helpers_js_1.base64urlTobase64)(session), 'base64'));
|
|
19
|
+
this._dcId = bytes.read(1).readUInt8();
|
|
20
|
+
Logger_js_1.Logger.debug(`[84] Found dcId: ${this._dcId}.`);
|
|
21
|
+
if (session.length === 352) {
|
|
22
|
+
const ipv4 = bytes.read(4);
|
|
23
|
+
this._ip = `${ipv4[0]}.${ipv4[1]}.${ipv4[2]}.${ipv4[3]}`;
|
|
24
|
+
Logger_js_1.Logger.debug(`[85] Found ip: ${this._ip}.`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const serverAddressLen = bytes.read(2).readInt16BE();
|
|
28
|
+
if (serverAddressLen > 1000) {
|
|
29
|
+
bytes.seek(-2, 1);
|
|
30
|
+
this._ip = bytes
|
|
31
|
+
.read(16)
|
|
32
|
+
.toString('hex')
|
|
33
|
+
.match(/.{1,4}/g)
|
|
34
|
+
.map((val) => val.replace(/^0+/, ''))
|
|
35
|
+
.join(':')
|
|
36
|
+
.replace(/0000\:/g, ':')
|
|
37
|
+
.replace(/:{2,}/g, '::');
|
|
38
|
+
Logger_js_1.Logger.debug(`[86] Found ip: ${this._ip}.`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this._ip = bytes.read(serverAddressLen).toString();
|
|
42
|
+
Logger_js_1.Logger.debug(`[87] Found ip: ${this._ip}.`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this._port = bytes.read(2).readInt16BE();
|
|
46
|
+
Logger_js_1.Logger.debug(`[88] Found port: ${this._port}.`);
|
|
47
|
+
this._authKey = bytes.read();
|
|
48
|
+
Logger_js_1.Logger.debug(`[89] Found authKey: ${platform_node_js_1.Buffer.byteLength(this._authKey)} bytes.`);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const bytes = platform_node_js_1.Buffer.from((0, helpers_js_1.base64urlTobase64)(session), 'base64');
|
|
52
|
+
if (platform_node_js_1.Buffer.byteLength(bytes) === 271) {
|
|
53
|
+
Logger_js_1.Logger.debug(`[90] The string session look like pyrogram or tgsnake string session, start parsing.`);
|
|
54
|
+
Logger_js_1.Logger.debug(`[91] String session have a ${platform_node_js_1.Buffer.byteLength(bytes)} bytes`);
|
|
55
|
+
this._dcId = bytes.readUInt8(0);
|
|
56
|
+
Logger_js_1.Logger.debug(`[92] Found dcId: ${this._dcId}.`);
|
|
57
|
+
this._apiId = bytes.readUInt32LE(1);
|
|
58
|
+
Logger_js_1.Logger.debug(`[93] Found apiId: ${this._apiId}.`);
|
|
59
|
+
this._testMode = bytes.readUInt8(5) ? true : false;
|
|
60
|
+
Logger_js_1.Logger.debug(`[94] Found testMode: ${this._testMode}.`);
|
|
61
|
+
this._authKey = bytes.subarray(6, 262);
|
|
62
|
+
Logger_js_1.Logger.debug(`[95] Found authKey: ${platform_node_js_1.Buffer.byteLength(this._authKey)} bytes.`);
|
|
63
|
+
this._userId = BigInt(`0x${bytes.subarray(262, 270).toString('hex')}`);
|
|
64
|
+
Logger_js_1.Logger.debug(`[96] Found userId: ${this._userId}.`);
|
|
65
|
+
this._isBot = bytes.readUInt8(270) ? true : false;
|
|
66
|
+
Logger_js_1.Logger.debug(`[97] Found isBot: ${this._isBot}.`);
|
|
67
|
+
Logger_js_1.Logger.debug(`[98] Done parsing string session (${Math.floor(Date.now() / 1000) - start}s)`);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
Logger_js_1.Logger.error(`[99] Can't parsing ${platform_node_js_1.Buffer.byteLength(bytes)} bytes of string session, we only supported string session from telethon,gramjs,tgsnake, and pyrogram (latest version, old version doesn't supported)`);
|
|
71
|
+
throw new Error(`Invalid String Session`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.StringSession = StringSession;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SecretChat = exports.StringSession = exports.getInputPeer = exports.BaseSession = exports.AbstractSession = void 0;
|
|
4
|
+
var Abstract_js_1 = require("./Abstract.js");
|
|
5
|
+
Object.defineProperty(exports, "AbstractSession", { enumerable: true, get: function () { return Abstract_js_1.AbstractSession; } });
|
|
6
|
+
var Session_js_1 = require("./Session.js");
|
|
7
|
+
Object.defineProperty(exports, "BaseSession", { enumerable: true, get: function () { return Session_js_1.BaseSession; } });
|
|
8
|
+
Object.defineProperty(exports, "getInputPeer", { enumerable: true, get: function () { return Session_js_1.getInputPeer; } });
|
|
9
|
+
var StringSession_js_1 = require("./StringSession.js");
|
|
10
|
+
Object.defineProperty(exports, "StringSession", { enumerable: true, get: function () { return StringSession_js_1.StringSession; } });
|
|
11
|
+
var SecretChat_js_1 = require("./SecretChat.js");
|
|
12
|
+
Object.defineProperty(exports, "SecretChat", { enumerable: true, get: function () { return SecretChat_js_1.SecretChat; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@armatofik/tgsnake-core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pure Telegram MTProto library for Javascript or Typescript. Fork from @tgsnake-core v1.13.15",
|
|
5
|
+
"main": "./lib/src/index.js",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@types/node": "22.14.0",
|
|
8
|
+
"deno2node": "1.15.0",
|
|
9
|
+
"prettier": "3.5.3"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "deno2node tsconfig.json",
|
|
13
|
+
"build:license": "node ./generator/license",
|
|
14
|
+
"build:chglog": "node ./generator/chglog",
|
|
15
|
+
"prepare": "yarn build",
|
|
16
|
+
"prettier": "NODE_OPTIONS=--max_old_space_size=4096 prettier -w ."
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://tgsnake.js.org",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"tgsnake",
|
|
21
|
+
"mtproto",
|
|
22
|
+
"telegram"
|
|
23
|
+
],
|
|
24
|
+
"bugs": "https://github.com/tgsnake/core/issues",
|
|
25
|
+
"repository": "https://github.com/Armatofik/tgsnake-core",
|
|
26
|
+
"author": "butthx",
|
|
27
|
+
"files": [
|
|
28
|
+
"lib/**/**"
|
|
29
|
+
],
|
|
30
|
+
"license": "(GPL-3.0 OR LGPL-3.0)",
|
|
31
|
+
"engineStrict": true,
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=12.0.0",
|
|
34
|
+
"npm": ">=6.0.0",
|
|
35
|
+
"yarn": ">=1.0.0"
|
|
36
|
+
},
|
|
37
|
+
"packageManager": "yarn@4.8.1",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@tgsnake/log": "latest",
|
|
40
|
+
"async-mutex": "0.5.0",
|
|
41
|
+
"big-integer": "1.6.52",
|
|
42
|
+
"socks": "2.8.4"
|
|
43
|
+
},
|
|
44
|
+
"isBeta": false,
|
|
45
|
+
"isPrivate": false
|
|
46
|
+
}
|