-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (87 loc) · 3.5 KB
/
app.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
const express = require('express');
var fs = require('fs');
const app = express();
const httpServer = require("http").createServer(app);
const port = 8080;
const io = require("socket.io")(httpServer);
if ((!process.env.DATABASE_TYPE) || (process.env.DATABASE_TYPE = "")) {
//default k8s
var databaseType = "k8s";
} else {
var databaseType = process.env.DATABASE_TYPE;
}
const datastore = require("./databases/" + databaseType + ".js");
//deprecated: used to create initial couchdb databases
//if (databaseType == "couchdb"){
// datastore.initdb();
//}
console.log(datastore);
io.on('connection', function(socket){
socket.hwid = socket.handshake.auth.hwid;
console.log('client connected: ' + socket.hwid);
socket.on('device_enroll', (data) => {
datastore.searchdevice(data.hwid, function(device){
datastore.searchtemplate(data.token, function(templatedb){
if((templatedb) && (templatedb != "template_not_found")){
datastore.searchuser(templatedb.owner, function(userdb){
if(userdb){
buildcfg(data, device, templatedb, userdb, function(devcfg){
if ((!device) || (device == "device_not_found")) {
devcfg.name = templatedb.device_name;
devcfg.description = templatedb.description;
var newdevice = { name: templatedb.device_name,
_id: data.hwid,
hwid: data.hwid,
token: data.token,
description: templatedb.description
};
datastore.insertdb('devices', newdevice);
socket.emit("devicecfg", devcfg);
} else {
socket.emit("devicecfg", devcfg);
}
});
}else{
console.log("user not found.");
}
});
}else{
console.log("token not found.");
}
});
});
});
socket.on('disconnect', reason => {
console.log("client disconnected.");
console.log(reason);
});
socket.on('update_plugins', (updateData) =>{
datastore.setdevicestatus(socket.hwid,updateData);
});
});
io.use((socket, next) => {
console.log("connection incomming");
const token = socket.handshake.auth.token;
console.log("token: " + token);
console.log("hwid: " + socket.handshake.auth.hwid);
datastore.searchtemplate(token, function(template){
console.log(template);
if(template){
datastore.searchuser(template.owner, function(user){
if(user){
next();
}
});
}
});
});
httpServer.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
var buildcfg = function (req, device, template, user, callback){
var deviceconfig = JSON.parse(JSON.stringify(template));
deviceconfig.name = device.name;
deviceconfig.description = device.description;
['_id','_rev','uid','token','owner', 'device_name','credentials'].forEach(e => delete deviceconfig[e]);
return callback(deviceconfig);
}