Skip to content

Commit

Permalink
Reset session state on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Jun 19, 2024
1 parent 1776e63 commit a182113
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions client/1_client_encrypted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import { gunzip, unreachable } from "../0_deps.ts";
import { ConnectionError } from "../0_errors.ts";
import { bigIntFromBuffer, CacheMap, drop, getLogger, getRandomBigInt, Logger, sha1 } from "../1_utilities.ts";
import { bigIntFromBuffer, CacheMap, drop, getLogger, getRandomId, Logger, sha1 } from "../1_utilities.ts";
import { Api, is, isOfEnum, message, ReadObject, TLError, TLReader } from "../2_tl.ts";
import { constructTelegramError } from "../4_errors.ts";
import { ClientAbstract } from "./0_client_abstract.ts";
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface Handlers {
export class ClientEncrypted extends ClientAbstract {
#authKey = new Uint8Array();
#authKeyId = 0n;
#sessionId = getRandomBigInt(8, true, false);
#sessionId = getRandomId();
#state = { serverSalt: 0n, seqNo: 0, messageId: 0n };
#toAcknowledge = new Set<bigint>();
#recentAcks = new CacheMap<bigint, { container?: bigint; message: message }>(20);
Expand All @@ -80,6 +80,12 @@ export class ClientEncrypted extends ClientAbstract {
drop(this.#receiveLoop()); // TODO: ability to join this promise
}

resetState() {
this.#state = { serverSalt: 0n, seqNo: 0, messageId: 0n };
this.#toAcknowledge.clear();
this.#recentAcks.clear();
}

async setAuthKey(key: Uint8Array) {
const hash = await sha1(key);
this.#authKeyId = bigIntFromBuffer(hash.slice(-8), true, false);
Expand Down
3 changes: 3 additions & 0 deletions client/5_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,9 @@ export class Client<C extends Context = Context> extends Composer<C> {

#lastPropagatedConnectionState: ConnectionState | null = null;
#stateChangeHandler: (connected: boolean) => void = ((connected: boolean) => {
if (!connected) {
this.#client.resetState();
}
const connectionState = connected ? "ready" : "notConnected";
if (this.#lastPropagatedConnectionState != connectionState) {
this.#propagateConnectionState(connectionState);
Expand Down
2 changes: 1 addition & 1 deletion connection/1_connection_tcp.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Socket } from "node:net";
import { Mutex } from "../1_utilities.ts";
import { ConnectionError } from "../0_errors.ts";
import { Mutex } from "../1_utilities.ts";
import { Connection } from "./0_connection.ts";

const errConnectionNotOpen = new ConnectionError("Connection not open");
Expand Down

0 comments on commit a182113

Please sign in to comment.