Skip to content

Commit

Permalink
Support generating mtcute sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Mar 19, 2024
1 parent 478da8b commit 87a3286
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
21 changes: 18 additions & 3 deletions islands/SessionStringGenerator.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, DEVICE_MODEL, StorageMemory } from "mtkruto/mod.ts";
import { Client, StorageMemory } from "mtkruto/mod.ts";
import { signal } from "@preact/signals";
import { Button } from "../components/Button.tsx";
import { Caption } from "../components/Caption.tsx";
Expand All @@ -9,6 +9,7 @@ import { Error, error } from "./Error.tsx";
import { getDcIps } from "mtkruto/transport/2_transport_provider.ts";
import {
serializeGramJS,
serializeMtcute,
serializePyrogram,
serializeTelethon,
} from "../lib/session_string.tsx";
Expand All @@ -30,7 +31,7 @@ const library = signal<
| "GramJS"
| "mtcute"
| "MTKruto"
>("Telethon");
>("Telethon"); // TODO: url-based

export function SessionStringGenerator() {
if (loading.value) {
Expand Down Expand Up @@ -193,6 +194,7 @@ async function generateSessionString() {

const ip = getDcIps(dc, "ipv4")[0]; // TODO
const dcId = Number(dc.split("-")[0]);
const testMode = environment.value == "Test" ? true : false;

switch (library.value) {
case "Telethon":
Expand All @@ -203,7 +205,7 @@ async function generateSessionString() {
sessionString.value = serializePyrogram(
dcId,
apiId_,
environment.value == "Test" ? true : false,
testMode,
authKey,
me.id,
me.isBot,
Expand All @@ -213,6 +215,19 @@ async function generateSessionString() {
case "GramJS":
sessionString.value = serializeGramJS(dcId, ip, 80, authKey);
break;
case "mtcute": {
const me = await client.getMe();
sessionString.value = serializeMtcute(
testMode,
dcId,
ip,
80,
me.id,
me.isBot,
authKey,
); // TODO: tests
return;
}
case "MTKruto":
sessionString.value = await client.exportAuthString();
break;
Expand Down
51 changes: 51 additions & 0 deletions lib/session_string.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,54 @@ export function serializePyrogram(
writer.write(new Uint8Array([isBot ? 1 : 0]));
return base64EncodeUrlSafe(writer.buffer).replace(/(=+)$/, "");
}

/**
* Serialize an mtcute session string.
*
* +--------+------------+------------+
* | Size | Type | Content |
* +--------+------------+------------+
* | 1 | 0x03 const | version |
* | 4 | int32 LE | flags |
* | 1 | 0x01 const | dc_version |
* | 1 | uint8 | dc_id |
* | 1 | uint8 | dc_flags |
* | varies | TL string | dc_ip |
* | 4 | int32 LE | dc_port |
* | 8 | int64 LE | user_id |
* | 1 | boolean | is_bot |
* | 256 | bytes | auth_key |
* +--------+------------+------------+
*
* Encoded in URL-safe Base64, with the "=" at its end trimmed.
*/
export function serializeMtcute(
testMode: boolean,
dcId: number,
ip: string,
port: number,
userId: number,
isBot: boolean,
authKey: Uint8Array,
) {
const writer = new TLRawWriter();
writer.write(new Uint8Array([0x03])); // version
writer.writeInt32(
MTCUTE_HAS_SELF_FLAG | (testMode ? MTCUTE_TEST_MODE_FLAG : 0),
); // flags

writer.write(new Uint8Array([0x01])); // dc_version
writer.write(new Uint8Array([dcId])); // dc_id
writer.writeInt32(ip.includes(":") ? MTCUTE_IPV6_FLAG : 0); // dc_flags
writer.writeString(ip); // dc_ip
writer.writeInt32(port); // dc_port

writer.writeInt64(BigInt(userId)); // user_id
writer.write(new Uint8Array([isBot ? 1 : 0])); // is_bot
writer.write(authKey); // authKey

return base64EncodeUrlSafe(writer.buffer);
}
const MTCUTE_HAS_SELF_FLAG = 1;
const MTCUTE_TEST_MODE_FLAG = 2; // TODO: flag 4?
const MTCUTE_IPV6_FLAG = 1;

0 comments on commit 87a3286

Please sign in to comment.