forked from DataSoft/Nova
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassifiers.js
127 lines (97 loc) · 3.22 KB
/
classifiers.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var fs = require('fs');
// Constructor should be passed the honeyd config binding object
var Classifiers = function(config) {
this.config = config
}
Classifiers.prototype = {
saveClassifier: function(c, index) {
var classifiers = this.getClassifiers();
if (index < classifiers.length && index >= 0) {
// If this is editin an existing index
classifiers[index] = c;
} else {
// Otherwise it's a new classifier
classifiers.push(c);
}
this.setClassifiers(classifiers);
},
deleteClassifier: function(index) {
var classifiers = this.getClassifiers();
classifiers.splice(index, 1);
this.setClassifiers(classifiers);
},
setClassifiers: function(classifierObjects) {
var engines = "";
var engineConfigs = "";
var engineModes = "";
var engineWeights = "";
for (var i = 0; i < classifierObjects.length; i++) {
var config = '/config/CE_' + i + '.config';
if (i != 0) {
engines += ';';
engineConfigs += ';';
engineModes += ';';
engineWeights += ';';
}
engines += classifierObjects[i].type;
engineConfigs += config;
engineModes += classifierObjects[i].mode;
engineWeights += classifierObjects[i].weight;
var stringsData = "";
for (var key in classifierObjects[i].strings) {
stringsData += key + " " + classifierObjects[i].strings[key] + "\n";
}
try {
fs.writeFileSync(this.config.GetPathHome() + '/' + config, stringsData, 'utf8');
} catch (err) {
console.log("ERROR WRITING: " + err);
}
}
this.config.WriteSetting("CLASSIFICATION_ENGINES", engines);
this.config.WriteSetting("CLASSIFICATION_CONFIGS", engineConfigs);
this.config.WriteSetting("CLASSIFICATION_MODES", engineModes);
this.config.WriteSetting("CLASSIFICATION_WEIGHTS", engineWeights);
},
getClassifiers: function() {
// Split out the classifiers into objects
var engines = this.config.ReadSetting("CLASSIFICATION_ENGINES").split(';');
var engineConfigs = this.config.ReadSetting("CLASSIFICATION_CONFIGS").split(';');
var engineModes = this.config.ReadSetting("CLASSIFICATION_MODES").split(';');
var engineWeights = this.config.ReadSetting("CLASSIFICATION_WEIGHTS").split(';');
var engineObjects = [];
// Split returns an array with an empty string if empty string is passed in (wtf Javascript)
if (engines.length === 1 && engines[0] === "") {
return engineObjects;
}
for (var i = 0; i < engines.length; i++) {
var obj = {};
obj.type = engines[i];
var configInstance = engineConfigs[i];
obj.mode = engineModes[i];
obj.weight = engineWeights[i];
obj.strings = {};
try {
var stringsData = fs.readFileSync(this.config.GetPathHome() + '/' + configInstance, 'utf8').split("\n");
} catch (err) {
console.log("ERROR: " + err);
}
for (var stringLine in stringsData) {
var line = stringsData[stringLine];
if (line == "") continue;
if (line[0] == "#") continue;
obj.strings[line.substring(0, line.indexOf(" "))] = line.substring(line.indexOf(" ") + 1);
}
engineObjects.push(obj);
}
return engineObjects;
},
getClassifier: function(index) {
var classifiers = this.getClassifiers();
if (classifiers.length > index) {
return classifiers[index];
} else {
return;
}
}
};
module.exports = Classifiers;