Skip to content

Commit bc4ebd6

Browse files
committed
Add https support
1 parent 0b2eacf commit bc4ebd6

File tree

5 files changed

+72
-18
lines changed

5 files changed

+72
-18
lines changed

.eslintrc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ module.exports = {
173173
"allowForLoopAfterthoughts": true
174174
}
175175
],
176-
"no-process-env": "error",
176+
"no-process-env": "off",
177177
"no-process-exit": "off",
178178
"no-proto": "error",
179179
"no-prototype-builtins": "error",
@@ -234,7 +234,7 @@ module.exports = {
234234
"prefer-const": "off",
235235
"prefer-destructuring": "off",
236236
"prefer-numeric-literals": "error",
237-
"prefer-object-spread": "error",
237+
"prefer-object-spread": "off",
238238
"prefer-promise-reject-errors": "error",
239239
"prefer-reflect": "error",
240240
"prefer-rest-params": "error",
@@ -294,4 +294,4 @@ module.exports = {
294294
"never"
295295
]
296296
}
297-
};
297+
};

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ Start the server at a specific address and port:
112112

113113
By default the server is running on port **2048** and the websocket on port **8080**.
114114

115+
## SSL
116+
117+
If you like to run the server over https you could use the `ssl`, `key` and `cert` arguments:
118+
119+
npm run start -- --port 2048 --host example.com --websocket 8080 --ssl=1 --key=./server.key --cert=./server.crt
120+
121+
Another way is to set the following variables in your environment:
122+
123+
SSL_ENABLED=1
124+
SSL_KEY=./server.key
125+
SSL_CERTIFICATE=./server.crt
126+
127+
Notice that the ssl key and certificate files need to be readable by the application.
128+
115129
# Testing
116130

117131
You can then test the behavior by running queries against your database:

package-lock.json

Lines changed: 35 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "server.js",
66
"dependencies": {
77
"cross-env": "^5.2.0",
8+
"https": "^1.0.0",
89
"lodash": "^4.17.14",
910
"minimist": "^1.2.0",
1011
"mysql": "^2.17.1",

scripts/server.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
22
const WebSocketServer = require('websocket').server
3-
const http = require('http')
43
const net = require('net')
54
const fs = require('fs')
65

@@ -9,11 +8,26 @@ let connections = []
98

109
// parse any arguments
1110
const argv = require('minimist')(process.argv.slice(2))
11+
const http = require(process.env.SSL_ENABLED || argv.ssl ? 'https' : 'http')
1212

1313
const SERVER_PORT = argv.port ? parseInt(argv.port) : 2048
1414
const WEBSOCKET_PORT = argv.websocket ? parseInt(argv.websocket) : 8080
1515
const SERVER_ADDR = argv.host ? argv.host.replace(/['"]+/g, '') : '127.0.0.1'
1616

17+
const config = Object.assign({}, argv, process.env)
18+
19+
let credentials = {}
20+
21+
if (config.SSL_ENABLED || config.ssl) {
22+
const privateKey = fs.readFileSync(config.SSL_KEY || config.key, 'utf8')
23+
const certificate = fs.readFileSync(config.SSL_CERTIFICATE || config.cert, 'utf8')
24+
25+
credentials = {
26+
key: privateKey,
27+
cert: certificate
28+
}
29+
}
30+
1731
// create a listening socket
1832
net.createServer((sock) => {
1933
sock.on('data', (data) => {
@@ -31,7 +45,7 @@ net.createServer((sock) => {
3145
}).listen(SERVER_PORT)
3246

3347
// create a http server
34-
const server = http.createServer((request, response) => {
48+
const server = http.createServer(credentials,(request, response) => {
3549
console.log((new Date()) + ' Received request for ' + request.url)
3650
response.writeHead(404)
3751
response.end()
@@ -56,7 +70,8 @@ function originIsAllowed (origin) {
5670
}
5771

5872
function createTemplate () {
59-
var output = '<!DOCTYPE html>\n' +
73+
const protocol = config.SSL_ENABLED || argv.ssl ? 'wss' : 'ws'
74+
const output = '<!DOCTYPE html>\n' +
6075
'<html>\n' +
6176
' <head>\n' +
6277
' <title>Example of a user defined function (UDF) in MySQL</title>\n' +
@@ -68,7 +83,7 @@ function createTemplate () {
6883
' crossorigin="anonymous"></script>\n' +
6984
' <script>\n' +
7085
' $(document).ready(function() {\n' +
71-
' var ws = new WebSocket(\'ws://' + SERVER_ADDR + ':' + WEBSOCKET_PORT + '\', \'echo-protocol\');\n' +
86+
' var ws = new WebSocket(\'' + protocol + '://' + SERVER_ADDR + ':' + WEBSOCKET_PORT + '\', \'echo-protocol\');\n' +
7287
' ws.onmessage = function(event) {\n' +
7388
' $(\'body\').append(event.data + "<br />");\n' +
7489
' };\n' +

0 commit comments

Comments
 (0)