6 examples of 'javascript dns lookup' in JavaScript

Every line of 'javascript dns lookup' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
7static lookup(host) {
8 return Promise.resolve(NetAddress.UNSPECIFIED);
9}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

12function dnsLookup (hostname: string): Promise<string> {
13 return new Promise((resolve, reject) => {
14 dns.lookup(hostname, 4, (err, address) => {
15 if (err) return reject(err)
16 resolve(address)
17 })
18 })
19}
95dns.resolve(host, function onLookup(err, addresses, family) {
96 if (err) {
97 return;
98 }
99 var bool = true;
100 addresses.forEach(function (item){
101 if (item == ip) {
102 bool = false;
103 return;
104 }
105 });
106 bool && logHost(host, ip, "被检测到可能配置了系统host:", "warn");
107});
125function dnsResolve(host) {
126 try {
127 return dns.resolve(host, 0).getNextAddrAsString();
128 } catch (e) {
129 return null;
130 }
131}
17async lookup(hostname) {
18 try {
19 let ip = this.cache.get(hostname);
20 if (ip) {
21 return ip;
22 }
23
24 const t = new Date();
25
26 ip = hostname;
27 for (let depth = 0; !_isIP(ip) && depth < 5; depth++) {
28 ip = await this._lookup(ip).catch(error => {
29 logger.debug(error);
30 return ip;
31 });
32 }
33
34 if (!_isIP(ip)) {
35 throw new Error(`BAD IP FORMAT (${ip})`);
36 }
37
38 logger.debug(`[DNS] ${hostname} -> ${ip} (${new Date() - t} ms)`);
39 this.cache.set(hostname, ip);
40 return ip;
41 } catch (error) {
42 logger.debug(`[DNS] cannot resolve hostname ${hostname} (${error})`);
43 }
44}
46async [DNSLOOKUP](url, args) {
47 let parsed;
48 if (typeof url === 'string') {
49 parsed = utils.safeParseURL(url);
50 // invalid url or relative url
51 if (!parsed) return { url, args };
52 } else {
53 parsed = url;
54 }
55 // hostname must exists
56 const hostname = parsed.hostname;
57
58 // don't lookup when hostname is IP
59 if (hostname && IP_REGEX.test(hostname)) {
60 return { url, args };
61 }
62
63 args = args || {};
64 args.headers = args.headers || {};
65 // set host header is not exists
66 if (!args.headers.host && !args.headers.Host) {
67 // host must combine with hostname:port, node won't use `parsed.host`
68 args.headers.host = parsed.port ? `${hostname}:${parsed.port}` : hostname;
69 }
70
71 const record = this.dnsCache.get(hostname);
72 const now = Date.now();
73 if (record) {
74 if (now - record.timestamp >= this.dnsCacheLookupInterval) {
75 // make sure next request don't refresh dns query
76 record.timestamp = now;
77 this[UPDATE_DNS](hostname, args).catch(err => this.app.emit('error', err));
78 }
79
80 return { url: formatDnsLookupUrl(hostname, url, record.ip), args };
81 }
82
83 const address = await this[UPDATE_DNS](hostname, args);
84 return { url: formatDnsLookupUrl(hostname, url, address), args };
85}

Related snippets