prelogin-payload.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _sprintfJs = require("sprintf-js");
  7. var _writableTrackingBuffer = _interopRequireDefault(require("./tracking-buffer/writable-tracking-buffer"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. const optionBufferSize = 20;
  10. const VERSION = 0x000000001;
  11. const SUBBUILD = 0x0001;
  12. const TOKEN = {
  13. VERSION: 0x00,
  14. ENCRYPTION: 0x01,
  15. INSTOPT: 0x02,
  16. THREADID: 0x03,
  17. MARS: 0x04,
  18. FEDAUTHREQUIRED: 0x06,
  19. TERMINATOR: 0xFF
  20. };
  21. const ENCRYPT = {
  22. OFF: 0x00,
  23. ON: 0x01,
  24. NOT_SUP: 0x02,
  25. REQ: 0x03
  26. };
  27. const encryptByValue = {};
  28. for (const name in ENCRYPT) {
  29. const value = ENCRYPT[name];
  30. encryptByValue[value] = name;
  31. }
  32. const MARS = {
  33. OFF: 0x00,
  34. ON: 0x01
  35. };
  36. const marsByValue = {};
  37. for (const name in MARS) {
  38. const value = MARS[name];
  39. marsByValue[value] = name;
  40. }
  41. /*
  42. s2.2.6.4
  43. */
  44. class PreloginPayload {
  45. constructor(bufferOrOptions = {
  46. encrypt: false
  47. }) {
  48. if (bufferOrOptions instanceof Buffer) {
  49. this.data = bufferOrOptions;
  50. this.options = {
  51. encrypt: false
  52. };
  53. } else {
  54. this.options = bufferOrOptions;
  55. this.createOptions();
  56. }
  57. this.extractOptions();
  58. }
  59. createOptions() {
  60. const options = [this.createVersionOption(), this.createEncryptionOption(), this.createInstanceOption(), this.createThreadIdOption(), this.createMarsOption(), this.createFedAuthOption()];
  61. let length = 0;
  62. for (let i = 0, len = options.length; i < len; i++) {
  63. const option = options[i];
  64. length += 5 + option.data.length;
  65. }
  66. length++; // terminator
  67. this.data = Buffer.alloc(length, 0);
  68. let optionOffset = 0;
  69. let optionDataOffset = 5 * options.length + 1;
  70. for (let j = 0, len = options.length; j < len; j++) {
  71. const option = options[j];
  72. this.data.writeUInt8(option.token, optionOffset + 0);
  73. this.data.writeUInt16BE(optionDataOffset, optionOffset + 1);
  74. this.data.writeUInt16BE(option.data.length, optionOffset + 3);
  75. optionOffset += 5;
  76. option.data.copy(this.data, optionDataOffset);
  77. optionDataOffset += option.data.length;
  78. }
  79. this.data.writeUInt8(TOKEN.TERMINATOR, optionOffset);
  80. }
  81. createVersionOption() {
  82. const buffer = new _writableTrackingBuffer.default(optionBufferSize);
  83. buffer.writeUInt32BE(VERSION);
  84. buffer.writeUInt16BE(SUBBUILD);
  85. return {
  86. token: TOKEN.VERSION,
  87. data: buffer.data
  88. };
  89. }
  90. createEncryptionOption() {
  91. const buffer = new _writableTrackingBuffer.default(optionBufferSize);
  92. if (this.options.encrypt) {
  93. buffer.writeUInt8(ENCRYPT.ON);
  94. } else {
  95. buffer.writeUInt8(ENCRYPT.NOT_SUP);
  96. }
  97. return {
  98. token: TOKEN.ENCRYPTION,
  99. data: buffer.data
  100. };
  101. }
  102. createInstanceOption() {
  103. const buffer = new _writableTrackingBuffer.default(optionBufferSize);
  104. buffer.writeUInt8(0x00);
  105. return {
  106. token: TOKEN.INSTOPT,
  107. data: buffer.data
  108. };
  109. }
  110. createThreadIdOption() {
  111. const buffer = new _writableTrackingBuffer.default(optionBufferSize);
  112. buffer.writeUInt32BE(0x00);
  113. return {
  114. token: TOKEN.THREADID,
  115. data: buffer.data
  116. };
  117. }
  118. createMarsOption() {
  119. const buffer = new _writableTrackingBuffer.default(optionBufferSize);
  120. buffer.writeUInt8(MARS.OFF);
  121. return {
  122. token: TOKEN.MARS,
  123. data: buffer.data
  124. };
  125. }
  126. createFedAuthOption() {
  127. const buffer = new _writableTrackingBuffer.default(optionBufferSize);
  128. buffer.writeUInt8(0x01);
  129. return {
  130. token: TOKEN.FEDAUTHREQUIRED,
  131. data: buffer.data
  132. };
  133. }
  134. extractOptions() {
  135. let offset = 0;
  136. while (this.data[offset] !== TOKEN.TERMINATOR) {
  137. let dataOffset = this.data.readUInt16BE(offset + 1);
  138. const dataLength = this.data.readUInt16BE(offset + 3);
  139. switch (this.data[offset]) {
  140. case TOKEN.VERSION:
  141. this.extractVersion(dataOffset);
  142. break;
  143. case TOKEN.ENCRYPTION:
  144. this.extractEncryption(dataOffset);
  145. break;
  146. case TOKEN.INSTOPT:
  147. this.extractInstance(dataOffset);
  148. break;
  149. case TOKEN.THREADID:
  150. if (dataLength > 0) {
  151. this.extractThreadId(dataOffset);
  152. }
  153. break;
  154. case TOKEN.MARS:
  155. this.extractMars(dataOffset);
  156. break;
  157. case TOKEN.FEDAUTHREQUIRED:
  158. this.extractFedAuth(dataOffset);
  159. break;
  160. }
  161. offset += 5;
  162. dataOffset += dataLength;
  163. }
  164. }
  165. extractVersion(offset) {
  166. this.version = {
  167. major: this.data.readUInt8(offset + 0),
  168. minor: this.data.readUInt8(offset + 1),
  169. patch: this.data.readUInt8(offset + 2),
  170. trivial: this.data.readUInt8(offset + 3),
  171. subbuild: this.data.readUInt16BE(offset + 4)
  172. };
  173. }
  174. extractEncryption(offset) {
  175. this.encryption = this.data.readUInt8(offset);
  176. this.encryptionString = encryptByValue[this.encryption];
  177. }
  178. extractInstance(offset) {
  179. this.instance = this.data.readUInt8(offset);
  180. }
  181. extractThreadId(offset) {
  182. this.threadId = this.data.readUInt32BE(offset);
  183. }
  184. extractMars(offset) {
  185. this.mars = this.data.readUInt8(offset);
  186. this.marsString = marsByValue[this.mars];
  187. }
  188. extractFedAuth(offset) {
  189. this.fedAuthRequired = this.data.readUInt8(offset);
  190. }
  191. toString(indent = '') {
  192. return indent + 'PreLogin - ' + (0, _sprintfJs.sprintf)('version:%d.%d.%d.%d %d, encryption:0x%02X(%s), instopt:0x%02X, threadId:0x%08X, mars:0x%02X(%s)', this.version.major, this.version.minor, this.version.patch, this.version.trivial, this.version.subbuild, this.encryption ? this.encryption : 0, this.encryptionString ? this.encryptionString : '', this.instance ? this.instance : 0, this.threadId ? this.threadId : 0, this.mars ? this.mars : 0, this.marsString ? this.marsString : '');
  193. }
  194. }
  195. var _default = PreloginPayload;
  196. exports.default = _default;
  197. module.exports = PreloginPayload;