dns-suite.js/bin/mdns-capture.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-09-27 23:29:27 +00:00
#!/usr/bin/env node
2017-01-21 01:15:16 +00:00
'use strict';
// pass a terminal arg
2017-01-21 10:16:08 +00:00
var type = process.argv[2];
2017-01-21 01:15:16 +00:00
var count = parseInt(process.argv[3]) || 0;
if (!type) {
2017-09-27 23:29:27 +00:00
console.error("Usage: mdns-capture.js <filename-prefix> [start-number]");
console.error("Example: mdns-capture.js _service 0");
console.error("Output: _service-0.mdns.bin");
2017-01-21 01:15:16 +00:00
process.exit(1);
}
var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs'));
var dgram = require('dgram');
var server = dgram.createSocket({
type: 'udp4'
, reuseAddr: true
});
var handlers = {};
handlers.onError = function (err) {
console.error("error:", err.stack);
server.close();
};
handlers.onMessage = function (buffer) {
2017-01-21 21:44:37 +00:00
var path = require('path');
2017-01-21 22:07:10 +00:00
var filename = type + '-' + count + '.mdns.bin';
var fullpath = path.join('samples', filename);
2017-01-21 10:16:08 +00:00
count += 1;
2017-01-21 01:15:16 +00:00
2017-01-21 22:07:10 +00:00
fs.writeFileAsync(fullpath, buffer).then(function () {
console.log('wrote ' + buffer.length + ' bytes to ' + fullpath);
2017-01-21 01:15:16 +00:00
});
};
handlers.onListening = function () {
/*jshint validthis:true*/
var server = this;
console.log(server.address());
2017-09-27 23:29:27 +00:00
2017-01-21 01:15:16 +00:00
server.setBroadcast(true);
server.addMembership('224.0.0.251');
2017-09-27 23:29:27 +00:00
console.log('CTRL+C to quit');
2017-01-21 01:15:16 +00:00
};
server.on('error', handlers.onError);
server.on('message', handlers.onMessage);
server.on('listening', handlers.onListening);
// 53 dns
// 5353 mdns
2017-01-21 21:44:37 +00:00
server.bind(5353);