initial-handshake.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. const Capabilities = require('../../const/capabilities');
  3. const ConnectionInformation = require('../../misc/connection-information');
  4. /**
  5. * Parser server initial handshake.
  6. * see https://mariadb.com/kb/en/library/1-connecting-connecting/#initial-handshake-packet
  7. */
  8. class InitialHandshake {
  9. constructor(packet, info) {
  10. //protocolVersion
  11. packet.skip(1);
  12. info.serverVersion = {};
  13. info.serverVersion.raw = packet.readStringNullEnded();
  14. info.threadId = packet.readUInt32();
  15. let seed1 = packet.readBuffer(8);
  16. packet.skip(1); //reserved byte
  17. let serverCapabilities = BigInt(packet.readUInt16());
  18. //skip characterSet
  19. packet.skip(1);
  20. info.status = packet.readUInt16();
  21. serverCapabilities += BigInt(packet.readUInt16()) << BigInt(16);
  22. let saltLength = 0;
  23. if (serverCapabilities & Capabilities.PLUGIN_AUTH) {
  24. saltLength = Math.max(12, packet.readUInt8() - 9);
  25. } else {
  26. packet.skip(1);
  27. }
  28. if (serverCapabilities & Capabilities.MYSQL) {
  29. packet.skip(10);
  30. } else {
  31. packet.skip(6);
  32. serverCapabilities += BigInt(packet.readUInt32()) << BigInt(32);
  33. }
  34. if (serverCapabilities & Capabilities.SECURE_CONNECTION) {
  35. let seed2 = packet.readBuffer(saltLength);
  36. info.seed = Buffer.concat([seed1, seed2]);
  37. } else {
  38. info.seed = seed1;
  39. }
  40. packet.skip(1);
  41. info.serverCapabilities = serverCapabilities;
  42. /**
  43. * check for MariaDB 10.x replication hack , remove fake prefix if needed
  44. * MDEV-4088: in 10.0+, the real version string maybe prefixed with "5.5.5-",
  45. * to workaround bugs in Oracle MySQL replication
  46. **/
  47. if (info.serverVersion.raw.startsWith('5.5.5-')) {
  48. info.serverVersion.mariaDb = true;
  49. info.serverVersion.raw = info.serverVersion.raw.substring('5.5.5-'.length);
  50. } else {
  51. //Support for MDEV-7780 faking server version
  52. info.serverVersion.mariaDb =
  53. info.serverVersion.raw.includes('MariaDB') ||
  54. (serverCapabilities & Capabilities.MYSQL) === BigInt(0);
  55. }
  56. if (serverCapabilities & Capabilities.PLUGIN_AUTH) {
  57. this.pluginName = packet.readStringNullEnded();
  58. } else {
  59. this.pluginName = '';
  60. }
  61. ConnectionInformation.parseVersionString(info);
  62. }
  63. }
  64. module.exports = InitialHandshake;