Skip to content

Commit

Permalink
Create new sessions every hour
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Jun 18, 2024
1 parent 1776e63 commit 1061602
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions client/1_client_encrypted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export class ClientEncrypted extends ClientAbstract {
this.#L.outBin(payload);
}

reassignSessionId() {
this.#sessionId = getRandomBigInt(8, true, false);
}

async invoke<T extends Api.AnyObject<P>, P extends Api.Function, R extends unknown = Promise<T["_"] extends keyof Api.Functions ? Api.ReturnType<Api.Functions[T["_"]]> : never>>(function_: T, noWait?: boolean): Promise<R | void> {
const messageId = this.#nextMessageId();
let message_: message = {
Expand Down
11 changes: 8 additions & 3 deletions client/5_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import { unreachable } from "../0_deps.ts";
import { AccessError, ConnectionError, InputError } from "../0_errors.ts";
import { cleanObject, drop, getLogger, getRandomId, Logger, MaybePromise, minute, mustPrompt, mustPromptOneOf, Mutex, second, ZERO_CHANNEL_ID } from "../1_utilities.ts";
import { cleanObject, drop, getLogger, getRandomId, hour, Logger, MaybePromise, minute, mustPrompt, mustPromptOneOf, Mutex, second, ZERO_CHANNEL_ID } from "../1_utilities.ts";
import { Api, as, chatIdToPeerId, getChatIdPeerType, is, peerToChatId } from "../2_tl.ts";
import { Storage, StorageMemory } from "../2_storage.ts";
import { DC } from "../3_transport.ts";
Expand Down Expand Up @@ -1022,6 +1022,9 @@ export class Client<C extends Context = Context> extends Composer<C> {
if (this.#lastConnect != null && Date.now() - this.#lastConnect.getTime() <= 10 * second) {
await new Promise((r) => setTimeout(r, 3 * second));
}
if (this.#lastConnect != null && Date.now() - this.#lastConnect.getTime() <= 1 * hour) {
this.#client.reassignSessionId();
}
await this.#initStorage();
const [authKey, dc] = await Promise.all([this.storage.getAuthKey(), this.storage.getDc()]);
if (authKey != null && dc != null) {
Expand Down Expand Up @@ -1282,7 +1285,7 @@ export class Client<C extends Context = Context> extends Composer<C> {
return;
}
this.#pingLoopAbortController = new AbortController();
while (this.connected) {
while (true) {
try {
await new Promise((resolve, reject) => {
const timeout = setTimeout(resolve, this.#pingInterval);
Expand All @@ -1296,7 +1299,9 @@ export class Client<C extends Context = Context> extends Composer<C> {
}
await this.invoke({ _: "ping_delay_disconnect", ping_id: getRandomId(), disconnect_delay: this.#pingInterval / second + 15 });
this.#pingLoopAbortController.signal.throwIfAborted();
if (Date.now() - this.#lastUpdates.getTime() >= 15 * minute) {
if (Date.now() - this.#lastUpdates.getTime() >= 2 * hour) {
drop(this.reconnect());
} else if (Date.now() - this.#lastUpdates.getTime() >= 15 * minute) {
drop(this.#updateManager.recoverUpdateGap("lastUpdates"));
}
} catch (err) {
Expand Down

0 comments on commit 1061602

Please sign in to comment.