Skip to content

Commit

Permalink
Session string analyzer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Mar 20, 2024
1 parent 3ba7aef commit 2a3ded4
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 58 deletions.
4 changes: 4 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as $filter_query_browser from "./routes/filter-query-browser.tsx";
import * as $index from "./routes/index.tsx";
import * as $inline_message_id_unpacker from "./routes/inline-message-id-unpacker.tsx";
import * as $nindex from "./routes/nindex.tsx";
import * as $session_string_analyzer from "./routes/session-string-analyzer.tsx";
import * as $session_string_generator from "./routes/session-string-generator.tsx";
import * as $test from "./routes/test.tsx";
import * as $update_explorer from "./routes/update-explorer.tsx";
Expand All @@ -24,6 +25,7 @@ import * as $FileIdAnalyzer from "./islands/FileIdAnalyzer.tsx";
import * as $FilterQueryBrowser from "./islands/FilterQueryBrowser.tsx";
import * as $InlineMessageIdUnpacker from "./islands/InlineMessageIdUnpacker.tsx";
import * as $Select from "./islands/Select.tsx";
import * as $SessionStringAnalyzer from "./islands/SessionStringAnalyzer.tsx";
import * as $SessionStringGenerator from "./islands/SessionStringGenerator.tsx";
import * as $UpdateExplorer from "./islands/UpdateExplorer.tsx";
import * as $WebhookManager from "./islands/WebhookManager.tsx";
Expand All @@ -40,6 +42,7 @@ const manifest = {
"./routes/index.tsx": $index,
"./routes/inline-message-id-unpacker.tsx": $inline_message_id_unpacker,
"./routes/nindex.tsx": $nindex,
"./routes/session-string-analyzer.tsx": $session_string_analyzer,
"./routes/session-string-generator.tsx": $session_string_generator,
"./routes/test.tsx": $test,
"./routes/update-explorer.tsx": $update_explorer,
Expand All @@ -55,6 +58,7 @@ const manifest = {
"./islands/FilterQueryBrowser.tsx": $FilterQueryBrowser,
"./islands/InlineMessageIdUnpacker.tsx": $InlineMessageIdUnpacker,
"./islands/Select.tsx": $Select,
"./islands/SessionStringAnalyzer.tsx": $SessionStringAnalyzer,
"./islands/SessionStringGenerator.tsx": $SessionStringGenerator,
"./islands/UpdateExplorer.tsx": $UpdateExplorer,
"./islands/WebhookManager.tsx": $WebhookManager,
Expand Down
50 changes: 23 additions & 27 deletions islands/InlineMessageIdUnpacker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,33 @@ export function InlineMessageIdUnpacker() {
return;
}

// try {
const buffer = base64DecodeUrlSafe(id);
console.log(buffer);
try {
const buffer = base64DecodeUrlSafe(id);

// if (buffer.size )
const reader = new TLReader(buffer);

const reader = new TLReader(buffer);
const cid = buffer.byteLength == 20
? inputBotInlineMessageID_CTR
: inputBotInlineMessageID64_CTR;

// AgAAAG0OBgDmwLU4SLlvGwusw7g
const cid = buffer.byteLength == 20
? inputBotInlineMessageID_CTR
: inputBotInlineMessageID64_CTR;

const object = reader.readObject(cid);
if (object instanceof types.InputBotInlineMessageID) {
data.value = {
dc: object.dc_id.toString(),
id: object.id.toString(),
accessHash: object.access_hash.toString(),
};
} else if (object instanceof types.InputBotInlineMessageID64) {
data.value = {
dc: object.dc_id.toString(),
userId: object.owner_id.toString(),
id: object.id.toString(),
accessHash: object.access_hash.toString(),
};
const object = reader.readObject(cid);
if (object instanceof types.InputBotInlineMessageID) {
data.value = {
dc: object.dc_id.toString(),
id: object.id.toString(),
accessHash: object.access_hash.toString(),
};
} else if (object instanceof types.InputBotInlineMessageID64) {
data.value = {
dc: object.dc_id.toString(),
userId: object.owner_id.toString(),
id: object.id.toString(),
accessHash: object.access_hash.toString(),
};
}
} catch {
// yes
}
// } catch {
// yes
// }
});

return (
Expand Down
116 changes: 116 additions & 0 deletions islands/SessionStringAnalyzer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Caption } from "../components/Caption.tsx";
import { Input } from "../components/Input.tsx";
import { ComponentChildren } from "preact";
import { Label } from "../components/Label.tsx";
import { getHashSignal } from "../lib/hash_signal.ts";
import { signal, useSignalEffect } from "@preact/signals";
import { id, types } from "mtkruto/2_tl.ts";
import { encodeHex } from "$std/encoding/hex.ts";
import {
CommonSessionStringFormat,
deserializeGramjs,
deserializePyrogram,
deserializeTelethon,
} from "../lib/session_string.tsx";

const hash = getHashSignal();
const getString = () => decodeURIComponent(hash.value.slice(1));

const data = signal<
| CommonSessionStringFormat & { format: "GramJS" | "Telethon" | "Pyrogram" }
| null
>(
null,
);

export function SessionStringAnalyzer() {
const string = getString();

useSignalEffect(() => {
const string = getString();
if (!string.trim()) {
return;
}

try {
data.value = { ...deserializeTelethon(string), format: "Telethon" };
return;
} catch {
//
}

try {
data.value = { ...deserializePyrogram(string), format: "Pyrogram" };
return;
} catch {
//
}

try {
data.value = { ...deserializeGramjs(string), format: "GramJS" };
return;
} catch {
//
}
});

return (
<div class="w-full gap-4 flex flex-col">
<Label>
<Input
placeholder="Session string"
value={string}
onKeyUp={(e) => location.hash = e.currentTarget.value}
onKeyPress={(e) => location.hash = e.currentTarget.value}
/>
<Caption>Enter a session string to analyze.</Caption>
</Label>
{data.value && (
<div class="gap-5 grid grid-cols-2">
<Kv k="Format" v={data.value.format} />
<Kv k="DC" v={data.value.dc} />
{data.value.format == "Pyrogram" && (
<>
<Kv k="Test" v={data.value.testMode ? "Yes" : "No"} />
<Kv k="API ID" v={data.value.apiId} />
<Kv k="User ID" v={data.value.userId} />
<Kv k="Bot" v={data.value.isBot ? "Yes" : "No"} />
</>
)}
{["Telethon", "GramJS"].includes(data.value.format) && (
<>
<Kv k="IP" v={data.value.ip} />
<Kv k="Port" v={data.value.port} />
</>
)}
<Kv
c="col-span-2"
k="Auth Key"
v={"0x" + encodeHex(data.value.authKey).toUpperCase()}
/>
</div>
)}
</div>
);
}

const inputBotInlineMessageID_CTR = new types.InputBotInlineMessageID({
dc_id: 0,
id: 0n,
access_hash: 0n,
})[id];
const inputBotInlineMessageID64_CTR = new types.InputBotInlineMessageID64({
dc_id: 0,
owner_id: 0n,
id: 0,
access_hash: 0n,
})[id];

function Kv({ k, v, c }: { k: string; v: ComponentChildren; c?: string }) { // TODO: merge with FileIdAnalyzer's
return (
<div class={"flex flex-col gap-0.5 " + (c ?? "")}>
<div class="font-bold text-xs">{k}</div>
<div class="select-text text-ellipsis overflow-hidden">{v}</div>
</div>
);
}
4 changes: 2 additions & 2 deletions islands/SessionStringGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Select } from "../components/Select.tsx";
import { Error, error } from "./Error.tsx";
import { getDcIps } from "mtkruto/transport/2_transport_provider.ts";
import {
serializeGramJS,
serializeGramjs,
serializeMtcute,
serializePyrogram,
serializeTelethon,
Expand Down Expand Up @@ -260,7 +260,7 @@ async function generateSessionString(library: ValidLibrary) {
break;
}
case "gramjs":
sessionString.value = serializeGramJS(dcId, ip, 80, authKey);
sessionString.value = serializeGramjs(dcId, ip, 80, authKey);
break;
case "mtcute": {
const me = await client.getMe();
Expand Down
1 change: 0 additions & 1 deletion islands/WebhookManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ function Manage() {

useEffect(() => {
Promise.resolve().then(async () => {
console.log(token);
const bot_ = new grammy.Bot(token);
const [{ username: name }, { url, allowed_updates: allowedUpdates }] =
await Promise.all([bot_.api.getMe(), bot_.api.getWebhookInfo()]);
Expand Down
Loading

0 comments on commit 2a3ded4

Please sign in to comment.