moll / node-mitm

Intercept and mock outgoing Node.js network TCP connections and HTTP requests for testing. Intercepts and gives you a Net.Socket, Http.IncomingMessage and Http.ServerResponse to test and respond with. Super useful when testing code that hits remote servers.
Other
637 stars 48 forks source link

How to get the request payload? #32

Closed camposfyi closed 7 years ago

camposfyi commented 8 years ago

I need to capture the payload from the request. I'm intercepting calls to LDAP. The opts variable only contains host and port. Is there a way to capture the payload?

moll commented 8 years ago

Hey!

The way TCP works in Node is that it's a stream you need to read from to get the bytes.

Does https://github.com/moll/node-mitm#intercepting-tcp-connections shine light in the right direction? The server object Mitm.js returns works exactly like a regular TCP server documented at https://nodejs.org/api/net.html.

camposfyi commented 8 years ago

Thanks for the info. I was able to get it.

One more thing, when you call socket.bypass(), it seems it doesn't let the request proceed to the original destination (which is not localhost). Is that the case? Is that how to module works? Or is there a way I can let a tcp request proceed to the actual destination? I noticed in your docs you mentioned you can use bypass() to forward it to the original destination (but it's not working for me). I assume it's only for localhost?

moll commented 8 years ago

Are you doing something like the example in https://github.com/moll/node-mitm#bypassing-interception and it's still not getting through? Would you mind sharing that piece of code? It should definitely allow the original request to go through.

camposfyi commented 8 years ago

Theoretically, bypassing every connection should allow my application to run as normal, but it just ends up hanging.

var ldapInterceptor = require("mitm")(); ldapInterceptor.enable();

ldapInterceptor.on('connection', function (socket, opts) { socket.bypass(); });

I also tried inside the 'connection' event callback: socket.on('data', function(data) { socket.bypass(); });

moll commented 8 years ago

I suspect this could be because you're calling bypass in the connection event and not in the connect event. Sorry, they are similarly named, but happen at two different times in the lifecycle.

moll commented 7 years ago

Can I close this or is there anything else, @jac0117?

camposfyi commented 7 years ago

Yes

On Wednesday, August 17, 2016, Andri Möll notifications@github.com wrote:

Can I close this or is there anything else, @jac0117 https://github.com/jac0117?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/moll/node-mitm/issues/32#issuecomment-240339967, or mute the thread https://github.com/notifications/unsubscribe-auth/AFBZeJpr4DA5Fj6a4PmQ5OHmCRYWzCkcks5qgr5jgaJpZM4ItDHD .

moll commented 7 years ago

Love the single answer to a disjunctive question. :D