A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

12 years ago
  1. var Sequence = require('./Sequence');
  2. var Util = require('util');
  3. var Packets = require('../packets');
  4. var Auth = require('../Auth');
  5. module.exports = Handshake;
  6. Util.inherits(Handshake, Sequence);
  7. function Handshake(config, callback) {
  8. Sequence.call(this, callback);
  9. this._config = config;
  10. this._handshakeInitializationPacket = null;
  11. }
  12. Handshake.prototype.determinePacket = function(firstByte) {
  13. if (firstByte === 0xff) {
  14. return Packets.ErrorPacket;
  15. }
  16. if (!this._handshakeInitializationPacket) {
  17. return Packets.HandshakeInitializationPacket;
  18. }
  19. if (firstByte === 0xfe) {
  20. return Packets.UseOldPasswordPacket;
  21. }
  22. };
  23. Handshake.prototype['HandshakeInitializationPacket'] = function(packet) {
  24. this._handshakeInitializationPacket = packet;
  25. this.emit('packet', new Packets.ClientAuthenticationPacket({
  26. clientFlags : this._config.clientFlags,
  27. maxPacketSize : this._config.maxPacketSize,
  28. charsetNumber : this._config.charsetNumber,
  29. user : this._config.user,
  30. scrambleBuff : Auth.token(this._config.password, packet.scrambleBuff()),
  31. database : this._config.database,
  32. }));
  33. };
  34. Handshake.prototype['UseOldPasswordPacket'] = function(packet) {
  35. if (!this._config.insecureAuth) {
  36. var err = new Error(
  37. 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' +
  38. 'Upgrade the user password or use the {insecureAuth: true} option.'
  39. );
  40. err.code = 'HANDSHAKE_INSECURE_AUTH';
  41. err.fatal = true;
  42. this.end(err);
  43. return;
  44. }
  45. this.emit('packet', new Packets.OldPasswordPacket({
  46. scrambleBuff : Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._config.password),
  47. }));
  48. };
  49. Handshake.prototype['ErrorPacket'] = function(packet) {
  50. var err = this._packetToError(packet, true);
  51. err.fatal = true;
  52. this.end(err);
  53. };