-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapiController.js
More file actions
391 lines (305 loc) · 13.6 KB
/
Copy pathapiController.js
File metadata and controls
391 lines (305 loc) · 13.6 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
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
382
383
384
385
386
387
388
389
390
391
var debug = require('debug')('ylt:server');
var Q = require('q');
var path = require('path');
var yltAgent = require('yellowlabtools');
var RunsQueue = require('../datastores/runsQueue');
var RunsDatastore = require('../datastores/runsDatastore');
var ResultsDatastore = require('../datastores/resultsDatastore');
var serverSettings = (process.env.IS_TEST) ? require('../../test/fixtures/settings.json') : require('../../server_config/settings.json');
var isAWS = !!serverSettings.awsHosting;
// The official YLT instance is hosted on AWS
// Private instances are hosted locally
// Depending on the configuration, we load various modules:
var AWS, ResultsDatastore;
if (isAWS) {
AWS = require('aws-sdk');
ResultsDatastore = require('../datastores/awsResultsDatastore');
// Increase AWS Lambda timeout
AWS.config.update({httpOptions: {timeout: 240000}});
} else {
ResultsDatastore = require('../datastores/resultsDatastore');
}
var ApiController = function(app) {
'use strict';
var queue = new RunsQueue();
var runsDatastore = new RunsDatastore();
var resultsDatastore = new ResultsDatastore();
// Create a new run
app.post('/api/runs', function(req, res) {
// Add https to the test URL
if (req.body.url && req.body.url.toLowerCase().indexOf('http://') !== 0 && req.body.url.toLowerCase().indexOf('https://') !== 0) {
req.body.url = 'https://' + req.body.url;
}
// Block requests to unwanted websites (=spam)
if (req.body.url && isBlocked(req.body.url)) {
console.error('Test blocked for URL: %s', req.body.url);
res.status(403).send('Forbidden');
return;
}
// Grab the test parameters and generate a random run ID
var run = {
runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
params: {
url: req.body.url,
partialResult: req.body.partialResult || null,
screenshot: req.body.screenshot || false,
device: req.body.device || 'desktop',
proxy: req.body.proxy || null,
waitForSelector: req.body.waitForSelector || null,
cookie: req.body.cookie || null,
authUser: req.body.authUser || null,
authPass: req.body.authPass || null,
blockDomain: req.body.blockDomain || null,
allowDomain: req.body.allowDomain || null,
noExternals: req.body.noExternals || false
}
};
// Add test to the testQueue
debug('Adding test %s to the queue', run.runId);
var queuePromise = queue.push(run.runId);
// Save the run to the datastore
if (isAWS) {
// There is no queue on an AWS instance, we assume the number of agents is sufficient
runsDatastore.add(run, 0);
} else {
runsDatastore.add(run, queuePromise.startingPosition);
// Listen for position updates
queuePromise.progress(function(position) {
runsDatastore.updatePosition(run.runId, position);
});
}
// Let's start the run
queuePromise.then(function() {
runsDatastore.updatePosition(run.runId, 0);
console.log('Launching test ' + run.runId + ' on ' + run.params.url);
var runOptions = {
screenshot: run.params.screenshot ? path.join(__dirname, '../../tmp/temp-screenshot.png') : false,
device: run.params.device,
proxy: run.params.proxy,
waitForSelector: run.params.waitForSelector,
cookie: run.params.cookie,
authUser: run.params.authUser,
authPass: run.params.authPass,
blockDomain: run.params.blockDomain,
allowDomain: run.params.allowDomain,
noExternals: run.params.noExternals
};
if (isAWS) {
const {region, arn} = chooseLambdaRegionByGeoIP(req.headers);
const lambda = new AWS.Lambda({region: region});
// Let's call the distant agent through AWS
return lambda.invoke({
FunctionName: arn,
InvocationType: 'RequestResponse',
Payload: JSON.stringify({url: run.params.url, id: run.runId, options: runOptions})
})
.promise()
.then(function(response) {
debug('We\'ve got a response from AWS Lambda');
debug('StatusCode = %d', response.StatusCode);
debug('Payload = %s', response.Payload);
if (response.StatusCode === 200 && response.Payload && response.Payload !== 'null') {
const payload = JSON.parse(response.Payload);
if (payload.status === 'failed') {
debug('Failed with error %s', payload.errorMessage);
runsDatastore.markAsFailed(run.runId, payload.errorMessage);
} else {
debug('Success!');
runsDatastore.markAsComplete(run.runId);
}
} else {
debug('Empty response from the lambda agent');
runsDatastore.markAsFailed(run.runId, "Empty response from the agent");
}
})
.catch(function(err) {
debug('Error from AWS Lambda:');
debug(err);
runsDatastore.markAsFailed(run.runId, err.toString());
});
} else {
// Specify the function that will be called to save the screenshot
// path: a string that represents the file name ('screenshot.jpg')
// content: a buffered jpeg image file
if (run.params.screenshot) {
runOptions.saveScreenshotFn = async (fileName, screenshotBuffer) => {
return resultsDatastore.saveScreenshot(run.runId, screenshotBuffer);
};
}
// Let's call the local agent
return yltAgent(run.params.url, runOptions)
// Update the progress bar on each progress
.progress(function(progress) {
runsDatastore.updateRunProgress(run.runId, progress);
})
.then(function(data) {
debug('Success');
data.runId = run.runId;
// Remove uneeded temp screenshot paths
delete data.params.options.screenshot;
// Here we can remove tools results if not needed
delete data.toolsResults.phantomas.offenders.requests;
runsDatastore.markAsComplete(run.runId);
debug('Saving results in datastore');
return resultsDatastore.saveResult(data);
})
.fail(function(err) {
console.error('Test failed for URL: %s', run.params.url);
console.error(err.toString());
runsDatastore.markAsFailed(run.runId, err.toString());
});
}
})
.finally(function() {
queue.remove(run.runId);
});
// Sending the run ID without waiting for the end of the run
debug('Sending response without waiting.');
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({runId: run.runId}));
});
// Reads the Geoip_Continent_Code header and chooses the right region from the settings
function chooseLambdaRegionByGeoIP(headers) {
// The settings can be configured like this in server_config/settings.json:
//
// "awsHosting": {
// "lambda": {
// "regionByContinent": {
// "AF": "eu-west-3",
// "AS": "ap-southeast-1",
// "EU": "eu-west-3",
// "NA": "us-east-1",
// "OC": "ap-southeast-1",
// "SA": "us-east-1",
// "default": "eu-west-3"
// },
// "arnByRegion": {
// "us-east-1": "arn:aws:lambda:us-east-1:xxx:function:xxx",
// "eu-west-3": "arn:aws:lambda:eu-west-3:xxx:function:xxx",
// "ap-southeast-1": "arn:aws:lambda:ap-southeast-1:xxx:function:xxx"
// }
// }
// },
const header = headers.geoip_continent_code;
debug('Value of the Geoip_Continent_Code header: %s', header);
const continent = header || 'default';
const region = serverSettings.awsHosting.lambda.regionByContinent[continent];
const arn = serverSettings.awsHosting.lambda.arnByRegion[region];
debug('The chosen AWS Lambda is: %s', arn);
return {region, arn};
}
// Retrive one run by id
app.get('/api/runs/:id', function(req, res) {
var runId = req.params.id;
var run = runsDatastore.get(runId);
if (run) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(run, null, 2));
} else {
res.status(404).send('Not found');
}
});
// Counts all pending runs
app.get('/api/runs', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
pendingRuns: queue.length(),
timeSinceLastTestStarted: queue.timeSinceLastTestStarted()
}, null, 2));
});
// Delete one run by id
/*app.delete('/api/runs/:id', function(req, res) {
deleteRun()
});*/
// Delete all
/*app.delete('/api/runs', function(req, res) {
purgeRuns()
});
// List all
app.get('/api/runs', function(req, res) {
listRuns()
});
// Exists
app.head('/api/runs/:id', function(req, res) {
existsX();
// Returns 200 if the result exists or 404 if not
});
*/
// Retrive one result by id
app.get('/api/results/:id', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
// Some fields can be excluded from the response, this way:
// /api/results/:id?exclude=field1,field2
if (req.query.exclude && typeof req.query.exclude === 'string') {
var excludedFields = req.query.exclude.split(',');
excludedFields.forEach(function(fieldName) {
if (data[fieldName]) {
delete data[fieldName];
}
});
}
return data;
});
});
// Retrieve one result and return only the generalScores part of the response
app.get('/api/results/:id/generalScores', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.scoreProfiles.generic;
});
});
app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.scoreProfiles[req.params.scoreProfile];
});
});
app.get('/api/results/:id/rules', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.rules;
});
});
app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.javascriptExecutionTree;
});
});
app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.toolsResults.phantomas;
});
});
function getPartialResults(runId, res, partialGetterFn) {
resultsDatastore.getResult(runId)
.then(function(data) {
var results = partialGetterFn(data);
results.screenshotUrl = '/api/results/' + results.runId + '/screenshot.jpg';
if (typeof results === 'undefined') {
res.status(404).send('Not found');
return;
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(results, null, 2));
}).fail(function() {
res.status(404).send('Not found');
});
}
// Retrive one result by id
app.get('/api/results/:id/screenshot.jpg', function(req, res) {
var runId = req.params.id;
resultsDatastore.getScreenshot(runId)
.then(function(screenshotBuffer) {
res.setHeader('Content-Type', 'image/jpeg');
res.send(screenshotBuffer);
}).fail(function() {
res.status(404).send('Not found');
});
});
function isBlocked(url) {
if (!serverSettings.blockedUrls) {
return false;
}
return serverSettings.blockedUrls.some(function(blockedUrl) {
return (url.indexOf(blockedUrl) >= 0);
});
}
};
module.exports = ApiController;