@mtcute/convert 0.16.7 → 0.16.9
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/{cjs/dcs.d.ts → dcs.d.ts} +1 -1
- package/gramjs/__fixtures__/session.d.ts +1 -0
- package/{esm/gramjs → gramjs}/convert.d.ts +2 -2
- package/{cjs/gramjs → gramjs}/parse.d.ts +1 -1
- package/{cjs/gramjs → gramjs}/serialize.d.ts +1 -1
- package/gramjs/serialize.test.d.ts +1 -0
- package/index.cjs +493 -0
- package/index.d.ts +5 -1
- package/index.js +488 -1
- package/mtkruto/__fixtures__/session.d.ts +1 -0
- package/{cjs/mtkruto → mtkruto}/convert.d.ts +2 -2
- package/mtkruto/convert.test.d.ts +1 -0
- package/{esm/mtkruto → mtkruto}/parse.d.ts +1 -1
- package/mtkruto/parse.test.d.ts +1 -0
- package/{cjs/mtkruto → mtkruto}/serialize.d.ts +1 -1
- package/mtkruto/serialize.test.d.ts +1 -0
- package/package.json +27 -23
- package/pyrogram/__fixtures__/session.d.ts +1 -0
- package/pyrogram/__fixtures__/session_old.d.ts +1 -0
- package/{esm/pyrogram → pyrogram}/convert.d.ts +2 -2
- package/pyrogram/convert.test.d.ts +1 -0
- package/{esm/pyrogram → pyrogram}/parse.d.ts +1 -1
- package/pyrogram/parse.test.d.ts +1 -0
- package/{esm/pyrogram → pyrogram}/serialize.d.ts +1 -1
- package/pyrogram/serialize.test.d.ts +1 -0
- package/telethon/__fixtures__/session.d.ts +1 -0
- package/telethon/__fixtures__/session_v6.d.ts +1 -0
- package/{esm/telethon → telethon}/convert.d.ts +2 -2
- package/telethon/convert.test.d.ts +1 -0
- package/{esm/telethon → telethon}/parse.d.ts +1 -1
- package/telethon/parse.test.d.ts +1 -0
- package/{esm/telethon → telethon}/serialize.d.ts +1 -1
- package/telethon/serialize.test.d.ts +1 -0
- package/types.d.ts +0 -0
- package/cjs/dcs.js +0 -113
- package/cjs/dcs.js.map +0 -1
- package/cjs/gramjs/convert.d.ts +0 -4
- package/cjs/gramjs/convert.js +0 -27
- package/cjs/gramjs/convert.js.map +0 -1
- package/cjs/gramjs/index.js +0 -21
- package/cjs/gramjs/index.js.map +0 -1
- package/cjs/gramjs/parse.js +0 -30
- package/cjs/gramjs/parse.js.map +0 -1
- package/cjs/gramjs/serialize.js +0 -23
- package/cjs/gramjs/serialize.js.map +0 -1
- package/cjs/gramjs/types.js +0 -3
- package/cjs/gramjs/types.js.map +0 -1
- package/cjs/index.js +0 -28
- package/cjs/index.js.map +0 -1
- package/cjs/mtkruto/convert.js +0 -30
- package/cjs/mtkruto/convert.js.map +0 -1
- package/cjs/mtkruto/index.js +0 -21
- package/cjs/mtkruto/index.js.map +0 -1
- package/cjs/mtkruto/parse.d.ts +0 -2
- package/cjs/mtkruto/parse.js +0 -27
- package/cjs/mtkruto/parse.js.map +0 -1
- package/cjs/mtkruto/serialize.js +0 -14
- package/cjs/mtkruto/serialize.js.map +0 -1
- package/cjs/mtkruto/types.js +0 -3
- package/cjs/mtkruto/types.js.map +0 -1
- package/cjs/package.json +0 -3
- package/cjs/pyrogram/convert.d.ts +0 -6
- package/cjs/pyrogram/convert.js +0 -39
- package/cjs/pyrogram/convert.js.map +0 -1
- package/cjs/pyrogram/index.js +0 -21
- package/cjs/pyrogram/index.js.map +0 -1
- package/cjs/pyrogram/parse.d.ts +0 -2
- package/cjs/pyrogram/parse.js +0 -55
- package/cjs/pyrogram/parse.js.map +0 -1
- package/cjs/pyrogram/serialize.d.ts +0 -2
- package/cjs/pyrogram/serialize.js +0 -39
- package/cjs/pyrogram/serialize.js.map +0 -1
- package/cjs/pyrogram/types.js +0 -3
- package/cjs/pyrogram/types.js.map +0 -1
- package/cjs/telethon/convert.d.ts +0 -4
- package/cjs/telethon/convert.js +0 -43
- package/cjs/telethon/convert.js.map +0 -1
- package/cjs/telethon/index.js +0 -21
- package/cjs/telethon/index.js.map +0 -1
- package/cjs/telethon/parse.d.ts +0 -2
- package/cjs/telethon/parse.js +0 -32
- package/cjs/telethon/parse.js.map +0 -1
- package/cjs/telethon/serialize.d.ts +0 -2
- package/cjs/telethon/serialize.js +0 -33
- package/cjs/telethon/serialize.js.map +0 -1
- package/cjs/telethon/types.js +0 -3
- package/cjs/telethon/types.js.map +0 -1
- package/cjs/types.js +0 -3
- package/cjs/types.js.map +0 -1
- package/cjs/utils/ip.js +0 -44
- package/cjs/utils/ip.js.map +0 -1
- package/cjs/utils/rle.js +0 -54
- package/cjs/utils/rle.js.map +0 -1
- package/esm/dcs.d.ts +0 -4
- package/esm/dcs.js +0 -109
- package/esm/dcs.js.map +0 -1
- package/esm/gramjs/convert.js +0 -23
- package/esm/gramjs/convert.js.map +0 -1
- package/esm/gramjs/index.d.ts +0 -4
- package/esm/gramjs/index.js +0 -5
- package/esm/gramjs/index.js.map +0 -1
- package/esm/gramjs/parse.d.ts +0 -2
- package/esm/gramjs/parse.js +0 -27
- package/esm/gramjs/parse.js.map +0 -1
- package/esm/gramjs/serialize.d.ts +0 -2
- package/esm/gramjs/serialize.js +0 -20
- package/esm/gramjs/serialize.js.map +0 -1
- package/esm/gramjs/types.d.ts +0 -7
- package/esm/gramjs/types.js +0 -2
- package/esm/gramjs/types.js.map +0 -1
- package/esm/index.d.ts +0 -5
- package/esm/index.js +0 -6
- package/esm/index.js.map +0 -1
- package/esm/mtkruto/convert.d.ts +0 -4
- package/esm/mtkruto/convert.js +0 -26
- package/esm/mtkruto/convert.js.map +0 -1
- package/esm/mtkruto/index.d.ts +0 -4
- package/esm/mtkruto/index.js +0 -5
- package/esm/mtkruto/index.js.map +0 -1
- package/esm/mtkruto/parse.js +0 -24
- package/esm/mtkruto/parse.js.map +0 -1
- package/esm/mtkruto/serialize.d.ts +0 -2
- package/esm/mtkruto/serialize.js +0 -11
- package/esm/mtkruto/serialize.js.map +0 -1
- package/esm/mtkruto/types.d.ts +0 -5
- package/esm/mtkruto/types.js +0 -2
- package/esm/mtkruto/types.js.map +0 -1
- package/esm/pyrogram/convert.js +0 -35
- package/esm/pyrogram/convert.js.map +0 -1
- package/esm/pyrogram/index.d.ts +0 -4
- package/esm/pyrogram/index.js +0 -5
- package/esm/pyrogram/index.js.map +0 -1
- package/esm/pyrogram/parse.js +0 -52
- package/esm/pyrogram/parse.js.map +0 -1
- package/esm/pyrogram/serialize.js +0 -36
- package/esm/pyrogram/serialize.js.map +0 -1
- package/esm/pyrogram/types.d.ts +0 -8
- package/esm/pyrogram/types.js +0 -2
- package/esm/pyrogram/types.js.map +0 -1
- package/esm/telethon/convert.js +0 -39
- package/esm/telethon/convert.js.map +0 -1
- package/esm/telethon/index.d.ts +0 -4
- package/esm/telethon/index.js +0 -5
- package/esm/telethon/index.js.map +0 -1
- package/esm/telethon/parse.js +0 -29
- package/esm/telethon/parse.js.map +0 -1
- package/esm/telethon/serialize.js +0 -30
- package/esm/telethon/serialize.js.map +0 -1
- package/esm/telethon/types.d.ts +0 -7
- package/esm/telethon/types.js +0 -2
- package/esm/telethon/types.js.map +0 -1
- package/esm/types.js +0 -2
- package/esm/types.js.map +0 -1
- package/esm/utils/ip.d.ts +0 -3
- package/esm/utils/ip.js +0 -39
- package/esm/utils/ip.js.map +0 -1
- package/esm/utils/rle.d.ts +0 -3
- package/esm/utils/rle.js +0 -49
- package/esm/utils/rle.js.map +0 -1
- /package/{cjs/types.d.ts → gramjs/convert.test.d.ts} +0 -0
- /package/{cjs/gramjs → gramjs}/index.d.ts +0 -0
- /package/{esm/types.d.ts → gramjs/parse.test.d.ts} +0 -0
- /package/{cjs/gramjs → gramjs}/types.d.ts +0 -0
- /package/{cjs/index.d.ts → index.d.cts} +0 -0
- /package/{cjs/mtkruto → mtkruto}/index.d.ts +0 -0
- /package/{cjs/mtkruto → mtkruto}/types.d.ts +0 -0
- /package/{cjs/pyrogram → pyrogram}/index.d.ts +0 -0
- /package/{cjs/pyrogram → pyrogram}/types.d.ts +0 -0
- /package/{cjs/telethon → telethon}/index.d.ts +0 -0
- /package/{cjs/telethon → telethon}/types.d.ts +0 -0
- /package/{cjs/utils → utils}/ip.d.ts +0 -0
- /package/{cjs/utils → utils}/rle.d.ts +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { DcOptions } from '@mtcute/core/utils.js';
|
|
2
2
|
export declare const DC_MAPPING_PROD: Record<number, DcOptions>;
|
|
3
3
|
export declare const DC_MAPPING_TEST: Record<number, DcOptions>;
|
|
4
4
|
export declare function isTestDc(ip: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const GRAMJS_SESSION = "1AgAOMTQ5LjE1NC4xNjcuNDABu60obcEYS8Yb/I7YlCwaLvW84dXCX2oGnBYG+zuMciJhHP99c8ZJvwxJgH8yU1QrqI+Gh0kK0JAuQucIpDfq/jJVLZ1ZRimq5yy1XbeEs65gtZA1+SUwZRXahh+NzGbPmOVUMCnCtRONo9GNvcx/QxSXRrh7T/K0YYN1iHsK1vJDk8/SUnthvTNmRycC+JLn4fMtctqP4Le2WPOH/deYbUF0BlwmR77M7fv1GZSInqCgWReaIl5nvn0IqA4mOCTkdOgcvwOiB2UmXwiyInxRuLdBIyLbBUDCuTlmL1m3FJqbuEpZEUJnoJf2YDFZ1wR6TfL0MUS1VwnjOcy3WIIFwwg=";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { StringSessionData } from '@mtcute/core/utils.js';
|
|
2
|
+
import { TelethonSession } from '../telethon/types.js';
|
|
3
3
|
export declare function convertFromGramjsSession(session: TelethonSession | string): StringSessionData;
|
|
4
4
|
export declare function convertToGramjsSession(session: StringSessionData | string): string;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { TelethonSession } from '../telethon/types.js';
|
|
2
2
|
export declare function parseGramjsSession(session: string): TelethonSession;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { TelethonSession } from '../telethon/types.js';
|
|
2
2
|
export declare function serializeGramjsSession(session: TelethonSession): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.cjs
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
|
+
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
+
console.warn("[@mtcute/convert] CommonJS support is deprecated and will be removed soon. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[@mtcute/convert] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
|
+
}
|
|
6
|
+
"use strict";
|
|
7
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
+
const utils_js = require("@mtcute/core/utils.js");
|
|
9
|
+
const core = require("@mtcute/core");
|
|
10
|
+
const platform_js = require("@mtcute/core/platform.js");
|
|
11
|
+
const DC_MAPPING_PROD = {
|
|
12
|
+
1: {
|
|
13
|
+
main: {
|
|
14
|
+
id: 1,
|
|
15
|
+
ipAddress: "149.154.175.56",
|
|
16
|
+
port: 443
|
|
17
|
+
},
|
|
18
|
+
media: {
|
|
19
|
+
id: 1,
|
|
20
|
+
ipAddress: "149.154.175.211",
|
|
21
|
+
port: 443
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
2: {
|
|
25
|
+
main: {
|
|
26
|
+
id: 2,
|
|
27
|
+
ipAddress: "149.154.167.41",
|
|
28
|
+
port: 443
|
|
29
|
+
},
|
|
30
|
+
media: {
|
|
31
|
+
id: 2,
|
|
32
|
+
ipAddress: "149.154.167.35",
|
|
33
|
+
port: 443
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
3: {
|
|
37
|
+
main: {
|
|
38
|
+
id: 3,
|
|
39
|
+
ipAddress: "149.154.175.100",
|
|
40
|
+
port: 443
|
|
41
|
+
},
|
|
42
|
+
media: {
|
|
43
|
+
id: 3,
|
|
44
|
+
ipAddress: "149.154.175.100",
|
|
45
|
+
port: 443
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
4: {
|
|
49
|
+
main: {
|
|
50
|
+
id: 4,
|
|
51
|
+
ipAddress: "149.154.167.91",
|
|
52
|
+
port: 443
|
|
53
|
+
},
|
|
54
|
+
media: {
|
|
55
|
+
id: 4,
|
|
56
|
+
ipAddress: "149.154.167.255",
|
|
57
|
+
port: 443
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
5: {
|
|
61
|
+
main: {
|
|
62
|
+
id: 5,
|
|
63
|
+
ipAddress: "91.108.56.179",
|
|
64
|
+
port: 443
|
|
65
|
+
},
|
|
66
|
+
media: {
|
|
67
|
+
id: 5,
|
|
68
|
+
ipAddress: "149.154.171.255",
|
|
69
|
+
port: 443
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const DC_MAPPING_TEST = {
|
|
74
|
+
1: {
|
|
75
|
+
main: {
|
|
76
|
+
id: 1,
|
|
77
|
+
ipAddress: "149.154.175.10",
|
|
78
|
+
port: 80
|
|
79
|
+
},
|
|
80
|
+
media: {
|
|
81
|
+
id: 1,
|
|
82
|
+
ipAddress: "149.154.175.10",
|
|
83
|
+
port: 80
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
2: {
|
|
87
|
+
main: {
|
|
88
|
+
id: 2,
|
|
89
|
+
ipAddress: "149.154.167.40",
|
|
90
|
+
port: 443
|
|
91
|
+
},
|
|
92
|
+
media: {
|
|
93
|
+
id: 2,
|
|
94
|
+
ipAddress: "149.154.167.40",
|
|
95
|
+
port: 443
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
3: {
|
|
99
|
+
main: {
|
|
100
|
+
id: 3,
|
|
101
|
+
ipAddress: "149.154.175.117",
|
|
102
|
+
port: 443
|
|
103
|
+
},
|
|
104
|
+
media: {
|
|
105
|
+
id: 3,
|
|
106
|
+
ipAddress: "149.154.175.117",
|
|
107
|
+
port: 443
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function isTestDc(ip) {
|
|
112
|
+
return Object.values(DC_MAPPING_TEST).some((dc) => dc.main.ipAddress === ip || dc.media.ipAddress === ip);
|
|
113
|
+
}
|
|
114
|
+
function parseIpFromBytes(data) {
|
|
115
|
+
if (data.length === 4) {
|
|
116
|
+
return `${data[0]}.${data[1]}.${data[2]}.${data[3]}`;
|
|
117
|
+
}
|
|
118
|
+
if (data.length === 16) {
|
|
119
|
+
let res = "";
|
|
120
|
+
for (let i = 0; i < 16; i += 2) {
|
|
121
|
+
res += data[i].toString(16).padStart(2, "0");
|
|
122
|
+
res += data[i + 1].toString(16).padStart(2, "0");
|
|
123
|
+
if (i < 14) res += ":";
|
|
124
|
+
}
|
|
125
|
+
return res;
|
|
126
|
+
}
|
|
127
|
+
throw new core.MtArgumentError("Invalid IP address length");
|
|
128
|
+
}
|
|
129
|
+
function serializeIpv4ToBytes(ip, buf) {
|
|
130
|
+
const parts = ip.split(".");
|
|
131
|
+
if (parts.length !== 4) {
|
|
132
|
+
throw new core.MtArgumentError("Invalid IPv4 address");
|
|
133
|
+
}
|
|
134
|
+
buf[0] = Number(parts[0]);
|
|
135
|
+
buf[1] = Number(parts[1]);
|
|
136
|
+
buf[2] = Number(parts[2]);
|
|
137
|
+
buf[3] = Number(parts[3]);
|
|
138
|
+
}
|
|
139
|
+
function serializeIpv6ToBytes(ip, buf) {
|
|
140
|
+
const parts = ip.split(":");
|
|
141
|
+
if (parts.length !== 8) {
|
|
142
|
+
throw new core.MtArgumentError("Invalid IPv6 address");
|
|
143
|
+
}
|
|
144
|
+
for (let i = 0; i < 8; i++) {
|
|
145
|
+
const val = Number.parseInt(parts[i], 16);
|
|
146
|
+
buf[i * 2] = val >> 8;
|
|
147
|
+
buf[i * 2 + 1] = val & 255;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function parseTelethonSession(session) {
|
|
151
|
+
if (session[0] !== "1") {
|
|
152
|
+
throw new core.MtArgumentError(`Invalid session string (version = ${session[0]})`);
|
|
153
|
+
}
|
|
154
|
+
session = session.slice(1);
|
|
155
|
+
const data = platform_js.getPlatform().base64Decode(session, true);
|
|
156
|
+
const dv = utils_js.dataViewFromBuffer(data);
|
|
157
|
+
const dcId = dv.getUint8(0);
|
|
158
|
+
const ipSize = session.length === 352 ? 4 : 16;
|
|
159
|
+
let pos = 1 + ipSize;
|
|
160
|
+
const ipBytes = data.subarray(1, pos);
|
|
161
|
+
const port = dv.getUint16(pos);
|
|
162
|
+
pos += 2;
|
|
163
|
+
const authKey = data.subarray(pos, pos + 256);
|
|
164
|
+
const ip = parseIpFromBytes(ipBytes);
|
|
165
|
+
return {
|
|
166
|
+
dcId,
|
|
167
|
+
ipAddress: ip,
|
|
168
|
+
ipv6: ipSize === 16,
|
|
169
|
+
port,
|
|
170
|
+
authKey
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function serializeTelethonSession(session) {
|
|
174
|
+
if (session.authKey.length !== 256) {
|
|
175
|
+
throw new core.MtArgumentError("authKey must be 256 bytes long");
|
|
176
|
+
}
|
|
177
|
+
const ipSize = session.ipv6 ? 16 : 4;
|
|
178
|
+
const u8 = new Uint8Array(259 + ipSize);
|
|
179
|
+
const dv = utils_js.dataViewFromBuffer(u8);
|
|
180
|
+
dv.setUint8(0, session.dcId);
|
|
181
|
+
let pos;
|
|
182
|
+
if (session.ipv6) {
|
|
183
|
+
serializeIpv6ToBytes(session.ipAddress, u8.subarray(1, 17));
|
|
184
|
+
pos = 17;
|
|
185
|
+
} else {
|
|
186
|
+
serializeIpv4ToBytes(session.ipAddress, u8.subarray(1, 5));
|
|
187
|
+
pos = 5;
|
|
188
|
+
}
|
|
189
|
+
dv.setUint16(pos, session.port);
|
|
190
|
+
pos += 2;
|
|
191
|
+
u8.set(session.authKey, pos);
|
|
192
|
+
let b64 = platform_js.getPlatform().base64Encode(u8, true);
|
|
193
|
+
while (b64.length % 4 !== 0) b64 += "=";
|
|
194
|
+
return `1${b64}`;
|
|
195
|
+
}
|
|
196
|
+
function convertFromTelethonSession(session) {
|
|
197
|
+
if (typeof session === "string") {
|
|
198
|
+
session = parseTelethonSession(session);
|
|
199
|
+
}
|
|
200
|
+
const dc = {
|
|
201
|
+
id: session.dcId,
|
|
202
|
+
ipAddress: session.ipAddress,
|
|
203
|
+
port: session.port,
|
|
204
|
+
ipv6: session.ipv6
|
|
205
|
+
};
|
|
206
|
+
return {
|
|
207
|
+
version: 3,
|
|
208
|
+
// we don't exactly have that information. try to deduce it from DC_MAPPING_TEST
|
|
209
|
+
// todo: we should maybe check this at connect?
|
|
210
|
+
testMode: isTestDc(session.ipAddress),
|
|
211
|
+
primaryDcs: {
|
|
212
|
+
main: dc,
|
|
213
|
+
media: dc
|
|
214
|
+
},
|
|
215
|
+
authKey: session.authKey
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function convertToTelethonSession(session) {
|
|
219
|
+
if (typeof session === "string") {
|
|
220
|
+
session = utils_js.readStringSession(session);
|
|
221
|
+
}
|
|
222
|
+
return serializeTelethonSession({
|
|
223
|
+
dcId: session.primaryDcs.main.id,
|
|
224
|
+
ipAddress: session.primaryDcs.main.ipAddress,
|
|
225
|
+
port: session.primaryDcs.main.port,
|
|
226
|
+
ipv6: session.primaryDcs.main.ipv6 ?? false,
|
|
227
|
+
authKey: session.authKey
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
function parseGramjsSession(session) {
|
|
231
|
+
if (session[0] !== "1") {
|
|
232
|
+
throw new core.MtArgumentError(`Invalid session string (version = ${session[0]})`);
|
|
233
|
+
}
|
|
234
|
+
session = session.slice(1);
|
|
235
|
+
const data = platform_js.getPlatform().base64Decode(session);
|
|
236
|
+
const dv = utils_js.dataViewFromBuffer(data);
|
|
237
|
+
const dcId = dv.getUint8(0);
|
|
238
|
+
const ipSize = dv.getUint16(1);
|
|
239
|
+
let pos = 3 + ipSize;
|
|
240
|
+
const ip = platform_js.getPlatform().utf8Decode(data.subarray(3, pos));
|
|
241
|
+
const port = dv.getUint16(pos);
|
|
242
|
+
pos += 2;
|
|
243
|
+
const authKey = data.subarray(pos, pos + 256);
|
|
244
|
+
return {
|
|
245
|
+
dcId,
|
|
246
|
+
ipAddress: ip,
|
|
247
|
+
ipv6: ip.includes(":"),
|
|
248
|
+
// dumb check but gramjs does this
|
|
249
|
+
port,
|
|
250
|
+
authKey
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function serializeGramjsSession(session) {
|
|
254
|
+
if (session.authKey.length !== 256) {
|
|
255
|
+
throw new core.MtArgumentError("authKey must be 256 bytes long");
|
|
256
|
+
}
|
|
257
|
+
const ipEncoded = platform_js.getPlatform().utf8Encode(session.ipAddress);
|
|
258
|
+
const u8 = new Uint8Array(261 + ipEncoded.length);
|
|
259
|
+
const dv = utils_js.dataViewFromBuffer(u8);
|
|
260
|
+
dv.setUint8(0, session.dcId);
|
|
261
|
+
dv.setUint16(1, ipEncoded.length);
|
|
262
|
+
u8.set(ipEncoded, 3);
|
|
263
|
+
let pos = 3 + ipEncoded.length;
|
|
264
|
+
dv.setUint16(pos, session.port);
|
|
265
|
+
pos += 2;
|
|
266
|
+
u8.set(session.authKey, pos);
|
|
267
|
+
return `1${platform_js.getPlatform().base64Encode(u8)}`;
|
|
268
|
+
}
|
|
269
|
+
function convertFromGramjsSession(session) {
|
|
270
|
+
if (typeof session === "string") {
|
|
271
|
+
session = parseGramjsSession(session);
|
|
272
|
+
}
|
|
273
|
+
return convertFromTelethonSession(session);
|
|
274
|
+
}
|
|
275
|
+
function convertToGramjsSession(session) {
|
|
276
|
+
if (typeof session === "string") {
|
|
277
|
+
session = utils_js.readStringSession(session);
|
|
278
|
+
}
|
|
279
|
+
return serializeGramjsSession({
|
|
280
|
+
dcId: session.primaryDcs.main.id,
|
|
281
|
+
ipAddress: session.primaryDcs.main.ipAddress,
|
|
282
|
+
port: session.primaryDcs.main.port,
|
|
283
|
+
ipv6: session.primaryDcs.main.ipv6 ?? false,
|
|
284
|
+
authKey: session.authKey
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
function telegramRleEncode(buf) {
|
|
288
|
+
const len = buf.length;
|
|
289
|
+
const ret = [];
|
|
290
|
+
let count = 0;
|
|
291
|
+
for (let i = 0; i < len; i++) {
|
|
292
|
+
const cur = buf[i];
|
|
293
|
+
if (cur === 0) {
|
|
294
|
+
count += 1;
|
|
295
|
+
} else {
|
|
296
|
+
if (count > 0) {
|
|
297
|
+
ret.push(0, count);
|
|
298
|
+
count = 0;
|
|
299
|
+
}
|
|
300
|
+
ret.push(cur);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (count > 0) {
|
|
304
|
+
ret.push(0, count);
|
|
305
|
+
}
|
|
306
|
+
return new Uint8Array(ret);
|
|
307
|
+
}
|
|
308
|
+
function telegramRleDecode(buf) {
|
|
309
|
+
const len = buf.length;
|
|
310
|
+
const ret = [];
|
|
311
|
+
let prev = -1;
|
|
312
|
+
for (let i = 0; i < len; i++) {
|
|
313
|
+
const cur = buf[i];
|
|
314
|
+
if (prev === 0) {
|
|
315
|
+
for (let j = 0; j < cur; j++) {
|
|
316
|
+
ret.push(prev);
|
|
317
|
+
}
|
|
318
|
+
prev = -1;
|
|
319
|
+
} else {
|
|
320
|
+
if (prev !== -1) ret.push(prev);
|
|
321
|
+
prev = cur;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (prev !== -1) ret.push(prev);
|
|
325
|
+
return new Uint8Array(ret);
|
|
326
|
+
}
|
|
327
|
+
function parseMtkrutoSession(session) {
|
|
328
|
+
const data = telegramRleDecode(platform_js.getPlatform().base64Decode(session, true));
|
|
329
|
+
const reader = utils_js.TlBinaryReader.manual(data);
|
|
330
|
+
let dcIdStr = reader.string();
|
|
331
|
+
const authKey = reader.bytes();
|
|
332
|
+
const isTest = dcIdStr.endsWith("-test");
|
|
333
|
+
if (isTest) {
|
|
334
|
+
dcIdStr = dcIdStr.slice(0, -5);
|
|
335
|
+
}
|
|
336
|
+
const dcId = Number(dcIdStr);
|
|
337
|
+
if (Number.isNaN(dcId)) {
|
|
338
|
+
throw new core.MtArgumentError(`Invalid DC ID: ${dcIdStr}`);
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
dcId,
|
|
342
|
+
isTest,
|
|
343
|
+
authKey
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function serializeMtkrutoSession(session) {
|
|
347
|
+
const dcIdStr = `${session.dcId}${session.isTest ? "-test" : ""}`;
|
|
348
|
+
const writer = utils_js.TlBinaryWriter.manual(session.authKey.length + dcIdStr.length + 8);
|
|
349
|
+
writer.string(dcIdStr);
|
|
350
|
+
writer.bytes(session.authKey);
|
|
351
|
+
return platform_js.getPlatform().base64Encode(telegramRleEncode(writer.result()), true);
|
|
352
|
+
}
|
|
353
|
+
function convertFromMtkrutoSession(session) {
|
|
354
|
+
if (typeof session === "string") {
|
|
355
|
+
session = parseMtkrutoSession(session);
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
version: 3,
|
|
359
|
+
testMode: session.isTest,
|
|
360
|
+
primaryDcs: (session.isTest ? DC_MAPPING_TEST : DC_MAPPING_PROD)[session.dcId],
|
|
361
|
+
authKey: session.authKey
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function convertToMtkrutoSession(session) {
|
|
365
|
+
if (typeof session === "string") {
|
|
366
|
+
session = utils_js.readStringSession(session);
|
|
367
|
+
}
|
|
368
|
+
return serializeMtkrutoSession({
|
|
369
|
+
dcId: session.primaryDcs.main.id,
|
|
370
|
+
isTest: session.testMode,
|
|
371
|
+
authKey: session.authKey
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
const SESSION_STRING_SIZE$1 = 351;
|
|
375
|
+
const SESSION_STRING_SIZE_64 = 356;
|
|
376
|
+
function parsePyrogramSession(session) {
|
|
377
|
+
const data = platform_js.getPlatform().base64Decode(session, true);
|
|
378
|
+
const dv = utils_js.dataViewFromBuffer(data);
|
|
379
|
+
if (session.length === SESSION_STRING_SIZE$1 || session.length === SESSION_STRING_SIZE_64) {
|
|
380
|
+
const dcId2 = dv.getUint8(0);
|
|
381
|
+
const isTest2 = dv.getUint8(1) !== 0;
|
|
382
|
+
const authKey2 = data.subarray(2, 258);
|
|
383
|
+
let userId2;
|
|
384
|
+
if (data.length === SESSION_STRING_SIZE$1) {
|
|
385
|
+
userId2 = dv.getUint32(258);
|
|
386
|
+
} else {
|
|
387
|
+
const high = dv.getUint32(258);
|
|
388
|
+
const low = dv.getUint32(262);
|
|
389
|
+
userId2 = core.Long.fromBits(low, high).toNumber();
|
|
390
|
+
}
|
|
391
|
+
const isBot2 = dv.getUint8(data.length - 1) !== 0;
|
|
392
|
+
return {
|
|
393
|
+
dcId: dcId2,
|
|
394
|
+
isTest: isTest2,
|
|
395
|
+
authKey: authKey2,
|
|
396
|
+
userId: userId2,
|
|
397
|
+
isBot: isBot2
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
const dcId = dv.getUint8(0);
|
|
401
|
+
const apiId = dv.getUint32(1);
|
|
402
|
+
const isTest = dv.getUint8(5) !== 0;
|
|
403
|
+
const authKey = data.subarray(6, 262);
|
|
404
|
+
const userId = utils_js.longFromBuffer(data.subarray(262, 270), true, false).toNumber();
|
|
405
|
+
const isBot = dv.getUint8(270) !== 0;
|
|
406
|
+
return {
|
|
407
|
+
dcId,
|
|
408
|
+
apiId,
|
|
409
|
+
isTest,
|
|
410
|
+
authKey,
|
|
411
|
+
userId,
|
|
412
|
+
isBot
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
const SESSION_STRING_SIZE_OLD = 267;
|
|
416
|
+
const SESSION_STRING_SIZE = 271;
|
|
417
|
+
function serializePyrogramSession(session) {
|
|
418
|
+
if (session.authKey.length !== 256) {
|
|
419
|
+
throw new core.MtArgumentError("authKey must be 256 bytes long");
|
|
420
|
+
}
|
|
421
|
+
const userIdLong = core.Long.fromNumber(session.userId, true);
|
|
422
|
+
let u8;
|
|
423
|
+
if (session.apiId === void 0) {
|
|
424
|
+
u8 = new Uint8Array(SESSION_STRING_SIZE_OLD);
|
|
425
|
+
const dv = utils_js.dataViewFromBuffer(u8);
|
|
426
|
+
dv.setUint8(0, session.dcId);
|
|
427
|
+
dv.setUint8(1, session.isTest ? 1 : 0);
|
|
428
|
+
u8.set(session.authKey, 2);
|
|
429
|
+
dv.setUint32(258, userIdLong.high);
|
|
430
|
+
dv.setUint32(262, userIdLong.low);
|
|
431
|
+
dv.setUint8(266, session.isBot ? 1 : 0);
|
|
432
|
+
} else {
|
|
433
|
+
u8 = new Uint8Array(SESSION_STRING_SIZE);
|
|
434
|
+
const dv = utils_js.dataViewFromBuffer(u8);
|
|
435
|
+
dv.setUint8(0, session.dcId);
|
|
436
|
+
dv.setUint32(1, session.apiId);
|
|
437
|
+
dv.setUint8(5, session.isTest ? 1 : 0);
|
|
438
|
+
u8.set(session.authKey, 6);
|
|
439
|
+
dv.setUint32(262, userIdLong.high);
|
|
440
|
+
dv.setUint32(266, userIdLong.low);
|
|
441
|
+
dv.setUint8(270, session.isBot ? 1 : 0);
|
|
442
|
+
}
|
|
443
|
+
return platform_js.getPlatform().base64Encode(u8, true);
|
|
444
|
+
}
|
|
445
|
+
function convertFromPyrogramSession(session) {
|
|
446
|
+
if (typeof session === "string") {
|
|
447
|
+
session = parsePyrogramSession(session);
|
|
448
|
+
}
|
|
449
|
+
return {
|
|
450
|
+
version: 3,
|
|
451
|
+
testMode: session.isTest,
|
|
452
|
+
primaryDcs: (session.isTest ? DC_MAPPING_TEST : DC_MAPPING_PROD)[session.dcId],
|
|
453
|
+
authKey: session.authKey,
|
|
454
|
+
self: {
|
|
455
|
+
userId: session.userId,
|
|
456
|
+
isBot: session.isBot,
|
|
457
|
+
isPremium: false,
|
|
458
|
+
usernames: []
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function convertToPyrogramSession(session, params) {
|
|
463
|
+
if (typeof session === "string") {
|
|
464
|
+
session = utils_js.readStringSession(session);
|
|
465
|
+
}
|
|
466
|
+
return serializePyrogramSession({
|
|
467
|
+
apiId: params?.apiId,
|
|
468
|
+
isBot: session.self?.isBot ?? false,
|
|
469
|
+
isTest: session.testMode,
|
|
470
|
+
userId: session.self?.userId ?? 0,
|
|
471
|
+
dcId: session.primaryDcs.main.id,
|
|
472
|
+
authKey: session.authKey
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
exports.DC_MAPPING_PROD = DC_MAPPING_PROD;
|
|
476
|
+
exports.DC_MAPPING_TEST = DC_MAPPING_TEST;
|
|
477
|
+
exports.convertFromGramjsSession = convertFromGramjsSession;
|
|
478
|
+
exports.convertFromMtkrutoSession = convertFromMtkrutoSession;
|
|
479
|
+
exports.convertFromPyrogramSession = convertFromPyrogramSession;
|
|
480
|
+
exports.convertFromTelethonSession = convertFromTelethonSession;
|
|
481
|
+
exports.convertToGramjsSession = convertToGramjsSession;
|
|
482
|
+
exports.convertToMtkrutoSession = convertToMtkrutoSession;
|
|
483
|
+
exports.convertToPyrogramSession = convertToPyrogramSession;
|
|
484
|
+
exports.convertToTelethonSession = convertToTelethonSession;
|
|
485
|
+
exports.isTestDc = isTestDc;
|
|
486
|
+
exports.parseGramjsSession = parseGramjsSession;
|
|
487
|
+
exports.parseMtkrutoSession = parseMtkrutoSession;
|
|
488
|
+
exports.parsePyrogramSession = parsePyrogramSession;
|
|
489
|
+
exports.parseTelethonSession = parseTelethonSession;
|
|
490
|
+
exports.serializeGramjsSession = serializeGramjsSession;
|
|
491
|
+
exports.serializeMtkrutoSession = serializeMtkrutoSession;
|
|
492
|
+
exports.serializePyrogramSession = serializePyrogramSession;
|
|
493
|
+
exports.serializeTelethonSession = serializeTelethonSession;
|
package/index.d.ts
CHANGED