Browse Source

Implement session and mtprotoRequest

painor 5 years ago
parent
commit
53f5a8c48b
4 changed files with 102 additions and 2 deletions
  1. 45 0
      tl/mtprotoRequest.js
  2. 52 0
      tl/session.js
  3. 0 0
      tl/telegramClient.js
  4. 5 2
      utils/Helpers.js

+ 45 - 0
tl/mtprotoRequest.js

@@ -0,0 +1,45 @@
+class MTProtoRequest {
+
+    constructor() {
+        this.sent = false;
+        this.msgId = 0; //long
+        this.sequence = 0;
+
+        this.dirty = false;
+        this.sendTime = 0;
+        this.confirmReceived = false;
+
+        // These should be overrode
+
+        this.constructorId = 0;
+        this.confirmed = false;
+        this.responded = false;
+    }
+
+    // these should not be overrode
+    onSendSuccess() {
+        this.sendTime = new Date().getTime();
+        this.sent = true;
+    }
+
+    onConfirm() {
+        this.confirmReceived = true;
+    }
+
+    needResend(){
+        return this.dirty || (this.confirmed && !this.confirmReceived && new Date().getTime() - this.sendTime > 3000);
+    }
+
+    // These should be overrode
+    onSend(buffer){
+
+    }
+
+    onResponse(buffer){
+
+    }
+
+    onException(exception){
+
+    }
+}

+ 52 - 0
tl/session.js

@@ -0,0 +1,52 @@
+const Helpers = require("../utils/Helpers");
+const fs = require("fs").promises;
+
+class Session {
+    constructor(sessionUserId) {
+        this.sessionUserId = sessionUserId;
+        this.serverAddress = "149.154.167.40";
+        this.port = 80;
+        this.authKey = undefined;
+        this.id = Helpers.generateRandomLong(false);
+        this.sequence = 0;
+        this.salt = 0; // Unsigned long
+        this.timeOffset = 0;
+        this.lastMessageId = 0;
+        this.user = undefined;
+    }
+
+    /**
+     * Saves the current session object as session_user_id.session
+     */
+    async save() {
+        if (this.sessionUserId) {
+            await fs.writeFile(`${this.sessionUserId}.session`, JSON.stringify(this));
+        }
+    }
+
+    static async tryLoadOrCreateNew(sessionUserId) {
+        if (sessionUserId === undefined) {
+            return new Session();
+        }
+        let filepath = `${sessionUserId}.session`;
+        if (await fs.exists(filepath)) {
+            return JSON.parse(await fs.readFile(filepath, "utf-8"));
+        } else {
+            return Session(sessionUserId);
+        }
+    }
+
+    getNewMsgId() {
+        let msTime = new Date().getTime();
+        let newMessageId = (BigInt(Math.floor(msTime / 1000) + this.timeOffset) << BigInt(32)) |
+            ((msTime % 1000) << 22) |
+            (Helpers.getRandomInt(0, 524288) << 2); // 2^19
+
+        if (this.lastMessageId >= newMessageId) {
+            newMessageId = this.lastMessageId + 4;
+        }
+        this.lastMessageId = newMessageId;
+        return newMessageId;
+    }
+}
+

+ 0 - 0
tl/telegramClient.js


+ 5 - 2
utils/Helpers.js

@@ -8,9 +8,12 @@ class Helpers {
      * Generates a random long integer (8 bytes), which is optionally signed
      * @returns {number}
      */
-    static generateRandomLong() {
+    static generateRandomLong(signed) {
         let buf = Buffer.from(this.generateRandomBytes(8)); // 0x12345678 = 305419896
-        return buf.readUInt32BE(0);
+        if (signed)
+            return buf.readInt32BE(0);
+        else
+            return buf.readUInt32LE(0);
     }
 
     /**