My socket.io project "xampp" works fine for localhost.
I have a Centos 7 (cPanel) server. I did everything when I threw files into my server. I wrote the command "nodemon server.js" and the server is running. But I am having SSL-related problems with Client.
My site has SSL. I'm using CloudFlare. For this reason, the client cannot communicate with the server.
My project is running when I disable SSL.
How does Socket.io work with SSL?
CLOUDFLARE:
CHROME CONSOLE LOG:
Client CODE:
var socket = io.connect('https://example.com:1347');
Server.js CODE
var server = require('https').createServer(),
io = require('socket.io')(server),
port = 1337;
server.listen(port);
Firstly if you use CloudFlare. You should know SSL Ports.
HTTPS ports supported by Cloudflare:
443 2053 2083 2087 2096 8443
Use whichever port is empty on your server. Recommended: (8443)
Connect to your server with PUTTY. Then locate the directory of the domain on your server. If you are using cpanel. Follow this road.
cd /home/domainname/public_html
You will come to the site's home directory.
Then you need to create the necessary files for SSL to the server. Enter the individual commands below.
openssl genrsa -out privatekey.pem 2048
openssl req -new -key privatekey.pem -out certrequest.csr
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
Client CODE:
var socket = io.connect('https://example.com:8443', {secure: true});
Server.js CODE
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
};
var server = https.createServer(options);
var io = require('socket.io').listen(server);
var port = 8443; // Enter any of the cloudflare ports.
server.listen(port, function(){
console.log('listening : ' + port);
});
Related
I am running a development Web API server on Node.js in Express environment. The server uses the Greenlock module for automatic Let's Encrypt certificate issuance.
When I run npm start prod, de server starts successfully and from the console log I can see that the server is listening on ports 80 and 443 for https connections:
Listening on 0.0.0.0:80 for ACME challenges, and redirecting to HTTPS
Listening on 0.0.0.0:443 for secure traffic
Ready to Serve:
web*****.club
ACME Directory URL: https://acme-v02.api.letsencrypt.org/directory
The NameSilo domain web*****.club points to my router's public ip address through an A record, and in the router, port 443 is mapped to port 443 and forwarded to one of the computers on my LAN where the API server resides.
As soon as I make a https API request from a webpage, the server crashes and it produces the following output:
Error cert_issue:
read ECONNRESET
code: ECONNRESET
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
The certificate issuance seems to be ok, because in the .config/acme dir the Let's Encrypt certificate with the private and the public key is added directly after the https request.
(Edit)
The server works, when accessed over https locally, and also when accessed locally over http.
It now also works from the domain name. I made a https server for port 443, without Greenlock, issued certificate cert.key/pem with help of openssl:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout cert.key -out cert.pem -sha256
used the following code in server.ts:
const express = require('express');
var app = require("../dist/app.js");
const https = require('https');
const fs = require('fs');
const port = 443
const httpsOptions = {
key: fs.readFileSync('./security/cert.key'),
cert: fs.readFileSync('./security/cert.pem')
}
const server = https.createServer(httpsOptions, app)
.listen(port, () => {
console.log('https server running on localhost at ' + port)
})
Thanks for your assistance!
I have created a Node js backend for my React application which is using https protocol.
For this :- I created SSL using these commands:-
openssl genrsa 1024 > private.key
openssl req -new -key private.key -out cert.csr
openssl x509 -req -in cert.csr -signkey private.key -out certificate.pem
And then i use that certificate to create Secure Server https:-
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
app.set('secPort',port+443);
/**
* Create HTTPS server.
*/
var options = {
key: fs.readFileSync(__dirname+'/private.key'),
cert: fs.readFileSync(__dirname+'/certificate.pem')
};
var secureServer = https.createServer(options,app);
/**
* Listen on provided port, on all network interfaces.
*/
secureServer.listen(app.get('secPort'), () => {
console.log('Server listening on port ',app.get('secPort'));
});
secureServer.on('error', onError);
secureServer.on('listening', onListening);
My Question is will I get ssl error when i deploy it on heroku? If yes, then what should i do to make it deployable with secure server?
You can include self signed certificate but users will see a warning message when coming to your site.
Heroku mentions they offer free SSL certificates but that's really not the case unless you have a Hobby ($7/mo) or Pro plan.
Although you can get a free certificate, it cannot be included in a free Heroku app.
on my apache server, I used that to allow nodejs to use ssl
var ssl = {
key: fs.readFileSync('/etc/letsencrypt/live/mysite.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/mysite.com/cert.pem')
};
My client switched his http server to Nginx ruled by PLESK
I tried with:
var ssl = {
key: fs.readFileSync('/usr/local/psa/var/certificates/cert-lcQuQ3'),
cert: fs.readFileSync('/usr/local/psa/var/certificates/cert-RVySSD')
};
and not good: Infact I have no idea where are the equivalent to privkey.pem and cert.pem with nginx
Any idea .
I'm trying to create a simple https server on Amazon EC2 to test a third party API.
Here are the steps I've followed:
Created an Amazon EC2 instance, and opened up HTTP and HTTPS ports:
Created simple ssl credentials using
openssl genrsa 2048 > privatekey.pem
openssl req -new -key privatekey.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey privatekey.pem -out
server.crt
Created a simple node js server
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('./privatekey.pem'),
cert: fs.readFileSync('./server.crt')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8080);
When I run the server, and attempt to connect to it using url https://ec2-XX-XXX-XXX-XXX.compute-1.amazonaws.com/, I keep getting a connection refused.
A telnet test also produces:
Trying XX.XXX.XXX.XXX...
telnet: connect to address XX.XXX.XXX.XXX: Connection refused
telnet: Unable to connect to remote host
Can someone please tell me what I need to fix to enable https on this EC2 instance?
Change your listen(8080) to listen(443) unless you have a web server listening on 443 and sending request to node on 8080.
I am trying to set up a telegram bot with nodejs https server with self singed certificate.
ssl certificate:
openssl req -newkey rsa:2048 -sha256 -nodes -keyout key.pem -x509 -days 365 -out crt.pem -subj "/C=IR/ST=A.Sh/L=Tabriz/O=DominoSystem/CN=5.235.36.42"
The very simple server:
var options = {
key : fs.readFileSync(__dirname + '/key.pem'),
cert: fs.readFileSync(__dirname + '/crt.pem')
};
https.createServer(options, function (req, res) {
console.log('https server');
console.log(req.url);
res.end('yoo hooo');
}).listen(8443,'0.0.0.0');
The server is accessible from internet : https://5.235.36.42:8443/
Telegram bot setWebhook returns ok {"ok":true,"result":true,"description":"Webhook was set"}
I can see in my filewall logs and DU Meters "Open TCP Connections" that nodejs.exe is receiving a connection from one of telegram's data centers and it always have the status ESTABLISHED and sometimes SYN_RCVD and then closes with send&receive=0KB but my nodejs server is not receiving any requests.
I have allowed my firewall(Comodo) to ACCEPT all incoming connections on port 8443.
I have been bashing my head around for 2 days now :( can someone help me pliz...
Windows 8.1 x64, Nodejs 5.9.1
OK, fixed it myself, it seems that Telegram didn't like the certificate generated by openssl on windows (8.1).
I generated the certificate on my linux (CentOS6) server and now it works :D, both in the server and in the local dev machine.