Skip to content

exposing proxySocket on socket to support sniffing messages coming from proxy target #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ http.createServer(function (req, res) {

* `error`: The error event is emitted if the request to the target fail.
* `proxyRes`: This event is emitted if the request to the target got a response.
* `proxySocket`: This event is emitted once the proxy websocket was created and piped into the target websocket.

```js
var httpProxy = require('http-proxy');
Expand Down Expand Up @@ -220,6 +221,13 @@ proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});

//
// Listen for the `proxySocket` event on `proxy`.
//
proxy.on('proxySocket', function (proxySocket) {
// listen for messages coming FROM the target here
proxySocket.on('data', hybiParseAndLogMessage);
});
```

#### Using HTTPS
Expand Down
1 change: 1 addition & 0 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ var passes = exports;
return i + ": " + proxyRes.headers[i];
}).join('\r\n') + '\r\n\r\n');
proxySocket.pipe(socket).pipe(proxySocket);
server.emit('proxySocket', proxySocket);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get TypeError: Object false has no method 'emit' on this line in my code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is odd, why would the server be a boolean?
We could add a check to keep in from blowing up:

if (typeof server.emit === 'function') server.emit('proxySocket', proxySocket);

but then it'd still suck cause it would emit only sometimes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may well be a bug in my code. I create an HTTP server separately, and register proxy methods in its handlers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh i missed that, fixed in 1.5.1. Its because when a callback is used on the web or ws method, it doesn't pass the instance into the function. Sub-ideal as it is always an event emitter instance now but it didn't always use to be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thlorenz its because of this which should be changed. the callback mechanics may need to be tweaked in web-incoming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcrugzz thanks for fixing and sorry I missed that myself as well.

});

return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
Expand Down