-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
381 lines (302 loc) · 8.5 KB
/
index.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var http = require('https');
var url = require('url');
var web = require('./web');
var multipart = require('./multipart');
var _cfg = {
apiKey: '',
host: 'retdec.com',
decompiler: [
'https://retdec.com/service/api/decompiler/decompilations/',
'/outputs/hll'
],
boundary: '------------------------b6e7313f78e3d875'
};
/**
* Wait till result is complete
*
* @param {object} info object containing api response
* @param {Function} callback(err, result)
*/
function waitForCompletion(info, callback)
{
var status = info.links.status;
var parsed = url.parse(status);
var reqopts = {
host: parsed.hostname,
path: parsed.path,
auth: _cfg.apiKey + ':',
};
/* Check status till finised */
web.request(reqopts, 'https', null, function(err, body, code) {
var json;
if (err) {
callback(err, body);
return;
}
try {
json = JSON.parse(body);
} catch (e) {
callback(-12, 'Error parsing response (' + e + ')');
return;
}
if (json.finished) {
if (json.failed) {
callback(-13, json.error);
return;
}
callback(0, 'done');
return;
}
/* Wait one second and check again */
setTimeout(function() {
waitForCompletion(info, callback);
}, 1000);
});
}
/**
* API simple echo test, mostly to verify API key
*
* @param {object} echoargs object containing parameters
* @param {Function} callback(err, res)
*/
function test(echoargs, callback)
{
var reqopts = {
host: _cfg.host,
path: '/service/api/test/echo?',
auth: _cfg.apiKey + ':',
method: 'GET'
};
/* Parse echoargs */
var keys = Object.keys(echoargs);
keys.forEach(function(key) {
reqopts.path += key + '=' + echoargs[key] + '&';
});
web.request(reqopts, 'https', null, function(err, body, code) {
var json;
if (err) {
callback(err, body);
return;
}
if (code !== 200) {
callback(-2, 'Bad HTTP response: ' + code);
} else {
try {
json = JSON.parse(body);
} catch (e) {
callback(-3, 'Error parsing JSON: ' + e);
return;
}
callback(0, json);
}
});
}
function fetchOutputs(info, callback)
{
var results = {};
var pending = 0;
var keys = Object.keys(info);
keys.forEach(function(key) {
if (typeof info[key] === 'object') {
/* Check for empty objects */
if (Object.keys(info[key]).length <= 0)
return;
pending++;
fetchOutputs(info[key], function(res) {
pending--;
results[key] = res;
if (pending === 0)
callback(results);
});
} else if (info[key].indexOf('https://') !== -1) {
pending++;
var newUrl = url.parse(info[key]);
var newReq = {
host: newUrl.hostname,
path: newUrl.path,
auth: _cfg.apiKey + ':',
};
web.request(newReq, 'https', null, function(err, body, code) {
pending--;
results[key] = body;
if (pending === 0)
callback(results);
});
}
});
}
function fetchDecompilationResults(info, callback)
{
var output = info.links.outputs;
var parsed = url.parse(output);
var reqopts = {
host: parsed.hostname,
path: parsed.path,
auth: _cfg.apiKey + ':',
};
web.request(reqopts, 'https', null, function(err, body, code) {
var json;
if (err) {
callback(err, body);
return;
}
try {
json = JSON.parse(body);
} catch (e) {
callback(-5, 'Error parsing JSON: ' + e);
return;
}
/* Get every output */
fetchOutputs(json.links, function(result) {
callback(0, result);
});
});
}
function decompile(file, mode, options, callback)
{
var reqopts = {
host: _cfg.host,
path: '/service/api/decompiler/decompilations',
auth: _cfg.apiKey + ':',
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data; boundary=' + _cfg.boundary,
'Content-Length': 0
}
};
var buf;
var parameters = [];
/* Parse parameters */
if (options !== null) {
var keys = Object.keys(options);
keys.forEach(function(key) {
buf = multipart.addParameter(key, options[key], _cfg.boundary);
parameters.push(buf);
});
}
/* Set decompilation mode */
buf = multipart.addParameter('mode', mode, _cfg.boundary);
parameters.push(buf);
/* Add file */
buf = multipart.addFile('input', file, _cfg.boundary);
parameters.push(buf);
if (!Buffer.isBuffer(buf)) {
callback(-2, 'Error reading input file');
return;
}
/* Add final boundary */
parameters.push(multipart.end(_cfg.boundary));
/* Join all the buffers and set content-length */
buf = Buffer.concat(parameters);
reqopts.headers['content-length'] = buf.length;
/* Make the request */
web.request(reqopts, 'https', buf, function(err, body, code) {
var json;
if (err) {
callback(err, body);
return;
}
try {
json = JSON.parse(body);
} catch (e) {
callback(-3, 'Error parsing response: ' + e);
return;
}
if (json.description) {
callback(-2, json.description);
return;
}
/* Wait for completion */
waitForCompletion(json, function(err, message) {
if (err) {
callback(-61, message);
return;
}
fetchDecompilationResults(json, callback);
});
});
}
/**
* API get fileInfo
*
* @param {path} file path to the file to be analysed
* @param {object} options object containing parameters
* @param {Function} callback(err, res)
*/
function fileInfo(file, options, callback)
{
var reqopts = {
host: _cfg.host,
path: '/service/api/fileinfo/analyses',
auth: _cfg.apiKey + ':',
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data; boundary=' + _cfg.boundary,
'Content-Length': 0
}
};
var buf;
var parameters = [];
/* Parse parameters */
if (options !== null) {
var keys = Object.keys(options);
keys.forEach(function(key) {
buf = multipart.addParameter(key, options[key], _cfg.boundary);
parameters.push(buf);
});
}
/* Add file */
buf = multipart.addFile('input', file, _cfg.boundary);
parameters.push(buf);
if (!Buffer.isBuffer(buf)) {
callback(-2, 'Error reading input file');
return;
}
/* Add final boundary */
parameters.push(multipart.end(_cfg.boundary));
/* Join all the buffers and set content-length */
buf = Buffer.concat(parameters);
reqopts.headers['content-length'] = buf.length;
web.request(reqopts, 'https', buf, function(err, body, code) {
var json;
if (err) {
callback(err, body);
return;
}
try {
json = JSON.parse(body);
} catch (e) {
callback(-3, 'Error parsing response: ' + e);
return;
}
if (json.description)
callback(-2, json.description);
/* Wait for completion & fetch output */
waitForCompletion(json, function(err, message) {
if (err) {
callback(-61, message);
return;
}
/* Fetch output */
var newUrl = url.parse(json.links.output);
var newReq = {
host: newUrl.hostname,
path: newUrl.path,
auth: _cfg.apiKey + ':',
};
web.request(newReq, 'https', null, callback);
});
});
}
/* Initial function */
function apiKey(key)
{
_cfg.apiKey = key;
var api = {
test: test,
decompile: decompile,
fileInfo: fileInfo
};
return api;
}
module.exports.apiKey = apiKey;