diff --git a/config/schema.js b/config/schema.js index c84f59c..02f287d 100644 --- a/config/schema.js +++ b/config/schema.js @@ -34,11 +34,6 @@ module.exports = convict({ format: String, default: '/myapp' }, - ip_limit: { - doc: 'Max connections per ip', - format: 'duration', - default: 100 - }, concurrent_limit: { doc: 'Max connections', format: 'duration', diff --git a/src/api/middleware/auth/index.js b/src/api/middleware/auth/index.js index 205f5de..77b750f 100644 --- a/src/api/middleware/auth/index.js +++ b/src/api/middleware/auth/index.js @@ -1,13 +1,12 @@ const config = require('../../../../config'); const realm = require('../../../services/realm'); +const { Errors } = require('../../../enums'); module.exports = (req, res, next) => { const { id, token, key } = req.params; - const sendAuthError = () => res.sendStatus(401); - if (key !== config.get('key')) { - return sendAuthError(); + return res.status(401).send(Errors.INVALID_KEY); } if (!id) { @@ -17,11 +16,11 @@ module.exports = (req, res, next) => { const client = realm.getClientById(id); if (!client) { - return sendAuthError(); + return res.sendStatus(401); } if (client.getToken() && token !== client.getToken()) { - return sendAuthError(); + return res.status(401).send(Errors.INVALID_TOKEN); } next(); diff --git a/src/enums.js b/src/enums.js index ea608db..93044a4 100644 --- a/src/enums.js +++ b/src/enums.js @@ -1,10 +1,18 @@ -module.exports.Errors = {}; +module.exports.Errors = { + INVALID_KEY: 'Invalid key provided', + INVALID_TOKEN: 'Invalid token provided', + INVALID_WS_PARAMETERS: 'No id, token, or key supplied to websocket server', + CONNECTION_LIMIT_EXCEED: 'Server has reached its concurrent user limit' +}; module.exports.MessageType = { + OPEN: 'OPEN', LEAVE: 'LEAVE', CANDIDATE: 'CANDIDATE', OFFER: 'OFFER', ANSWER: 'ANSWER', EXPIRE: 'EXPIRE', - HEARTBEAT: 'HEARTBEAT' + HEARTBEAT: 'HEARTBEAT', + ID_TAKEN: 'ID-TAKEN', + ERROR: 'ERROR' }; diff --git a/src/index.js b/src/index.js index 4d44205..2079dfc 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,10 @@ const messageHandler = require('./messageHandler'); const realm = require('./services/realm'); const { MessageType } = require('./enums'); +process.on('uncaughtException', (e) => { + logger.error('Error: ' + e); +}); + // parse config let path = config.get('path'); const port = config.get('port'); diff --git a/src/models/client.js b/src/models/client.js index e69fb7b..aaf0e5b 100644 --- a/src/models/client.js +++ b/src/models/client.js @@ -1,8 +1,7 @@ class Client { - constructor ({ id, token, ip }) { + constructor ({ id, token }) { this.id = id; this.token = token; - this.ip = ip; this.socket = null; } @@ -14,10 +13,6 @@ class Client { return this.token; } - getIp () { - return this.ip; - } - setSocket (socket) { this.socket = socket; } diff --git a/src/services/errors/index.js b/src/services/errors/index.js deleted file mode 100644 index a6735a7..0000000 --- a/src/services/errors/index.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - INVALID_KEY: 'Invalid key provided' -}; diff --git a/src/services/webSocketServer/index.js b/src/services/webSocketServer/index.js index 93db3cb..da4015d 100644 --- a/src/services/webSocketServer/index.js +++ b/src/services/webSocketServer/index.js @@ -2,7 +2,7 @@ const WSS = require('ws').Server; const url = require('url'); const EventEmitter = require('events'); const logger = require('../logger'); - +const { MessageType, Errors } = require('../../enums'); const config = require('../../../config'); const realm = require('../realm'); const Client = require('../../models/client'); @@ -12,8 +12,6 @@ class WebSocketServer extends EventEmitter { super(); this.setMaxListeners(0); - this._ips = {}; - let path = config.get('path'); path = path + (path[path.length - 1] !== '/' ? '/' : '') + 'peerjs'; @@ -33,11 +31,11 @@ class WebSocketServer extends EventEmitter { const { id, token, key } = query; if (!id || !token || !key) { - return this._sendErrorAndClose(socket, 'No id, token, or key supplied to websocket server'); + return this._sendErrorAndClose(socket, Errors.INVALID_WS_PARAMETERS); } if (key !== config.get('key')) { - return this._sendErrorAndClose(socket, 'Invalid key provided'); + return this._sendErrorAndClose(socket, Errors.INVALID_KEY); } const client = realm.getClientById(id); @@ -46,7 +44,7 @@ class WebSocketServer extends EventEmitter { if (token !== client.getToken()) { // ID-taken, invalid token socket.send(JSON.stringify({ - type: 'ID-TAKEN', + type: MessageType.ID_TAKEN, payload: { msg: 'ID is taken' } })); @@ -66,59 +64,27 @@ class WebSocketServer extends EventEmitter { } _registerClient ({ socket, id, token }) { - const ip = socket.remoteAddress; - - if (!this._ips[ip]) { - this._ips[ip] = 0; - } - // Check concurrent limit const clientsCount = realm.getClientsIds().length; if (clientsCount >= config.get('concurrent_limit')) { - return this._sendErrorAndClose(socket, 'Server has reached its concurrent user limit'); + return this._sendErrorAndClose(socket, Errors.CONNECTION_LIMIT_EXCEED); } - const connectionsPerIP = this._ips[ip]; - - if (connectionsPerIP >= config.get('ip_limit')) { - return this._sendErrorAndClose(socket, `${ip} has reached its concurrent user limit`); - } - - const oldClient = realm.getClientById(id); - - if (oldClient) { - return this._sendErrorAndClose(socket, `${id} already registered`); - } - - const newClient = new Client({ id, token, ip }); + const newClient = new Client({ id, token }); realm.setClient(newClient, id); - socket.send(JSON.stringify({ type: 'OPEN' })); - this._ips[ip]++; + socket.send(JSON.stringify({ type: MessageType.OPEN })); + this._configureWS(socket, newClient); } _configureWS (socket, client) { - if (client.socket && socket !== client.socket) { - // TODO remove old ip, add new ip - } - client.setSocket(socket); // Cleanup after a socket closes. socket.on('close', () => { logger.info('Socket closed:', client.getId()); - const ip = socket.remoteAddress; - - if (this._ips[ip]) { - this._ips[ip]--; - - if (this._ips[ip] === 0) { - delete this._ips[ip]; - } - } - if (client.socket === socket) { realm.removeClientById(client.getId()); this.emit('close', client); @@ -145,7 +111,7 @@ class WebSocketServer extends EventEmitter { _sendErrorAndClose (socket, msg) { socket.send( JSON.stringify({ - type: 'ERROR', + type: MessageType.ERROR, payload: { msg } }) );