OpenShift : Error implementing DNS Client using nodejs [SOLVED] - node.js

I am implementing a DNS Lookup client on OpenShift using Nodejs and "native-dns" is the npm module. However I am getting the below mentioned error whenever I try to lookup for the DNS records of the domain using 8.8.8.8 as the DNS server IP.
events.js:72
throw er; // Unhandled 'error' event
^ Error: bind EACCES
at errnoException (dgram.js:458:11)
at dgram.js:211:28
at dns.js:72:18
at process._tickCallback (node.js:442:13)
the code for the function is as follows
function DNS_Domain_Lookup_function(dns,domain_name,rbl, ns_ip, record_type ,DNS_cb) {
var dns_response = '';
var question = dns.Question({
name: domain_name + rbl,
type: record_type,
});
var req = dns.Request({
question: question,
server: { address: ns_ip, port: 53, type: 'udp' },
timeout: 4000,
});
req.on('timeout', function () {
if (rbl === '') {
rbl='query'
}
dns_response = { question: [ { name: domain_name + ',' + rbl} ], answer: [ { name: rbl, address: 'timeout' } ]};//Clean(2)
DNS_cb(dns_response); //work on this to get the exact output
});
req.on('message', function (err, answer) {
rbl !== '' ? (answer.question[0].name = domain_name + ',' + rbl) : (answer.question[0].name = domain_name);
DNS_cb(answer);
});
req.on('end', function () {
});
req.send();
}
Update : Cannot Implement DNS Server on OpenShift.

Related

Node JS Events Emitter on net.createserver

So i got a problem and stuck around 3 days, i'm using node js as a socket server to receive string and processing some json data to give a string response to socket client, stack used in node js(net, events), my code have no problem for the first request, but after the second request it got an error like this(server):
[2020-12-23T07:45:13.821Z] server listening on port: 4444
[2020-12-23T07:45:15.696Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:15.701Z] message: string
[2020-12-23T07:45:15.702Z] executing call function
[2020-12-23T07:45:15.703Z] oncall function
[2020-12-23T07:45:15.703Z] executing emit
[2020-12-23T07:45:15.704Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:15.707Z] Returning : diterima
[2020-12-23T07:45:42.403Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:42.407Z] message: string
[2020-12-23T07:45:42.408Z] executing call function
[2020-12-23T07:45:42.408Z] oncall function
[2020-12-23T07:45:42.409Z] executing emit
[2020-12-23T07:45:42.409Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.410Z] Returning : diterima
[2020-12-23T07:45:42.411Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.412Z] Returning : diterima
events.js:288
throw er; // Unhandled 'error' event
^
Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:451:14)
at HanaconsResponded.<anonymous> (C:\Users\ralfian\--\--\--.js:47:11)
at HanaconsResponded.emit (events.js:323:22)
at callFunction (C:\Users\ralfian\--\--\--.js:79:23)
at Socket.<anonymous> (C:\Users\ralfian\--\--\--.js:35:17)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:271:13)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on Socket instance at:
at emitErrorNT (net.js:1336:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EPIPE'
}
and bellow is error on the client side at second request, (first request have no problem):
CLIENT: I connected to the server.
{"orig":"orig","origParam":{"number":"123456"},"resp":[{"content":[{"gender":"not match"}],"firstPage":true,"lastPage":true,"number":0,"numb
erOfElements":1,"size":1,"sort":null,"totalElements":1},{"result":"UNMATCHED"}]}
events.js:288
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
Here is my server Code:
var net = require('net');
var server = net.createServer();
require('log-timestamp');
var hostport = 4444;
var EventEmitter = require('events');
class CheckResponded extends EventEmitter {}
var checkResponded = new CheckResponded();
server.listen({
port : hostport,
exclusive: true
},);
console.log('server listening on ' + 'port: ' + hostport);
server.on('connection', (e) => {
console.log( 'New Connection : ' + e.remoteAddress );
e.setEncoding('utf8');
e.setTimeout(60000);
e.on('end', () => {})
e.on( 'timeout', () => {
console.log('Socket Timeout. Reseting.');
e.end();
});
e.on( 'data', (buff) => {
console.log("message: " + typeof buff);
try {
if (buff === "exec"){
console.log("executing call function")
callFunction()
}
} catch(error){
console.log('error on socket -> ' + error)
}
});
checkResponded.on( 'event1', (f) => {
console.log(f)
var rmsg;
rmsg = f;
console.log("Returning : " + 'diterima');
e.write(JSON.stringify(rmsg));
});
});
function callFunction() {
console.log('oncall function')
var imsg = {
"orig": "orig",
"origParam": {
"number": "123456",
},
"resp": [
{
"content": [
{
"gender": "not match",
}
],
"firstPage": true,
"lastPage": true,
"number": 0,
"numberOfElements": 1,
"size": 1,
"sort": null,
"totalElements": 1
},
{
"result": "UNMATCHED"
}
]
}
console.log('executing emit')
checkResponded.emit('event1', imsg);
return
}
and bellow is the client request example:
const net = require('net');
var host = 'localhost';
const client = net.createConnection({ port: 4444, host: host }, () => {
console.log('CLIENT: I connected to the server.');
client.write('exec')
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('CLIENT: I disconnected from the server.');
});
looking for help , Thanks!
One thing I see is that your client code assumes that your entire response arrives in one data event. That is not a safe assumption. It may sometimes happen that way, but it is not guaranteed and if the server is not done sending data when you call client.end(), you will get an error (like you are) on the server such as:
Error: This socket has been ended by the other party
You will need some sort of protocol in the data you send so that you can parse the incoming data and you can then know when you have a complete response and when you need to wait for the rest of the data to arrive.
Similar issue discussed here: Detect complete data received on 'data' Event in Net module of Node.js

One signal create notification REST API fails sometimes

I am using oneSignal for push notifications node.js. I am using the create notification api to send notification to the users, but i dont know why it works some times and sometimes gives timeout error
sendNotificationToUser(data) {
try {
var notificationData = {}
notificationData.app_id = oneSignalAppId
notificationData.headings = {
en: "Heading"
}
notificationData.contents = {
en: data.message
}
notificationData.include_player_ids = [data.deviceId]
var headers = {
"Content-Type": "application/json; charset=utf-8"
}
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
}
var https = require("https")
var req = https.request(options, function (res) {
res.on("data", function (data1) {
console.log("Response:")
console.log(JSON.parse(data1))
})
})
req.on("error", function (e) {
console.log("ERROR:")
console.log(e)
})
req.write(JSON.stringify(notificationData))
req.end()
} catch (err) {
console.log("err in notification", err)
}
}
this api works 50% of times and 50% of times it responds with time out error, even all the inputs are correct
ERROR:
{
Error: connect ETIMEDOUT 104.18.225.52:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
errno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
address: '104.18.225.52',
port: 443
}
one simple solution is to directly hit the working ip, you can do this by including
host: 'onesignal.con' in headers and
host: '104.18.226.52' in options
this resolved my issue
to know more about how you can specify ip with host in https request
go here HTTPS request, specifying hostname and specific IP address

getting Error: getaddrinfo ENOTFOUND while performing rest api call in node.js using http.request

i have created api in node.js which consume set of api hosted at http://dev.abc.co.in:20081
not every time but randomly sometimes it throws the error
Error: getaddrinfo ENOTFOUND dev.abc.co.in
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'dev.abc.co.in'
}
to call those api i have used request node module because i started getting this error i switched to fetch-node npm module and finally replace the code with internal node module http but getting same error
here is the code i have written using http.request
try{
const options = {
hostname: "dev.abc.co.in",
port : 20081,
path: "/api/entity/workorder",
method: Config.method
};
if(Config.headers){
options.headers = Config.headers
}
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(res, data);
});
req.socket.destroy();
}).on("error", (err) => {
console.log("===Error: ", err);
callback(null, err);
});
if(Config.method!="GET" && Config.body){
Config.headers["Content-Length"] = Config.body.length;
req.write(Config.body);
}
req.end();
}catch(e){
console.log("Exception=====",e);
}
as shown in error message issue related to DNS so i try to resolve this DNS using
node -pe 'require("dns").lookup("dev-vsg.dovertech.co.in",function(){console.dir(arguments)})
but still not resolved.
1) Omit 'http://' from the beginning of your demain and all slashes from the end or any path after the actual domain.
2) Try to resolve your hostname:
const dns = require('dns');
dns.resolve("testdomain.com", 'ANY', (err, records) => {
if (err) {
console.log("Error: ", err);
} else {
console.log(records);
}
});
If dns records has been returned, then you will know it's a node js problem and after that we can investigate further. If not, then it's a domain configuration issue.

How should I do to make an elasticSearch , search query from Node.js correctly?

I am stuck in a problem and can not figure out the solution.
I want to use an elasticSearch query from my nodejs.
The problem is, I can make it work from postman, but not from node.
http://user:psd#my_domain:9200/ra_autocomplete/search
And from my nodejs app :
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'my_domain',
port : 9200,
protocol : 'http',
auth : 'user:psd',
maxRetries : 2
});
And then,
client.search({
index: "ra_autocomplete",
body: {
query: {
m_prefix : {
r_n : {
query : my_var
}
}
}
}
} , function(err, res) {
console.log(err);
console.log(res);
});
I get this error :
Error: Not Found
at respond (my_path\node_modules\elasticsearch\src\lib\transport.js:307:15)
at checkRespForFailure
(my_path\node_modules\elasticsearch\src\lib\transport.js:266:7)
at HttpConnector.
(my_path\node_modules\elasticsearch\src\lib\connectors\http.js:159:7)
at IncomingMessage.bound
(my_path\node_modules\lodash\dist\lodash.js:729:21)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
status: 404,
displayName: 'NotFound',
message: 'Not Found',
path: '/roads_autocomplete/_search',
query: {},
body: '{"query":{"m_prefix":{"r_n":{"query":"montexte a analyser"}}}}',
statusCode: 404,
response: '\r\n404 Not Found\r\n\r\n404 Not Found\r\nnginx/1.10.3 (Ubuntu)\r\n\r\n\r\n',
toString: [Function],
toJSON: [Function] }
Any help would be appreciated, the problem is, when I try to make it with postman, it goes well.
Thank you.
I think the problem is that you are not connecting to elastic search node.
Add your port and domain to into one line and then start by running a simple query.
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.search({
index: 'products',
type: 'product',
body: {
query: {
bool: {
}
}
}
}).then((body) => {
return body;
}, (error) => {
console.trace(error.message);
});

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.

Resources