UNPKG

2.58 kBJavaScriptView Raw
1var dns = require('../dns'),
2 util = require('util');
3
4var question = dns.Question({
5 name: 'www.google.com',
6 type: dns.consts.NAME_TO_QTYPE.A,
7});
8
9var start = new Date().getTime();
10
11var req = dns.Request({
12 question: question,
13 server: { address: '8.8.8.8', port: 53, type: 'udp' },
14 timeout: 1000,
15});
16
17req.on('timeout', function () {
18 console.log('Timeout in making request');
19});
20
21req.on('message', function (err, answer) {
22 console.log(toDig.call(answer));
23});
24
25req.on('end', function () {
26 var delta = (new Date().getTime()) - start;
27 console.log('Finished processing request: ' + delta.toString() + 'ms');
28});
29
30req.send();
31
32toDig = function() {
33 var ret = [], tmp, flags = [];
34
35 tmp = ';; ->>HEADER<<- opcode: ';
36 switch (this.header.opcode) {
37 case 0:
38 tmp += 'QUERY';
39 break;
40 case 1:
41 tmp += 'IQUERY';
42 break;
43 case 2:
44 tmp += 'STATUS';
45 break;
46 default:
47 tmp += 'UNKNOWN';
48 break;
49 }
50 tmp += ', status: ' + dns.consts.RCODE_TO_NAME[this.header.rcode];
51 tmp += ', id: ' + this.header.id;
52 ret.push(tmp);
53
54 tmp = ';; flags: ';
55
56 if (this.header.qr)
57 flags.push('qr');
58 if (this.header.rd)
59 flags.push('rd');
60 if (this.header.aa)
61 flags.push('aa');
62 if (this.header.tc)
63 flags.push('tc');
64 if (this.header.ra)
65 flags.push('ra');
66
67 tmp += flags.join(' ') + ';';
68
69 tmp += ' QUESTON: ' + this.question.length;
70 tmp += ', ANSWER: ' + this.answer.length;
71 tmp += ', AUTHORITY: ' + this.authority.length;
72 tmp += ', ADDITIONAL: ' + this.additional.length;
73
74 ret.push(tmp);
75 ret.push('');
76
77 var pushit = function(p) {
78 ret.push([
79 p.name,
80 dns.consts.QCLASS_TO_NAME[p.class],
81 dns.consts.QTYPE_TO_NAME[p.type],
82 p.address || p.data || '',
83 ].join('\t'));
84 };
85
86 if (this.question.length) {
87 ret.push(';; QUESTION SECTION:');
88 this.question.forEach(function(q) {
89 ret.push('; ' + [q.name,
90 dns.consts.QCLASS_TO_NAME[q.class],
91 dns.consts.QTYPE_TO_NAME[q.type]
92 ].join('\t'));
93 });
94 ret.push('');
95 }
96
97 if (this.answer.length) {
98 ret.push(';; ANSWER SECTION:');
99 this.answer.forEach(pushit);
100 ret.push('');
101 }
102
103 if (this.authority.length) {
104 ret.push(';; AUTHORITY SECTION:');
105 this.authority.forEach(pushit);
106 ret.push('');
107 }
108
109 if (this.additional.length) {
110 if (this.additional[0].type !== dns.consts.NAME_TO_QTYPE.OPT) {
111 ret.push(';; ADDITIONAL SECTION:');
112 this.additional.forEach(pushit);
113 ret.push('');
114 }
115 }
116
117 ret.push(';; END');
118
119 return ret.join('\n');
120};
121