Skip to content

Instantly share code, notes, and snippets.

@ohmtrivedi
Last active Oct 15, 2018
Embed
What would you like to do?
SenecaJS Client Request Timeout Error
module.exports = function api(options) {
let seneca = this;
seneca.add('role:api,path:msOne', function (msg, respond) {
// console.log(msg);
let { operation } = msg.args.params;
let { target } = msg.args.query;
// let operation = msg.args.body.operation;
// let target = msg.args.body.remote_ip;
if (operation == 'fpOS') {
seneca.act({ timeout$: 300000, role: 'msOne', cmd: 'fpOS', remoteIP: target }, function (err, result) {
if (err) throw err;
console.log(result);
respond(result);
});
} else if (operation == 'getCVEs') {
seneca.act({ role: 'msOne', cmd: 'getCVEs', osQuery: target }, function (err, result) {
if (err) throw err;
console.log(result);
respond(result);
});
}
});
}
"use strict"
const Seneca = require('seneca');
const SenecaWeb = require('seneca-web');
const Express = require('express');
const Routes = require('./routes');
let senecaWebConfig = {
routes: Routes,
context: Express().use(require('body-parser').json()),
adapter: require('seneca-web-adapter-express'),
options: { parseBody: false }
};
const request_timeout = 300000;
let seneca = Seneca({
tag: 'expressAPI',
timeout: request_timeout,
transport: {
web: { timeout: request_timeout }
}
})
.use('api')
.use(SenecaWeb, senecaWebConfig)
.client({ host: 'localhost', port: 9091, pin: 'role:msOne' })
.client({ host: 'localhost', port: 9092, pin: 'role:msTwo' })
.client({ host: 'localhost', port: 9093, pin: 'role:msThree' })
.ready(() => {
let server = seneca.export('web/context')();
server.listen('4000', () => {
console.log('App started on: 4000');
});
});
{"actid":"x2eiuiusk203/dkdttsenjo3n","msg":{"timeout$":300000,"role":"msOne","cmd":"fpOS","remoteIP":"200.162.132.66","plugin$":{"name":"client$","tag":"-","fullname":"client$"},"fatal$":true,"tx$":"dkdttsenjo3n"},"meta":{"start":1539647625931,"end":1539647745972,"pattern":"role:msOne","action":"transport_client_10","mi":"bdgrlmpknn1s","tx":"dkdttsenjo3n","id":"bdgrlmpknn1s/dkdttsenjo3n","instance":"ytsfpfhhe0zg/1539647617461/4572/3.7.0/expressAPI","tag":"expressAPI","seneca":"3.7.0","version":"0.1.0","fatal":true,"timeout":300000,"custom":{},"plugin":{"name":"client$","tag":"-","fullname":"client$"},"parents":[["name:api,plugin:define,role:seneca,seq:2,tag:undefined","x2eiuiusk203/dkdttsenjo3n","ytsfpfhhe0zg/1539647617461/4572/3.7.0/expressAPI","expressAPI","0.1.0",1539647617525,1539647617555,false,"plugin_definition_8"]],"sync":true,"trace":[{"desc":[null,"bdgrlmpknn1s/dkdttsenjo3n",null,null,null,null,null,null,null],"trace":[]}],"sub":null,"data":null,"err":null,"err_trace":null,"error":true,"empty":null},"actdef":{"plugin_name":"client$","plugin_tag":"-","plugin_fullname":"client$","raw":{"role":"msOne","client$":true,"strict$":{"add":true}},"plugin":{"name":"client$","tag":"-","fullname":"client$"},"sub":false,"client":true,"rules":{},"id":"transport_client_10","name":"transport_client","pattern":"role:msOne","msgcanon":{"role":"msOne"},"priorpath":""},"client":true,"listen":false,"transport":{},"kind":"act","case":"ERR","info":"seneca: Action role:msOne callback threw: Client request error: socket hang up.","code":"act_callback","err":{"code":"act_callback","trace":[{"method":"POST","url":"http://localhost:9091/act"}],"isBoom":true,"isServer":true,"data":null,"output":{"statusCode":502,"payload":{"message":"Client request error: socket hang up","statusCode":502,"error":"Bad Gateway"},"headers":{}},"eraro":true,"orig":{"code":"ECONNRESET","trace":[{"method":"POST","url":"http://localhost:9091/act"}],"isBoom":true,"isServer":true,"data":null,"output":{"statusCode":502,"payload":{"message":"Client request error: socket hang up","statusCode":502,"error":"Bad Gateway"},"headers":{}}},"seneca":true,"package":"seneca","msg":"seneca: Action role:msOne callback threw: Client request error: socket hang up.","details":{"message":"Client request error: socket hang up","pattern":"role:msOne","instance":"Seneca/ytsfpfhhe0zg/1539647617461/4572/3.7.0/expressAPI","orig$":{"code":"ECONNRESET","trace":[{"method":"POST","url":"http://localhost:9091/act"}],"isBoom":true,"isServer":true,"data":null,"output":{"statusCode":502,"payload":{"message":"Client request error: socket hang up","statusCode":502,"error":"Bad Gateway"},"headers":{}}},"message$":"Client request error: socket hang up"},"callpoint":"at module.exports.internals.Utils.internals.Utils.handle_response (/root/Documents/msThesis/theBackEnd-v2/node_modules/seneca-transport/lib/transport-utils.js:71:11)"},"duration":120041,"level":"error","plugin_name":"api","pattern":"role:msOne","seneca":"ytsfpfhhe0zg/1539647617461/4572/3.7.0/expressAPI","when":1539647745989}
=== SENECA FATAL ERROR ===
MESSAGE: ::: seneca: Action role:msOne failed: Client request error: socket hang up.
CODE: ::: act_execute
INSTANCE ::: Seneca/ytsfpfhhe0zg/1539647617461/4572/3.7.0/expressAPI
DETAILS ::: { message: 'Client request error: socket hang up',
pattern: 'role:msOne',
fn: { [Function: transport_client] id: 'host:localhost,pg:role:msOne,pin:role:msOne,port:9091' },
callback: [Function],
callpoint: undefined,
plugin: {} }
STACK ::: Error: Client request error: socket hang up
at module.exports.internals.Utils.internals.Utils.handle_response (/root/Documents/msThesis/theBackEnd-v2/node_modules/seneca-transport/lib/transport-utils.js:71:11)
at /root/Documents/msThesis/theBackEnd-v2/node_modules/seneca-transport/lib/http.js:154:25
at req.request (/root/Documents/msThesis/theBackEnd-v2/node_modules/wreck/lib/index.js:584:20)
at finish (/root/Documents/msThesis/theBackEnd-v2/node_modules/wreck/lib/index.js:249:20)
at wrapped (/root/Documents/msThesis/theBackEnd-v2/node_modules/hoek/lib/index.js:879:20)
at ClientRequest.onError (/root/Documents/msThesis/theBackEnd-v2/node_modules/wreck/lib/index.js:186:16)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketOnEnd (_http_client.js:423:9)
WHEN ::: 2018-10-15T23:55:45.984Z
LOG ::: {kind:null,plugin:seneca,tag:3.7.0,id:ytsfpfhhe0zg/1539647617461/4572/3.7.0/expressAPI,code:act_execute,notice:
NODE ::: { http_parser: '2.8.0', node: '8.11.2', v8: '6.2.414.54', uv: '1.19.1', zlib: '1.2.11', ares: '1.10.1-DEV', modules: '57', nghttp2: '1.29.0', napi: '3', openssl: '1.0.2o', icu: '60.1', unicode: '10.0', cldr: '32.0', tz: '2017c' }, { debug: false, uv: true, ipv6: true, tls_npn: true, tls_alpn: true, tls_sni: true, tls_ocsp: true, tls: true }
PROCESS ::: pid=4572, arch=x64, platform=linux, argv=[ '/usr/bin/node', '/root/Documents/msThesis/theBackEnd-v2/app.js' ]
SENECA TERMINATED at 2018-10-15T23:55:45.996Z. See above for error report.
'use strict'
const { exec } = require('child_process');
const rp = require('request-promise');
const cheerio = require('cheerio');
const util = require('util');
module.exports = function msOne (options) {
let seneca = this;
// let identifiedOS = '';
seneca.add({ init: 'msOne' }, function(pluginInfo, respond) {
console.log(options.message);
respond();
});
seneca.add({ role: 'msOne', cmd: 'fpOS' }, function (msg, respond) {
// const remoteIP = "62.103.72.66";
// console.log(msg);
const { remoteIP } = msg;
let commandToExec = `sudo nmap -sV -T5 -O -Pn ${remoteIP} | grep -m 1 -e "Aggressive OS guesses" -e "OS details" -e "No OS"`;
console.log('Executing: ' + commandToExec);
exec(commandToExec, (error, stdout, stderr) => {
let output_split, answer;
console.log(`Nmap execution complete! Result: ${stdout}`);
if (error) {
console.log(error);
} else if (stderr) {
console.log(stderr);
} else if (stdout.startsWith('Aggressive')) {
output_split = stdout.slice(22).split(',');
answer = output_split[0].trim();
} else if (stdout.startsWith('OS')) {
output_split = stdout.slice(11).split(',');
answer = output_split[0].trim();
} else {
answer = stdout.trim();
}
if (answer.indexOf('(') != -1) {
answer = answer.substring(0, answer.indexOf('(')-1);
}
// identifiedOS = answer;
respond(null, { osGuess: answer });
});
});
seneca.add({ role: 'msOne', cmd: 'getCVEs' }, async function (msg, respond) {
// let osQuery = msg.args.body.os_name;
let { osQuery } = msg;
let osCVEs = await scrapCVEs(osQuery);
respond(null, osCVEs);
});
}
let scrapCVEs = (osQuery) => {
let osName = osQuery.split(' ');
let url = 'http://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=' + osName.join('+');
// console.log('URL ' + url);
let os_cves = { cve_id: [], cve_desc: [] };
const options = {
uri: url,
transform: function(body) {
// console.log(body);
return cheerio.load(body);
}
};
return rp(options)
.then($ => {
let desiredTableElem = $('#TableWithRules').children('table');
desiredTableElem.find('td').each((i, elem) => {
// console.log(i + elem.name);
if (elem.children[0].type === 'tag') {
// console.log(elem.children[0].name);
os_cves.cve_id.push(elem.children[0].children[0].data);
// console.log(elem.children[0].children[0].data);
}
else if (elem.children[0].type === 'text') {
os_cves.cve_desc.push(elem.children[0].data);
// console.log(elem.children[0].data);
}
});
console.log('Scrapping complete!');
return os_cves;
})
.catch(err => {
console.log(err);
});
}
'use strict'
const request_timeout = 300000;
const Seneca = require('seneca')({
tag: 'msOneService',
timeout: request_timeout,
transport: {
web: { timeout: request_timeout }
}
})
.use('./msOnePlugin', { message: 'msOne Plugin Added!' })
.listen({
host: 'localhost',
port: 9091,
pin: 'role:msOne'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment