diff --git a/lib/ClientConnection.js b/lib/ClientConnection.js index 0299087..50f12b0 100644 --- a/lib/ClientConnection.js +++ b/lib/ClientConnection.js @@ -3,11 +3,10 @@ const DeepExtend = require('deep-extend'); const Moment = require('moment'); const GuacdClient = require('./GuacdClient.js'); -const Crypt = require('./Crypt.js'); class ClientConnection { - constructor(server, connectionId, webSocket) { + constructor(server, connectionId, connectionSettings, webSocket) { this.STATE_OPEN = 1; this.STATE_CLOSED = 2; @@ -23,14 +22,11 @@ class ClientConnection { this.log(this.server.LOGLEVEL.VERBOSE, 'Client connection open'); try { - this.connectionSettings = this.decryptToken(); - + this.connectionSettings = connectionSettings; this.connectionType = this.connectionSettings.connection.type; - this.connectionSettings['connection'] = this.mergeConnectionOptions(); } catch (error) { - this.log(this.server.LOGLEVEL.ERRORS, 'Token validation failed'); this.close(error); return; } @@ -56,15 +52,6 @@ class ClientConnection { } - decryptToken() { - const crypt = new Crypt(this.server); - - const encrypted = this.query.token; - delete this.query.token; - - return crypt.decrypt(encrypted); - } - log(level, ...args) { if (level > this.server.clientOptions.log.level) { return; diff --git a/lib/Crypt.js b/lib/Crypt.js deleted file mode 100644 index e50eded..0000000 --- a/lib/Crypt.js +++ /dev/null @@ -1,29 +0,0 @@ -const Crypto = require('crypto'); - -class Crypt { - - constructor(app) { - this.server = app; - } - - decrypt(encodedString) { - let encoded = JSON.parse(this.constructor.base64decode(encodedString)); - - encoded.iv = this.constructor.base64decode(encoded.iv); - encoded.value = this.constructor.base64decode(encoded.value, 'binary'); - - const decipher = Crypto.createDecipheriv(this.server.clientOptions.crypt.cypher, this.server.clientOptions.crypt.key, encoded.iv); - - let decrypted = decipher.update(encoded.value, 'binary', 'ascii'); - decrypted += decipher.final('ascii'); - - return JSON.parse(decrypted); - } - - static base64decode(string, mode) { - return Buffer.from(string, 'base64').toString(mode || 'ascii'); - } - -} - -module.exports = Crypt; diff --git a/lib/Server.js b/lib/Server.js index 368f556..1bddf7e 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -146,9 +146,16 @@ class Server extends EventEmitter { }); } - newConnection(webSocketConnection) { + async newConnection(connection) { + const connectionSettings = await this.callbacks.authorizeConnection(connection); + + if (!connectionSettings) { + connection.close(); + return; + } + this.connectionsCount++; - this.activeConnections.set(this.connectionsCount, new ClientConnection(this, this.connectionsCount, webSocketConnection)); + this.activeConnections.set(this.connectionsCount, new ClientConnection(this, this.connectionsCount, connectionSettings, connection)); } }