Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node override request IP resolution

I'm struggling to find the correct terminology to accurately phrase this problem, but I'm gonna give it my best shot:

In node.js, is there a way to manually override the IP address when making an HTTP request (e.g. request some-domain.com/whatever and instead of resolving the IP address through DNS, manually provide some IP address 1.2.3.4) ?

This would, effectively speaking, be the equivalent of setting 1.2.3.4 some-domain.com in /etc/hosts

like image 645
CookieMonster Avatar asked Jan 26 '16 23:01

CookieMonster


2 Answers

There is a tiny module that does exactly that: evil-dns.

evilDns.add('foo.com', '1.2.3.4');
like image 52
Aurélien Nicolas Avatar answered Nov 08 '22 01:11

Aurélien Nicolas


Node's http and https modules take an Agent as an argument, and you can override the dns resolver with the lookup parameter.

const http = require("http");
const https = require("https");


const staticLookup = (ip, v) => (hostname, opts, cb) => cb(null, ip, v || 4)
const staticDnsAgent = (scheme, ip) => new require(scheme).Agent({lookup: staticLookup(ip)})

http.get("http://some-domain.com/whatever", {agent: staticDnsAgent('http', '1.2.3.4')})
like image 10
Chad Avatar answered Nov 08 '22 02:11

Chad