Re-send post request to antoher server - node.js

I would like to re-send any POST request from my server (1234 port) to another server (another.server.com:80). Note: Post requests are soap calls.
This is my code:
var http = require('http');
var LISTEN_PORT = 1234;
var HOST = 'another.server.com';
var PORT = 80;
http.createServer(onRequest).listen(LISTEN_PORT);
function onRequest(client_req, client_res) {
var options = {
hostname: HOST,
port: PORT,
path: client_req.url,
method: 'POST'
};
var proxy = http.request(options, function (res) {
res.pipe(client_res, {
end: true
});
});
client_req.pipe(proxy, {
end: true
});
}
But it does not work.

Related

nodejs http-proxy not passing URL

I'm using http-proxy module and I'm trying to send the request to port 1234 and gets its reply back.
But in the apache logs I can see that request is only to /. The documentation says to use toProxy: true in order to pass the URL but it isn't working.
https://github.com/http-party/node-http-proxy
toProxy: true/false, passes the absolute URL as the path (useful for proxying to proxies)
Here is the code:
var http = require('http');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
var fs = require('fs');
http.createServer(function(req, res) {
return proxy.web(req, res, { target: {host: 'localhost', port: 1234},
ssl: {
key: fs.readFileSync('/root/test.pem', 'utf8'),
cert: fs.readFileSync('/root/test_cert.pem', 'utf8')
},
toProxy: true
});
}).listen(3000);
I'm using curl to test and checking the apache logs which is listening on port 1234.
curl -v http://localhost:3000/getsomething.html
Figured it out. I had to include protocol.
return proxy.web(req, res, { target: { protocol: 'https:', host: 'localhost', port: 1234},

Redirect http to https in Hapi.js

I do have the following configuration for my hapi server
const server = new Hapi.Server();
const tls = {
cert: fs.readFileSync(path.join(__dirname, '../certificates/cert.crt')),
key: fs.readFileSync(path.join(__dirname, '../certificates/cert.key')),
};
server.connection({
port: process.env.PORT_HTTP || 80,
host: process.env.HOST || 'localhost',
});
server.connection({
port: process.env.PORT_HTTPS || 443,
host: process.env.HOST || 'localhost',
tls,
});
The server is working ok on both, http and https, but I would like to redirect all the traffic from the http to https.
How should I proceed, tried already to register the hapi-require-https npm module but the traffic still remain the same, nothing happens.
Create an extra server for http requests and bind them to redirect function.
var Hapi = require('hapi');
var http = new Hapi.Server(80);
var server = new Hapi.Server(443, { tls: {} });
var redirect = function () {
this.reply.redirect('https://your.site/' + this.params.path);
});
http.route({ method: '*', path: '/{path*}', handler: redirect });
Update(other option)
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
if(request.headers.referer.split(':')[0] == "http"){
this.reply.redirect('https://your.site' + this.params.path);
}
}
});
How about this? Binding them both
var http = new Hapi.Server(80); // our extra server
http.route({
method: '*',
path: '/{path*}',
handler:
function (request, reply) {
// if(request.headers.referer.split(':')[0] == "http"){
this.reply.redirect('https://your.site' + this.params.path);
// }
}
});
Create two server instances to handle http & https traffic seperately.
var Hapi = require('hapi');
var server = new Hapi.Server(80);
var httpsServer = new Hapi.Server(443, { tls: { // your certificates here} });
Now register the hapi-gate plugin to the base server so that it redirects the traffic to https.
server.register({
register: require('hapi-gate'),
options: {https: true} // will force https on all requests
});
You can also use the hapi-require-https plugin instead.

Create Websocket Secure on a HTTPS web server in express app

I have a application in express.js. Am unable to create wss on a HTTPS web server.
var fs = require('fs');
var express = require('express');
var app = express();
var cfg = {
ssl: true,
port: 8001,
ssl_key: 'sslcert/ssl.key',
ssl_cert: 'sslcert/ssl.crt'
};
var httpServ = (cfg.ssl) ? require('https') : require('http');
if (cfg.ssl) {
httpsServer = httpServ.createServer({
key: fs.readFileSync(cfg.ssl_key),
cert: fs.readFileSync(cfg.ssl_cert)
}, app)
.listen(cfg.port, function () {
console.log('Magic happening at https://Secure');
});
} else {
httpsServer = httpServ.createServer(app)
.listen(cfg.port, function () {
console.log('Magic happening at http://InSecure');
});
}
var WebSocketServer = require('websocket')
.server;
var wsServer = new WebSocketServer({
httpServer: httpsServer
});
wsServer.on('connection', function connection(ws) {
console.log('Socket Connected');
});
I'm under the impression that passing a reference of the web server to WebSocket, then WebSocket would know the port and SSL capabilities of the server. But every time I get an error in the console
WebSocket connection to 'ws://localhost:8001/?trackme=TUKOCVPAF' failed: Connection closed before receiving a handshake response
How can I create a wss://localhost:8001/?trackme=TUKOCVPAF' when creating the Websocket..??

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