Browse Source

add support for node netsockets

painor 5 years ago
parent
commit
32cde7cff0

+ 93 - 0
gramjs/extensions/PromisedNetSockets.js

@@ -0,0 +1,93 @@
+const Socket = require('net').Socket
+
+const closeError = new Error('WebSocket was closed')
+
+class PromisedNetSockets {
+    constructor() {
+        this.stream = Buffer.alloc(0)
+        this.client = null
+
+        this.canRead = new Promise((resolve) => {
+            this.resolveRead = resolve
+        })
+        this.closed = false
+    }
+
+    async read(number) {
+        if (this.closed) {
+            console.log('couldn\'t read')
+            throw closeError
+        }
+        const canWe = await this.canRead
+
+        const toReturn = this.stream.slice(0, number)
+        this.stream = this.stream.slice(number)
+        if (this.stream.length === 0) {
+            this.canRead = new Promise((resolve) => {
+                this.resolveRead = resolve
+            })
+        }
+
+        return toReturn
+    }
+
+    async readAll() {
+        if (this.closed || !await this.canRead) {
+            throw closeError
+        }
+        const toReturn = this.stream
+        this.stream = Buffer.alloc(0)
+        this.canRead = new Promise((resolve) => {
+            this.resolveRead = resolve
+        })
+        return toReturn
+    }
+
+    /**
+     * Creates a new connection
+     * @param port
+     * @param ip
+     * @returns {Promise<void>}
+     */
+    async connect(port, ip) {
+        this.client = new Socket()
+        return new Promise(function(resolve, reject) {
+            this.client.connect(port, ip, function() {
+                this.receive()
+                resolve(this)
+            }.bind(this))
+            this.client.on('error', function(error) {
+                reject(error)
+            })
+            this.client.on('close', function() {
+                if (this.client.closed) {
+                    this.resolveRead(false)
+                    this.closed = true
+                }
+            }.bind(this))
+        }.bind(this))
+    }
+
+    write(data) {
+        if (this.closed) {
+            throw closeError
+        }
+        this.client.write(data)
+    }
+
+    async close() {
+        await this.client.destroy()
+        this.resolveRead(false)
+        this.closed = true
+    }
+
+    async receive() {
+        this.client.on('data', async function(message) {
+            const data = Buffer.from(message)
+            this.stream = Buffer.concat([this.stream, data])
+            this.resolveRead(true)
+        }.bind(this))
+    }
+}
+
+module.exports = PromisedNetSockets

+ 15 - 1
gramjs/extensions/index.js

@@ -1,2 +1,16 @@
 const Logger = require('./Logger')
-const BinaryWriter = require()
+const BinaryWriter = require('./BinaryWriter')
+const BinaryReader = require('./BinaryReader')
+const PromisedWebSockets = require('./PromisedWebSockets')
+const MessagePacker = require('./MessagePacker')
+const AsyncQueue = require('./AsyncQueue')
+const PromisedNetSocket = require('./PromisedNetSocket')
+module.exports = {
+    BinaryWriter,
+    BinaryReader,
+    MessagePacker,
+    AsyncQueue,
+    Logger,
+    PromisedWebSockets,
+    PromisedNetSocket,
+}

+ 8 - 4
gramjs/network/connection/Connection.js

@@ -1,4 +1,6 @@
 const PromisedWebSockets = require('../../extensions/PromisedWebSockets')
+const PromisedNetSockets = require('../../extensions/PromisedNetSockets')
+
 const AsyncQueue = require('../../extensions/AsyncQueue')
 
 /**
@@ -15,7 +17,7 @@ const AsyncQueue = require('../../extensions/AsyncQueue')
 class Connection {
     PacketCodecClass = null
 
-    constructor(ip, port, dcId, loggers) {
+    constructor(ip, port, dcId, loggers, browser = false) {
         this._ip = ip
         this._port = port
         this._dcId = dcId
@@ -27,9 +29,11 @@ class Connection {
         this._obfuscation = null // TcpObfuscated and MTProxy
         this._sendArray = new AsyncQueue()
         this._recvArray = new AsyncQueue()
-        //this.socket = new PromiseSocket(new Socket())
-
-        this.socket = new PromisedWebSockets()
+        if (browser) {
+            this.socket = new PromisedWebSockets()
+        } else {
+            this.socket = new PromisedNetSockets()
+        }
     }
 
     async _connect() {

+ 0 - 1
gramjs/sessions/PouchSession.js

@@ -1,3 +1,2 @@
 const EXTENSION = '.session'
 const CURRENT_VERSION = 1
-

File diff suppressed because it is too large
+ 514 - 34
package-lock.json


+ 1 - 0
package.json

@@ -36,6 +36,7 @@
     "node-rsa": "^1.0.6",
     "python-struct": "^1.1.1",
     "regenerator-runtime": "^0.13.3",
+    "sqlite3": "^4.1.0",
     "stack-trace": "0.0.10",
     "string-format": "^2.0.0",
     "websocket": "^1.0.30"

Some files were not shown because too many files changed in this diff