2017-01-14 02:04:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports.respond = function (socket, packets, rinfo) {
|
2017-05-22 20:14:18 +00:00
|
|
|
var dns = require('../');
|
2017-01-14 02:04:27 +00:00
|
|
|
var os = require('os');
|
|
|
|
var queryname = '_cloud._tcp.local';
|
|
|
|
|
|
|
|
console.log(packets);
|
|
|
|
|
|
|
|
packets.forEach(function (packet) {
|
2017-05-22 20:14:18 +00:00
|
|
|
// Only respond to queries, otherwise we'll end up responding to ourselves forever.
|
|
|
|
if (packet.header.qr !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-14 02:04:27 +00:00
|
|
|
packet.question.forEach(function (q) {
|
|
|
|
if (queryname !== q.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('question', q.name, q.typeName, q.className, q.flag, q);
|
2017-05-22 20:14:18 +00:00
|
|
|
var rpacket = {
|
|
|
|
header: {
|
|
|
|
id: packet.header.id
|
|
|
|
, qr: 1
|
|
|
|
, opcode: 0
|
|
|
|
, aa: 1
|
|
|
|
, tc: 0
|
|
|
|
, rd: 0
|
|
|
|
, ra: 0
|
|
|
|
, res1: 0
|
|
|
|
, res2: 0
|
|
|
|
, res3: 0
|
|
|
|
, rcode: 0
|
|
|
|
, }
|
|
|
|
, question: [q]
|
|
|
|
, answer: []
|
|
|
|
, authority: []
|
|
|
|
, additional: []
|
|
|
|
, edns_options: []
|
|
|
|
};
|
2017-05-22 21:05:48 +00:00
|
|
|
|
|
|
|
var myRndId = 'be1af7a';
|
|
|
|
|
|
|
|
rpacket.answer.push({
|
|
|
|
name: q.name
|
|
|
|
, typeName: 'PTR'
|
|
|
|
, ttl: 10
|
|
|
|
, className: 'IN'
|
|
|
|
, data: myRndId + '.' + queryname
|
|
|
|
});
|
|
|
|
|
2017-01-14 02:04:27 +00:00
|
|
|
var ifaces = os.networkInterfaces();
|
|
|
|
//var llRe = /^(fe80|169)/i; // link-local
|
|
|
|
Object.keys(ifaces).forEach(function (iname) {
|
|
|
|
var iface = ifaces[iname];
|
|
|
|
|
|
|
|
iface = iface.filter(function (pface) {
|
|
|
|
// nix loopback, internal and ipv6 link-local (non-routable) and ipv4 link-local
|
|
|
|
return !pface.internal;// && !(pface.scopeid > 1) && !llRe.test(pface.address);
|
|
|
|
});
|
|
|
|
|
|
|
|
iface.forEach(function (pface) {
|
|
|
|
rpacket.additional.push({
|
2017-05-22 21:05:48 +00:00
|
|
|
name: myRndId + '.' + q.name
|
2017-05-22 20:14:18 +00:00
|
|
|
, typeName: ('IPv4' === pface.family ? 'A' : 'AAAA')
|
2017-01-14 02:04:27 +00:00
|
|
|
, ttl: 10
|
2017-05-22 20:14:18 +00:00
|
|
|
, className: 'IN'
|
2017-01-14 02:04:27 +00:00
|
|
|
, address: pface.address // '_workstation._tcp.local'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
rpacket.additional.push({
|
|
|
|
name: myRndId + '.' + queryname
|
2017-05-22 20:14:18 +00:00
|
|
|
, typeName: 'SRV'
|
2017-01-14 02:04:27 +00:00
|
|
|
, ttl: 10
|
2017-05-22 20:14:18 +00:00
|
|
|
, className: 'IN'
|
|
|
|
, priority: 1
|
2017-01-14 02:04:27 +00:00
|
|
|
, weight: 0
|
|
|
|
, port: 443
|
2017-05-22 20:14:18 +00:00
|
|
|
, target: myRndId + ".local"
|
2017-01-14 02:04:27 +00:00
|
|
|
});
|
|
|
|
rpacket.additional.push({
|
|
|
|
name: myRndId + '.' + '_device-info._tcp.local'
|
2017-05-22 20:14:18 +00:00
|
|
|
, typeName: 'TXT'
|
2017-01-14 02:04:27 +00:00
|
|
|
, ttl: 10
|
2017-05-22 20:14:18 +00:00
|
|
|
, className: 'IN'
|
2017-01-14 02:04:27 +00:00
|
|
|
, data: ["model=CloudHome1,1", "dappsvers=1"]
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('');
|
|
|
|
console.log('START JSON PACKET');
|
|
|
|
console.log(rpacket);
|
2017-05-22 20:14:18 +00:00
|
|
|
var buf = dns.DNSPacket.write(rpacket);
|
2017-01-14 02:04:27 +00:00
|
|
|
console.log(buf.toString('hex'));
|
|
|
|
console.log('END JSON PACKET');
|
|
|
|
console.log('');
|
|
|
|
|
|
|
|
console.log('');
|
|
|
|
console.log('START DNS PACKET');
|
|
|
|
var pkt = dns.DNSPacket.parse(buf);
|
|
|
|
console.log(pkt);
|
|
|
|
console.log('END DNS PACKET');
|
|
|
|
console.log('');
|
|
|
|
socket.send(buf, rinfo.port, rinfo.address);
|
|
|
|
});
|
2017-05-22 20:14:18 +00:00
|
|
|
|
2017-01-14 02:04:27 +00:00
|
|
|
packet.answer.forEach(function (a) {
|
|
|
|
console.log('answer', a.name, a.typeName, a.className, a.flag, a);
|
|
|
|
});
|
|
|
|
packet.authority.forEach(function (a) {
|
|
|
|
console.log('authority', a.name, a.typeName, a.className, a.flag, a);
|
|
|
|
});
|
|
|
|
packet.additional.forEach(function (a) {
|
|
|
|
console.log('additional', a.name, a.typeName, a.className, a.flag, a);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
console.log('\n');
|
|
|
|
};
|