File: FakeServer.js

package info (click to toggle)
node-mysql 2.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,740 kB
  • sloc: makefile: 4
file content (491 lines) | stat: -rw-r--r-- 13,710 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// An experimental fake MySQL server for tricky integration tests. Expanded
// as needed.

var Buffer          = require('safe-buffer').Buffer;
var common          = require('./common');
var Charsets        = common.Charsets;
var ClientConstants = common.ClientConstants;
var Crypto          = require('crypto');
var Net             = require('net');
var tls             = require('tls');
var Packets         = common.Packets;
var PacketWriter    = common.PacketWriter;
var Parser          = common.Parser;
var Types           = common.Types;
var Errors          = common.Errors;
var EventEmitter    = require('events').EventEmitter;
var Util            = require('util');

module.exports = FakeServer;
Util.inherits(FakeServer, EventEmitter);
function FakeServer() {
  EventEmitter.call(this);

  this._server      = null;
  this._connections = [];
}

FakeServer.prototype.listen = function(port, cb) {
  this._server = Net.createServer(this._handleConnection.bind(this));
  this._server.listen(port, cb);
};

FakeServer.prototype._handleConnection = function(socket) {
  var connection = new FakeConnection(socket);

  if (!this.emit('connection', connection)) {
    connection.handshake();
  }

  this._connections.push(connection);
};

FakeServer.prototype.destroy = function() {
  if (this._server._handle) {
    // close server if listening
    this._server.close();
  }

  // destroy all connections
  this._connections.forEach(function(connection) {
    connection.destroy();
  });
};

Util.inherits(FakeConnection, EventEmitter);
function FakeConnection(socket) {
  EventEmitter.call(this);

  this.database = null;
  this.user     = null;

  this._cipher = null;
  this._socket = socket;
  this._stream = socket;
  this._parser = new Parser({onPacket: this._parsePacket.bind(this)});

  this._expectedNextPacket            = null;
  this._handshakeInitializationPacket = null;
  this._handshakeOptions              = {};

  socket.on('data', this._handleData.bind(this));
}

FakeConnection.prototype.authSwitchRequest = function authSwitchRequest(options) {
  this._sendPacket(new Packets.AuthSwitchRequestPacket(options));
};

FakeConnection.prototype.deny = function deny(message, errno) {
  message = message || 'Access Denied';
  errno   = errno || Errors.ER_ACCESS_DENIED_ERROR;
  this.error(message, errno);
};

FakeConnection.prototype.error = function deny(message, errno) {
  this._sendPacket(new Packets.ErrorPacket({
    message : (message || 'Error'),
    errno   : (errno || Errors.ER_UNKNOWN_COM_ERROR)
  }));
  this._parser.resetPacketNumber();
};

FakeConnection.prototype.handshake = function(options) {
  this._handshakeOptions = options || {};

  var packetOptions = common.extend({
    scrambleBuff1       : Buffer.from('1020304050607080', 'hex'),
    scrambleBuff2       : Buffer.from('0102030405060708090A0B0C', 'hex'),
    serverCapabilities1 : 512, // only 1 flag, PROTOCOL_41
    protocol41          : true
  }, this._handshakeOptions);

  this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket(packetOptions);

  this._sendPacket(this._handshakeInitializationPacket);
};

FakeConnection.prototype.ok = function ok() {
  this._sendPacket(new Packets.OkPacket());
  this._parser.resetPacketNumber();
};

FakeConnection.prototype._sendAuthResponse = function _sendAuthResponse(got, expected) {
  if (expected.toString('hex') === got.toString('hex')) {
    this.ok();
  } else {
    this.deny('expected ' + expected.toString('hex') + ' got ' + got.toString('hex'));
  }

  this._parser.resetPacketNumber();
};

FakeConnection.prototype._sendPacket = function(packet) {
  switch (packet.constructor) {
    case Packets.AuthSwitchRequestPacket:
      this._expectedNextPacket = Packets.AuthSwitchResponsePacket;
      break;
    case Packets.HandshakeInitializationPacket:
      this._expectedNextPacket = Packets.ClientAuthenticationPacket;
      break;
    case Packets.UseOldPasswordPacket:
      this._expectedNextPacket = Packets.OldPasswordPacket;
      break;
    default:
      this._expectedNextPacket = null;
      break;
  }

  var writer = new PacketWriter();
  packet.write(writer);
  this._stream.write(writer.toBuffer(this._parser));
};

FakeConnection.prototype._handleData = function(buffer) {
  this._parser.write(buffer);
};

FakeConnection.prototype._handleQueryPacket = function _handleQueryPacket(packet) {
  var conn = this;
  var match;
  var sql = packet.sql;

  if ((match = /^SELECT ([0-9]+);?$/i.exec(sql))) {
    var num = match[1];

    this._sendPacket(new Packets.ResultSetHeaderPacket({
      fieldCount: 1
    }));

    this._sendPacket(new Packets.FieldPacket({
      catalog    : 'def',
      charsetNr  : Charsets.UTF8_GENERAL_CI,
      default    : '0',
      name       : num,
      protocol41 : true,
      type       : Types.LONG
    }));

    this._sendPacket(new Packets.EofPacket());

    var writer = new PacketWriter();
    writer.writeLengthCodedString(num);
    this._socket.write(writer.toBuffer(this._parser));

    this._sendPacket(new Packets.EofPacket());
    this._parser.resetPacketNumber();
    return;
  }

  if ((match = /^SELECT CURRENT_USER\(\);?$/i.exec(sql))) {
    this._sendPacket(new Packets.ResultSetHeaderPacket({
      fieldCount: 1
    }));

    this._sendPacket(new Packets.FieldPacket({
      catalog    : 'def',
      charsetNr  : Charsets.UTF8_GENERAL_CI,
      name       : 'CURRENT_USER()',
      protocol41 : true,
      type       : Types.VARCHAR
    }));

    this._sendPacket(new Packets.EofPacket());

    var writer = new PacketWriter();
    writer.writeLengthCodedString((this.user || '') + '@localhost');
    this._socket.write(writer.toBuffer(this._parser));

    this._sendPacket(new Packets.EofPacket());
    this._parser.resetPacketNumber();
    return;
  }

  if ((match = /^SELECT SLEEP\(([0-9]+)\);?$/i.exec(sql))) {
    var sec = match[1];
    var time = sec * 1000;

    setTimeout(function () {
      conn._sendPacket(new Packets.ResultSetHeaderPacket({
        fieldCount: 1
      }));

      conn._sendPacket(new Packets.FieldPacket({
        catalog    : 'def',
        charsetNr  : Charsets.UTF8_GENERAL_CI,
        name       : 'SLEEP(' + sec + ')',
        protocol41 : true,
        type       : Types.LONG
      }));

      conn._sendPacket(new Packets.EofPacket());

      var writer = new PacketWriter();
      writer.writeLengthCodedString(0);
      conn._socket.write(writer.toBuffer(conn._parser));

      conn._sendPacket(new Packets.EofPacket());
      conn._parser.resetPacketNumber();
    }, time);
    return;
  }

  if ((match = /^SELECT \* FROM stream LIMIT ([0-9]+);?$/i.exec(sql))) {
    var num = match[1];

    this._writePacketStream(num);
    return;
  }

  if ((match = /^SHOW STATUS LIKE 'Ssl_cipher';?$/i.exec(sql))) {
    this._sendPacket(new Packets.ResultSetHeaderPacket({
      fieldCount: 2
    }));

    this._sendPacket(new Packets.FieldPacket({
      catalog    : 'def',
      charsetNr  : Charsets.UTF8_GENERAL_CI,
      name       : 'Variable_name',
      protocol41 : true,
      type       : Types.VARCHAR
    }));

    this._sendPacket(new Packets.FieldPacket({
      catalog    : 'def',
      charsetNr  : Charsets.UTF8_GENERAL_CI,
      name       : 'Value',
      protocol41 : true,
      type       : Types.VARCHAR
    }));

    this._sendPacket(new Packets.EofPacket());

    var writer = new PacketWriter();
    writer.writeLengthCodedString('Ssl_cipher');
    writer.writeLengthCodedString(this._cipher ? this._cipher.name : '');
    this._stream.write(writer.toBuffer(this._parser));

    this._sendPacket(new Packets.EofPacket());
    this._parser.resetPacketNumber();
    return;
  }

  if (/INVALID/i.test(sql)) {
    this.error('Invalid SQL', Errors.ER_PARSE_ERROR);
    return;
  }

  this.error('Interrupted unknown query', Errors.ER_QUERY_INTERRUPTED);
};

FakeConnection.prototype._parsePacket = function _parsePacket(packetHeader) {
  var Packet = this._determinePacket(packetHeader);
  var packet = new Packet({protocol41: true});

  packet.parse(this._parser);

  switch (Packet) {
    case Packets.AuthSwitchResponsePacket:
      if (!this.emit('authSwitchResponse', packet)) {
        this.deny('No auth response handler');
      }
      break;
    case Packets.ClientAuthenticationPacket:
      this.database = (packet.database || null);
      this.user     = (packet.user || null);

      if (!this.emit('clientAuthentication', packet)) {
        this.ok();
      }
      break;
    case Packets.SSLRequestPacket:
      this._startTLS();
      break;
    case Packets.ComQueryPacket:
      if (!this.emit('query', packet)) {
        this._handleQueryPacket(packet);
      }
      break;
    case Packets.ComPingPacket:
      if (!this.emit('ping', packet)) {
        this.ok();
      }
      break;
    case Packets.ComChangeUserPacket:
      this.database = (packet.database || null);
      this.user     = (packet.user || null);

      if (!this.emit('changeUser', packet)) {
        if (packet.user === 'does-not-exist') {
          this.deny('User does not exist');
          break;
        } else if (packet.database === 'does-not-exist') {
          this.error('Database does not exist', Errors.ER_BAD_DB_ERROR);
          break;
        }

        this.ok();
      }
      break;
    case Packets.ComQuitPacket:
      if (!this.emit('quit', packet)) {
        this._socket.end();
      }
      break;
    default:
      if (!this.emit(packet.constructor.name, packet)) {
        throw new Error('Unexpected packet: ' + Packet.name);
      }
  }
};

FakeConnection.prototype._determinePacket = function _determinePacket(packetHeader) {
  if (this._expectedNextPacket) {
    var Packet = this._expectedNextPacket;

    if (Packet === Packets.ClientAuthenticationPacket) {
      return !this._cipher && (this._parser.peak(1) << 8) & ClientConstants.CLIENT_SSL
        ? Packets.SSLRequestPacket
        : Packets.ClientAuthenticationPacket;
    }

    this._expectedNextPacket = null;

    return Packet;
  }

  if (packetHeader.length === 0) {
    return Packets.EmptyPacket;
  }

  var firstByte = this._parser.peak();
  switch (firstByte) {
    case 0x01: return Packets.ComQuitPacket;
    case 0x03: return Packets.ComQueryPacket;
    case 0x0e: return Packets.ComPingPacket;
    case 0x11: return Packets.ComChangeUserPacket;
    default:
      throw new Error('Unknown packet, first byte: ' + firstByte);
  }
};

FakeConnection.prototype.destroy = function() {
  this._socket.destroy();
};

FakeConnection.prototype._writePacketStream = function _writePacketStream(count) {
  var remaining = count;
  var timer = setInterval(writeRow.bind(this), 20);

  this._socket.on('close', cleanup);
  this._socket.on('error', cleanup);

  this._sendPacket(new Packets.ResultSetHeaderPacket({
    fieldCount: 2
  }));

  this._sendPacket(new Packets.FieldPacket({
    catalog    : 'def',
    charsetNr  : Charsets.UTF8_GENERAL_CI,
    name       : 'id',
    protocol41 : true,
    type       : Types.LONG
  }));

  this._sendPacket(new Packets.FieldPacket({
    catalog    : 'def',
    charsetNr  : Charsets.UTF8_GENERAL_CI,
    name       : 'title',
    protocol41 : true,
    type       : Types.VARCHAR
  }));

  this._sendPacket(new Packets.EofPacket());

  function cleanup() {
    clearInterval(timer);
  }

  function writeRow() {
    if (remaining === 0) {
      cleanup();

      this._socket.removeListener('close', cleanup);
      this._socket.removeListener('error', cleanup);

      this._sendPacket(new Packets.EofPacket());
      this._parser.resetPacketNumber();
      return;
    }

    remaining -= 1;

    var num = count - remaining;
    var writer = new PacketWriter();
    writer.writeLengthCodedString(num);
    writer.writeLengthCodedString('Row #' + num);
    this._socket.write(writer.toBuffer(this._parser));
  }
};

if (tls.TLSSocket) {
  // 0.11+ environment
  FakeConnection.prototype._startTLS = function _startTLS() {
    // halt parser
    this._parser.pause();
    this._socket.removeAllListeners('data');

    // socket <-> encrypted
    var secureContext = tls.createSecureContext(common.getSSLConfig());
    var secureSocket  = new tls.TLSSocket(this._socket, {
      secureContext : secureContext,
      isServer      : true
    });

    // cleartext <-> protocol
    secureSocket.on('data', this._handleData.bind(this));
    this._stream = secureSocket;

    var conn = this;
    secureSocket.on('secure', function () {
      conn._cipher = this.getCipher();
    });

    // resume
    var parser = this._parser;
    process.nextTick(function() {
      var buffer = parser._buffer.slice(parser._offset);
      parser._offset = parser._buffer.length;
      parser.resume();
      secureSocket.ssl.receive(buffer);
    });
  };
} else {
  // pre-0.11 environment
  FakeConnection.prototype._startTLS = function _startTLS() {
    // halt parser
    this._parser.pause();
    this._socket.removeAllListeners('data');

    // inject secure pair
    var credentials = Crypto.createCredentials(common.getSSLConfig());
    var securePair = tls.createSecurePair(credentials, true);
    this._socket.pipe(securePair.encrypted);
    this._stream = securePair.cleartext;
    securePair.cleartext.on('data', this._handleData.bind(this));
    securePair.encrypted.pipe(this._socket);

    var conn = this;
    securePair.on('secure', function () {
      conn._cipher = securePair.cleartext.getCipher();
    });

    // resume
    var parser = this._parser;
    process.nextTick(function() {
      var buffer = parser._buffer.slice(parser._offset);
      parser._offset = parser._buffer.length;
      parser.resume();
      securePair.encrypted.write(buffer);
    });
  };
}