Node Fetch resolve host like curl request - node.js

can we replicate curl resolve host in node-fetch or any other node http library.
curl https://www.example.net --resolve www.example.net:443:127.0.0.1

You don't need another lib to do this. The builtin http module works fine:
const http = require('http');
const options = {
hostname: '2606:2800:220:1:248:1893:25c8:1946',
port: 80,
path: '/',
method: 'GET',
headers: {
'Host': 'example.com'
}
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.end();
In HTTP protocol, the host name is in headers, and the IP used to establish the TCP connection is independent from that.

Related

NodeJS https request failing with Error: socket hang up - code: ECONNRESET - failing on AWS EC2 but working perfectly locally on MacOS

I am using this code to connect to a 3rd party server via HTTP GET. Locally on my MacOS this script works perfectly and I get statusCode:200 together with a valid message from the server. Am I missing something which should be added to this request when connecting from AWS?
const https = require("https");
var fs = require("fs");
var httpsAgent = require("https-agent");
var agent = httpsAgent({
pfx: fs.readFileSync("certs/test.com.pfx"),
passphrase: "xxxxxx",
rejectUnauthorized: true,
//enableTrace: true,
ca: fs.readFileSync("certs/ca-bundle.pem"),
});
const path = "/testapp?application=TEST&method=send&message=TEST"
const options = {
hostname: "test.server.com",
port: 443,
path: path,
method: "GET",
agent: agent,
};
''
console.log("Connecting to: https://test.server.com" + path)
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", (d) => {
process.stdout.write(d);
});
});
req.on("error", (error) => {
console.error(error);
});
req.end();
Issue Solved: Issue was actually not related to SSL. Packet was being reject to invalid MTU size. Adjusted MTU value and worked as expected.

Error "ssl3_get_record:wrong version number" in node.js when making https post request

I'm trying to make a https request to an api of my work using node.js through the following script
const https = require('https');
const options = {
hostname,
path: fullPath,
port: 80,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('data', chunk => console.log('chunk:', chunk));
});
req.on('error', error => {
console.log('Failed!');
console.log(error);
});
req.write(data);
req.end();
And as a response i get
Failed!
Error: write EPROTO 6772:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332:
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:87:16) {
errno: 'EPROTO',
code: 'EPROTO',
syscall: 'write'
}
I made this request on a browser using jQuery and it worked
$.post({
url, data,
success: res => console.log(res),
});
I tryed to use the header X-SSL-PROTOCOL with no success.
The url and data are hidden because they are confidential, but they are the same in the jQuery example and in node.js.
port: 80,
method: 'POST',
...
const req = https.request(options, (res) => {
You are using HTTPS against port 80, which is usually plain HTTP. The response you get suggest that the server is not providing HTTPS on this port, which is actually the expected behavior. HTTPS is instead usually done on port 443.
I made this request on a browser using jQuery and it worked
It is unclear what url is in this example but if it just was https://domain/path then port 443 was used. Port 80 would have only been used with HTTPS if explicitly given, i.e. https://domain:80/path.

How to do HTTPS GET with client certificate in node

I can use curl for making a GET request ->
`curl -v https://example.com:82/v1/api?a=b` -E client_cert.pem:password
How can I use same in node. I tried request, superagent but not able to pass certificate.
Thanks in advance!
This worked for me -
var https = require('https');
var fs = require('fs');
var options = {
hostname: 'example.com',
port: 83,
path: '/v1/api?a=b',
method: 'GET',
key: fs.readFileSync('/path/to/private-key/key.pem'),
cert: fs.readFileSync('/path/to/certificate/client_cert.pem'),
passphrase: 'password'
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end()

multiple http connections to test NAT

I'am in a project where i need to establish the most possible http connections and keep them open for nat port testing, using node.js but I'm not sure how I could do it, till now i got this:
var http = require('http');
var http_options = {
hostname: '193.136.212.161',
port: 80,
path: '/',
method: 'GET',
agent: false,
headers: {
'Connection':'keep-alive'
}
};
var req = http.request(http_options)
.on("socket", function (socket) {
console.log('got connected!');
});
req.end();
unfortunely it closes the connection not keeping it alive, if i could have some tips to advance would be great.

How turn off curl's verification of the certificate in nodejs?

If I send curl -k "https://abc.com?querystring"
-k option to turn off curl's verification of the certificate
How to do this in nodejs If I want to make a GET request?
How to override all http GET request do it in the same way?
Thank you for your support.
Set the rejectUnauthorized option to false.
var https = require('https');
var req = https.request({
hostname: 'example.com',
port: 443,
path: '/',
method: 'GET',
rejectUnauthorized: false
}, function() { ... });
Check the following code:
var http = require('http');
var target = {
host : 'localhost',
port : 3000,
path : 'URL'
//pfx: Certificate, Private key and CA certificates to use for SSL. Default is null.
//cert: Public x509 certificate to use. Default null.
};
var Req_options = {
host: target.host,
port: target.port,
path: target.path,
agent: false,
method: 'GET'
};
callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = http.request(Req_options, callback);
req.end();
Updated as per comments:
In the above code, I have changed the https & target only as follows:
var https = require('https');
var target = {
host : 'www.google.co.in',
port : 443,
path : '/'
};
The output is as follows:
</html>
.........
.........
.........
attachEvent&&window.attachEvent("onload",n);google.timers.load.t.prt=e=(new Date).getTime();})();
</script></body></html>
For more information, check this Node.js API docs

Resources