From 8741981989c513549a1d0053353bfc40761bedb2 Mon Sep 17 00:00:00 2001 From: dx9s Date: Fri, 7 Jul 2017 07:20:20 -0700 Subject: [PATCH 1/5] Added SSL/TLS testing reference example. Useful in testing your version of Node! --- .gitignore | 1 + README.md | 9 +++++++++ generate.sh | 3 +++ package.json | 3 ++- server-ssl.js | 21 +++++++++++++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 generate.sh create mode 100644 server-ssl.js diff --git a/.gitignore b/.gitignore index 82d30cc..dd89203 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ +*.pem diff --git a/README.md b/README.md index a2e83ad..daf2067 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,19 @@ # Socket.IO Fiddle ``` +$ sh generate.sh # create self-signed keys for server-ssl.js $ npm install $ npm start # run the server +$ npm run startssl # run the server over SSL/TLS $ npm run client # run the nodejs client ``` And point your browser to `http://localhost:3000`. Optionally, specify a port by supplying the `PORT` env variable. + +NOTE: SSL/TLS (startssl) defaults the port to 3001. Open browser to +'https://localhost:3001'. You will have to accept the self-signed warning +if used. Older versions of node are known to have issues. This is a way +to test your version of node. + +**Known versions to work with SSL/TLS: v8.1.3 -- expect newest versions of v6 and v4 to also work (untested)!** diff --git a/generate.sh b/generate.sh new file mode 100644 index 0000000..6155fa4 --- /dev/null +++ b/generate.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo "You can optionally answer the question, or 'enter' for defaults:" +openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 3650 -out cert.pem diff --git a/package.json b/package.json index ab1fe24..8b62eea 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "scripts": { "start": "node server.js", - "client": "node client.js" + "client": "node client.js", + "startssl": "node server-ssl.js" } } diff --git a/server-ssl.js b/server-ssl.js new file mode 100644 index 0000000..394ad51 --- /dev/null +++ b/server-ssl.js @@ -0,0 +1,21 @@ + +const express = require('express'); +const app = express(); +const fs = require('fs'); +const server = require('https').createServer({ + key: fs.readFileSync('./key.pem'), + cert: fs.readFileSync('./cert.pem'), +}, app); +const io = require('socket.io')(server); +const port = process.env.PORT || 3001; + +app.use(express.static(__dirname + '/public')); + +io.on('connect', onConnect); +server.listen(port, () => console.log('server listening on port ' + port)); + +function onConnect(socket) { + console.log('connect ' + socket.id); + + socket.on('disconnect', () => console.log('disconnect ' + socket.id)); +} From f0077cac4c7248e5dd5da0f0bd08a104753eef5d Mon Sep 17 00:00:00 2001 From: dx9s Date: Fri, 7 Jul 2017 07:22:50 -0700 Subject: [PATCH 2/5] Fix quotes around SSL/TLS URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index daf2067..15090c9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ And point your browser to `http://localhost:3000`. Optionally, specify a port by supplying the `PORT` env variable. NOTE: SSL/TLS (startssl) defaults the port to 3001. Open browser to -'https://localhost:3001'. You will have to accept the self-signed warning +`https://localhost:3001`. You will have to accept the self-signed warning if used. Older versions of node are known to have issues. This is a way to test your version of node. From a73b457047d31ba73037db03f90ac28118bb0c71 Mon Sep 17 00:00:00 2001 From: dx9s Date: Fri, 7 Jul 2017 07:27:01 -0700 Subject: [PATCH 3/5] changed whitespace to match upstream --- package.json | 2 +- server-ssl.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 8b62eea..f8da7ae 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ "scripts": { "start": "node server.js", "client": "node client.js", - "startssl": "node server-ssl.js" + "startssl": "node server-ssl.js" } } diff --git a/server-ssl.js b/server-ssl.js index 394ad51..6402bdf 100644 --- a/server-ssl.js +++ b/server-ssl.js @@ -3,8 +3,8 @@ const express = require('express'); const app = express(); const fs = require('fs'); const server = require('https').createServer({ - key: fs.readFileSync('./key.pem'), - cert: fs.readFileSync('./cert.pem'), + key: fs.readFileSync('./key.pem'), + cert: fs.readFileSync('./cert.pem'), }, app); const io = require('socket.io')(server); const port = process.env.PORT || 3001; @@ -15,7 +15,7 @@ io.on('connect', onConnect); server.listen(port, () => console.log('server listening on port ' + port)); function onConnect(socket) { - console.log('connect ' + socket.id); + console.log('connect ' + socket.id); - socket.on('disconnect', () => console.log('disconnect ' + socket.id)); + socket.on('disconnect', () => console.log('disconnect ' + socket.id)); } From 193a58349b59447300ccf3c928db371f6e2b1446 Mon Sep 17 00:00:00 2001 From: dx9s Date: Fri, 7 Jul 2017 07:20:20 -0700 Subject: [PATCH 4/5] Added SSL/TLS testing reference example. Useful in testing your version of Node! --- .gitignore | 1 + README.md | 7 ++++++- client.js | 14 +++++++++++++- generate.sh | 3 +++ server.js | 6 +++++- 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100755 generate.sh diff --git a/.gitignore b/.gitignore index 82d30cc..dd89203 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ +*.pem diff --git a/README.md b/README.md index a2e83ad..993055c 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,15 @@ # Socket.IO Fiddle ``` +$ sh generate.sh # create self-signed keys for server-ssl.js $ npm install $ npm start # run the server $ npm run client # run the nodejs client ``` -And point your browser to `http://localhost:3000`. Optionally, specify +And point your browser to `https://localhost:3000`. Optionally, specify a port by supplying the `PORT` env variable. + +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. + +**Known versions to work with SSL/TLS: v8.1.3 -- expect newest versions of v6 and v4 to also work (untested)!** diff --git a/client.js b/client.js index fe9879e..5677731 100644 --- a/client.js +++ b/client.js @@ -1,5 +1,17 @@ -const socket = require('socket.io-client')('http://localhost:3000'); +// for the following to work, you'll have to answer "localhost" at the Common Name question when running generate.sh +// > Common Name (e.g. server FQDN or YOUR name) []:localhost +const fs = require('fs'); +const socket = require('socket.io-client')('https://localhost:3000', { + rejectUnauthorized: true, // default value + ca: fs.readFileSync('./cert.pem') +}); + +// USE WITH CAUTION! The following disables the validation of the server's identity +// see https://nodejs.org/docs/latest/api/tls.html#tls_tls_createserver_options_secureconnectionlistener +// const socket = require('socket.io-client')('https://localhost:3000', { +// rejectUnauthorized: false +// }); socket.on('connect', onConnect); diff --git a/generate.sh b/generate.sh new file mode 100755 index 0000000..6155fa4 --- /dev/null +++ b/generate.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo "You can optionally answer the question, or 'enter' for defaults:" +openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 3650 -out cert.pem diff --git a/server.js b/server.js index 3d5ef34..76dc020 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,11 @@ const express = require('express'); const app = express(); -const server = require('http').createServer(app); +const fs = require('fs'); +const server = require('https').createServer({ + key: fs.readFileSync('./key.pem'), + cert: fs.readFileSync('./cert.pem'), +}, app); const io = require('socket.io')(server); const port = process.env.PORT || 3000; From 6666678dd3874c13ae2790c6189191ff408be5a0 Mon Sep 17 00:00:00 2001 From: dx9s Date: Wed, 30 Aug 2017 10:36:25 -0700 Subject: [PATCH 5/5] Initial commit (hopefully no cleanup) of merging SSL example and non-ssl example into one code base intended for merging into upstream master --- README.md | 10 ++++++---- client-ssl.js | 20 ++++++++++++++++++++ client.js | 14 +------------- generate.sh | 2 +- package.json | 3 +++ server-ssl.js | 21 +++++++++++++++++++++ server.js | 6 +----- 7 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 client-ssl.js create mode 100644 server-ssl.js diff --git a/README.md b/README.md index 993055c..7147bc8 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,16 @@ # Socket.IO Fiddle ``` -$ sh generate.sh # create self-signed keys for server-ssl.js +$ sh generate.sh # create self-signed keys for server-ssl.js and client-ssl.js $ npm install -$ npm start # run the server +$ npm start # run the server or optionally npm run server $ npm run client # run the nodejs client +$ npm run serverssl # run the server-ssl +$ npm run clientssl # run the nodejs client-ssl ``` -And point your browser to `https://localhost:3000`. Optionally, specify -a port by supplying the `PORT` env variable. +And point your browser to either `http://localhost:3000` or `https://localhost:3000` depending on which server is running. +Optionally, specify a port by supplying the `PORT` env variable. 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. diff --git a/client-ssl.js b/client-ssl.js new file mode 100644 index 0000000..5677731 --- /dev/null +++ b/client-ssl.js @@ -0,0 +1,20 @@ + +// for the following to work, you'll have to answer "localhost" at the Common Name question when running generate.sh +// > Common Name (e.g. server FQDN or YOUR name) []:localhost +const fs = require('fs'); +const socket = require('socket.io-client')('https://localhost:3000', { + rejectUnauthorized: true, // default value + ca: fs.readFileSync('./cert.pem') +}); + +// USE WITH CAUTION! The following disables the validation of the server's identity +// see https://nodejs.org/docs/latest/api/tls.html#tls_tls_createserver_options_secureconnectionlistener +// const socket = require('socket.io-client')('https://localhost:3000', { +// rejectUnauthorized: false +// }); + +socket.on('connect', onConnect); + +function onConnect(){ + console.log('connect ' + socket.id); +} diff --git a/client.js b/client.js index 5677731..fe9879e 100644 --- a/client.js +++ b/client.js @@ -1,17 +1,5 @@ -// for the following to work, you'll have to answer "localhost" at the Common Name question when running generate.sh -// > Common Name (e.g. server FQDN or YOUR name) []:localhost -const fs = require('fs'); -const socket = require('socket.io-client')('https://localhost:3000', { - rejectUnauthorized: true, // default value - ca: fs.readFileSync('./cert.pem') -}); - -// USE WITH CAUTION! The following disables the validation of the server's identity -// see https://nodejs.org/docs/latest/api/tls.html#tls_tls_createserver_options_secureconnectionlistener -// const socket = require('socket.io-client')('https://localhost:3000', { -// rejectUnauthorized: false -// }); +const socket = require('socket.io-client')('http://localhost:3000'); socket.on('connect', onConnect); diff --git a/generate.sh b/generate.sh index 6155fa4..14b8b73 100755 --- a/generate.sh +++ b/generate.sh @@ -1,3 +1,3 @@ #!/bin/bash -echo "You can optionally answer the question, or 'enter' for defaults:" +echo "You can optionally answer the question, or 'enter' for defaults: (recommend setting common name to localhost)" openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 3650 -out cert.pem diff --git a/package.json b/package.json index ab1fe24..a1b2933 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,9 @@ }, "scripts": { "start": "node server.js", + "serverssl": "node server-ssl.js", + "clientssl": "node client-ssl.js", + "server": "node server.js", "client": "node client.js" } } diff --git a/server-ssl.js b/server-ssl.js new file mode 100644 index 0000000..76dc020 --- /dev/null +++ b/server-ssl.js @@ -0,0 +1,21 @@ + +const express = require('express'); +const app = express(); +const fs = require('fs'); +const server = require('https').createServer({ + key: fs.readFileSync('./key.pem'), + cert: fs.readFileSync('./cert.pem'), +}, app); +const io = require('socket.io')(server); +const port = process.env.PORT || 3000; + +app.use(express.static(__dirname + '/public')); + +io.on('connect', onConnect); +server.listen(port, () => console.log('server listening on port ' + port)); + +function onConnect(socket){ + console.log('connect ' + socket.id); + + socket.on('disconnect', () => console.log('disconnect ' + socket.id)); +} diff --git a/server.js b/server.js index 76dc020..3d5ef34 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,7 @@ const express = require('express'); const app = express(); -const fs = require('fs'); -const server = require('https').createServer({ - key: fs.readFileSync('./key.pem'), - cert: fs.readFileSync('./cert.pem'), -}, app); +const server = require('http').createServer(app); const io = require('socket.io')(server); const port = process.env.PORT || 3000;