@mtproto/core
Advanced tools
Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "@mtproto/core", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Telegram API for browser", | ||
@@ -31,2 +31,3 @@ "keywords": [ | ||
"axios": "0.19.0", | ||
"events": "3.1.0", | ||
"leemon": "6.2.0", | ||
@@ -33,0 +34,0 @@ "lodash.debounce": "4.0.8", |
@@ -5,4 +5,5 @@ # @mtproto/core | ||
layer = 74 | ||
> Library for working with the Telegram API in pure JavaScript. | ||
https://core.telegram.org/mtproto/transports#uri-format | ||
* **Relevant.** 108 layer in the API scheme | ||
* **Easy.** Cryptography is hidden. Just make requests to the API |
@@ -29,2 +29,45 @@ module.exports = { | ||
{ | ||
id: '-1443537003', | ||
predicate: 'p_q_inner_data_dc', | ||
params: [ | ||
{ name: 'pq', type: 'bytes' }, | ||
{ name: 'p', type: 'bytes' }, | ||
{ name: 'q', type: 'bytes' }, | ||
{ name: 'nonce', type: 'int128' }, | ||
{ name: 'server_nonce', type: 'int128' }, | ||
{ name: 'new_nonce', type: 'int256' }, | ||
{ name: 'dc', type: 'int' }, | ||
], | ||
type: 'P_Q_inner_data', | ||
}, | ||
{ | ||
id: '1013613780', | ||
predicate: 'p_q_inner_data_temp', | ||
params: [ | ||
{ name: 'pq', type: 'bytes' }, | ||
{ name: 'p', type: 'bytes' }, | ||
{ name: 'q', type: 'bytes' }, | ||
{ name: 'nonce', type: 'int128' }, | ||
{ name: 'server_nonce', type: 'int128' }, | ||
{ name: 'new_nonce', type: 'int256' }, | ||
{ name: 'expires_in', type: 'int' }, | ||
], | ||
type: 'P_Q_inner_data', | ||
}, | ||
{ | ||
id: '1459478408', | ||
predicate: 'p_q_inner_data_temp_dc', | ||
params: [ | ||
{ name: 'pq', type: 'bytes' }, | ||
{ name: 'p', type: 'bytes' }, | ||
{ name: 'q', type: 'bytes' }, | ||
{ name: 'nonce', type: 'int128' }, | ||
{ name: 'server_nonce', type: 'int128' }, | ||
{ name: 'new_nonce', type: 'int256' }, | ||
{ name: 'dc', type: 'int' }, | ||
{ name: 'expires_in', type: 'int' }, | ||
], | ||
type: 'P_Q_inner_d', | ||
}, | ||
{ | ||
id: '2043348061', | ||
@@ -173,14 +216,2 @@ predicate: 'server_DH_params_fail', | ||
{ | ||
id: '-501201412', | ||
predicate: 'destroy_session_ok', | ||
params: [{ name: 'session_id', type: 'long' }], | ||
type: 'DestroySessionRes', | ||
}, | ||
{ | ||
id: '1658015945', | ||
predicate: 'destroy_session_none', | ||
params: [{ name: 'session_id', type: 'long' }], | ||
type: 'DestroySessionRes', | ||
}, | ||
{ | ||
id: '-1631450872', | ||
@@ -258,2 +289,8 @@ predicate: 'new_session_created', | ||
{ | ||
id: '-2045723925', | ||
predicate: 'msg_resend_ans_req', | ||
params: [{ name: 'msg_ids', type: 'Vector<long>' }], | ||
type: 'MsgResendReq', | ||
}, | ||
{ | ||
id: '-630588590', | ||
@@ -303,2 +340,44 @@ predicate: 'msgs_state_req', | ||
}, | ||
{ | ||
id: '1973679973', | ||
predicate: 'bind_auth_key_inner', | ||
params: [ | ||
{ name: 'nonce', type: 'long' }, | ||
{ name: 'temp_auth_key_id', type: 'long' }, | ||
{ name: 'perm_auth_key_id', type: 'long' }, | ||
{ name: 'temp_session_id', type: 'long' }, | ||
{ name: 'expires_at', type: 'int' }, | ||
], | ||
type: 'BindAuthKeyInner', | ||
}, | ||
{ | ||
id: '-161422892', | ||
predicate: 'destroy_auth_key_ok', | ||
params: [], | ||
type: 'DestroyAuthKeyRes', | ||
}, | ||
{ | ||
id: '178201177', | ||
predicate: 'destroy_auth_key_none', | ||
params: [], | ||
type: 'DestroyAuthKeyRes', | ||
}, | ||
{ | ||
id: '-368010477', | ||
predicate: 'destroy_auth_key_fail', | ||
params: [], | ||
type: 'DestroyAuthKeyRes', | ||
}, | ||
{ | ||
id: '-501201412', | ||
predicate: 'destroy_session_ok', | ||
params: [{ name: 'session_id', type: 'long' }], | ||
type: 'DestroySessionRes', | ||
}, | ||
{ | ||
id: '1658015945', | ||
predicate: 'destroy_session_none', | ||
params: [{ name: 'session_id', type: 'long' }], | ||
type: 'DestroySessionRes', | ||
}, | ||
], | ||
@@ -313,3 +392,3 @@ methods: [ | ||
{ | ||
id: '3195965169', | ||
id: '-1099002127', | ||
method: 'req_pq_multi', | ||
@@ -370,8 +449,2 @@ params: [{ name: 'nonce', type: 'int128' }], | ||
{ | ||
id: '-414113498', | ||
method: 'destroy_session', | ||
params: [{ name: 'session_id', type: 'long' }], | ||
type: 'DestroySessionRes', | ||
}, | ||
{ | ||
id: '-1835453025', | ||
@@ -386,3 +459,15 @@ method: 'http_wait', | ||
}, | ||
{ | ||
id: '-784117408', | ||
method: 'destroy_auth_key', | ||
params: [], | ||
type: 'DestroyAuthKeyRes', | ||
}, | ||
{ | ||
id: '-414113498', | ||
method: 'destroy_session', | ||
params: [{ name: 'session_id', type: 'long' }], | ||
type: 'DestroySessionRes', | ||
}, | ||
], | ||
}; |
@@ -28,4 +28,6 @@ const MTProto = require('../main'); | ||
.call('auth.sendCode', { | ||
flags: 0, | ||
phone_number: phoneNumber, | ||
settings: { | ||
_: 'codeSettings', | ||
}, | ||
}) | ||
@@ -43,1 +45,5 @@ .then(result => { | ||
}); | ||
mtproto.api.on('updateShort', message => { | ||
console.log(`updateShort[message]:`, message); | ||
}); |
const debounce = require('lodash.debounce'); | ||
const EventEmitter = require('events'); | ||
const http = require('../transport'); | ||
const config = require('../config'); | ||
const { BigInteger, SecureRandom } = require('../vendors/jsbn'); | ||
@@ -48,14 +48,18 @@ const { TLSerialization, TLDeserialization } = require('../tl'); | ||
class API { | ||
class API extends EventEmitter { | ||
constructor({ api_id, api_hash, test, https }) { | ||
super(); | ||
this.api_id = api_id; | ||
this.api_hash = api_hash; | ||
this.test = test; | ||
this.https = https; | ||
this.authObject = {}; | ||
this.lastMessageID = [0, 0]; | ||
this.lastMessageId = [0, 0]; | ||
this.timeOffset = 0; | ||
this._seqNo = 0; | ||
this.sessionID = null; | ||
this.prevSessionID = null; | ||
this.sessionId = null; | ||
this.prevSessionId = null; | ||
this.longPollRunning = false; | ||
@@ -66,8 +70,2 @@ | ||
const urlPath = test ? '/apiw_test1' : '/apiw1'; | ||
this.url = https | ||
? `https://venus.web.telegram.org${urlPath}` | ||
: `http://149.154.167.40${urlPath}`; | ||
this.sendAcks = debounce(() => { | ||
@@ -87,3 +85,3 @@ if (!this.pendingAcks.length) { | ||
const waitMessage = { | ||
msg_id: this.generateMessageID(), | ||
msg_id: this.generateMessageId(), | ||
seq_no: this.generateSeqNo(), | ||
@@ -100,3 +98,3 @@ body: waitSerializer.getBytes(), | ||
const message = { | ||
msg_id: this.generateMessageID(), | ||
msg_id: this.generateMessageId(), | ||
seq_no: this.generateSeqNo(true), | ||
@@ -112,2 +110,3 @@ body: serializer.getBytes(), | ||
this.updateSession(); | ||
this.setUrl(); | ||
} | ||
@@ -130,3 +129,3 @@ | ||
request.storeMethod('req_pq_multi', { nonce }); | ||
// request.storeMethod('req_pq', { nonce }); | ||
return this.sendPlainRequest(request.getBuffer()).then(deserializer => { | ||
@@ -529,3 +528,3 @@ const responsePQ = deserializer.fetchObject('ResPQ'); | ||
const containerSentMessage = { | ||
msg_id: this.generateMessageID(), | ||
msg_id: this.generateMessageId(), | ||
seq_no: this.generateSeqNo(true), | ||
@@ -567,3 +566,3 @@ container: true, | ||
data.storeIntBytes(serverSalt, 64, 'salt'); | ||
data.storeIntBytes(this.sessionID, 64, 'session_id'); | ||
data.storeIntBytes(this.sessionId, 64, 'session_id'); | ||
@@ -664,4 +663,4 @@ data.storeLong(message.msg_id, 'message_id'); | ||
if ( | ||
!bytesCmp(serverSessionID, this.sessionID) && | ||
(!this.prevSessionID || !bytesCmp(this.sessionID, this.prevSessionID)) | ||
!bytesCmp(serverSessionID, this.sessionId) && | ||
(!this.prevSessionId || !bytesCmp(this.sessionId, this.prevSessionId)) | ||
) { | ||
@@ -671,4 +670,4 @@ console.warn( | ||
serverSessionID, | ||
this.sessionID, | ||
this.prevSessionID | ||
this.sessionId, | ||
this.prevSessionId | ||
); | ||
@@ -755,3 +754,3 @@ throw new Error( | ||
messageID, | ||
sessionID: this.sessionID, | ||
sessionID: this.sessionId, | ||
seqNo, | ||
@@ -769,3 +768,3 @@ messageDeferred: message.deferred.promise, | ||
header.storeLongP(0, 0, 'auth_key_id'); | ||
header.storeLong(this.generateMessageID(), 'msg_id'); | ||
header.storeLong(this.generateMessageId(), 'msg_id'); | ||
header.storeInt(requestLength, 'request_length'); | ||
@@ -828,3 +827,3 @@ | ||
processMessage(message, messageID) { | ||
console.log('processMessage', message, messageID); | ||
// console.log('processMessage', message, messageID); | ||
let sentMessage; | ||
@@ -938,14 +937,4 @@ | ||
if (message.result._ == 'rpc_error') { | ||
// console.log('Rpc error', message.result); | ||
deferred.reject(message.result); | ||
} else { | ||
// var dRes = message.result._; | ||
// if (!dRes) { | ||
// if (message.result.length > 5) { | ||
// dRes = '[..' + message.result.length + '..]'; | ||
// } else { | ||
// dRes = message.result; | ||
// } | ||
// } | ||
// console.log('Rpc response', dRes); | ||
deferred.resolve(message.result); | ||
@@ -963,4 +952,6 @@ } | ||
default: | ||
// console.log('default', message); | ||
this.ackMessage(messageID); | ||
//console.log('default', message); | ||
this.emit(message._, message); | ||
// console.log('processMessage', message, messageID); | ||
break; | ||
@@ -998,3 +989,3 @@ } | ||
this.lastMessageID = [0, 0]; | ||
this.lastMessageId = [0, 0]; | ||
this.timeOffset = newTimeOffset; | ||
@@ -1014,5 +1005,5 @@ | ||
updateSession() { | ||
this.prevSessionID = this.sessionID; | ||
this.sessionID = new Array(8); | ||
secureRandom.nextBytes(this.sessionID); | ||
this.prevSessionId = this.sessionId; | ||
this.sessionId = new Array(8); | ||
secureRandom.nextBytes(this.sessionId); | ||
this._seqNo = 0; | ||
@@ -1043,3 +1034,3 @@ } | ||
generateMessageID() { | ||
generateMessageId() { | ||
const timeTicks = tsNow(); | ||
@@ -1050,16 +1041,16 @@ const timeSec = Math.floor(timeTicks / 1000) + this.timeOffset; | ||
const { lastMessageID } = this; | ||
const { lastMessageId } = this; | ||
let messageID = [timeSec, (timeMSec << 21) | (random << 3) | 4]; | ||
let messageId = [timeSec, (timeMSec << 21) | (random << 3) | 4]; | ||
if ( | ||
lastMessageID[0] > messageID[0] || | ||
(lastMessageID[0] == messageID[0] && lastMessageID[1] >= messageID[1]) | ||
lastMessageId[0] > messageId[0] || | ||
(lastMessageId[0] == messageId[0] && lastMessageId[1] >= messageId[1]) | ||
) { | ||
messageID = [lastMessageID[0], lastMessageID[1] + 4]; | ||
messageId = [lastMessageId[0], lastMessageId[1] + 4]; | ||
} | ||
this.lastMessageID = messageID; | ||
this.lastMessageId = messageId; | ||
return longFromInts(messageID[0], messageID[1]); | ||
return longFromInts(messageId[0], messageId[1]); | ||
} | ||
@@ -1072,2 +1063,34 @@ | ||
setUrl(dcId = 2) { | ||
const subdomainsMap = { | ||
1: 'pluto', | ||
2: 'venus', | ||
3: 'aurora', | ||
4: 'vesta', | ||
5: 'flora', | ||
}; | ||
const ipMap = this.test | ||
? { | ||
1: '149.154.175.10', | ||
2: '149.154.167.40', | ||
3: '149.154.175.117', | ||
} | ||
: { | ||
1: '149.154.175.50', | ||
2: '149.154.167.51', | ||
3: '149.154.175.100', | ||
4: '149.154.167.91', | ||
5: '149.154.171.5', | ||
}; | ||
const urlPath = this.test ? '/apiw_test1' : '/apiw1'; | ||
if (this.https) { | ||
this.url = `https://${subdomainsMap[dcId]}.web.telegram.org${urlPath}`; | ||
} else { | ||
this.url = `http://${ipMap[dcId]}${urlPath}`; | ||
} | ||
} | ||
runLongPoll() { | ||
@@ -1079,7 +1102,4 @@ if (this.longPollRunning) { | ||
const self = this; | ||
(function longPollInner() { | ||
//console.log('long poll'); | ||
var serializer = new TLSerialization({ mtproto: true }); | ||
const longPollInner = () => { | ||
const serializer = new TLSerialization({ mtproto: true }); | ||
serializer.storeMethod('http_wait', { | ||
@@ -1091,20 +1111,21 @@ max_delay: 500, | ||
var messageID = self.generateMessageID(); | ||
var seqNo = self.generateSeqNo(); | ||
var message = { | ||
msg_id: messageID, | ||
seq_no: seqNo, | ||
const message = { | ||
msg_id: this.generateMessageId(), | ||
seq_no: this.generateSeqNo(), | ||
body: serializer.getBytes(), | ||
}; | ||
self.sendEncryptedRequest(message).finally(longPollInner); | ||
})(); | ||
this.sendEncryptedRequest(message).finally(longPollInner); | ||
}; | ||
longPollInner(); | ||
} | ||
getApiCallMessage(method, params = {}, options = {}) { | ||
const serializer = new TLSerialization(options); | ||
getApiCallMessage(method, params = {}) { | ||
const serializer = new TLSerialization(); | ||
serializer.storeInt(config.invokeWithLayer, 'invokeWithLayer'); | ||
serializer.storeInt(config.layer, 'layer'); | ||
serializer.storeInt(config.initConnection, 'initConnection'); | ||
serializer.storeInt(0xda9b0d0d, 'invokeWithLayer'); | ||
serializer.storeInt(108, 'layer'); | ||
serializer.storeInt(0x785188b8, 'initConnection'); | ||
serializer.storeInt(0, 'flags'); // because the proxy is not set | ||
serializer.storeInt(this.api_id, 'api_id'); | ||
@@ -1124,8 +1145,11 @@ serializer.storeString( | ||
options.resultType = serializer.storeMethod(method, params); | ||
serializer.storeMethod(method, { | ||
api_hash: this.api_hash, | ||
api_id: this.api_id, | ||
...params, | ||
}); | ||
let toAck = []; //msgs_ack | ||
const msg_id = this.generateMessageID(); | ||
const message = { | ||
msg_id, | ||
msg_id: this.generateMessageId(), | ||
seq_no: this.generateSeqNo(), | ||
@@ -1142,23 +1166,15 @@ body: serializer.getBytes(true), | ||
apiCall(method, params = {}, options = {}) { | ||
const message = this.getApiCallMessage(method, params, options); | ||
this.sendAcks(); | ||
call(method, params) { | ||
return this.init().then(() => { | ||
const message = this.getApiCallMessage(method, params); | ||
this.sendAcks(); | ||
return new Promise((resolve, reject) => { | ||
this.sendEncryptedRequest(message) | ||
.then(response => { | ||
const { messageDeferred } = response; | ||
messageDeferred.then(resolve); | ||
messageDeferred.catch(reject); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
call(method, data) { | ||
return this.init().then(() => { | ||
return this.apiCall(method, { | ||
api_hash: this.api_hash, | ||
api_id: this.api_id, | ||
...data, | ||
return new Promise((resolve, reject) => { | ||
this.sendEncryptedRequest(message) | ||
.then(response => { | ||
const { messageDeferred } = response; | ||
messageDeferred.then(resolve); | ||
messageDeferred.catch(reject); | ||
}) | ||
.catch(reject); | ||
}); | ||
@@ -1165,0 +1181,0 @@ }); |
@@ -9,9 +9,2 @@ const apiSchema = require('../scheme/api'); | ||
}, | ||
layer: 74, | ||
invokeWithLayer: 0xda9b0d0d, | ||
initConnection: 0xc7481da6, | ||
invokeAfterMsg: 0xcb9f372d, | ||
app: {}, | ||
}; |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
478088
16613
9
6
+ Addedevents@3.1.0
+ Addedevents@3.1.0(transitive)