forked from FabMo/FabMo-Engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdetection_daemon.js
89 lines (75 loc) · 2.86 KB
/
detection_daemon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var os=require('os');
var util=require('util');
var EventEmitter = require('events').EventEmitter;
var dgram = require('dgram');
var log = require('./log').logger('detection');
var config = require('./config');
var bonjour = require('bonjour')();
// Direct socket messages
var OK = "YES I M !\0";
var ERR = "I DNT UNDRSTND !\0";
var HOSTNAME = "U NAME ?\0";
var REQ = "R U A SBT ?\0";
var default_port = 24862; // = 7777 without conversion
// Kick off the "detection daemon" which is the process that listens for incoming scans by the FabMo linker
// The detection daemon is what we as a FabMo device run so that we can be discovered by the FabMo linker/dashboard
var start = function(port) {
bonjour.unpublishAll();
bonjour.publish({ name: os.hostname()+" - FabMo Tool Minder daemon", host:os.hostname()+'.local', type: 'fabmo',protocol:'tcp', port: config.engine.get('server_port'),txt : {fabmo:JSON.stringify(getMachineInfo())}});
var socketOpts = {'type':'udp4','reuseAddr':true};
if (process.version.match(/v0\.10\.\d*/i)) {
socketOpts = 'udp4';
}
var socket = dgram.createSocket(socketOpts);
var that = this;
port = port || default_port;
// Listen on the specified port
socket.bind(port);
socket.on("message", function ( data, rinfo ) {
if(data.toString() == REQ) // Respond properly to queries asking if we are a FabMo device
{
//log.debug('scan in progress by '+ rinfo.address);
// Respond indicating that we are a FabMo system
socket.send(new Buffer(OK), 0, OK.length, rinfo.port, rinfo.address, function (err) {
if (err) {
log.error(err);
}
});
}
else if(data.toString() == HOSTNAME) // Respond properly to continued dialog in the autodetect process
{
//log.debug(JSON.stringify(result));
result = getMachineInfo();
// advertise an HTTP server on port 80
socket.send(new Buffer(JSON.stringify(result)), 0, JSON.stringify(result).length, rinfo.port, rinfo.address, function (err) {
if (err) log.error(err);
//console.log("ask info");
});
//that.emit('client_scan',rinfo.address);
}
else
{
log.error("received from "+rinfo.address+" : unknown message : '"+ data.toString() +"'");
}
});
//this.on('newListener', function(listener) {});
};
function getMachineInfo(){
var result = {};
result.hostname= os.hostname();
result.networks=[];
result.server_port = config.engine.get('server_port');
Object.keys(os.networkInterfaces()).forEach(function(key,index,arr){ //val = ip adresses , key = name of interface
var networks_list = this;
networks_list[key].forEach(function(val2,key2,arr2){
if (val2.internal === false && val2.family === 'IPv4')
{
result.networks.push({'interface' : key , 'ip_address' : val2.address});
}
});
},os.networkInterfaces());
return result;
}
//util.inherits(start , EventEmitter);
// detection_daemon.start kicks off the listening process
module.exports = start;