Error EADDRNOTAVAIL on socket.send() in node (SSDP protocol) - node.js

I'm trying to implement a UPnP discovery service tool (SSDP protocol), I did something similar in python following this post: https://www.electricmonk.nl/log/2016/07/05/exploring-upnp-with-python/ and I would like to port it to node (v. 8.6.0) and typescript however I'm getting the following error when I try to send the message (socket.send(...)):
{ Error: send EADDRNOTAVAIL 239.255.255.250:1900
at Object._errnoException (util.js:1019:11)
at _exceptionWithHostPort (util.js:1041:20)
at SendWrap.afterSend [as oncomplete] (dgram.js:475:11)
code: 'EADDRNOTAVAIL',
errno: 'EADDRNOTAVAIL',
syscall: 'send',
address: '239.255.255.250',
port: 1900 }
I've found a snippet of code for node, that makes this exact thing (https://coolaj86.com/articles/adventures-in-upnp-with-node-js/) and I think that my code is quite equivalent however I cannot see why my code isn't working
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
let msg_txt = 'M-SEARCH * HTTP/1.1\r\n' +
'HOST:239.255.255.250:1900\r\n' +
'ST:upnp:rootdevice\r\n' +
'MX:2\r\n' +
'MAN:"ssdp:discover"\r\n\r\n';
const message = Buffer.from(msg_txt);
socket.on('message', (msg: Buffer, info: any) => {
console.log(msg.toString());
});
socket.bind({
address: '239.255.255.250',
port: 1900
}, (err) => {
!!err && console.error(err);
});
socket.on('listening', () => {
console.log('Sending msg...');
socket.send(message, 0, message.length, 1900, '239.255.255.250', (err) => {
!!err && console.error(err); // err != null
});
});
I suspect that is a typical one-line-problem but after a while I couldn't find it out, any help is welcome.

I saw it, in node the API is a bit different, the binding should be against the '0.0.0.0' iface and the port 0 (for a random number), so changing the binding command to the following code just fix it:
socket.bind({
address: '0.0.0.0',
port: 0
}, (err) => {
!!err && console.error(err);
});
In python I called to socket.recvfrom() method to get the UPnP devices responses, there is not an explicit socket binding.

For what it's worth, I had a similar setup using Node 10 on Windows and creating a udp4 socket on localhost. Weirdly enough, the setup described in the accepted answer worked on every OS and Node version except Node 10 on Windows in my CI tests. In order to get things to work, I had to explicitly bind to the address localhost instead of 0.0.0.0, like so:
const socket = dgram.createSocket('udp4').unref();
socket.bind({
address: 'localhost',
port: 0
}, (err) => {
!!err && console.error(err);
});

Related

Error:read ECONNRESET - I cannot successfully authenticate with the hp-ux server in node.js

I have a problem with the connection into a hpux server that we host locally in our local network. I get the following error:
Hello world
Connected to Nemesis
true
rejected: Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
level: 'client-socket'
}
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
level: 'client-socket'
}
Here is my code:
console.log('Hello world');
const { NodeSSH } = require('node-ssh');
const ssh = new NodeSSH();
ssh.connect({
host: "server",
username: "admin account",
password: "password"
}).then(console.log("Connected to Nemesis"))
console.log(ssh.isConnected());
ssh.exec('hh_client', ['--json'], { cwd: '/', stream: 'stdout', options: { pty: true } }).then(fulfilled => {
console.log("fulfilled:", fulfilled)
}).catch(rejected => {
console.log("rejected:", rejected)
})
I believe it is connecting to the server OK, tested by changing the IP, where I get a message to say that it cannot find the server. That said, the username and password does not seem to be being used, as I can type the user and password wrong, and I get the same error message.
the exec code is just lifted from the npm website for the module.
for a little more context, I am fairly new to hpux and linux in general, as most of this is inherited. I have seen a lot of information about using RSA and public/private keys, but there are already some on the server and I don't want to overwrite anything in the .ssh folder if I can help it.
In terms of connecting via other methods, I can use the username and password using ssh user#server and connect in fine, and do anything I want on the server with full permissions.
Any help appreciated.
Thank you,
Craig
Seing the exact same problem.
It's seems from the logs on the target server that the library is still trying to use key auth, maybe because we are not using it correctly or because it's simply considered insecure by the developers, or maybe they just negleted that option since most people won't use it for security reasons.
Here is the relevant server log:
Jan 23 20:43:55 debian sshd[6152]: error: kex_exchange_identification: banner line contains invalid characters
Jan 23 20:43:55 debian sshd[6152]: banner exchange: Connection from <source_ip_edited_for_privacy> port 42544: invalid format
Here is the code:
const readline = require("readline");
const { NodeSSH } = require("node-ssh");
// function to get input from user
const getInput = () => {
// required to take input from user
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// required to connect
const ssh = new NodeSSH();
rl.question("What is the host? ", (host) => {
console.log(host);
rl.question("What is the user? ", (user) => {
console.log(user);
rl.question("What is the password?", (password) => {
console.log(password);
rl.question("What is the command?", (command) => {
console.log(command);
// Connect
ssh.connect({
host: host,
username: user,
password: password,
//privateKeyPath: '/home/steel/.ssh/id_rsa'
});
// Excute Command
ssh
.execCommand(`${command}`, { cwd: `/home/${user}` })
.then((result) => {
// rl.write(result.stdout);
console.log("STDOUT: " + result.stdout);
console.log("STDERR: " + result.stderr);
})
.catch((err) => {
console.log(err);
})
.finally(() => {
ssh.dispose();
rl.close();
});
});
});
});
});
};
getInput();

Error: read ECONNRESET while connection rabbitmq with nodejs

I've encountered following error message while connection our external RabbitMQ with NodeJS as follow:
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
and my nodejs code is as follow:
const amqp_url = "amqp://un:pw#sb-mq.com:9901/my-vhost";
amqp.connect(amqp_url, function (error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function (error1, channel) {
if (error1) {
throw error1;
}
var queue = 'hello';
var msg = 'Hello World!';
channel.assertQueue(queue, {
durable: false
});
channel.sendToQueue(queue, Buffer.from(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function () {
connection.close();
process.exit(0);
}, 500);
});
But the thing is when I've setup RabbidMQ locally with same configuration but using default port (like amqp://un:pw#localhost:5672/my-vhost), it was working perfectly. Please let me know how to troubleshoot that one, thanks.
"ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection.
see How do I debug error ECONNRESET in Node.js?
about RabbitMQ check if rabbitmq actually is active in that port, just:
telnet sb-mq.com 9901
from your client machine and check the firewall configuration.
You may have another service running on 9901
ECONNRESET is network problem, rabbitmq can work in different ports without problems
I found that issue has been resolved when I've tried to use amqps instead of amqp.

How do you run RServe on AWS Lambda with NodeJS?

While this question is quite open-ended, I'm generally trying to follow this excellent post here: https://aws.amazon.com/blogs/compute/analyzing-genomics-data-at-scale-using-r-aws-lambda-and-amazon-api-gateway/ which describes setting up R to run with python. I, on the other hand, am trying to get R to work with NodeJs.
I've packaged up my dependencies, deployed to Lambda, and can run simple Node scripts. However, I am having difficulty connecting to RServe from Node using the npm package Rio (https://www.npmjs.com/package/rio). RServe, on both my localhost and on Heroku, will accept the default connection of 127.0.0.1 and port 6331. No luck with AWS Lambda.
'use strict';
var rio = require('rio');
var Promise = require('bluebird');
var exec = require('child_process').exec;
var whenReady = new Promise(function(resolve){
// require libraries and bootup RServe
exec('Rscript init.R', function(error, stdout, stderr) {
(function check() {
// Attempt to connect to RServe through Rio using my 'up' test function
rio.e({
entrypoint: 'up',
callback: function (err) {
console.log(err);
if (err) return setTimeout(check, 100);
// If no connection error, rserve is running
console.log("Rserve up");
resolve();
}
});
})();
});
});
exports.handler = function(event, context, callback) {
whenReady.then(function () {
// Call hello world
rio.e({
entrypoint: 'hello',
data: {name:'Will'},
callback: function(err, result){
console.log("Error", err);
callback(null, result);
}
});
});
};
This ends with connection refused errors
2017-03-01T22:58:33.210Z 96f69baf-fed2-11e6-9164-e91b9773d645 {
[Error: connect ECONNREFUSED 127.0.0.1:6311] code: 'ECONNREFUSED',
errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1',
port: 6311 }
Any ideas on how to fix this one? I'm hoping we don't need to get complicated: https://aws.amazon.com/blogs/aws/new-access-resources-in-a-vpc-from-your-lambda-functions/
Thank you in advance!
** Update **
init.R does the following
// Require some libraries
...
require('jsonlite');
up <- function () {
toJSON(TRUE)
}
run.Rserve()
** Last Update **
Gave up and went to the python example as posted in the first link.
Will

Node.js Error : connect ECONN Refused

I am new to Node.js and am unable to resolve this error:
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect (as oncomplete) (net.js:892)
The code I was trying out follows :
var async = require('async'),
request = require('request');
function done(err,results) {
if (err) {
throw err;
}
console.log('Done ! results: %j',results);
}
var collection = [1,2,3,4];
function iterator(value,callback) {
request.post({
url: 'http://localhost:8080',
body: JSON.stringify(value)
}, function (err,res,body){
if (err) {
callback(err,body && JSON.parse(body));
}
});
}
async.map(collection,iterator,done);
ECONNREFUSED – Connection refused by server error
A port is being blocked can be the root cause of this issue, check if your connection is being blocked or even the changed default port can also cause this issue. Identify which app/service you are connecting to and its port is being blocked or changed.
And in your case check whether the application is hosted on port: 8080 or not.
But, this most likely occurs with FileZilla.

Coffeescript HTTP client

I am running server.coffee from this page: https://github.com/xenph/foaas
From the command line, you can do:
curl http://localhost:5000/off/Name1/Name2
!##$ off, Name1. - Name2
But I'm using the code from this page: http://coffeescriptcookbook.com/chapters/networking/basic-http-client
http = require 'http'
http.get { host: 'http://localhost:5000/off/Name1/Name2' }, (res) ->
data = ''
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
console.log data
The error I get is:
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
Which leads me to believe that it's not finding the url.
What can I do?
try
http.get 'http://localhost:5000/off/Name1/Name2', (res) ->
or
http.get { hostname: 'localhost', port: 5000, path: '/off/Name1/Name2' }, (res) ->
instead of
http.get { host: 'http://localhost:5000/off/Name1/Name2' }, (res) ->
maybe?
Firstly extract a little more info from exceptions:
.on('error',function(e){
console.log("Error: " + hostNames[i] + "\n" + e.message);
console.log( e.stack );
});
Secondly as curl is working we can assume that the service is up and accessible from localhost so there must be a deeper issue. What flavour of linux are you using? I've solved similar issues before by disabling firewall (iptables) and selinux.
Also double check to make sure that you have dns configured and that it will return 127.0.0.1 for localhost. Use nslookup.

Resources