greim / hoxy

Web-hacking proxy API for node
http://greim.github.io/hoxy/
MIT License
599 stars 97 forks source link

Use this._reverse instead of opts.reverse #103

Closed vieira closed 6 years ago

vieira commented 6 years ago

To allow to programmatically control the URL we are reverse proxying after creating an instance of the proxy, use the attribute this._reverse instead of the command line opts.reverse.

Example Use Case:

proxy.intercept({
  phase: 'request',
  mimeType: 'text/html',
  as: '$'
}, (req, resp, cycle) => {
  if (resp.$('something') == 'something') {
    proxy._reverse = 'https://www.reddit.com'; // next request will proxy reddit.com instead
  }
});
greim commented 6 years ago

Thanks for your contribution. However, you should be able to leverage the existing API to do this, since reverse is just a fallback in case nobody tells hoxy which host to forward to.

let sendHost = 'foo.com';

proxy.intercept('request', req => req.hostname = sendHost);

proxy.intercept({
  phase: 'response',
  mimeType: 'text/html',
  as: '$'
}, (req, resp, cycle) => {
  if (resp.$('something') == 'something') {
    sendHost = 'www.reddit.com';
  }
});

I'd rather not introduce new API surface to Hoxy itself just to support this use case. However, let me know if I haven't understood your use case.

Cheers!