src/lib/app.js

changeset 10
f48fa3532729
parent 5
ee8de27ff264
equal deleted inserted replaced
0:72a9514ba122 1:74312a39280e
35 | Execute: To start this application, launch it with the | 35 | Execute: To start this application, launch it with the |
36 | script named fork.js: $ ./fork.js | 36 | script named fork.js: $ ./fork.js |
37 | | 37 | |
38 | Support: http://list.europalab.com/mailman/mdnsgs/ | 38 | Support: http://list.europalab.com/mailman/mdnsgs/ |
39 | | 39 | |
40 | Test: dig @nodeapp.host.tld A realhost.local |
41 | |
40 ***********************************************************/ 42 ***********************************************************/
41 43
42 // import module dependencies 44 // import module dependencies
43 var mdnsinst = require('mdns'); 45 var mdnsinst = require('mdns');
44 var redisdat = require('redis'); 46 var redisdat = require('redis');
52 }); 54 });
53 process.on('SIGHUP', function() { 55 process.on('SIGHUP', function() {
54 console.log('SIGHUP: Cleared all database entries at', Date()); 56 console.log('SIGHUP: Cleared all database entries at', Date());
55 cleardb(); 57 cleardb();
56 }); 58 });
59
60 // format a date and time
61 function getDateTime() {
62 var date = new Date();
63 var hour = date.getHours();
64 hour = (hour < 10 ? '0' : '') + hour;
65 var min = date.getMinutes();
66 min = (min < 10 ? '0' : '') + min;
67 var sec = date.getSeconds();
68 sec = (sec < 10 ? '0' : '') + sec;
69 var year = date.getFullYear();
70 var month = date.getMonth() + 1;
71 month = (month < 10 ? '0' : '') + month;
72 var day = date.getDate();
73 day = (day < 10 ? '0' : '') + day;
74
75 return year + '.' + month + '.' + day + '-' + hour + ':' + min + ':' + sec;
76 }
57 77
58 // instantiate a new redis client 78 // instantiate a new redis client
59 // http://www.rediscookbook.org/ 79 // http://www.rediscookbook.org/
60 var rediscli = redisdat.createClient(); 80 var rediscli = redisdat.createClient();
61 rediscli.on('error', function (error) { 81 rediscli.on('error', function (error) {
94 114
95 // common logic for all transports (TCP, UDP, SCTP, etcetera) 115 // common logic for all transports (TCP, UDP, SCTP, etcetera)
96 browserv.on('serviceUp', function(service) { 116 browserv.on('serviceUp', function(service) {
97 //console.log('service up: ', service); 117 //console.log('service up: ', service);
98 //{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']} 118 //{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']}
99 119 rediscli.hget('hostnames', service.host.replace(/\.$/, ''), function (error, value) {
100 // insert one or more IP addresses for each hostname.local. 120 // handle unexpected errors
101 rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses); 121 if (error) throw(error);
122
123 // insert one or more IP addresses for each hostname.local.
124 if (!value) { // only visit new (or with changed IPs) hosts
125 var rcmulti = rediscli.multi(); // start a new transaction
126 rcmulti.hsetnx('hostnames', service.host.replace(/\.$/, ''), service.addresses, function (error, nret) {
127 if (nret === 1) // database wrote a new entry
128 console.log(' ' + getDateTime() + ' Detected host: ' + service.host.replace(/\.$/, '') + ' ' + service.addresses);
129 }); //rcmulti.hsetnx();
130 rcmulti.exec(); // flush transaction queue
131 }
132 });
102 }); 133 });
103 browserv.on('serviceDown', function(service) { 134 browserv.on('serviceDown', function(service) {
104 console.log('service down: ', service); 135 //console.log('service down: ', service);
105 //FIXME: still need to selectively remove hosts 136 //FIXME: still need to selectively remove hosts
106 }); 137 });
107 browserv.on('serviceChanged', function(service) { 138 browserv.on('serviceChanged', function(service) {
108 console.log('service changed: ', service); 139 //console.log('service changed: ', service);
109 //FIXME: still need to selectively update hosts 140 //FIXME: still need to selectively update hosts
110 }); 141 });
111 browserv.start(); 142 browserv.start();
112 } 143 }
113 }); 144 });
123 rediscli.hget('hostnames', request.question[0].name, function (error, value) { 154 rediscli.hget('hostnames', request.question[0].name, function (error, value) {
124 // handle unexpected errors 155 // handle unexpected errors
125 if (error) throw(error); 156 if (error) throw(error);
126 157
127 if (value) { // the db succeeded in finding a match 158 if (value) { // the db succeeded in finding a match
159 // FIXME: need to test incoming questions stripping trailing '.'
160 // FIXME: and adding '.local' to handle cases of non FQDNs.
161 // FIXME: var found; // = {}; doesnt work unfortunately
162 // FIXME: if (request.question[0].name == host)
163 // FIXME: found = host;
164 // FIXME: else if (request.question[0].name + '.local' == host)
165 // FIXME: found = host.replace(/\.local$/, '');
166 //
167 // FIXME: replace silly new block with simple 'push(nameinst.A)
168 // FIXME: since we already know that the host in question exists
169 //
128 // populate the DNS response with the chosen hostname 170 // populate the DNS response with the chosen hostname
129 rediscli.hkeys('hostnames', function (error, replies) { 171 rediscli.hkeys('hostnames', function (error, replies) {
130 replies.forEach(function (host, index) { 172 replies.forEach(function (host, index) {
131 if (request.question[0].name == host) { 173 if (request.question[0].name == host) {
132 rediscli.hget('hostnames', host, function (error, value) { 174 rediscli.hget('hostnames', host, function (error, value) {

mercurial