|
| 1 | +const WebSocket = require('ws'); |
| 2 | +const Redis = require("ioredis"); |
| 3 | +const isAllow = require('./src/blacklist'); |
| 4 | + |
| 5 | +const port = 8080; |
| 6 | +//https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback |
| 7 | +const wss = new WebSocket.Server({ |
| 8 | + port: port, |
| 9 | + maxPayload: process.env.MAX_PAYLOAD ? process.env.MAX_PAYLOAD : 1000, |
| 10 | + verifyClient: function(info) { |
| 11 | + // console.log(process.env); |
| 12 | + console.log('Origin:', info.origin); |
| 13 | + let allowedOrigins = process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS : 'origin-unknown'; |
| 14 | + allowedOrigins = allowedOrigins.split(','); |
| 15 | + console.log('Allowed Origins:', allowedOrigins); |
| 16 | + let verified = false; |
| 17 | + allowedOrigins.forEach(function each(allow) { |
| 18 | + if(info.origin === allow) { |
| 19 | + verified = true; |
| 20 | + } |
| 21 | + }); |
| 22 | + return verified; |
| 23 | + } |
| 24 | +}); |
| 25 | +console.log('Start WebSocket.Server on: %s', port); |
| 26 | + |
| 27 | +const createRedisClient = function(data) { |
| 28 | + const sessionId = data.sessionId ? data.sessionId : 'xxxx'; |
| 29 | + const client = new Redis({ |
| 30 | + keyPrefix: `${sessionId}:`, |
| 31 | + host: process.env.REDIS_HOST ? process.env.REDIS_HOST : '127.0.0.1' |
| 32 | + }); |
| 33 | + console.log('Create new Redis client with prefix: %s', sessionId); |
| 34 | + return client; |
| 35 | +}; |
| 36 | + |
| 37 | +const noop = function() {}; |
| 38 | + |
| 39 | +const heartbeat = function() { |
| 40 | + console.log('pong heartbeat...'); |
| 41 | + this.isAlive = true; |
| 42 | +}; |
| 43 | + |
| 44 | +const interval = setInterval(function ping() { |
| 45 | + wss.clients.forEach(function each(ws) { |
| 46 | + if (ws.isAlive === false) { |
| 47 | + console.log('terminate ws!'); |
| 48 | + return ws.terminate(); |
| 49 | + } |
| 50 | + |
| 51 | + ws.isAlive = false; |
| 52 | + ws.ping(noop); |
| 53 | + }); |
| 54 | +}, 10000); |
| 55 | + |
| 56 | +wss.on('connection', function connection(ws) { |
| 57 | + |
| 58 | + ws.isAlive = true; |
| 59 | + ws.on('pong', heartbeat); |
| 60 | + ws.on('ping', console.info); |
| 61 | + |
| 62 | + // https://github.com/websockets/ws/issues/1417 |
| 63 | + ws.on('error', console.error); |
| 64 | + |
| 65 | + ws.on('open', function open() { |
| 66 | + console.log('open'); |
| 67 | + }); |
| 68 | + |
| 69 | + ws.on('close', function close() { |
| 70 | + console.log('close'); |
| 71 | + }); |
| 72 | + |
| 73 | + ws.on('message', async function incoming(data) { |
| 74 | + console.log('message: %s', data); |
| 75 | + let request,response = {}; |
| 76 | + let client; |
| 77 | + |
| 78 | + try { |
| 79 | + request = JSON.parse(data);//from string to object |
| 80 | + } catch (ex) { |
| 81 | + response.command = 'unknown'; |
| 82 | + response.output = 'Invalid JSON'; |
| 83 | + response.status = 'error'; |
| 84 | + return ws.send(JSON.stringify(response)); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + client = createRedisClient(request); |
| 89 | + client.on("error", function(error) { |
| 90 | + console.log(error); |
| 91 | + }); |
| 92 | + |
| 93 | + const commandString = request.command; |
| 94 | + const commandArray = commandString.split(" "); |
| 95 | + const command = commandArray[0]; |
| 96 | + const args = commandArray.slice(1); |
| 97 | + |
| 98 | + response.command = commandString;//early set |
| 99 | + |
| 100 | + if(!isAllow(command)) { |
| 101 | + throw new Error('Command not allowed'); |
| 102 | + } |
| 103 | + |
| 104 | + response.status = '-'; |
| 105 | + response.output = await client.send_command(command,args); |
| 106 | + response.status = 'ok'; |
| 107 | + |
| 108 | + } catch (ex) { |
| 109 | + // console.log(ex); |
| 110 | + console.log('Exception on send_command'); |
| 111 | + response.output = ex.message; |
| 112 | + response.status = 'error'; |
| 113 | + } finally { |
| 114 | + client.quit(); |
| 115 | + //send response |
| 116 | + return ws.send(JSON.stringify(response)); |
| 117 | + } |
| 118 | + |
| 119 | + }); |
| 120 | +}); |
0 commit comments