client_handshake.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var util = require('util');
  2. var Command = require('./command.js');
  3. var Packets = require('../packets/index.js');
  4. var ClientConstants = require('../constants/client.js');
  5. function ClientHandshake(clientFlags)
  6. {
  7. this.handshake = null;
  8. this.clientFlags = clientFlags;
  9. Command.call(this);
  10. }
  11. util.inherits(ClientHandshake, Command);
  12. ClientHandshake.prototype.start = function() {
  13. return ClientHandshake.prototype.handshakeInit;
  14. };
  15. ClientHandshake.prototype.sendSSLRequest = function(connection) {
  16. var sslRequest = new Packets.SSLRequest(this.clientFlags);
  17. connection.writePacket(sslRequest.toPacket());
  18. };
  19. function flagNames(flags) {
  20. var res = [];
  21. for (var c in ClientConstants) {
  22. if (flags & ClientConstants[c])
  23. res.push(c.replace(/_/g, ' ').toLowerCase());
  24. }
  25. return res;
  26. }
  27. ClientHandshake.prototype.sendCredentials = function(connection) {
  28. if (connection.config.debug) {
  29. console.log('Sending handshake packet: flags:%d=(%s)', this.clientFlags,
  30. flagNames(this.clientFlags).join(', '));
  31. }
  32. var handshakeResponse = new Packets.HandshakeResponse({
  33. flags : this.clientFlags,
  34. user : connection.config.user,
  35. database: connection.config.database,
  36. password: connection.config.password,
  37. passwordSha1 : connection.config.passwordSha1,
  38. charsetNumber : connection.config.charsetNumber,
  39. authPluginData1: this.handshake.authPluginData1,
  40. authPluginData2: this.handshake.authPluginData2,
  41. compress: connection.config.compress
  42. });
  43. connection.writePacket(handshakeResponse.toPacket());
  44. };
  45. ClientHandshake.prototype.handshakeInit = function(helloPacket, connection) {
  46. var command = this;
  47. this.on('connect', function(connArgs) {
  48. connection.emit('connect', connArgs);
  49. });
  50. this.on('error', function(err) {
  51. connection._protocolError = err;
  52. connection.emit('error', err);
  53. });
  54. this.handshake = Packets.Handshake.fromPacket(helloPacket);
  55. if (connection.config.debug) {
  56. console.log('Server hello packet: capability flags:%d=(%s)', this.handshake.capabilityFlags,
  57. flagNames(this.handshake.capabilityFlags).join(', '));
  58. }
  59. connection.serverCapabilityFlags = this.handshake.capabilityFlags;
  60. connection.connectionId = this.handshake.connectionId;
  61. var serverSSLSupport = this.handshake.capabilityFlags & ClientConstants.SSL;
  62. // use compression only if requested by client and supported by server
  63. connection.config.compress = connection.config.compress && (this.handshake.capabilityFlags & ClientConstants.COMPRESS);
  64. this.clientFlags = this.clientFlags | connection.config.compress;
  65. if (connection.config.ssl) {
  66. if (!serverSSLSupport)
  67. command.emit('error', new Error('Server does not support secure connnection'));
  68. // send ssl upgrade request and immediately upgrade connection to secure
  69. this.clientFlags |= ClientConstants.SSL;
  70. this.sendSSLRequest(connection);
  71. connection.startTLS(function() {
  72. // after connection is secure
  73. command.sendCredentials(connection);
  74. });
  75. } else {
  76. this.sendCredentials(connection);
  77. }
  78. return ClientHandshake.prototype.handshakeResult;
  79. };
  80. ClientHandshake.prototype.handshakeResult = function(okPacket, connection) {
  81. // error is already checked in base class. Done auth.
  82. connection.authorized = true;
  83. if (connection.config.compress)
  84. connection.packetParser.onPacket = connection.handleCompressedPacket.bind(connection);
  85. // TODO any useful information in ok packet to pass as argument?
  86. connection.emit('connect', true);
  87. return null;
  88. };
  89. module.exports = ClientHandshake;