Skip to content

Commit

Permalink
[Session String Generator] Remember generated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Mar 20, 2024
1 parent ea10f4c commit 6c0dd71
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 17 deletions.
29 changes: 21 additions & 8 deletions islands/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { IS_BROWSER } from "$fresh/runtime.ts";
import { Button } from "../components/Button.tsx";

export const error = signal<null | ComponentChildren>(null);
export const showDismissButton = signal(true);

IS_BROWSER && effect(() => {
if (error.value == null) {
showDismissButton.value = true;
}
});

IS_BROWSER && effect(() => {
if (error.value != null) {
Expand Down Expand Up @@ -39,14 +46,20 @@ export function Error({ onDismiss }: { onDismiss?: () => void }) {
>
<div class="w-full max-w-lg p-5 bg-background rounded-xl flex flex-col gap-5 justify-between shadow-sm">
<div class="flex flex-col gap-4">
<p>
{error.value}
</p>
<Button
onClick={dismiss}
>
Dismiss
</Button>
{typeof error.value == "string"
? (
<p>
{error.value}
</p>
)
: error.value}
{showDismissButton.value && (
<Button
onClick={dismiss}
>
Dismiss
</Button>
)}
</div>
</div>
</div>
Expand Down
98 changes: 89 additions & 9 deletions islands/SessionStringGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { Caption } from "../components/Caption.tsx";
import { Input } from "../components/Input.tsx";
import { Label } from "../components/Label.tsx";
import { Select } from "../components/Select.tsx";
import { Error, error } from "./Error.tsx";
import { Error, error, showDismissButton } from "./Error.tsx";
import { getDcIps } from "mtkruto/transport/2_transport_provider.ts";
import {
serializeGramjs,
serializeMtcute,
serializeMtkruto,
serializePyrogram,
serializeTelethon,
} from "../lib/session_string.ts";
Expand All @@ -18,6 +19,9 @@ import { Spinner2 } from "../components/icons/Spinner.tsx";
import { storedString } from "../lib/stored_signals.tsx";
import { getHashSignal } from "../lib/hash_signal.ts";
import { IS_BROWSER } from "$fresh/runtime.ts";
import { Db, SessionString } from "../lib/session_string_generator_db.ts";

const db = new Db();

const hash = getHashSignal();
const sessionString = signal("");
Expand Down Expand Up @@ -166,11 +170,37 @@ export function SessionStringGenerator() {
</Label>
<Label>
<Button
onClick={() => {
loading.value = true;
generateSessionString(library).finally(() => {
loading.value = false;
});
onClick={async () => {
const generate = () => {
loading.value = true;
error.value = null;
generateSessionString(library).finally(() => {
loading.value = false;
});
};
const string = await db.strings.get({ account: account.value });
if (string && "string" in string) {
showDismissButton.value = false;
error.value = (
<>
<p>
A session string was recently generated for this account.
Do you want to see that one?
</p>
<Button
onClick={() => {
fromStorage(string.string, library);
error.value = null;
}}
>
Yes
</Button>
<Button muted onClick={generate}>No, regenerate</Button>
</>
);
return;
}
generate();
}}
>
Next
Expand Down Expand Up @@ -206,6 +236,51 @@ function LibraryPicker() {
);
}

async function fromStorage(
{ apiId, dcId, ip, testMode, me, authKey }: SessionString["string"],
library: ValidLibrary,
) {
switch (library) {
case "telethon":
sessionString.value = serializeTelethon(dcId, ip, 80, authKey);
break;
case "pyrogram": {
sessionString.value = serializePyrogram(
dcId,
apiId,
testMode,
authKey,
me.id,
me.isBot,
);
break;
}
case "gramjs":
sessionString.value = serializeGramjs(dcId, ip, 80, authKey);
break;
case "mtcute": {
sessionString.value = serializeMtcute(
testMode,
{ id: dcId, ip, port: 80 },
null,
me.id,
me.isBot,
authKey,
);
break;
}
case "mtkruto":
sessionString.value = await serializeMtkruto(
`${dcId}${testMode ? "-test" : ""}`,
authKey,
);
break;
default:
error.value = "The chosen library is currently not supported.";
return;
}
}

async function generateSessionString(library: ValidLibrary) {
if (accountType.value != "Bot") {
error.value = "The chosen account type is currently not supported.";
Expand Down Expand Up @@ -238,13 +313,13 @@ async function generateSessionString(library: ValidLibrary) {
const ip = getDcIps(dc, "ipv4")[0]; // TODO
const dcId = Number(dc.split("-")[0]);
const testMode = environment.value == "Test" ? true : false;
const me = await client.getMe();

switch (library) {
case "telethon":
sessionString.value = serializeTelethon(dcId, ip, 80, authKey);
break;
case "pyrogram": {
const me = await client.getMe();
sessionString.value = serializePyrogram(
dcId,
apiId_,
Expand All @@ -259,7 +334,6 @@ async function generateSessionString(library: ValidLibrary) {
sessionString.value = serializeGramjs(dcId, ip, 80, authKey);
break;
case "mtcute": {
const me = await client.getMe();
sessionString.value = serializeMtcute(
testMode,
{ id: dcId, ip, port: 80 },
Expand All @@ -268,12 +342,18 @@ async function generateSessionString(library: ValidLibrary) {
me.isBot,
authKey,
);
return;
break;
}
case "mtkruto":
sessionString.value = await client.exportAuthString();
break;
default:
error.value = "The chosen library is currently not supported.";
return;
}

await db.strings.put({
account: account_,
string: { apiId: apiId_, ip, dcId, testMode, me, authKey },
});
}
7 changes: 7 additions & 0 deletions lib/session_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
base64EncodeUrlSafe,
bufferFromBigInt,
rleDecode,
rleEncode,
} from "mtkruto/1_utilities.ts";

function writeUint16(value: number, writer: TLRawWriter) {
Expand Down Expand Up @@ -297,6 +298,12 @@ const MTCUTE_MEDIA_DC_FLAG = 4;
const MTCUTE_DC_IPV6_FLAG = 1;
const MTCUTE_DC_MEDIA_FLAG = 2;

export function serializeMtkruto(dc: string, authKey: Uint8Array) { // TODO: test
const writer = new TLRawWriter();
writer.writeString(dc);
writer.writeBytes(authKey);
return base64EncodeUrlSafe(rleEncode(writer.buffer));
}
export function deserializeMtkruto(string: string): CommonSessionStringFormat {
const reader = new TLRawReader(rleDecode(base64DecodeUrlSafe(string)));
const dc = reader.readString();
Expand Down
25 changes: 25 additions & 0 deletions lib/session_string_generator_db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Dexie, Table } from "dexie";
import type { User } from "mtkruto/3_types.ts";

export interface SessionString {
account: string;
string: {
apiId: number;
ip: string;
dcId: number;
testMode: boolean;
me: User;
authKey: Uint8Array;
};
}

export class Db extends Dexie {
strings!: Table<SessionString, number>;

constructor() {
super("session-string-generator");
this.version(2).stores({
strings: "&account",
});
}
}

0 comments on commit 6c0dd71

Please sign in to comment.