Handshake.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var Sequence = require('./Sequence');
  2. var Util = require('util');
  3. var Packets = require('../packets');
  4. var Auth = require('../Auth');
  5. var ClientConstants = require('../constants/client');
  6. module.exports = Handshake;
  7. Util.inherits(Handshake, Sequence);
  8. function Handshake(options, callback) {
  9. Sequence.call(this, options, callback);
  10. options = options || {};
  11. this._config = options.config;
  12. this._handshakeInitializationPacket = null;
  13. }
  14. Handshake.prototype.determinePacket = function(firstByte) {
  15. if (firstByte === 0xff) {
  16. return Packets.ErrorPacket;
  17. }
  18. if (!this._handshakeInitializationPacket) {
  19. return Packets.HandshakeInitializationPacket;
  20. }
  21. if (firstByte === 0xfe) {
  22. return Packets.UseOldPasswordPacket;
  23. }
  24. };
  25. Handshake.prototype['HandshakeInitializationPacket'] = function(packet) {
  26. this._handshakeInitializationPacket = packet;
  27. this._config.protocol41 = packet.protocol41;
  28. var serverSSLSupport = packet.serverCapabilities1 & ClientConstants.CLIENT_SSL;
  29. if (this._config.ssl) {
  30. if (!serverSSLSupport) {
  31. var err = new Error('Server does not support secure connnection');
  32. err.code = 'HANDSHAKE_NO_SSL_SUPPORT';
  33. err.fatal = true;
  34. this.end(err);
  35. return;
  36. }
  37. this._config.clientFlags |= ClientConstants.CLIENT_SSL;
  38. this.emit('packet', new Packets.SSLRequestPacket({
  39. clientFlags : this._config.clientFlags,
  40. maxPacketSize : this._config.maxPacketSize,
  41. charsetNumber : this._config.charsetNumber
  42. }));
  43. this.emit('start-tls');
  44. } else {
  45. this._sendCredentials();
  46. }
  47. };
  48. Handshake.prototype._tlsUpgradeCompleteHandler = function() {
  49. this._sendCredentials();
  50. };
  51. Handshake.prototype._sendCredentials = function(serverHello) {
  52. var packet = this._handshakeInitializationPacket;
  53. this.emit('packet', new Packets.ClientAuthenticationPacket({
  54. clientFlags : this._config.clientFlags,
  55. maxPacketSize : this._config.maxPacketSize,
  56. charsetNumber : this._config.charsetNumber,
  57. user : this._config.user,
  58. scrambleBuff : (packet.protocol41)
  59. ? Auth.token(this._config.password, packet.scrambleBuff())
  60. : Auth.scramble323(packet.scrambleBuff(), this._config.password),
  61. database : this._config.database,
  62. protocol41 : packet.protocol41
  63. }));
  64. };
  65. Handshake.prototype['UseOldPasswordPacket'] = function(packet) {
  66. if (!this._config.insecureAuth) {
  67. var err = new Error(
  68. 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' +
  69. 'Upgrade the user password or use the {insecureAuth: true} option.'
  70. );
  71. err.code = 'HANDSHAKE_INSECURE_AUTH';
  72. err.fatal = true;
  73. this.end(err);
  74. return;
  75. }
  76. this.emit('packet', new Packets.OldPasswordPacket({
  77. scrambleBuff : Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._config.password),
  78. }));
  79. };
  80. Handshake.prototype['ErrorPacket'] = function(packet) {
  81. var err = this._packetToError(packet, true);
  82. err.fatal = true;
  83. this.end(err);
  84. };