Skip to content

Commit 193a583

Browse files
dx9sdarrachequesne
authored andcommitted
Added SSL/TLS testing reference example. Useful in testing your version of Node!
1 parent d16ebf2 commit 193a583

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
node_modules/
3+
*.pem

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
# Socket.IO Fiddle
33

44
```
5+
$ sh generate.sh # create self-signed keys for server-ssl.js
56
$ npm install
67
$ npm start # run the server
78
$ npm run client # run the nodejs client
89
```
910

10-
And point your browser to `http://localhost:3000`. Optionally, specify
11+
And point your browser to `https://localhost:3000`. Optionally, specify
1112
a port by supplying the `PORT` env variable.
13+
14+
You will have to accept the self-signed warning. Older versions of node are known to have issues. This is a way to test your version of node.
15+
16+
**Known versions to work with SSL/TLS: v8.1.3 -- expect newest versions of v6 and v4 to also work (untested)!**

client.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11

2-
const socket = require('socket.io-client')('http://localhost:3000');
2+
// for the following to work, you'll have to answer "localhost" at the Common Name question when running generate.sh
3+
// > Common Name (e.g. server FQDN or YOUR name) []:localhost
4+
const fs = require('fs');
5+
const socket = require('socket.io-client')('https://localhost:3000', {
6+
rejectUnauthorized: true, // default value
7+
ca: fs.readFileSync('./cert.pem')
8+
});
9+
10+
// USE WITH CAUTION! The following disables the validation of the server's identity
11+
// see https://nodejs.org/docs/latest/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
12+
// const socket = require('socket.io-client')('https://localhost:3000', {
13+
// rejectUnauthorized: false
14+
// });
315

416
socket.on('connect', onConnect);
517

generate.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
echo "You can optionally answer the question, or 'enter' for defaults:"
3+
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 3650 -out cert.pem

server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

22
const express = require('express');
33
const app = express();
4-
const server = require('http').createServer(app);
4+
const fs = require('fs');
5+
const server = require('https').createServer({
6+
key: fs.readFileSync('./key.pem'),
7+
cert: fs.readFileSync('./cert.pem'),
8+
}, app);
59
const io = require('socket.io')(server);
610
const port = process.env.PORT || 3000;
711

0 commit comments

Comments
 (0)