Skip to content

Commit

Permalink
Added option to get TX/RX statistics from handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
gippy committed Jan 9, 2018
1 parent 0f46f3b commit eacb598
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
# editorconfig-tools is unable to ignore longs strings or urls
max_line_length = null
9 changes: 9 additions & 0 deletions src/handler_base.js
Expand Up @@ -170,6 +170,15 @@ export default class HandlerBase extends EventEmitter {
}
}

getStatistics() {
return {
srcTxBytes: this.srcSocket ? this.srcSocket.bytesWritten : undefined,
srcRxBytes: this.srcSocket ? this.srcSocket.bytesRead : undefined,
trgTxBytes: this.trgSocket ? this.trgSocket.bytesWritten : undefined,
trgRxBytes: this.trgSocket ? this.trgSocket.bytesRead : undefined,
};
}

/**
* Detaches all listeners and destroys all sockets.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/handler_forward.js
Expand Up @@ -115,6 +115,8 @@ export default class HandlerForward extends HandlerBase {
this.trgRequest.on('socket', (socket) => {
this.log('Target socket assigned');

this.trgSocket = socket;

socket.once('close', () => {
this.log('Target socket closed');
});
Expand Down
15 changes: 14 additions & 1 deletion src/server.js
Expand Up @@ -131,7 +131,6 @@ export class Server extends EventEmitter {
*/
onConnect(request) {
this.log(`${request.method} ${request.url} HTTP/${request.httpVersion}`);
// console.dir(request.headers);

this.prepareRequestHandling(request)
.then((handlerOpts) => {
Expand Down Expand Up @@ -209,6 +208,7 @@ export class Server extends EventEmitter {
socket.pause();

const funcOpts = {
handlerId: result.id,
request,
username: null,
password: null,
Expand Down Expand Up @@ -256,6 +256,7 @@ export class Server extends EventEmitter {
handler.once('destroy', () => {
delete this.handlers[handler.id];
});

handler.run();
}

Expand Down Expand Up @@ -357,6 +358,18 @@ export class Server extends EventEmitter {
.nodeify(callback);
}


/**
* Calls handler by ID and reads it's usage statistics.
* @return {Object} statistics { srcTxBytes, srcRxBytes, trgTxBytes, trgRxBytes }
*/
getStatisticsForHandler(handlerId) {
const handler = this.handlers && this.handlers[handlerId];
if (!handler) return undefined;

return handler.getStatistics();
}

/**
* Closes the proxy server.
* @param [closeConnections] If true, then all the pending connections from clients
Expand Down
7 changes: 2 additions & 5 deletions test/anonymize_proxy.js
Expand Up @@ -67,9 +67,9 @@ before(() => {
app.listen(testServerPort, (err) => {
if (err) reject(err);
resolve();
})
});
});
})
});
});

after(function () {
Expand All @@ -85,9 +85,6 @@ const requestPromised = (opts) => {
request(opts, (error, response, body) => {
if (error) return reject(error);
if (response.statusCode !== 200) {
//console.log('ERROR VOLE');
// console.dir(response);
//console.dir(body);
return reject(new Error(`Received invalid response code: ${response.statusCode}`));
}
if (opts.expectBodyContainsText) expect(body).to.contain(opts.expectBodyContainsText);
Expand Down
34 changes: 30 additions & 4 deletions test/server.js
Expand Up @@ -114,6 +114,8 @@ const createTestSuite = ({
let upstreamProxyRequestCount = 0;

let mainProxyServer;
let mainProxyServerStatisticsInterval;
const mainProxyServerHandlers = {};
let mainProxyServerPort;
const mainProxyRequestCount = 0;

Expand Down Expand Up @@ -198,7 +200,7 @@ const createTestSuite = ({

if (mainProxyAuth || useUpstreamProxy) {
opts.prepareRequestFunction = ({
request, username, password, hostname, port, isHttp,
request, username, password, hostname, port, isHttp, handlerId
}) => {
const result = {
requestAuthentication: false,
Expand Down Expand Up @@ -245,6 +247,12 @@ const createTestSuite = ({
result.upstreamProxyUrl = upstreamProxyUrl;
}

mainProxyServerHandlers[handlerId] = {
groups: username ? username.replace('groups-', '').split('+') : [],
token: password,
hostname,
};

// Sometimes return a promise, sometimes the result directly
if (counter++ % 2 === 0) return result;
return Promise.resolve(result);
Expand Down Expand Up @@ -275,7 +283,6 @@ const createTestSuite = ({
// Tests for 502 Bad gateway or 407 Proxy Authenticate
// Unfortunately the request library throws for HTTPS and sends status code for HTTP
const testForErrorResponse = (opts, expectedStatusCode) => {

let requestError = null;
const onRequestFailed = (err) => {
requestError = err;
Expand Down Expand Up @@ -433,10 +440,29 @@ const createTestSuite = ({
.then((response) => {
expect(response.body).to.match(/^a{1000000}$/);
expect(response.statusCode).to.eql(200);
const expectedSize = 1000000; // "a" takes one byte, so one 1 milion "a" should be 1MB

// this if is here because some tests do not use prepareRequestFunction and therefore are not
// trackable
if (mainProxyServerHandlers && Object.keys(mainProxyServerHandlers).length) {
const sortedIds = Object.keys(mainProxyServerHandlers).sort((a, b) => {
if (Number(a) < Number(b)) return -1;
if (Number(a) > Number(b)) return 1;
return 0;
});
const lastHandler = sortedIds[sortedIds.length - 1];
const stats = mainProxyServer.getStatisticsForHandler(lastHandler);

// 5% range because network negotiation adds to network trafic
expect(stats.srcTxBytes).to.be.within(expectedSize, expectedSize * 1.05);
expect(stats.trgRxBytes).to.be.within(expectedSize, expectedSize * 1.05);
}
});
};
_it('handles large GET response', test1MAChars);
_it('handles large streamed GET response', test1MAChars);

// TODO: Test streamed GET
// _it('handles large streamed GET response', test1MAChars);

_it('handles 301 redirect', () => {
const opts = getRequestOpts('/redirect-to-hello-world');
Expand Down Expand Up @@ -632,6 +658,7 @@ const createTestSuite = ({

after(function () {
this.timeout(2 * 1000);
if (mainProxyServerStatisticsInterval) clearInterval(mainProxyServerStatisticsInterval);

// Shutdown all servers
return Promise.resolve().then(() => {
Expand Down Expand Up @@ -735,4 +762,3 @@ useSslVariants.forEach((useSsl) => {
});
});
});

0 comments on commit eacb598

Please sign in to comment.