nodejs cloudflare ERR_CERT_AUTHORITY_INVALID - javascript

When I build my site with ssl on cloudflare my site works clearly
but in nodejs ws server i change with wss server
and read key and pem files on nodejs
I got an error console like this ERR_CERRT_AUTHORITY_INVALID
how can i fix this error? Any idea?
Notes: my wss server port is :8443
I try cloudflare ssl optiond flexible, full and full (stricth)
My app is live chat script coded with nodejs
Here is the code:
var https = require('https');
var fs = require('fs');
var webSocketsServerPort = 8443;
var webSocketServer = require('websocket').server;
var options = { key: fs.readFileSync('key.key'), cert: fs.readFileSync('x.pem') };
var server = https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("deneme\n");
}).listen(webSocketsServerPort);
server.listen(webSocketsServerPort, function () {
console.log((new Date()) + " server geldi " + webSocketsServerPort);
});
var wsServer = new webSocketServer({ httpServer: server });

Related

Javascript client will not connect to node.js server through websocket

I am trying to connect a client, ran in a web browser, to a server using websockets.
My client is written in javascript and my server is node.js. I have managed to connect a node.js client with my server but cannot seem to connect my javascript client to the server.
server code:
var webSocketServer = require("websocket").server;
var http = require("http");
var port = 9000;
var server = http.createServer(function (request, response) {
console.log(new Date() + " Received request");
});
server.listen(port, function () {
console.log(new Date() + " listening on port 9000");
});
var socket = new webSocketServer({
httpServer: server
});
socket.on("request", function (request) {
var connection = request.accept(null, request.origin);
connection.on("message", function (message) {
console.log(message.utf8Data);
});
});
client code:
var client = new WebSocket("ws://localhost:9000");
client.onopen = function () {
client.send("Handshake from client");
}
I expected to see the handshake from client output in my command prompt from which I ran my server. This does not happen. In my browsers console I eventually get this error:
WebSocket connection to 'ws://localhost:9000/' failed: WebSocket opening handshake timed out
I tried to console.log my client object and the output was this:
WebSocket {url: "ws://localhost:9000/", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
So as far as I know the object is being built correctly and pointing at the correct URL but still fails to make the connection.

Socket.io client doesn't receive message from Server

I'm using Socket.IO for websockets and I want clients receive a welcome message in console from server when they connect but it's not working:
Server:
var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();
var options = {
key:
fs.readFileSync('/myfolder/mykey.pem'),
cert:
fs.readFileSync('/myfolder/mychain.pem')
};
var serverPort = 3080;
var server = https.createServer(options,app);
var io = require('socket.io')(server);
app.get('/',function(req,res){
res.sendFile(__dirname+'/index.html');
});
server.listen(serverPort, function(){
console.log('Server is working');
//console.log(__dirname);
});
io.on('connection', function(socket){
console.log("Connected!");
socket.broadcast.emit("Welcome","Good day sunshine!");
});
Client:
<script src="https://localhost:3080/socket.io/socket.io.js"></script>
<script>
var URL_SERVER = 'https://localhost:3080';
var socket = io.connect(URL_SERVER);
socket.on("Welcome", function(data){
console.log(data);
});
</script>
I'm getting message console in server side but not the server answer in the console client.
How can I fix it?
To broadcast, simply add a broadcast flag to emit and send method
calls. Broadcasting means sending a message to everyone else except
for the socket that starts it.
Reference : https://socket.io/docs/

Chat application using socket.io over https

I've been working on a chat application using node.js and socket.io . I'm trying to run it over https .I'm a newbie in node.js and socket.io so please bear with me . Every help will be highly appreciated. I follow the instructions from here : http://kaworu.jpn.org/javascript/node.js%E3%81%AB%E3%82%88%E3%82%8BHTTPS%E3%82%B5%E3%83%BC%E3%83%90%E3%81%AE%E4%BD%9C%E3%82%8A%E6%96%B9
but i get this error :
GET https://mydomain.link:3000/socket.io/?EIO=3&transport=polling&t=1502934404775-5 net::ERR_INSECURE_RESPONSE
here is the code
server.js
var https = require('https');
var fs = require('fs');
var ssl_server_key = 'server_key.pem';
var ssl_server_crt = 'server_crt.pem';
var port = process.env.PORT || 3000;
var options = {
key: fs.readFileSync(ssl_server_key),
cert: fs.readFileSync(ssl_server_crt)
};
https.createServer(options, function (req,res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end("Hello, world\n");
}).listen(port);
clientside (chatnew.js)
$(function(){
var socket = io.connect('https://mydomain.link:3000');
});
Thank you in advance !

Local node.js server GET error when trying to load an HTML file

Quite new to this. I have the below node sever that runs locally on my machine. In index.html page I made a XMLHttpRequest to some web API, and my browser will be redirected to a url that starts with 10.200.100.200:8080/auth(suppose 10.200.100.200 is my IP)
The problem is that, currently a GET request to http://10.200.100.200:8080/auth returns an empty response. It doesn't work either when I paste that url to browser. What am I doing wrong with that node server? The index page seems to be working ok. Many thanks for your help!
node server:
var fs = require('fs');
var http = require('http');
var https = require('https');
var request = require('request');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var path = require('path');
var express = require('express');
var app = express();
var certificate = fs.readFileSync( 'something.0.0.1.cert' );
var privateKey = fs.readFileSync('something.0.0.1.key');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
app.use(express.static(__dirname+'/public'));
app.get('/auth', function(req, res) {
res.sendFile(path.join(__dirname + '/public'+ '/html'+'/authCode.html'));
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/public'+ '/html'+'/index.html'));
});
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(8080,'0.0.0.0');

Getting socket.io, express & node-http2 to communicate though HTTP/2

I wrote a Web Socket server using socket.io, node-http2 and express in Node.js. The server works as intended, except for the fact that according to Chrome's DevTools socket.io's negotiation requests go through HTTP/1.1 (shown below). The "Protocol" column should be displaying h2 if the request was sent using HTTP/2.
This only happens in Chrome, other browsers use the correct protocol.
The server code (shortened):
var PORT = 8667,
config = require('./config'),
socketioServer = require('socket.io'),
app = express(),
https = require('http2'),
cors = require('cors');
app.use(cors(function(req, callback){
var corsOptions = { origin: false };
if (/^https:\/\/mlpvc-rr\.lc/.test(req.header('Origin')))
corsOptions.origin = true;
callback(null, corsOptions);
}));
app.get('/', function (req, res) {
res.sendStatus(403);
});
var server = https.createServer({
cert: fs.readFileSync(config.SSL_CERT),
key: fs.readFileSync(config.SSL_KEY),
}, app);
server.listen(PORT);
var io = socketioServer.listen(server);
// ...
Browser connection code:
var conn = io('https://ws.'+location.hostname+':8667/', { reconnectionDelay: 5000 });
conn.on('connect', function(){
console.log('[WS] Connected');
});
conn.on('disconnect',function(){
console.log('[WS] Disconnected');
});
Output of testssl.sh:
What do I need to change to make the socket.io requests go through HTTP/2?
A little bit late but with Express4 and Spdy (npm) is working great.
bin/www:
var app = require('../app');
var debug = require('debug')('gg:server');
var spdy = require('spdy');
var fs = require('fs');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var options = {
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
}
var server = spdy.createServer(options, app);
var io = app.io
io.attach(server);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
...
app.js:
...
var app = express();
var io = app.io = require('socket.io')();
...
client screenshot:
As discussed in comments Chrome has recently stopped allowing the older NPN negotiation for HTTP/2 and insists on the newer ALPN protocol instead. See this article for more info: https://ma.ttias.be/day-google-chrome-disables-http2-nearly-everyone-may-31st-2016/
So you basically need Node.js to support ALPN which it looks as has only been added in v5 so far: https://github.com/nodejs/node/pull/2564 . An alternative would be to route your NodeJs calls through a webserver which is easier to upgrade OpenSSL (e.g. Nginx or Apache) to support HTTP/2 over ALPN.
You confirmed this was the issue by using the testssl.sh program which confirmed no ALPN support and the fact Firefox uses HTTP/2.

Categories