Skip to content

Commit e097d19

Browse files
committed
fixup
1 parent 79eae6f commit e097d19

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed

lib/random-util.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/sender.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@
44

55
const net = require('net');
66
const tls = require('tls');
7-
const { randomFillMask } = require('./random-util');
87

98
const PerMessageDeflate = require('./permessage-deflate');
109
const { EMPTY_BUFFER } = require('./constants');
1110
const { isValidStatusCode } = require('./validation');
1211
const { mask: applyMask, toBuffer } = require('./buffer-util');
12+
const { randomBytes } = require('crypto');
1313

1414
const mask = Buffer.alloc(4);
1515

16+
const RANDOM_POOL_SIZE = 8192;
17+
const RANDOM_POOL_REFRESH = 1024;
18+
19+
let randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
20+
let randomPoolIdx = RANDOM_POOL_SIZE;
21+
let randomPending = true;
22+
23+
function onRandomBytes (err, buf) {
24+
randomPending = false;
25+
if (!err) {
26+
randomPool = buf;
27+
randomPoolIdx = 0;
28+
}
29+
}
30+
randomBytes(RANDOM_POOL_SIZE, onRandomBytes);
31+
1632
/**
1733
* HyBi Sender implementation.
1834
*/
@@ -81,13 +97,21 @@ class Sender {
8197

8298
if (!options.mask) return [target, data];
8399

84-
randomFillMask(mask);
100+
if (randomPool.length - randomPoolIdx < 4) {
101+
randomPool = randomBytes(32);
102+
randomPoolIdx = 0;
103+
}
85104

86105
target[1] |= 0x80;
87-
target[offset - 4] = mask[0];
88-
target[offset - 3] = mask[1];
89-
target[offset - 2] = mask[2];
90-
target[offset - 1] = mask[3];
106+
target[offset - 4] = mask[0] = randomPool[randomPoolIdx++];
107+
target[offset - 3] = mask[1] = randomPool[randomPoolIdx++];
108+
target[offset - 2] = mask[2] = randomPool[randomPoolIdx++];
109+
target[offset - 1] = mask[3] = randomPool[randomPoolIdx++];
110+
111+
if (randomPool.length - randomPoolIdx < RANDOM_POOL_REFRESH && !randomPending) {
112+
randomPending = true;
113+
randomBytes(RANDOM_POOL_SIZE, onRandomBytes);
114+
}
91115

92116
if (merge) {
93117
applyMask(data, mask, target, offset, data.length);

0 commit comments

Comments
 (0)