-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsemantic.js
129 lines (119 loc) · 4.55 KB
/
semantic.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
126
127
128
129
/**********************************
************** INIT **************
**********************************/
const GEPHISRV = 'http://127.0.0.1:8080/workspace2';
const fs = require('graceful-fs');
const request = require('request');
const chokidar = require('chokidar');
const LanguageDetect = require('languagedetect');
const unfluff = require('unfluff');
const lda = require('lda');
var lngDetector = new LanguageDetect();
var limitedRequest = request.defaults({
pool: {
maxSockets: 10
}
});
/**********************************
******** GRAPHER FUNCTIONS *******
**********************************/
// Helper to send node info to Gephi
function AddGephiNode(node, type, callback) {
var jsonToSend = '{"an":{"' + node + '":{"size":10,"Label":"' + node + '","node_type":"' + type + '"}}}';
var options = {
uri: GEPHISRV + '?operation=updateGraph',
method: 'POST',
json: JSON.parse(jsonToSend)
};
request(options, function(error, response, body) {
if (!error) {
return callback(node);
} else {
console.log('[!!!] Gephi link broken...');
return;
}
});
}
// Helper to send edge info to Gephi
function AddGephiEdge(node1, node2, weight, callback) {
var jsonToSend = '{"ae":{"' + node1 + '_' + node2 + '":{"source":"' + node1 + '","target":"' + node2 + '","directed":false,"weight":"' + weight + '"}}}';
var options = {
uri: GEPHISRV + '?operation=updateGraph',
method: 'POST',
json: JSON.parse(jsonToSend)
};
request(options, function(error, response, body) {
if (!error) {
return callback();
} else {
console.log('[!!!] Gephi link broken...');
return;
}
});
}
// Main function
function onionDataAnalysis(onion, onionSnpsht, callback) {
var onionLang = [];
if (onionSnpsht.text != '') {
onionLang = lngDetector.detect(onionSnpsht.text, 1);
} else if (onionSnpsht.description !== undefined) {
onionLang = lngDetector.detect(onionSnpsht.description, 1);
} else if (onionSnpsht.title != '') {
onionLang = lngDetector.detect(onionSnpsht.title, 1);
}
if (onionLang[0] !== undefined) {
console.log('Language : ' + onionLang[0][0] + ' with a probability of : ' + onionLang[0][1]);
var fulldoc = onionSnpsht.title.concat('\n' + onionSnpsht.text);
if (onionSnpsht.description !== undefined) {
fulldoc.concat('\n' + onionSnpsht.description);
}
if (fulldoc != '\n' && fulldoc.split('\n').filter(String) > 5) {
var documents = fulldoc.split('\n').filter(String);
} else {
var documents = fulldoc.match(/[^\.!\?]+[\.!\?]+/g);
}
if (onionLang[0][0] === 'english') {
var onionLDA = lda(documents, 1, 10, ['en']);
return callback(onion, onionLDA);
} else return callback(onion, []);
} else {
console.log('Language : no language detected...');
return callback(onion, []);
}
}
/**********************************
********** MAIN PROGRAMM *********
**********************************/
chokidar.watch('./ScanResults/', {
ignored: /[\/\\]\./
}).on('add', function(path, ev) {
fs.readFile(path, 'utf-8', function(err, content) {
if (err) {
throw err;
}
if (path.endsWith('.json')) {
var scandata = JSON.parse(content);
if (scandata.hasOwnProperty('snapshot')) {
console.log('\n[*] Parsing data for : ' + scandata.hiddenService);
var onionSnpsht = unfluff(scandata.snapshot);
onionDataAnalysis(scandata.hiddenService, onionSnpsht, function(onion, onionLDA) {
if (onionLDA.length > 0) {
AddGephiNode(onion, 'hiddenService', function() {
for (var i in onionLDA) {
var row = onionLDA[i];
for (var j in row) {
var term = row[j];
AddGephiNode(term.term, 'keyword', function(addedNode) {
AddGephiEdge(onion, addedNode, term.probability, function() {
//console.log('edge added ' + addedNode);
});
});
}
}
});
}
});
}
}
});
});