apiController.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. var debug = require('debug')('ylt:server');
  2. var Q = require('q');
  3. var AWS = require('aws-sdk');
  4. var ylt = require('../../index');
  5. var ScreenshotHandler = require('../../screenshotHandler');
  6. var RunsQueue = require('../datastores/runsQueue');
  7. var RunsDatastore = require('../datastores/runsDatastore');
  8. var serverSettings = (process.env.IS_TEST) ? require('../../../test/fixtures/settings.json') : require('../../../server_config/settings.json');
  9. var ResultsDatastore = (serverSettings.awsHosting) ? require('../datastores/awsResultsDatastore') : require('../datastores/resultsDatastore');
  10. var ApiController = function(app) {
  11. 'use strict';
  12. var queue = new RunsQueue();
  13. var runsDatastore = new RunsDatastore();
  14. var resultsDatastore = new ResultsDatastore();
  15. // Create a new run
  16. app.post('/api/runs', function(req, res) {
  17. // Add http to the test URL
  18. if (req.body.url && req.body.url.toLowerCase().indexOf('http://') !== 0 && req.body.url.toLowerCase().indexOf('https://') !== 0) {
  19. req.body.url = 'http://' + req.body.url;
  20. }
  21. // Block requests to unwanted websites (=spam)
  22. if (req.body.url && isBlocked(req.body.url)) {
  23. console.error('Test blocked for URL: %s', req.body.url);
  24. res.status(403).send('Forbidden');
  25. return;
  26. }
  27. // Grab the test parameters and generate a random run ID
  28. var run = {
  29. runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
  30. params: {
  31. url: req.body.url,
  32. waitForResponse: req.body.waitForResponse === true || req.body.waitForResponse === 'true' || req.body.waitForResponse === 1,
  33. partialResult: req.body.partialResult || null,
  34. screenshot: req.body.screenshot || false,
  35. device: req.body.device || 'desktop',
  36. proxy: req.body.proxy || null,
  37. waitForSelector: req.body.waitForSelector || null,
  38. cookie: req.body.cookie || null,
  39. authUser: req.body.authUser || null,
  40. authPass: req.body.authPass || null,
  41. blockDomain: req.body.blockDomain || null,
  42. allowDomain: req.body.allowDomain || null,
  43. noExternals: req.body.noExternals || false
  44. }
  45. };
  46. // Add test to the testQueue
  47. debug('Adding test %s to the queue', run.runId);
  48. var queuePromise = queue.push(run.runId);
  49. // Save the run to the datastore
  50. //runsDatastore.add(run, queuePromise.startingPosition);
  51. runsDatastore.add(run, 0);
  52. // Let's start the run
  53. queuePromise.then(function() {
  54. runsDatastore.updatePosition(run.runId, 0);
  55. console.log('Launching test ' + run.runId + ' on ' + run.params.url);
  56. var runOptions = {
  57. screenshot: run.params.screenshot ? ScreenshotHandler.getTmpFileRelativePath() : false,
  58. device: run.params.device,
  59. proxy: run.params.proxy,
  60. waitForSelector: run.params.waitForSelector,
  61. cookie: run.params.cookie,
  62. authUser: run.params.authUser,
  63. authPass: run.params.authPass,
  64. blockDomain: run.params.blockDomain,
  65. allowDomain: run.params.allowDomain,
  66. noExternals: run.params.noExternals
  67. };
  68. const {region, arn} = chooseLambdaRegionByGeoIP(req.headers);
  69. const lambda = new AWS.Lambda({region: region});
  70. return lambda.invoke({
  71. FunctionName: arn,
  72. InvocationType: 'RequestResponse',
  73. Payload: JSON.stringify({url: run.params.url, id: run.runId, options: runOptions})
  74. }).promise();
  75. })
  76. .then(function(response) {
  77. debug('We\'ve got a response from AWS Lambda');
  78. debug('StatusCode = %d', response.StatusCode);
  79. debug('Payload = %s', response.Payload);
  80. if (response.StatusCode === 200 && response.Payload && response.Payload !== 'null') {
  81. debug('Success!');
  82. runsDatastore.markAsComplete(run.runId);
  83. } else {
  84. debug('Empty response from the lambda agent');
  85. runsDatastore.markAsFailed(run.runId, "Empty response from the agent");
  86. }
  87. })
  88. .catch(function(err) {
  89. debug('Error from AWS Lambda:');
  90. debug(err);
  91. runsDatastore.markAsFailed(run.runId, err.toString());
  92. });
  93. // The user doesn't want to wait for the response, sending the run ID only
  94. debug('Sending response without waiting.');
  95. res.setHeader('Content-Type', 'application/json');
  96. res.send(JSON.stringify({runId: run.runId}));
  97. });
  98. // Reads the Geoip_Continent_Code header and chooses the right region from the settings
  99. function chooseLambdaRegionByGeoIP(headers) {
  100. // The settings can be configured like this in server_config/settings.json:
  101. //
  102. // "awsHosting": {
  103. // "lambda": {
  104. // "regionByContinent": {
  105. // "AF": "eu-west-3",
  106. // "AS": "ap-southeast-1",
  107. // "EU": "eu-west-3",
  108. // "NA": "us-east-1",
  109. // "OC": "ap-southeast-1",
  110. // "SA": "us-east-1",
  111. // "default": "eu-west-3"
  112. // },
  113. // "arnByRegion": {
  114. // "us-east-1": "arn:aws:lambda:us-east-1:xxx:function:xxx",
  115. // "eu-west-3": "arn:aws:lambda:eu-west-3:xxx:function:xxx",
  116. // "ap-southeast-1": "arn:aws:lambda:ap-southeast-1:xxx:function:xxx"
  117. // }
  118. // }
  119. // },
  120. const header = headers.geoip_continent_code;
  121. debug('Value of the Geoip_Continent_Code header: %s', header);
  122. const continent = header || 'default';
  123. const region = serverSettings.awsHosting.lambda.regionByContinent[continent];
  124. const arn = serverSettings.awsHosting.lambda.arnByRegion[region];
  125. debug('The chosen AWS Lambda is: %s', arn);
  126. return {region, arn};
  127. }
  128. // Retrive one run by id
  129. app.get('/api/runs/:id', function(req, res) {
  130. var runId = req.params.id;
  131. var run = runsDatastore.get(runId);
  132. if (run) {
  133. res.setHeader('Content-Type', 'application/json');
  134. res.send(JSON.stringify(run, null, 2));
  135. } else {
  136. res.status(404).send('Not found');
  137. }
  138. });
  139. // Counts all pending runs
  140. app.get('/api/runs', function(req, res) {
  141. res.setHeader('Content-Type', 'application/json');
  142. res.send(JSON.stringify({
  143. pendingRuns: queue.length(),
  144. timeSinceLastTestStarted: queue.timeSinceLastTestStarted()
  145. }, null, 2));
  146. });
  147. // Delete one run by id
  148. /*app.delete('/api/runs/:id', function(req, res) {
  149. deleteRun()
  150. });*/
  151. // Delete all
  152. /*app.delete('/api/runs', function(req, res) {
  153. purgeRuns()
  154. });
  155. // List all
  156. app.get('/api/runs', function(req, res) {
  157. listRuns()
  158. });
  159. // Exists
  160. app.head('/api/runs/:id', function(req, res) {
  161. existsX();
  162. // Returns 200 if the result exists or 404 if not
  163. });
  164. */
  165. // Retrive one result by id
  166. app.get('/api/results/:id', function(req, res) {
  167. getPartialResults(req.params.id, res, function(data) {
  168. // Some fields can be excluded from the response, this way:
  169. // /api/results/:id?exclude=field1,field2
  170. if (req.query.exclude && typeof req.query.exclude === 'string') {
  171. var excludedFields = req.query.exclude.split(',');
  172. excludedFields.forEach(function(fieldName) {
  173. if (data[fieldName]) {
  174. delete data[fieldName];
  175. }
  176. });
  177. }
  178. return data;
  179. });
  180. });
  181. // Retrieve one result and return only the generalScores part of the response
  182. app.get('/api/results/:id/generalScores', function(req, res) {
  183. getPartialResults(req.params.id, res, function(data) {
  184. return data.scoreProfiles.generic;
  185. });
  186. });
  187. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  188. getPartialResults(req.params.id, res, function(data) {
  189. return data.scoreProfiles[req.params.scoreProfile];
  190. });
  191. });
  192. app.get('/api/results/:id/rules', function(req, res) {
  193. getPartialResults(req.params.id, res, function(data) {
  194. return data.rules;
  195. });
  196. });
  197. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  198. getPartialResults(req.params.id, res, function(data) {
  199. return data.javascriptExecutionTree;
  200. });
  201. });
  202. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  203. getPartialResults(req.params.id, res, function(data) {
  204. return data.toolsResults.phantomas;
  205. });
  206. });
  207. function getPartialResults(runId, res, partialGetterFn) {
  208. resultsDatastore.getResult(runId)
  209. .then(function(data) {
  210. var results = partialGetterFn(data);
  211. if (typeof results === 'undefined') {
  212. res.status(404).send('Not found');
  213. return;
  214. }
  215. res.setHeader('Content-Type', 'application/json');
  216. res.send(JSON.stringify(results, null, 2));
  217. }).fail(function() {
  218. res.status(404).send('Not found');
  219. });
  220. }
  221. // Retrive one result by id
  222. app.get('/api/results/:id/screenshot.jpg', function(req, res) {
  223. var runId = req.params.id;
  224. resultsDatastore.getScreenshot(runId)
  225. .then(function(screenshotBuffer) {
  226. res.setHeader('Content-Type', 'image/jpeg');
  227. res.send(screenshotBuffer);
  228. }).fail(function() {
  229. res.status(404).send('Not found');
  230. });
  231. });
  232. function isBlocked(url) {
  233. if (!serverSettings.blockedUrls) {
  234. return false;
  235. }
  236. return serverSettings.blockedUrls.some(function(blockedUrl) {
  237. return (url.indexOf(blockedUrl) === 0);
  238. });
  239. }
  240. };
  241. module.exports = ApiController;