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

Add lookup option to override dns lookups #1574

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -395,6 +395,22 @@ proxyServer.listen(8015);

};
```
* **lookup**: define a custom dns [lookup](https://nodejs.org/docs/latest-v12.x/api/dns.html#dns_dns_lookup_hostname_options_callback) function to use when resolving target/forward hostnames.

Example: add dns caching

```js
const dlc = require('dns-lookup-cache');

module.exports = (req, res, next) => {

proxy.web(req, res, {
target: 'http://example.com',
lookup: dlc.lookup,
}, next);

};
```

**NOTE:**
`options.ws` and `options.ssl` are optional.
Expand Down
23 changes: 23 additions & 0 deletions examples/http/custom-lookup.js
@@ -0,0 +1,23 @@
var colors = require('colors'),
httpProxy = require('../../lib/http-proxy')
dns = require('dns');


httpProxy.createServer({
target: 'http://example.com:80',
changeOrigin: true,

// Define custom dns lookup function
lookup: function (host, options, callback) {
console.log('Looking up', host);

dns.lookup(host, options, function (err, address, family) {
console.log('Result: err:', err, ', address:', address, 'family:', family);

callback(err, address, family);
});
},
}).listen(8003);


console.log('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8003'.yellow);
3 changes: 3 additions & 0 deletions lib/http-proxy/common.js
Expand Up @@ -58,6 +58,9 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
}

if (options.lookup) {
outgoing.lookup = options.lookup
}

outgoing.agent = options.agent || false;
outgoing.localAddress = options.localAddress;
Expand Down
11 changes: 11 additions & 0 deletions test/lib-http-proxy-common-test.js
Expand Up @@ -367,6 +367,17 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.path).to.be('');
});

it('should pass through lookup', function() {
var outgoing = {};
function lookup(hostname, options, callback) {
callback('This is just a test');
}
common.setupOutgoing(outgoing, {
target: 'http://example.com',
lookup: lookup,
}, { url: '' });
expect(outgoing.lookup).to.be(lookup);
});
});

describe('#setupSocket', function () {
Expand Down