Переглянути джерело

add support for telethon string sessions

painor 4 роки тому
батько
коміт
262f3d246e
1 змінених файлів з 28 додано та 16 видалено
  1. 28 16
      gramjs/sessions/StringSession.ts

+ 28 - 16
gramjs/sessions/StringSession.ts

@@ -2,7 +2,7 @@ import {MemorySession} from "./Memory";
 import {BinaryReader} from "../extensions";
 import {BinaryReader} from "../extensions";
 import {AuthKey} from "../crypto/AuthKey";
 import {AuthKey} from "../crypto/AuthKey";
 
 
-const CURRENT_VERSION = '1'
+const CURRENT_VERSION = '1';
 
 
 
 
 export class StringSession extends MemorySession {
 export class StringSession extends MemorySession {
@@ -24,21 +24,33 @@ export class StringSession extends MemorySession {
      * @param session {string|null}
      * @param session {string|null}
      */
      */
     constructor(session?: string) {
     constructor(session?: string) {
-        super()
+        super();
         if (session) {
         if (session) {
             if (session[0] !== CURRENT_VERSION) {
             if (session[0] !== CURRENT_VERSION) {
                 throw new Error('Not a valid string')
                 throw new Error('Not a valid string')
             }
             }
-            session = session.slice(1)
-            const r = StringSession.decode(session)
-            const reader = new BinaryReader(r)
+            session = session.slice(1);
+            const r = StringSession.decode(session);
+
+            const reader = new BinaryReader(r);
             this._dcId = reader.read(1)
             this._dcId = reader.read(1)
-                .readUInt8(0)
-            const serverAddressLen = reader.read(2)
-                .readInt16BE(0)
-            this._serverAddress = String(reader.read(serverAddressLen))
+                .readUInt8(0);
+            if (session.length == 352) {
+                // Telethon session
+                const ip_v4 = reader.read(4);
+                // TODO looks ugly smh
+                this._serverAddress = ip_v4[0].toString() + '.' + ip_v4[1].toString() + '.' + ip_v4[2].toString() + '.' + ip_v4[3].toString();
+
+            } else {
+                const serverAddressLen = reader.read(2)
+                    .readInt16BE(0);
+                this._serverAddress = reader.read(serverAddressLen).toString();
+
+            }
+
+
             this._port = reader.read(2)
             this._port = reader.read(2)
-                .readInt16BE(0)
+                .readInt16BE(0);
             this._key = reader.read(-1)
             this._key = reader.read(-1)
         }
         }
     }
     }
@@ -75,12 +87,12 @@ export class StringSession extends MemorySession {
         if (!key) {
         if (!key) {
             return '';
             return '';
         }
         }
-        const dcBuffer = Buffer.from([this.dcId])
-        const addressBuffer = Buffer.from(this.serverAddress)
-        const addressLengthBuffer = Buffer.alloc(2)
-        addressLengthBuffer.writeInt16BE(addressBuffer.length, 0)
-        const portBuffer = Buffer.alloc(2)
-        portBuffer.writeInt16BE(this.port, 0)
+        const dcBuffer = Buffer.from([this.dcId]);
+        const addressBuffer = Buffer.from(this.serverAddress);
+        const addressLengthBuffer = Buffer.alloc(2);
+        addressLengthBuffer.writeInt16BE(addressBuffer.length, 0);
+        const portBuffer = Buffer.alloc(2);
+        portBuffer.writeInt16BE(this.port, 0);
 
 
         return CURRENT_VERSION + StringSession.encode(Buffer.concat([
         return CURRENT_VERSION + StringSession.encode(Buffer.concat([
             dcBuffer,
             dcBuffer,