dns-suite.js/cloud-respond.js

106 lines
3.2 KiB
JavaScript

'use strict';
module.exports.respond = function (socket, packets, rinfo) {
var dns = require('dns-js');
var os = require('os');
var queryname = '_cloud._tcp.local';
console.log(packets);
packets.forEach(function (packet) {
packet.question.forEach(function (q) {
if (queryname !== q.name) {
return;
}
console.log('question', q.name, q.typeName, q.className, q.flag, q);
var rpacket = new dns.DNSPacket();
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({
name: q.name
, type: ('IPv4' === pface.family ? dns.DNSRecord.Type.A : dns.DNSRecord.Type.AAAA)
, ttl: 10
, class: dns.DNSRecord.Class.IN
, address: pface.address // '_workstation._tcp.local'
});
});
});
var myRndId = 'be1af7a';
rpacket.answer.push({
name: q.name
, type: dns.DNSRecord.Type.PTR
, ttl: 10
, class: dns.DNSRecord.Class.IN
, data: myRndId + '.' + queryname
});
rpacket.question.push(new dns.DNSRecord(
queryname // Name
, dns.DNSRecord.Type.PTR // Type
, dns.DNSRecord.Class.IN // Class
//, null // TTL
));
rpacket.additional.push({
name: myRndId + '.' + queryname
, type: dns.DNSRecord.Type.SRV
, ttl: 10
, class: dns.DNSRecord.Class.IN
, priority: 0
, weight: 0
, port: 443
, target: myRndId + ".local"
});
rpacket.additional.push({
name: myRndId + '.' + '_device-info._tcp.local'
, type: dns.DNSRecord.Type.TXT
, ttl: 10
, class: dns.DNSRecord.Class.IN
, data: ["model=CloudHome1,1", "dappsvers=1"]
});
rpacket.header.id = packet.header.id;
rpacket.header.aa = 1;
rpacket.header.qr = 1;
rpacket.header.rd = 0;
console.log('');
console.log('START JSON PACKET');
console.log(rpacket);
var buf = dns.DNSPacket.toBuffer(rpacket);
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);
});
/*
*/
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');
};