handshake_response.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. const ClientConstants = require('../constants/client.js');
  3. const CharsetToEncoding = require('../constants/charset_encodings.js');
  4. const Packet = require('../packets/packet.js');
  5. const auth41 = require('../auth_41.js');
  6. class HandshakeResponse {
  7. constructor(handshake) {
  8. this.user = handshake.user || '';
  9. this.database = handshake.database || '';
  10. this.password = handshake.password || '';
  11. this.passwordSha1 = handshake.passwordSha1;
  12. this.authPluginData1 = handshake.authPluginData1;
  13. this.authPluginData2 = handshake.authPluginData2;
  14. this.compress = handshake.compress;
  15. this.clientFlags = handshake.flags;
  16. // TODO: pre-4.1 auth support
  17. let authToken;
  18. if (this.passwordSha1) {
  19. authToken = auth41.calculateTokenFromPasswordSha(
  20. this.passwordSha1,
  21. this.authPluginData1,
  22. this.authPluginData2
  23. );
  24. } else {
  25. authToken = auth41.calculateToken(
  26. this.password,
  27. this.authPluginData1,
  28. this.authPluginData2
  29. );
  30. }
  31. this.authToken = authToken;
  32. this.charsetNumber = handshake.charsetNumber;
  33. this.encoding = CharsetToEncoding[handshake.charsetNumber];
  34. this.connectAttributes = handshake.connectAttributes;
  35. }
  36. serializeResponse(buffer) {
  37. const isSet = flag => this.clientFlags & ClientConstants[flag];
  38. const packet = new Packet(0, buffer, 0, buffer.length);
  39. packet.offset = 4;
  40. packet.writeInt32(this.clientFlags);
  41. packet.writeInt32(0); // max packet size. todo: move to config
  42. packet.writeInt8(this.charsetNumber);
  43. packet.skip(23);
  44. const encoding = this.encoding;
  45. packet.writeNullTerminatedString(this.user, encoding);
  46. let k;
  47. if (isSet('PLUGIN_AUTH_LENENC_CLIENT_DATA')) {
  48. packet.writeLengthCodedNumber(this.authToken.length);
  49. packet.writeBuffer(this.authToken);
  50. } else if (isSet('SECURE_CONNECTION')) {
  51. packet.writeInt8(this.authToken.length);
  52. packet.writeBuffer(this.authToken);
  53. } else {
  54. packet.writeBuffer(this.authToken);
  55. packet.writeInt8(0);
  56. }
  57. if (isSet('CONNECT_WITH_DB')) {
  58. packet.writeNullTerminatedString(this.database, encoding);
  59. }
  60. if (isSet('PLUGIN_AUTH')) {
  61. // TODO: pass from config
  62. packet.writeNullTerminatedString('mysql_native_password', 'latin1');
  63. }
  64. if (isSet('CONNECT_ATTRS')) {
  65. const connectAttributes = this.connectAttributes || {};
  66. const attrNames = Object.keys(connectAttributes);
  67. let keysLength = 0;
  68. for (k = 0; k < attrNames.length; ++k) {
  69. keysLength += Packet.lengthCodedStringLength(attrNames[k], encoding);
  70. keysLength += Packet.lengthCodedStringLength(
  71. connectAttributes[attrNames[k]],
  72. encoding
  73. );
  74. }
  75. packet.writeLengthCodedNumber(keysLength);
  76. for (k = 0; k < attrNames.length; ++k) {
  77. packet.writeLengthCodedString(attrNames[k], encoding);
  78. packet.writeLengthCodedString(
  79. connectAttributes[attrNames[k]],
  80. encoding
  81. );
  82. }
  83. }
  84. return packet;
  85. }
  86. toPacket() {
  87. if (typeof this.user !== 'string') {
  88. throw new Error('"user" connection config property must be a string');
  89. }
  90. if (typeof this.database !== 'string') {
  91. throw new Error('"database" connection config property must be a string');
  92. }
  93. // dry run: calculate resulting packet length
  94. const p = this.serializeResponse(Packet.MockBuffer());
  95. return this.serializeResponse(Buffer.alloc(p.offset));
  96. }
  97. static fromPacket(packet) {
  98. const args = {};
  99. args.clientFlags = packet.readInt32();
  100. function isSet(flag) {
  101. return args.clientFlags & ClientConstants[flag];
  102. }
  103. args.maxPacketSize = packet.readInt32();
  104. args.charsetNumber = packet.readInt8();
  105. const encoding = CharsetToEncoding[args.charsetNumber];
  106. args.encoding = encoding;
  107. packet.skip(23);
  108. args.user = packet.readNullTerminatedString(encoding);
  109. let authTokenLength;
  110. if (isSet('PLUGIN_AUTH_LENENC_CLIENT_DATA')) {
  111. authTokenLength = packet.readLengthCodedNumber(encoding);
  112. args.authToken = packet.readBuffer(authTokenLength);
  113. } else if (isSet('SECURE_CONNECTION')) {
  114. authTokenLength = packet.readInt8();
  115. args.authToken = packet.readBuffer(authTokenLength);
  116. } else {
  117. args.authToken = packet.readNullTerminatedString(encoding);
  118. }
  119. if (isSet('CONNECT_WITH_DB')) {
  120. args.database = packet.readNullTerminatedString(encoding);
  121. }
  122. if (isSet('PLUGIN_AUTH')) {
  123. args.authPluginName = packet.readNullTerminatedString(encoding);
  124. }
  125. if (isSet('CONNECT_ATTRS')) {
  126. const keysLength = packet.readLengthCodedNumber(encoding);
  127. const keysEnd = packet.offset + keysLength;
  128. const attrs = {};
  129. while (packet.offset < keysEnd) {
  130. attrs[
  131. packet.readLengthCodedString(encoding)
  132. ] = packet.readLengthCodedString(encoding);
  133. }
  134. args.connectAttributes = attrs;
  135. }
  136. return args;
  137. }
  138. }
  139. module.exports = HandshakeResponse;