diff -r 3d4d0e0dd4de -r f48fa3532729 src/lib/app.js --- a/src/lib/app.js Thu Aug 14 19:14:04 2014 +0200 +++ b/src/lib/app.js Thu Aug 14 19:15:12 2014 +0200 @@ -37,6 +37,8 @@ | | | Support: http://list.europalab.com/mailman/mdnsgs/ | | | +| Test: dig @nodeapp.host.tld A realhost.local | +| | ***********************************************************/ // import module dependencies @@ -55,6 +57,24 @@ cleardb(); }); +// format a date and time +function getDateTime() { + var date = new Date(); + var hour = date.getHours(); + hour = (hour < 10 ? '0' : '') + hour; + var min = date.getMinutes(); + min = (min < 10 ? '0' : '') + min; + var sec = date.getSeconds(); + sec = (sec < 10 ? '0' : '') + sec; + var year = date.getFullYear(); + var month = date.getMonth() + 1; + month = (month < 10 ? '0' : '') + month; + var day = date.getDate(); + day = (day < 10 ? '0' : '') + day; + + return year + '.' + month + '.' + day + '-' + hour + ':' + min + ':' + sec; +} + // instantiate a new redis client // http://www.rediscookbook.org/ var rediscli = redisdat.createClient(); @@ -96,16 +116,27 @@ browserv.on('serviceUp', function(service) { //console.log('service up: ', service); //{interfaceIndex: 2, type: {name: 'ssh', protocol: 'tcp', subtypes: [], fullyQualified: true}, replyDomain: 'local.', flags: 2, name: 'hostname-mich', networkInterface: 'eth0', fullname: 'hostname-mich._ssh._tcp.local.', host: 'hostname-mich.local.', port: 22, addresses: ['192.168.1.50']} + rediscli.hget('hostnames', service.host.replace(/\.$/, ''), function (error, value) { + // handle unexpected errors + if (error) throw(error); - // insert one or more IP addresses for each hostname.local. - rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses); + // insert one or more IP addresses for each hostname.local. + if (!value) { // only visit new (or with changed IPs) hosts + var rcmulti = rediscli.multi(); // start a new transaction + rcmulti.hsetnx('hostnames', service.host.replace(/\.$/, ''), service.addresses, function (error, nret) { + if (nret === 1) // database wrote a new entry + console.log(' ' + getDateTime() + ' Detected host: ' + service.host.replace(/\.$/, '') + ' ' + service.addresses); + }); //rcmulti.hsetnx(); + rcmulti.exec(); // flush transaction queue + } + }); }); browserv.on('serviceDown', function(service) { - console.log('service down: ', service); + //console.log('service down: ', service); //FIXME: still need to selectively remove hosts }); browserv.on('serviceChanged', function(service) { - console.log('service changed: ', service); + //console.log('service changed: ', service); //FIXME: still need to selectively update hosts }); browserv.start(); @@ -125,6 +156,17 @@ if (error) throw(error); if (value) { // the db succeeded in finding a match + // FIXME: need to test incoming questions stripping trailing '.' + // FIXME: and adding '.local' to handle cases of non FQDNs. + // FIXME: var found; // = {}; doesnt work unfortunately + // FIXME: if (request.question[0].name == host) + // FIXME: found = host; + // FIXME: else if (request.question[0].name + '.local' == host) + // FIXME: found = host.replace(/\.local$/, ''); + // + // FIXME: replace silly new block with simple 'push(nameinst.A) + // FIXME: since we already know that the host in question exists + // // populate the DNS response with the chosen hostname rediscli.hkeys('hostnames', function (error, replies) { replies.forEach(function (host, index) {