src/app.js

changeset 3
42e83b3431d9
parent 0
eb6d4ce6fd78
equal deleted inserted replaced
0:2efff1978918 1:34809b28628a
43 var mdnsinst = require('mdns'); 43 var mdnsinst = require('mdns');
44 var redisdat = require('redis'); 44 var redisdat = require('redis');
45 var nameinst = require('native-dns'); 45 var nameinst = require('native-dns');
46 46
47 47
48 // install POSIX signal handlers
49 process.on('SIGUSR2', function() {
50 console.log('SIGUSR2: Dumping mDNSGw entries at', Date());
51 rediscli.hgetall('hostnames', function (error, object) {console.dir(object);});
52 });
53 process.on('SIGHUP', function() {
54 console.log('SIGHUP: Cleared all database entries at', Date());
55 cleardb();
56 });
57
48 // instantiate a new redis client 58 // instantiate a new redis client
49 // http://www.rediscookbook.org/ 59 // http://www.rediscookbook.org/
50 var rediscli = redisdat.createClient(); 60 var rediscli = redisdat.createClient();
51 rediscli.on('error', function (error) { 61 rediscli.on('error', function (error) {
52 console.log('Error ' + error); 62 console.log('Error ' + error);
53 }); 63 });
54 64
55 // clear mDNS service keys 65 // clear mDNS service keys
56 rediscli.del('hostnames'); 66 function cleardb () {
57 // this is not working unfortunately for the loop 67 rediscli.del('hostnames');
58 rediscli.keys('*', function (error, replies) { 68 // this is not working unfortunately for the loop
59 replies.forEach(function (reply, ident) { 69 rediscli.keys('*', function (error, replies) {
60 rediscli.del(reply, function (error, value) { 70 replies.forEach(function (reply, ident) {
61 if (error) throw(error); 71 rediscli.del(reply, function (error, value) {
72 if (error) throw(error);
73 });
62 }); 74 });
63 }); 75 });
64 }); 76 }
65 77
66 // scan all advertised mDNS service types 78 // scan all advertised mDNS service types
79 cleardb(); // clear old data first
67 var browsall = mdnsinst.browseThemAll(); 80 var browsall = mdnsinst.browseThemAll();
68 browsall.on('serviceUp', function(service) { 81 browsall.on('serviceUp', function(service) {
69 // iterate through hosts and watch accordingly 82 // iterate through hosts and watch accordingly
70 if (service.type.name.match(/^[a-zA-Z0-9\-]+$/)) { // mdns module hack 83 if (service.type.name.match(/^[a-zA-Z0-9\-]+$/)) { // mdns module hack
71 if (service.type.protocol == 'tcp') { 84 if (service.type.protocol == 'tcp') {
86 99
87 // insert one or more IP addresses for each hostname.local. 100 // insert one or more IP addresses for each hostname.local.
88 rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses); 101 rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses);
89 }); 102 });
90 browserv.on('serviceDown', function(service) { 103 browserv.on('serviceDown', function(service) {
91 //console.log('service down: ', service); 104 console.log('service down: ', service);
92 //FIXME: still need to selectively remove hosts 105 //FIXME: still need to selectively remove hosts
106 });
107 browserv.on('serviceChanged', function(service) {
108 console.log('service changed: ', service);
109 //FIXME: still need to selectively update hosts
93 }); 110 });
94 browserv.start(); 111 browserv.start();
95 } 112 }
96 }); 113 });
97 browsall.start(); 114 browsall.start();
145 // DNS error handler logic 162 // DNS error handler logic
146 nameserv.on('error', function (err, buff, req, res) { 163 nameserv.on('error', function (err, buff, req, res) {
147 console.log('DNS problem: ', err.stack); 164 console.log('DNS problem: ', err.stack);
148 }); 165 });
149 166
150 nameserv.serve(15353); 167 //// debug process user
168 //console.log('Start.');
169 //console.log(process.env.USER);
170 //console.log(process.env.SUDO_USER);
171 //console.log('Done.');
172 //
173 // <1024 must run privileged
174 var nudpport = 53; // default DNS
175 if (nudpport < 1024 && process.getuid() !== 0) {
176 //console.log('Serving on port <1024 from an unprivileged user.\nChange to root if using a privileged port number.')
177 throw new Error('Serving on port <1024 from an unprivileged user.')
178 }
179
180 // start the DNS process
181 nameserv.serve(nudpport, function () {
182 try {
183 console.log('Starting mDNSGw on', Date());
184 process.stdout.write('Old UID: ' + process.getuid() + ', Old GID: ' + process.getgid() + '... ');
185 process.umask('0644');
186 process.setgid('daemon');
187 if (process.env.SUDO_USER)
188 process.setuid(process.env.SUDO_USER);
189 else
190 process.setuid('daemon');
191 console.log('New UID: ' + process.getuid() + ', New GID: ' + process.getgid());
192 } catch (err) {
193 console.log('Cowardly refusing to keep the process alive as root.');
194 process.exit(1);
195 }
196 });
151 197
152 //// debug print all key and value database entries 198 //// debug print all key and value database entries
153 //rediscli.hgetall('hostnames', function (error, object) {console.dir(object);}); 199 //rediscli.hgetall('hostnames', function (error, object) {console.dir(object);});
154 200
155 //// display stored mDNS service data entries 201 //// display stored mDNS service data entries

mercurial