|
| 1 | +var VERSION = 0.1; |
| 2 | + |
| 3 | +var util = require('util'); |
| 4 | +var aws = require('aws-sdk'); |
| 5 | +var s3 = new aws.S3({apiVersion: '2006-03-01'}); |
| 6 | +var http = require('https'); |
| 7 | +var qs = require('querystring'); |
| 8 | +const API_ENDPOINT = '/v2.1/files/'; |
| 9 | + |
| 10 | +console.log('Loading function v', VERSION); |
| 11 | + |
| 12 | +// replace below with your credentials in the form key:secret |
| 13 | +// to create your key go to https://scanii.com/account/settings/keys |
| 14 | +var SCANII_CREDS = 'KEY:SECRET'; |
| 15 | + |
| 16 | +// callbacks |
| 17 | + |
| 18 | +onFindings = function (bucket, key, result) { |
| 19 | + console.log('findings:', result.findings); |
| 20 | +}; |
| 21 | + |
| 22 | +onNoFindings = function (bucket, key, result) { |
| 23 | + console.log('file has no findings'); |
| 24 | +}; |
| 25 | + |
| 26 | +// do not edit below this line |
| 27 | +fetch = function (id, context, bucket, key) { |
| 28 | + console.log('looking up ', id); |
| 29 | + var req = http.request({ |
| 30 | + auth: SCANII_CREDS, |
| 31 | + port: 443, |
| 32 | + host: 'api.scanii.com', |
| 33 | + path: API_ENDPOINT + id, |
| 34 | + method: 'GET' |
| 35 | + }, function (res) { |
| 36 | + console.log("statusCode: ", res.statusCode); |
| 37 | + console.log("headers: ", res.headers); |
| 38 | + |
| 39 | + if (res.statusCode == 200) { |
| 40 | + console.log('response found, processing'); |
| 41 | + } else { |
| 42 | + console.log('response pending, waiting'); |
| 43 | + setTimeout(fetch(id, context), 1000); |
| 44 | + return; |
| 45 | + } |
| 46 | + res.on('data', function (data) { |
| 47 | + var r = JSON.parse(data); |
| 48 | + console.log('response:', JSON.stringify(r, null, 2)); |
| 49 | + |
| 50 | + if (r.findings !== undefined) { |
| 51 | + if (r.findings.size > 0) { |
| 52 | + onFindings(bucket, key, r); |
| 53 | + } else { |
| 54 | + onNoFindings(bucket, key, r); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + context.done(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + req.end(); |
| 62 | + req.on('error', function (error) { |
| 63 | + console.log(error); |
| 64 | + }); |
| 65 | +}; |
| 66 | + |
| 67 | +exports.handler = function (event, context) { |
| 68 | + console.log('Received event:', JSON.stringify(event, null, 2)); |
| 69 | + |
| 70 | + // Get the object from the event and show its content type |
| 71 | + var bucket = event.Records[0].s3.bucket.name; |
| 72 | + var key = event.Records[0].s3.object.key; |
| 73 | + |
| 74 | + console.log(util.format('processing s3://%s/%s', bucket, key)); |
| 75 | + |
| 76 | + // creating signed url for processing |
| 77 | + var url = s3.getSignedUrl('getObject', { |
| 78 | + Bucket: bucket, |
| 79 | + Key: key, |
| 80 | + Expires: 3600 // 1 hour |
| 81 | + }); |
| 82 | + console.log('created signed url', url); |
| 83 | + console.log('submitting content for malware processing'); |
| 84 | + |
| 85 | + var payload = qs.stringify({ |
| 86 | + location: url |
| 87 | + }); |
| 88 | + var options = { |
| 89 | + auth: SCANII_CREDS, |
| 90 | + port: 443, |
| 91 | + host: 'api.scanii.com', |
| 92 | + path: API_ENDPOINT + 'fetch', |
| 93 | + method: 'POST', |
| 94 | + headers: { |
| 95 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 96 | + 'Content-Length': payload.length, |
| 97 | + 'User-Agent': 'scanii-mu/v' + VERSION |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + // calling scanii API v2.1 (http://docs.scanii.com/v2.1/resources.html): |
| 102 | + var req = http.request(options, function (res) { |
| 103 | + console.log("statusCode: ", res.statusCode); |
| 104 | + console.log("headers: ", res.headers); |
| 105 | + |
| 106 | + res.on('data', function (data) { |
| 107 | + var serviceResponse = JSON.parse(data); |
| 108 | + console.log('file id:', serviceResponse.id); |
| 109 | + // polling for response: |
| 110 | + setTimeout(fetch(serviceResponse.id, context, bucket, key), 1000); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + req.write(payload); |
| 115 | + req.end(); |
| 116 | + |
| 117 | + req.on('error', function (error) { |
| 118 | + console.log(error); |
| 119 | + }); |
| 120 | +}; |
0 commit comments