Skip to content
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

Make connect opts available to request listeners #14

Open
papandreou opened this issue Feb 12, 2015 · 2 comments · May be fixed by #33
Open

Make connect opts available to request listeners #14

papandreou opened this issue Feb 12, 2015 · 2 comments · May be fixed by #33

Comments

@papandreou
Copy link
Contributor

I'm implementing a "recording" mode in my testing lib where it would be really useful to know the
originally intended host/port/etc. of the intercepted connection, which were passed as the second parameter to the connect listeners. Right now I can either try to get the host and port number from the Host header, which might not be the correct value, or I can have listeners for both the connect and request events and try to pair them up, which also seems like a fool's errand as they aren't necessarily one-to-one when keep-alive is in play.

Would it be possible to pass the connect opts as the third parameter to the request listeners?

Properly mocking out {req,res}.socket.{remote,local}{Family,Address,Port} would also be really nice, but even if they were, it would be nice to have the original, non-resolved host available.

@vvo
Copy link
Contributor

vvo commented Apr 1, 2015

👍

@jakzo
Copy link

jakzo commented Nov 9, 2018

Just documenting here my hacks to get around this until a proper solution is implemented:

mitm.on('connect', (socket, opts) => {
  let serverSocket = undefined;
  Object.defineProperty(socket, 'serverSocket', {
    set(server) {
      serverSocket = server;
      server._mitm = { client: socket, opts };
    },
    get() {
      return serverSocket;
    },
  });
});

mitm.on('request', (req, res) => {
  const { client, opts } = req.connection._mitm;
});

OR

mitm.on('connect', (socket, opts) => {
  socket._handle.remote._mitm = { client: socket, opts };
});

mitm.on('request', (req, res) => {
  const { client, opts } = req.connection._handle._mitm;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants