Kaynağa Gözat

clean up code\nbump version

painor 5 yıl önce
ebeveyn
işleme
637ae3214e

+ 1 - 0
gramjs/Version.js

@@ -0,0 +1 @@
+module.exports = '0.0.2'

+ 2 - 4
gramjs/client/TelegramClient.js

@@ -1,4 +1,4 @@
-const logger = require('log4js').getLogger()
+const Logger = require('../extensions/Logger')
 const { sleep } = require('../Helpers')
 const { sleep } = require('../Helpers')
 const errors = require('../errors')
 const errors = require('../errors')
 const MemorySession = require('../sessions/Memory')
 const MemorySession = require('../sessions/Memory')
@@ -52,7 +52,7 @@ class TelegramClient {
         this._useIPV6 = args.useIPV6
         this._useIPV6 = args.useIPV6
         this._entityCache = new Set()
         this._entityCache = new Set()
         if (typeof args.baseLogger == 'string') {
         if (typeof args.baseLogger == 'string') {
-            this._log = logger
+            this._log = new Logger()
         } else {
         } else {
             this._log = args.baseLogger
             this._log = args.baseLogger
         }
         }
@@ -375,10 +375,8 @@ class TelegramClient {
         botToken: null,
         botToken: null,
         phoneCodeHash: null,
         phoneCodeHash: null,
     }) {
     }) {
-        console.log('signing in now')
         let result
         let result
         if (args.phone && !args.code && !args.password) {
         if (args.phone && !args.code && !args.password) {
-            console.log('first sign in')
             return await this.sendCodeRequest(args.phone)
             return await this.sendCodeRequest(args.phone)
         } else if (args.code) {
         } else if (args.code) {
             const [phone, phoneCodeHash] =
             const [phone, phoneCodeHash] =

+ 1 - 8
gramjs/crypto/RSA.js

@@ -37,14 +37,7 @@ function encrypt(fingerprint, data) {
         return undefined
         return undefined
     }
     }
     const buf = readBigIntFromBuffer(key.keyPair.n.toBuffer(), false)
     const buf = readBigIntFromBuffer(key.keyPair.n.toBuffer(), false)
-    let rand = generateRandomBytes(235 - data.length)
-    rand = Buffer.from(
-        '66a6f809e0dfd71d9dbbc2d6b5fe5fc0be9f5b2b0f2f85688843eea6b2c6d51329750f020c8de27a0a911b07d2a46600493d1abb7caf24' +
-        '01ccd815d7de7c5ea830cdf6cce8bff12f77db589f233bce436b644c3415f16d073335fdadfe313c603485b3274e8fcd148fd1a5e18bd2' +
-        '4b3e983df94d58b61c150333ab8d614101e7a904dc38af3a3b29e73d62',
-        'hex',
-    )
-
+    const rand = generateRandomBytes(235 - data.length)
     const toEncrypt = Buffer.concat([sha1(data), data, rand])
     const toEncrypt = Buffer.concat([sha1(data), data, rand])
     const payload = readBigIntFromBuffer(toEncrypt, false)
     const payload = readBigIntFromBuffer(toEncrypt, false)
     const encrypted = modExp(payload, BigInt(key.keyPair.e), buf)
     const encrypted = modExp(payload, BigInt(key.keyPair.e), buf)

+ 0 - 3
gramjs/errors/index.js

@@ -24,9 +24,6 @@ function RPCMessageToError(rpcError, request) {
 
 
 module.exports = {
 module.exports = {
     RPCMessageToError,
     RPCMessageToError,
-}
-module.exports = {
-    ...module.exports,
     ...require('./Common'),
     ...require('./Common'),
     ...require('./RPCBaseErrors'),
     ...require('./RPCBaseErrors'),
     ...require('./RPCErrorList'),
     ...require('./RPCErrorList'),

+ 4 - 1
gramjs/events/index.js

@@ -1,5 +1,8 @@
+const NewMessage = require('./NewMessage')
+const Raw = require('./Raw')
+
 class StopPropagation extends Error {
 class StopPropagation extends Error {
 
 
 }
 }
 
 
-module.exports = StopPropagation
+module.exports = { NewMessage, StopPropagation, Raw }

+ 31 - 40
gramjs/extensions/PromisedWebSockets.js

@@ -1,30 +1,28 @@
-const WebSocketClient = require('websocket').client
-const tunnel = require('tunnel')
+const WebSocketClient = require('websocket').w3cwebsocket
 
 
 const closeError = new Error('WebSocket was closed')
 const closeError = new Error('WebSocket was closed')
 
 
 class PromisedWebSockets {
 class PromisedWebSockets {
-    constructor(website) {
-        this.website = website
+    constructor() {
+        this.isBrowser = typeof process === 'undefined' ||
+            process.type === 'renderer' ||
+            process.browser === true ||
+            process.__nwjs
         this.stream = Buffer.alloc(0)
         this.stream = Buffer.alloc(0)
-        this.connection = null
-        this.client = new WebSocketClient()
+        this.client = null
 
 
         this.canRead = new Promise((resolve) => {
         this.canRead = new Promise((resolve) => {
             this.resolveRead = resolve
             this.resolveRead = resolve
         })
         })
         this.closed = false
         this.closed = false
-        this.client.on('close', function() {
-            this.resolveRead(false)
-            this.closed = true
-        }.bind(this))
     }
     }
 
 
     async read(number) {
     async read(number) {
-        if (this.closed || !await this.canRead) {
+        if (this.closed) {
             console.log('couldn\'t read')
             console.log('couldn\'t read')
             throw closeError
             throw closeError
         }
         }
+        const canWe = await this.canRead
 
 
         const toReturn = this.stream.slice(0, number)
         const toReturn = this.stream.slice(0, number)
         this.stream = this.stream.slice(number)
         this.stream = this.stream.slice(number)
@@ -51,36 +49,30 @@ class PromisedWebSockets {
 
 
     getWebSocketLink(ip, port) {
     getWebSocketLink(ip, port) {
         if (port === 443) {
         if (port === 443) {
-            return 'wss://' + ip
+            return 'wss://' + ip + '/apiws'
         } else {
         } else {
-            return 'ws://' + ip
+            return 'ws://' + ip + '/apiws'
         }
         }
     }
     }
 
 
     async connect(port, ip) {
     async connect(port, ip) {
-        const tunnelingAgent = tunnel.httpOverHttp({
-            proxy: {
-                host: '127.0.0.1',
-                port: 8888,
-            },
-        })
-
-        const requestOptions = {
-            agent: tunnelingAgent,
-        }
-
+        console.log('trying to connect')
         this.website = this.getWebSocketLink(ip, port)
         this.website = this.getWebSocketLink(ip, port)
-        //this.website = 'ws://echo.websocket.org'
+        this.client = new WebSocketClient(this.website, 'binary')
         return new Promise(function(resolve, reject) {
         return new Promise(function(resolve, reject) {
-            this.client.on('connect', function(connection) {
-                this.connection = connection
+            this.client.onopen = function() {
                 this.receive()
                 this.receive()
-                resolve(connection)
-            }.bind(this))
-            this.client.on('connectFailed', function(error) {
+                resolve(this)
+            }.bind(this)
+            this.client.onerror = function(error) {
                 reject(error)
                 reject(error)
-            })
-            this.client.connect(this.website, null, null, null, requestOptions)
+            }
+            this.client.onclose = function() {
+                if (this.client.closed) {
+                    this.resolveRead(false)
+                    this.closed = true
+                }
+            }.bind(this)
         }.bind(this))
         }.bind(this))
     }
     }
 
 
@@ -88,28 +80,27 @@ class PromisedWebSockets {
         if (this.closed) {
         if (this.closed) {
             throw closeError
             throw closeError
         }
         }
-        this.connection.send(data)
+        this.client.send(data)
     }
     }
 
 
     async close() {
     async close() {
         console.log('something happened. closing')
         console.log('something happened. closing')
-        await this.connection.close()
+        await this.client.close()
         this.resolveRead(false)
         this.resolveRead(false)
         this.closed = true
         this.closed = true
     }
     }
 
 
     async receive() {
     async receive() {
-        this.connection.on('message', function(message) {
+        this.client.onmessage = async function(message) {
             let data
             let data
-            if (message.binaryData) {
-                data = Buffer.from(message.binaryData)
+            if (this.isBrowser) {
+                data = Buffer.from(await new Response(message.data).arrayBuffer())
             } else {
             } else {
-                data = Buffer.from(message.utf8Data, 'utf-8')
+                data = Buffer.from(message.data)
             }
             }
-
             this.stream = Buffer.concat([this.stream, data])
             this.stream = Buffer.concat([this.stream, data])
             this.resolveRead(true)
             this.resolveRead(true)
-        }.bind(this))
+        }.bind(this)
     }
     }
 }
 }
 
 

+ 14 - 4
gramjs/index.js

@@ -1,6 +1,16 @@
-//require('regenerator-runtime/runtime')
-//require('regenerator-runtime')
+require('regenerator-runtime/runtime')
+require('regenerator-runtime')
+
 const TelegramClient = require('./client/TelegramClient')
 const TelegramClient = require('./client/TelegramClient')
-const StringSession = require('./sessions/StringSession')
+const connection = require('./network')
+const tl = require('./tl')
+const version = require('./Version')
+const events = require('./events')
+const utils = require('./Utils')
+const errors = require('./errors')
+const session = require('./sessions')
 
 
-module.exports = { TelegramClient, StringSession }
+module.exports = {
+    TelegramClient, session, connection,
+    tl, version, events, utils, errors,
+}

+ 2 - 2
gramjs/network/MTProtoSender.js

@@ -253,7 +253,7 @@ class MTProtoSender {
         // or errors after which the sender cannot continue such
         // or errors after which the sender cannot continue such
         // as failing to reconnect or any unexpected error.
         // as failing to reconnect or any unexpected error.
 
 
-        this._log.info('Connection to %s complete!', this._connection.toString())
+        this._log.info('Connection to %s complete!'.replace('%s', this._connection.toString()))
     }
     }
 
 
     async _disconnect(error = null) {
     async _disconnect(error = null) {
@@ -261,7 +261,7 @@ class MTProtoSender {
             this._log.info('Not disconnecting (already have no connection)')
             this._log.info('Not disconnecting (already have no connection)')
             return
             return
         }
         }
-        this._log.info('Disconnecting from %s...', this._connection.toString())
+        this._log.info('Disconnecting from %s...'.replace('%s', this._connection.toString()))
         this._user_connected = false
         this._user_connected = false
         this._log.debug('Closing current connection...')
         this._log.debug('Closing current connection...')
         await this._connection.disconnect()
         await this._connection.disconnect()

+ 0 - 2
gramjs/network/connection/Connection.js

@@ -1,7 +1,5 @@
 const PromisedWebSockets = require('../../extensions/PromisedWebSockets')
 const PromisedWebSockets = require('../../extensions/PromisedWebSockets')
 const AsyncQueue = require('../../extensions/AsyncQueue')
 const AsyncQueue = require('../../extensions/AsyncQueue')
-const Socket = require('net').Socket
-const { PromiseSocket } = require('promise-socket')
 
 
 /**
 /**
  * The `Connection` class is a wrapper around ``asyncio.open_connection``.
  * The `Connection` class is a wrapper around ``asyncio.open_connection``.

+ 11 - 0
gramjs/network/connection/index.js

@@ -0,0 +1,11 @@
+const { Connection } = require('./Connection')
+const { ConnectionTCPFull } = require('./TCPFull')
+const { ConnectionTCPAbridged } = require('./TCPAbridged')
+const { ConnectionTCPObfuscated } = require('./TCPObfuscated')
+
+module.exports = {
+    Connection,
+    ConnectionTCPFull,
+    ConnectionTCPAbridged,
+    ConnectionTCPObfuscated,
+}

+ 18 - 0
gramjs/network/index.js

@@ -0,0 +1,18 @@
+const MTProtoPlainSender = require('./MTProtoPlainSender')
+const doAuthentication = require('./Authenticator')
+const MTProtoSender = require('./MTProtoSender')
+const {
+    Connection,
+    ConnectionTCPFull,
+    ConnectionTCPAbridged,
+    ConnectionTCPObfuscated,
+} = require('./connection')
+module.exports = {
+    Connection,
+    ConnectionTCPFull,
+    ConnectionTCPAbridged,
+    ConnectionTCPObfuscated,
+    MTProtoPlainSender,
+    doAuthentication,
+    MTProtoSender,
+}

+ 8 - 0
gramjs/sessions/index.js

@@ -0,0 +1,8 @@
+const JSONSession = require('./JSONSession')
+const Memory = require('./Memory')
+const StringSession = require('./StringSession')
+module.exports = {
+    JSONSession,
+    Memory,
+    StringSession,
+}

+ 0 - 0
gramjs/tl/custom/index.js


+ 5 - 1
gramjs/tl/index.js

@@ -1,9 +1,13 @@
 const types = require('./types')
 const types = require('./types')
 const functions = require('./functions')
 const functions = require('./functions')
+const custom = require('./custom')
 const patched = null
 const patched = null
-
+const { TLObject, TLRequest } = require('./tlobject')
 module.exports = {
 module.exports = {
     types,
     types,
     functions,
     functions,
+    custom,
     patched,
     patched,
+    TLObject,
+    TLRequest,
 }
 }

+ 1 - 5
package.json

@@ -25,20 +25,18 @@
     "aes-js": "^3.1.2",
     "aes-js": "^3.1.2",
     "babel-loader": "^8.0.6",
     "babel-loader": "^8.0.6",
     "babel-register": "^6.26.0",
     "babel-register": "^6.26.0",
+    "chalk": "^2.4.2",
     "crc": "^3.8.0",
     "crc": "^3.8.0",
     "csv-parse": "^4.4.6",
     "csv-parse": "^4.4.6",
     "fast-csv": "^3.4.0",
     "fast-csv": "^3.4.0",
     "glob": "^7.1.4",
     "glob": "^7.1.4",
-    "loglevel": "^1.6.4",
     "net": "^1.0.2",
     "net": "^1.0.2",
     "node-gzip": "^1.1.2",
     "node-gzip": "^1.1.2",
     "node-rsa": "^1.0.6",
     "node-rsa": "^1.0.6",
-    "promise-socket": "^6.0.2",
     "python-struct": "^1.1.1",
     "python-struct": "^1.1.1",
     "regenerator-runtime": "^0.13.3",
     "regenerator-runtime": "^0.13.3",
     "stack-trace": "0.0.10",
     "stack-trace": "0.0.10",
     "string-format": "^2.0.0",
     "string-format": "^2.0.0",
-    "tunnel": "0.0.6",
     "websocket": "^1.0.30"
     "websocket": "^1.0.30"
   },
   },
   "devDependencies": {
   "devDependencies": {
@@ -46,8 +44,6 @@
     "babel-eslint": "^10.0.3",
     "babel-eslint": "^10.0.3",
     "eslint": "^6.5.1",
     "eslint": "^6.5.1",
     "eslint-config-google": "^0.14.0",
     "eslint-config-google": "^0.14.0",
-    "log4js": "^5.3.0",
-    "loglevelnext": "^3.0.1",
     "webpack": "^4.41.2",
     "webpack": "^4.41.2",
     "webpack-cli": "^3.3.10",
     "webpack-cli": "^3.3.10",
     "websocket-as-promised": "^0.10.1"
     "websocket-as-promised": "^0.10.1"