Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit f899f77

Browse files
Merge pull request #56 from magcius/fix-whitelist-hang
command_processor: Use the normal state machine for blacklisted clients
2 parents 45d5a39 + 87e5a53 commit f899f77

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

lib/server/command_processor.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ const helpers = require('./../helpers');
22
const config = require('config');
33
const filesize = require('filesize');
44
const consts = require('./../constants');
5-
const Duplex = require('stream').Duplex;
5+
const { Duplex, Writable } = require('stream');
66
const { promisify } = require('util');
77

88
const kSource = Symbol("source");
99
const kCache = Symbol("cache");
1010
const kSendFileQueue = Symbol("sendFileQueue");
1111

12+
class NullStream extends Writable {
13+
_write(chunk, encoding, cb) {
14+
setImmediate(cb);
15+
}
16+
}
17+
1218
class CommandProcessor extends Duplex {
1319

1420
/**
@@ -24,7 +30,6 @@ class CommandProcessor extends Duplex {
2430
putStream: this._handleWrite.bind(this),
2531
command: this._handleCommand.bind(this),
2632
version: this._handleVersion.bind(this),
27-
none: () => Promise.resolve()
2833
};
2934

3035
this._writeHandler = this._writeHandlers.version;
@@ -40,6 +45,7 @@ class CommandProcessor extends Duplex {
4045
this._putWhitelist = this._options.putWhitelist;
4146
this._whitelistEmpty = (!Array.isArray(this._putWhitelist) || !this._putWhitelist.length);
4247

48+
this._nullStream = new NullStream();
4349
this._putStream = null;
4450
this._putSize = 0;
4551
this._putSent = 0;
@@ -344,14 +350,14 @@ class CommandProcessor extends Duplex {
344350

345351
if (this._isWhitelisted(this._trx.clientAddress)) {
346352
this._putStream = await this._trx.getWriteStream(type, size);
347-
this._putStream.promiseWrite = promisify(this._putStream.write).bind(this._putStream);
348-
this._putSize = size;
349-
this._writeHandler = this._writeHandlers.putStream;
350-
}
351-
else {
352-
this._writeHandler = this._writeHandlers.none;
353+
} else {
354+
this._putStream = this._nullStream;
353355
helpers.log(consts.LOG_DBG, `PUT rejected from non-whitelisted IP: ${this._trx.clientAddress}`);
354356
}
357+
358+
this._putStream.promiseWrite = promisify(this._putStream.write).bind(this._putStream);
359+
this._putSize = size;
360+
this._writeHandler = this._writeHandlers.putStream;
355361
}
356362
}
357363

test/command_processor.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("CommandProcessor", () => {
1919
const p = this.cmdProc._onPut("a", 999);
2020
p.catch(function () {});
2121

22-
assert(spy.called)
22+
assert(spy.called);
2323
});
2424

2525
it("should implement PUT when whitelisted (multiple)", async () => {
@@ -33,7 +33,7 @@ describe("CommandProcessor", () => {
3333
const p = this.cmdProc._onPut("a", 999);
3434
p.catch(function () {});
3535

36-
assert(spy.called)
36+
assert(spy.called);
3737
});
3838

3939
it("should implement PUT when whitelist empty", async () => {
@@ -47,29 +47,20 @@ describe("CommandProcessor", () => {
4747
const p = this.cmdProc._onPut("a", 999);
4848
p.catch(function () {});
4949

50-
assert(spy.called)
50+
assert(spy.called);
5151
});
5252

53-
it("should not implement PUT when not whitelisted", async () => {
53+
it("should allow commands after writing when being whitelisted", async () => {
5454
this.cmdProc._whitelistEmpty = false;
5555
this.cmdProc._putWhitelist = ["127.0.0.1"];
5656

5757
this.cmdProc._trx = new PutTransaction();
5858
this.cmdProc._trx.clientAddress = "127.0.0.2";
5959

60-
await this.cmdProc._onPut("a", 999);
61-
assert.strictEqual(this.cmdProc._writeHandler, this.cmdProc._writeHandlers.none);
62-
});
63-
64-
it("should not implement PUT when not whitelisted (multiple)", async () => {
65-
this.cmdProc._whitelistEmpty = false;
66-
this.cmdProc._putWhitelist = ["127.0.0.6", "127.0.0.3", "127.0.0.1"];
67-
68-
this.cmdProc._trx = new PutTransaction();
69-
this.cmdProc._trx.clientAddress = "127.0.0.2";
70-
71-
await this.cmdProc._onPut("a", 999);
72-
assert.strictEqual(this.cmdProc._writeHandler, this.cmdProc._writeHandlers.none);
60+
await this.cmdProc._onPut("a", 6);
61+
assert.strictEqual(this.cmdProc._writeHandler, this.cmdProc._writeHandlers.putStream);
62+
this.cmdProc._writeHandler('abcdef');
63+
assert(this.cmdProc._writeHandlers.command);
7364
});
7465
});
7566
});

0 commit comments

Comments
 (0)