Node.js:DNS模塊的使用


Nodejs的DNS模塊包涵有關DNS查詢和操作的方法,下面介紹該模塊的基本用法以及實現一個DNS查詢小工具。

1.獲取DNS服務器地址

使用getServers方法,該方法返回一個IP地址組成的數組,如下所示:

const dns = require('dns');
const servers = dns.getServers();
console.log(servers);

返回結果為:

[ '114.114.114.114', '8.8.8.8',
'fec0:0:0:ffff::1', '114.114.114.114',
'8.8.8.8', '114.114.114.114',
'8.8.8.8' ]

2.使用系統特性域名解析獲取IP地址

使用dns.lookup(hostname[, options], callback)方法,options參數包涵以下屬性:

  • family:地址協議族,必須為4或6的整數
  • hints:設置getaddrinfo的標志,dns.ADDRCONFIG 或者 dns.V4MAPPED(ipv4映射成ipv6)
  • all:false(默認),布爾值,如設置為true,則返回IP數組,否則返回單個IP地址

callback回調函數有三個參數(err,address,family),如果options的all屬性設置為true,則只有(err,addresses)參數且addresses為一個數組,數組元素為{address,family}對象。使用如下所示:

dns.lookup('www.baidu.com',(err,address,family)=>{
	if(err) throw err;
	console.log('百度網站的IP地址是:'+address+'地址協議族是:IPV'+family);
});

結果如下:

E:\developmentdocument\nodejsdemo>node dns-example.js
百度網站的IP地址是:14.215.177.37地址協議族是:IPV4

設置options的all為true時,結果如下:

dns.lookup('www.baidu.com',{family:4,all:!0,hints:dns.ADDRCONFIG|dns.V4MAPPED},(err,addresses)=>{
	if(err) throw err;
	addresses.forEach((ele,idx,arr)=>{
		console.log('百度網站的IP地址'+(idx+1)+'是:'+ele.address);
	});
});

結果如下:

E:\developmentdocument\nodejsdemo>node dns-example.js
百度網站的IP地址1是:14.215.177.38
百度網站的IP地址2是:14.215.177.37

3.根據IP和端口獲取主機名

使用dns.lookupService(address, port, callback)方法,該方法依賴getnameinfo底層函數。
callback函數有三個參數(err, hostname, service),service是protocol,為http或https,使用如下所示:

dns.lookupService('127.0.0.1',80,(err,hostname,service)=>{
	if(err) console.log(err);
	console.log('該IP對應的主機為:'+hostname+' 協議為:'+service);
});

結果如下:

E:\developmentdocument\nodejsdemo>node dns-example.js
該IP對應的主機為:www.test.zmx.com 協議為:http

4.使用網絡域名解析獲取IP地址

使用dns.resolve(hostname[, rrtype], callback)方法,rrtype有以下選擇:

  • 'A':IPV4,default
  • 'AAAA':IPV6
  • 'MX' - mail exchange records 郵件交換記錄
  • 'TXT' - text records 域名配置說明
  • 'SRV' - SRV records 服務器提供的服務
  • 'PTR' - PTR records
  • 'NS' - name server records 域名服務器
  • 'CNAME' - canonical name records 別名記錄
  • 'SOA' - start of authority record 起始授權機構
  • 'NAPTR' - name authority pointer record

callback函數有(err, addresses)兩個參數,addresses是一個數組,具體成員需要看具體的rrtype,使用如下所示:

//獲取IPV4
dns.resolve('www.qq.com','A',(err,address)=>{
	if(err) throw err;
	console.log(address);//結果為[ '14.17.32.211', '14.17.42.40', '59.37.96.63' ]
});
//獲取IPV6
dns.resolve('www.qq.com','AAAA',(err,address)=>{
	if(err) throw err;
	console.log(address);//結果為[ '240e:ff:f040:28::a' ]
});
//獲取SOA信息
dns.resolve('www.qq.com','SOA',(err,address)=>{
	if(err) throw err;
	console.log(address);
	//結果為
	{ nsname: 'ns-tel1.qq.com',
      hostmaster: 'webmaster.qq.com',
      serial: 1380440321,
      refresh: 300,
      retry: 600,
      expire: 86400,
      minttl: 300 }
});
//獲取別名CNAME
dns.resolve('www.baidu.com','CNAME',(err,address)=>{
	if(err) throw err;
	console.log(address);//結果為[ 'www.a.shifen.com' ]
});

resovle還存在很多快捷方法,例如:resolve4,resolve6,resolveCname...等等

5.反向域名解析

使用dns.reverse(ip, callback)方法,callback有兩個參數(err, hostnames),hostnames是一個域名數組,使用如下所示:

dns.reverse('114.114.114.114',(err,hostnames)=>{
	if(err) throw err;
	console.log(hostnames);//結果為[ 'public1.114dns.com' ]
});

學完了以上的知識后,可以做個DNS查詢的小工具,如下所示:
第一步,寫個HTML靜態頁面,如下:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>DNS查詢工具</title>
	<style type="text/css">
		html,body{ width: 100%; height: 100%; }
		body{ display: flex; align-items: center; justify-content: center; flex-direction: column; }
		*{ margin:0; padding: 0; }
		ul{ list-style: none; }
		.res{line-height: 24px; color:#333; }
		.clearfix:after{ display: block; content:''; height: 0; visibility: hidden; clear: both;}
		.fl{ float:left; }
		.g-wrap{  display: flex;  width:560px;  height: 40px; }
		.u-list{position: relative; flex:1;  }
		.u-inp{flex:3; border:1px solid #ccc; border-left: none; border-right:none; padding:11px 0 11px 10px;}
		.u-btn{ flex:1; }
		.list{ display: none; position: absolute; left: 0px; top:40px; width: 100%; border:1px solid #ccc; border-top:none; border-bottom:none; box-sizing: content-box; }
		.item{ height: 30px; line-height: 30px; text-align: center; color: #666; border-bottom: 1px solid #ccc; cursor:pointer;}
		.item:hover{ color:#0087dc; }
		.u-list .type{ display: block;  width: 100%;  line-height: 38px; border:1px solid #ccc;  text-align: center; color:#666; text-decoration: none; }
		.u-list .type:after{ content: ''; position: absolute; width:0; height:0; border:8px solid transparent; border-top-color:#ccc; right:4px; top:16px;}
		.u-inp input{ width: 100%; border:none; outline: none; height: 18px; line-height: 18px; color:#666; vertical-align: top; font-size: 14px; }
		.u-btn .btn{ display: block; line-height: 40px; text-align: center; background-color: #0087dc; color:#fff; font-size: 16px; cursor:pointer; transition: background-color .3s;}
		.u-btn .btn:hover{ background-color: #0060b2; }
	</style>
</head>
<body>
	<div id="res" class="res"></div>
	<div class="g-wrap clearfix">
		<div class="u-list fl">
			<a href="javascript:;" class="type" id="type" data-value="A">IPV4</a>
			<ul id="list" class="list">
				<li class="item" data-value="A">IPV4</li>
				<li class="item" data-value="AAAA">IPV6</li>
				<li class="item" data-value="CNAME">CNAME</li>
				<li class="item" data-value="SOA">SOA</li>
			</ul>
		</div>
		<div class="u-inp fl">
			<input type="text" class="host" id="host" placeholder="請輸入域名">
		</div>
		<div class="u-btn fl">
			<span class="btn" id="btn">查詢</span>
		</div>
	</div>
	<script>
		function hide(el){
			el.style.display = 'none';
		}
		function show(el){
			el.style.display = 'block';
		}
		function dealResult(responseText){
			var ips = [],
				result = '';
			ips = JSON.parse(responseText).ips;
			if(Array.isArray(ips)){
				result = ips.length > 0 ? ips.join('<br />') : '沒有查詢到結果';
			}else if({}.toString.call(ips) === '[object Object]'){
				result = JSON.stringify(ips);
			}
			res.innerHTML = result;
		}
		type.addEventListener('click',function(e){
			e.stopPropagation();
			show(list);
		},!1);
		[].slice.call(document.body.querySelectorAll('.item')).forEach(function(el,idx,arr){
			el.addEventListener('click',function(e){
				type.innerText = this.innerText;
				type.dataset.value = this.dataset.value;
			},!1);
		});
		document.body.addEventListener('click',function(e){
			if(list.style.display === 'block'){ hide(list); }
		},!1);
		btn.addEventListener('click',function(e){
			var hostname = host.value.trim(),
				rrtype   = type.dataset.value.toUpperCase();
			if(hostname == '') return;
			if(hostname.indexOf('http://') === 0) hostname = hostname.replace('http://','');
			var xhr = new XMLHttpRequest(),
				method = "POST",
				url = "/dnslookup";

			xhr.open(method, url, true);
			xhr.onreadystatechange = function() {
				if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
					dealResult(xhr.responseText);
				}
			};
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xhr.send('host='+hostname+'&rrtype='+rrtype);
		},!1);
	</script>
</body>
</html>

接着編寫服務端代碼,如下:

var http = require('http'),
	url  = require('url'),
	dns  = require('dns'),
	qs   = require('querystring'),
	fs   = require('fs');

function router(req,res,pathname){
	switch(pathname){
		case '/dnslookup':
			lookup(req,res);
			break;
		default:
			showIndex(req,res);
	}
}

function showIndex(req,res){
	var pagePath = __dirname+'/'+'dns-lookup.html';
	var html = fs.readFileSync(pagePath);
	res.end(html);
}

function lookup(req,res){
	var postData = '';
	req.on('data',function(data){
		postData+=data;
	});
	req.on('end',function(data){
		var json = qs.parse(postData);
		var hostname = json.host;
		var rrtype = json.rrtype;
		dns.resolve(hostname,rrtype,function(err,adresses){
			if(err){
				res.end(JSON.stringify({errcode:1,ips:[]}));
			}
			res.end(JSON.stringify({errcode:0,ips:adresses}));
		});
		
	});
}

http.createServer(function(req,res){
    var pathname = url.parse(req.url).pathname;
    req.setEncoding("utf8");
    res.writeHead(200,{'Content-Type':'text/html'});
    router(req,res,pathname);
}).listen(3000,'127.0.0.1');

運行效果如下:

到此這個小工具便完成了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM