|
| 1 | +/* jamesjara, netdb.io */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +var shodanLib = require('shodan-client'), |
| 6 | + util = require('util'), |
| 7 | + Connector = require('loopback-connector').Connector, |
| 8 | + Shodan = function Shodan() {}; |
| 9 | + |
| 10 | +/* eslint-disable no-unused-vars */ |
| 11 | +var shodanInstance = {}; |
| 12 | +/* eslint-enable no-unused-vars */ |
| 13 | + |
| 14 | +var ShodanConnector = function ShodanConnector(settings) { |
| 15 | + assert(typeof settings.api_key === 'string', 'cannot init connector without api key'); |
| 16 | + if (settings.api_key){ |
| 17 | + this.shodan = shodanLib; |
| 18 | + } |
| 19 | + |
| 20 | + this.key = settings.api_key; |
| 21 | + shodanInstance = this.shodan; |
| 22 | +}; |
| 23 | + |
| 24 | +ShodanConnector.initialize = function (dataSource, callback) { |
| 25 | + dataSource.connector = new ShodanConnector(dataSource.settings); |
| 26 | + callback(); |
| 27 | +}; |
| 28 | + |
| 29 | +ShodanConnector.prototype.DataAccessObject = Shodan; |
| 30 | + |
| 31 | +/** |
| 32 | + * Send transactional email with options |
| 33 | + * Full list of options are available here: https://www.npmjs.com/package/sendgrid#available-params |
| 34 | + * |
| 35 | + * @param {Object} options |
| 36 | + * { |
| 37 | + * from: { name: "John", email: "john@cellarise.com" }, |
| 38 | + |
| 39 | + * subject: "Test mail", |
| 40 | + * text: "Plain text message", |
| 41 | + * html: "<b>Html messages</b> here" |
| 42 | + * } |
| 43 | + * |
| 44 | + * @param {Function} cb callback |
| 45 | + * @returns {Function} deferred promise |
| 46 | + */ |
| 47 | +Shodan.query = function (options, cb) { // eslint-disable-line |
| 48 | + var dataSource = this.dataSource, |
| 49 | + connector = dataSource.connector, |
| 50 | + deferred = Q.defer(), |
| 51 | + request; |
| 52 | + |
| 53 | + var fn = function (err, result) { |
| 54 | + if (err) { |
| 55 | + deferred.reject(err); |
| 56 | + } else { |
| 57 | + deferred.resolve(result); |
| 58 | + } |
| 59 | + return cb && cb(err, result); |
| 60 | + }; |
| 61 | + |
| 62 | + if (options.__data) { |
| 63 | + options = R.clone(options.__data); |
| 64 | + } else { |
| 65 | + options = R.clone(options); |
| 66 | + } |
| 67 | + |
| 68 | + assert(connector, 'Cannot query without a connector!'); |
| 69 | + |
| 70 | + if (connector.shodan) { |
| 71 | + |
| 72 | + shodanLib.search('asterisk port:5061', 'YOURKEYHERE', searchOpts) |
| 73 | + .then(res => { |
| 74 | + console.log('Result:'); |
| 75 | + fn(null, options); |
| 76 | + }) |
| 77 | + .catch(err => { |
| 78 | + console.log('Error:'); |
| 79 | + console.log(err); |
| 80 | + fn(null, response); |
| 81 | + }); |
| 82 | + |
| 83 | + |
| 84 | + } else { |
| 85 | + process.nextTick(function nextTick() { // eslint-disable-line |
| 86 | + fn(null, options); |
| 87 | + }); |
| 88 | + } |
| 89 | + return deferred.promise; |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * Query Shodan instance using instance |
| 94 | + */ |
| 95 | + |
| 96 | +Shodan.prototype.query = function protoQuery(fn) { |
| 97 | + return this.constructor.query(this, fn); |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * Export the connector class |
| 102 | + */ |
| 103 | +module.exports = ShodanConnector; |
| 104 | + |
0 commit comments