This repository was archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
262 lines (212 loc) · 8.36 KB
/
app.js
File metadata and controls
262 lines (212 loc) · 8.36 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'use strict'
const flash = require('connect-flash');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const jsmfjson = require('jsmf-json');
const fs = require('fs');
const multer = require('multer')
const express = require('./bootstrap.js').express;
const app = require('./bootstrap.js').app;
const http = require('./bootstrap.js').http;
const io = require('./bootstrap.js').io;
const log_web_socket = require('./bootstrap.js').log_web_socket;
const buildModel = require('./helper.js').buildModel;
const _ = require('lodash');
const conf = require('./conf.js');
var IC3Proto = require('./conf.js').IC3Proto,
IC3ProtoGrammar = require('./conf.js').IC3ProtoGrammar,
IC3EntryPoint = require('./conf.js').IC3EntryPoint,
BinaryAppProtoBuf = require('./conf.js').BinaryAppProtoBuf
const apk_analyzer = require('./process');
const comparator = require('./comparator.js');
var protoBufModels = require('builder');
// load the default model
protoBufModels.build(IC3Proto, IC3ProtoGrammar, IC3EntryPoint,
BinaryAppProtoBuf);
const listening_port = process.env.PORT || 3000;
app.use(session({
secret: 'sessio0-Id',
saveUninitialized: true,
resave: true
}));
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
//app.set('views', __dirname + '/views'); //default
app.set('view engine', 'ejs');
app.engine('.html', require('ejs').renderFile);
//configure the static content (bower components) and links to views.
//app.use(express.static(__dirname + '/views'));
app.use('/images', express.static(__dirname + '/static/images'));
app.use('/js', express.static(__dirname + '/static/js'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
//configuration of the read file method
var upload = multer({ dest: 'uploads/' });//.array('files', 2);
http.listen(listening_port, function () {
console.log('Listening on port ' + listening_port);
});
app.get('/', function (req, res) {
var errorMessage = JSON.stringify('');
res.render('index.html', {errorMessage: errorMessage });
});
app.get('/sun', function (req, res) {
var M = protoBufModels.model;
var serializedModel = jsmfjson.stringify(M);
var source_code = {};
M.modellingElements['Component']
//.concat(M.modellingElements['Instruction'])
.map(function(component) {
var name;
name = component.name || component.class_name;
if (name) {
var file = conf.bin_outputs + 'jdcmd/' +
name.replace(/\./g, '/') + '.java';
var content;
try {
content = fs.readFileSync(file, 'utf-8')
} catch (err) {
console.log("Error when reading: " + err);
}
source_code[name] = escape(content);
}
});
source_code = JSON.stringify(source_code);
fs.readFile(conf.bin_outputs + 'apk_ast.json', 'utf-8', (err, data) => {
if (err) {
console.log(`APK not found: ${err}`);
data = {};
}
res.render('sunburst.html',{
serializedModel: serializedModel,
sourceCode: source_code,
sourceCodeAST: data
});
});
});
app.get('/s', function(req, res) {
var M = protoBufModels.model;
var serializedModel = jsmfjson.stringify(M);
res.render('graph.html',{serializedModel: serializedModel });
});
app.get('/models', function(req, res){
res.render('modelsbehind.html' );
});
app.post('/upload', upload.array('files[]', 2), function(req, res, next) {
// We can receive 1 or 2 APK through the POST request.
const spawn_sync = require('child_process').spawnSync;
spawn_sync('rm', ['-Rf', conf.bin_outputs]);
apk_analyzer.ICC_models = [];
var generate_ast = true;
if (req.body.generate_ast == 'false') {
generate_ast = false;
}
req.files.map(function(file) {
// A binary protobuf is directly submitted -
// simply relaunch the protobufModel construction.
if (file && file.originalname.split('.').pop() == "dat") {
console.log(`A binary protobuf file has been received: ${file.originalname}`)
protoBufModels.build(IC3Proto, IC3ProtoGrammar, IC3EntryPoint,
file.path);
}
// An APK is submitted - launch the full process.
else if (file && file.originalname.split('.').pop() == "apk") {
log_web_socket(io,
`An APK file has been received: ${file.originalname}`)
apk_analyzer.start_process(file, generate_ast);
}
else {
req.flash('error', `File ignored: ${file.originalname}. ` +
'You must submit a *.apk or *.dat file.');
}
})
return res.status(200).send('process launched in background');
});
app.get('/compare', function(req, res) {
var model1 = false;
var model2 = false;
var source_code_1 = {};
var source_code_2 = {};
// try {
// var model1 = jsmfjson.stringify(apk_analyzer.ICC_models[0]);
// }
// catch (err) {
// console.log(`ICC model not found: ${err}`);
// req.flash('error', `ICC model not found.`);
// }
// try {
// var model2 = jsmfjson.stringify(apk_analyzer.ICC_models[1]);
// }
// catch (err) {
// console.log(`ICC model not found: ${err}`);
// req.flash('error', `ICC model not found.`);
// }
var data = fs.readFileSync(conf.bin_outputs + 'FFE44A8695CCEB4979825C54A29B4CB9765057308A97E49F6C6C5C473DED0AB3.apk.json', 'utf8');
model1 = jsmfjson.parse(data);
model1.modellingElements['Component'].map(function(component) {
var name;
name = component.name || component.class_name;
if (name) {
var file = conf.bin_outputs + 'jdcmd/' +
'FFE44A8695CCEB4979825C54A29B4CB9765057308A97E49F6C6C5C473DED0AB3.apk/' +
name.replace(/\./g, '/') + '.java';
var content;
try {
content = fs.readFileSync(file, 'utf-8')
} catch (err) {
console.log("Error when reading source code: " + err);
}
source_code_1[name] = escape(content);
}
});
source_code_1 = JSON.stringify(source_code_1);
var data = fs.readFileSync(conf.bin_outputs + '1CA20CC2CCF0E981781B25BAD314098A168B6FAF848393A37D19A302A40F3F4C.apk.json', 'utf8');
model2 = jsmfjson.parse(data);
model2.modellingElements['Component'].map(function(component) {
var name;
name = component.name || component.class_name;
if (name) {
var file = conf.bin_outputs + 'jdcmd/' +
'1CA20CC2CCF0E981781B25BAD314098A168B6FAF848393A37D19A302A40F3F4C.apk/' +
name.replace(/\./g, '/') + '.java';
var content;
try {
content = fs.readFileSync(file, 'utf-8')
} catch (err) {
console.log("Error when reading source code: " + err);
}
source_code_2[name] = escape(content);
}
});
source_code_2 = JSON.stringify(source_code_2);
model1 = jsmfjson.stringify(model1);
model2 = jsmfjson.stringify(model2);
// Done during the hackathon: try to get directly the tab from compartor not the map.
if(model1!==undefined && model2 !==undefined) {
var metrics = comparator.compare(jsmfjson.parse(model1),jsmfjson.parse(model2));
var sourceMetricsTab = [];
var targetMetricsTab = [];
var diffMetrics = [];
for (var [key, value] of metrics.sourceMetrics) {
sourceMetricsTab.push({"key":key,"val":value});
}
for (var [key, value] of metrics.targetMetrics) {
targetMetricsTab.push({"key":key,"val":value});
}
//List are ordered the same way...
for(var i in sourceMetricsTab) {
var diffVal = (targetMetricsTab[i].val-sourceMetricsTab[i].val);
diffMetrics.push({"key":sourceMetricsTab[i].key,"val":diffVal});
}
var metricsToSend={sourceMetrics: sourceMetricsTab,targetMetrics:targetMetricsTab,diffMetrics:diffMetrics};
metrics = JSON.stringify(metricsToSend);
}
res.render('compare.html',{
model1: model1,
model2: model2,
source_code_1: source_code_1,
source_code_2: source_code_2,
metrics: metrics
});
});