Session.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { generateRandomLong, getRandomInt } = require('../Helpers')
  2. const fs = require('fs').promises
  3. const { existsSync, readFileSync } = require('fs')
  4. const AuthKey = require('../crypto/AuthKey')
  5. BigInt.toJSON = function() {
  6. return { fool: this.fool.toString('hex') }
  7. }
  8. BigInt.parseJson = function() {
  9. return { fool: BigInt('0x' + this.fool) }
  10. }
  11. class Session {
  12. constructor(sessionUserId) {
  13. this.sessionUserId = sessionUserId
  14. this._serverAddress = null
  15. this._dcId = 0
  16. this._port = null
  17. // this.serverAddress = "localhost";
  18. // this.port = 21;
  19. this.authKey = undefined
  20. this.id = generateRandomLong(false)
  21. this.sequence = 0
  22. this.salt = 0n // Unsigned long
  23. this.timeOffset = 0n
  24. this.lastMessageId = 0n
  25. this.user = undefined
  26. }
  27. /**
  28. * Saves the current session object as session_user_id.session
  29. */
  30. async save() {
  31. if (this.sessionUserId) {
  32. const str = JSON.stringify(this, function(key, value) {
  33. if (typeof value === 'bigint') {
  34. return value.toString() + 'n'
  35. } else {
  36. return value
  37. }
  38. })
  39. console.log('current auth key will be ', this.authKey)
  40. await fs.writeFile(`${this.sessionUserId}.session`, str)
  41. }
  42. }
  43. setDC(dcId, serverAddress, port) {
  44. this._dcId = dcId | 0
  45. this._serverAddress = serverAddress
  46. this._port = port
  47. }
  48. get serverAddress() {
  49. return this._serverAddress
  50. }
  51. get port() {
  52. return this._port
  53. }
  54. get dcId() {
  55. return this._dcId
  56. }
  57. static tryLoadOrCreateNew(sessionUserId) {
  58. if (sessionUserId === undefined) {
  59. return new Session()
  60. }
  61. const filepath = `${sessionUserId}.session`
  62. if (existsSync(filepath)) {
  63. try {
  64. const ob = JSON.parse(readFileSync(filepath, 'utf-8'), function(key, value) {
  65. if (typeof value == 'string' && value.match(/(\d+)n/)) {
  66. return BigInt(value.slice(0, -1))
  67. } else {
  68. return value
  69. }
  70. })
  71. const authKey = new AuthKey(Buffer.from(ob.authKey._key.data))
  72. const session = new Session(ob.sessionUserId)
  73. session._serverAddress = ob._serverAddress
  74. session._port = ob._port
  75. session._dcId = ob._dcId
  76. // this.serverAddress = "localhost";
  77. // this.port = 21;
  78. session.authKey = authKey
  79. session.id = ob.id
  80. session.sequence = ob.sequence
  81. session.salt = ob.salt // Unsigned long
  82. session.timeOffset = ob.timeOffset
  83. session.lastMessageId = ob.lastMessageId
  84. session.user = ob.user
  85. return session
  86. } catch (e) {
  87. return new Session(sessionUserId)
  88. }
  89. } else {
  90. return new Session(sessionUserId)
  91. }
  92. }
  93. getNewMsgId() {
  94. const msTime = new Date().getTime()
  95. let newMessageId =
  96. (BigInt(BigInt(Math.floor(msTime / 1000)) + this.timeOffset) << 32n) |
  97. (BigInt(msTime % 1000) << 22n) |
  98. (BigInt(getRandomInt(0, 524288)) << 2n) // 2^19
  99. if (this.lastMessageId >= newMessageId) {
  100. newMessageId = this.lastMessageId + 4n
  101. }
  102. this.lastMessageId = newMessageId
  103. return newMessageId
  104. }
  105. processEntities(result) {
  106. console.log('saving entities')
  107. }
  108. }
  109. module.exports = Session