Node.js socket.io with ssl connection - node.js

I am trying to connect node.js server with socket.io using ssl. My server has windows 2008 server and iis installed. I intalled ssl my domain name on IIS and I can connect with my domain via http s://mydomain.com. But I cant connect my node.js server with ssl. I am using following code on my server,Have you got any ideas what the problem might be?
var fs = require('fs');
var options = {
pfx: fs.readFileSync('sslkey.pfx'),
passphrase:'password'
};
var express = require('express'),
app = express(),
server = require('https').createServer(options,app),
server2 = require('http').createServer(app),
io = require('socket.io').listen(server, {log: true});
function sendCrossDomain(req, res){
//return;
res.set('Content-Type', 'text/xml; charset=utf-8');
res.sendfile(__dirname + '/crossdomain-test.xml');
};
app.get('/crossdomain.xml', sendCrossDomain);
app.get('/', sendCrossDomain);
server.listen(9595);

Assuming there are no errors about your certificate when you run the node program,
I would check to make sure the firewall port is opened on TCP 9595.
You will also have to request the page with https://yourdomain.com:9595 in order to access the running program since it's SSL and on a different port than 443.

Related

How to properly create an HTTPS transparent proxy server with node?

I am using the npm package http-proxy that claims to help with that, however I am totally incapable to make it work. So far I have only success by making an transparent HTTP proxy server, however when it goes to create a transparent HTTPS proxy server then nothing happens.
I am using an Android device configured to use a proxy with the port where the proxy is expecting to be configured, but nothing is triggered in the nodejs side. Only if I have configured an HTTP proxy server then things seems to be working.
This is the code I have for the HTTPS:
var https = require('https');
var fs = require('fs');
var httpProxy = require('http-proxy');
var options = {
key: fs.readFileSync('./client-key.pem', 'utf8'),
cert: fs.readFileSync('./client-cert.pem', 'utf8')
};
var proxy = httpProxy.createProxyServer({
ssl: options
});
https.createServer(options, function (req, res) {
console.log("new", req.url);
proxy.web(req, res, {
target: req.url
});
}).listen(8000);
If I use the createServer from the http package then it works for http calls (as in that the callback is being fired), however it does not for https with these instruction. Anybody knows what am I doing wrong?
PS: I do not care if I have to use a different npm package.
you can use https://www.npmjs.com/package/transparent-proxy
It is built extending native net.createServer. It acts like a REAL transparent http proxy and It allows you, for example, to easy upstream requests to other proxies and make some requests-chaining through multiple proxies... etc...
Install
npm i transparent-proxy
Use
const ProxyServer = require('transparent-proxy');
//init ProxyServer
const server = new ProxyServer();
//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
console.log('TCP-Proxy-Server started!', server.address());
});
It works on Termux too :)

Socket.IO with Express will not connect using https (Apache2 Ubuntu16.04)

I am not very experienced with SSL certs and Im hopping someone can help me find out what I am doing wrong.
I am trying to host a NodeJS application with it's own port (*:1729) with Apache2 which has SSL enabled on port 443 (from which it servers a client application and not the NodeJS/Express/Socket.io application in question). When I set up Express with http it works fine, my client application communicates without error to the NodeJS application, however when I use https with express such as this:
this.express = require('express');
this.app = this.express();
var https = require('https');
var fs = require('fs');
var sslPath = '/etc/letsencrypt/live/yourdomain.example.com/';
var options = {
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
};
this.server = https.createServer(options, this.app);
this.io = require('socket.io').listen(this.server);
this.server.listen(1729);
When ever my client app tries to connect I get this error:
node: ../src/util-inl.h:196: TypeName* node::Unwrap(v8::Local<v8::Object>) [with TypeName = node::TLSWrap]: Assertion ``(object->InternalFieldCount()) > (0)' failed.
Aborted (core dumped)
Any advice or corrections that could point me in the right direction would be much appreciated, thanks!
Thank you of the help! :) Updating my NodeJS to the latest version made the error disappear and the application is communicating with the client with no error's as well.
node: Github issue # 3682
I just followed these steps.
AskUbuntu: How can I update my nodeJS to the latest version?

Solving a Proxy Error (Node.js server)

Basically I get a 502 Proxy Error when running my node.js app.
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /play.
Reason: Error reading from remote server
My server looks like this.
var express = require('express');
var https = require('https');
var http = require('http');
var path = require('path');
var fs = require('fs');
var mysql = require('mysql');
var app = express();
var options = {
key: fs.readFileSync('sslcert/keyfile.key', 'utf8'),
cert: fs.readFileSync('sslcert/crtfile.crt', 'utf8')
};
var httpsServer = https.createServer(options, app);
// stuff
httpsServer.listen(process.env.PORT);
I am really sorry if this is a noob question, actually I am still a beginner in things related to node.js. Thank you for your help!
Noël.
I just ran into the same problem. It's possible your problem was different, but in my case, the 502 error was coming from Apache. My httpd.conf file was referencing the same 2 certificate files that my node app was referencing.
Instead of using
var httpsServer = https.createServer(options, app);
try just
app.listen(3000);
I'm not sure exactly why it wasn't working. My theory is the node app was using these cert files, and apache was unable to access them, and thus creating this situation. However, just using a normal express app fixed the problem for me.
I hope this helps somebody!

Bridged Adapter Issue Windows 10 VB 5.0.12 Ubuntu 14.04

I'm learning Node.js and I've installed VirtualBox and there ubuntu server 14.04. Node.js is installed as well on ubuntu. I'm doing an exercise in which I created a server which is accessible from guest_localhost:3000 (in ubuntu). The thing is I've not installed any GUI (and I wish to continue in that way). I want to test that the server I created using Node.js is running. To do that I just need to go to server_localhost:3000.
Apparently everything is ok (for example ping host to guest ip), but for some reason it´s not working.
ping ok __ not working
`var http = require('http');
var handleRequest = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to Node Essential Training\n');
};
var server = http.createServer(handleRequest);
server.listen(3000, 'localhost');`
I'll appreciate any help.
Thanks in advance guys.
If you do not have any GUI to visibly verify your page layout, you should be able to access it from your windows machine if they are in the same local network by simply accessing the url http://your_ubuntu_local_ip:3000 where your_ubuntu_local_ip is the local ip your ubuntu machine has internally in your local network. You can find this ip by typing ifconfig in your ubuntu terminal and looking for the ip the network adapter you are using has.
It's solved. The problem was in the code to create the server after all. As I'm learning JavaScript and Node.js I'm watching a tutorial in Lynda.com. The code which doesn't work comes from Lynda. The other from this url: http://blog.modulus.io/build-your-first-http-server-in-nodejs
// THIS CODE DOES NOT WORK
//var http = require('http');
//
//var handleRequest = function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Welcome to Node Essential Training\n');
//};
//
//var server = http.createServer(handleRequest);
//
//server.listen(8080, 'localhost');
//THIS CODE WORKS
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=3000;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});

Problems installing SSL Certificates on Node.JS server

I bought SSL Certificates from Godaddy for our website, with two web servers on the same AWS EC2 instance - Apache Tomcat and Node/Express.
After installing the SSL Certificates on both the web servers, https://example.com is opening (Apache), but the GET request from Apache to Node is failing.
The GET request to Node is working fine with the self-generated certificate (though it shows a crossed out https). When we replace that with CA certificates, it is not working. Please see the relevant code below.
var https = require("https"); // https server core module
var fs = require("fs"); // file system core module
var express = require("express"); // web framework external module
// Setup and configure Express http server. Expect a subfolder called
“static” to be the web root.
var httpApp = express();
httpApp.configure(function() {
httpApp.use(express.static(__dirname + "/static/"));
});
// Start Express https server on port 443
var webServer = https.createServer(
{
key: fs.readFileSync("/pathtokeys/ssl.private.key"),
cert: fs.readFileSync("/pathtokeys/1.crt"),
cert: fs.readFileSync("/pathtokeys/2.crt"),
cert: fs.readFileSync("/pathtokeys/3.crt"),
passprase:"miljul123$$$"
},
httpApp).listen(4431);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer, {"log level":1});
var rtc = apsrtc.listen(httpApp, socketServer);
Any thoughts or suggestions? Thanks.

Resources