2016-08-08 16:20:00 +00:00
|
|
|
"use strict";
|
2017-07-07 20:02:19 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2016-08-09 09:42:21 +00:00
|
|
|
const plugins = require("./smartsocket.plugins");
|
|
|
|
const helpers = require("./smartsocket.helpers");
|
2016-08-15 00:36:17 +00:00
|
|
|
const lik_1 = require("lik");
|
2016-08-09 14:33:56 +00:00
|
|
|
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
|
2016-08-09 09:42:21 +00:00
|
|
|
// export classes
|
2016-08-15 00:36:17 +00:00
|
|
|
exports.allSocketConnections = new lik_1.Objectmap();
|
2016-08-09 09:42:21 +00:00
|
|
|
/**
|
|
|
|
* class SocketConnection represents a websocket connection
|
|
|
|
*/
|
2016-08-08 16:20:00 +00:00
|
|
|
class SocketConnection {
|
|
|
|
constructor(optionsArg) {
|
2016-08-14 23:38:28 +00:00
|
|
|
this.authenticated = false;
|
2016-08-08 16:20:00 +00:00
|
|
|
this.alias = optionsArg.alias;
|
|
|
|
this.authenticated = optionsArg.authenticated;
|
|
|
|
this.role = optionsArg.role;
|
2016-08-15 00:36:17 +00:00
|
|
|
this.side = optionsArg.side;
|
2016-09-04 22:34:09 +00:00
|
|
|
this.smartsocketHost = optionsArg.smartsocketHost;
|
2016-08-08 16:20:00 +00:00
|
|
|
this.socket = optionsArg.socket;
|
2016-08-14 01:25:26 +00:00
|
|
|
// standard behaviour that is always true
|
2016-08-15 00:36:17 +00:00
|
|
|
exports.allSocketConnections.add(this);
|
2017-07-07 20:02:19 +00:00
|
|
|
this.socket.on('disconnect', () => {
|
2016-08-15 00:36:17 +00:00
|
|
|
plugins.beautylog.info(`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`);
|
2017-10-09 08:28:18 +00:00
|
|
|
this.socket.disconnect();
|
2016-08-15 00:36:17 +00:00
|
|
|
exports.allSocketConnections.remove(this);
|
2016-08-14 01:25:26 +00:00
|
|
|
});
|
2016-08-08 16:20:00 +00:00
|
|
|
}
|
2016-08-12 03:56:40 +00:00
|
|
|
// authenticating --------------------------
|
2016-08-09 09:42:21 +00:00
|
|
|
/**
|
|
|
|
* authenticate the socket
|
|
|
|
*/
|
|
|
|
authenticate() {
|
2017-07-07 20:02:19 +00:00
|
|
|
let done = plugins.smartq.defer();
|
|
|
|
this.socket.on('dataAuth', (dataArg) => {
|
|
|
|
plugins.beautylog.log('received authentication data. now hashing and comparing...');
|
|
|
|
this.socket.removeListener('dataAuth', () => { });
|
2016-09-04 22:34:09 +00:00
|
|
|
if (helpers.checkPasswordForRole(dataArg, this.smartsocketHost)) {
|
2018-03-15 01:29:40 +00:00
|
|
|
// TODO: authenticate password
|
2016-08-09 09:42:21 +00:00
|
|
|
this.alias = dataArg.alias;
|
|
|
|
this.authenticated = true;
|
2016-09-04 22:34:09 +00:00
|
|
|
this.role = helpers.getSocketRoleByName(dataArg.role, this.smartsocketHost);
|
2017-07-07 20:02:19 +00:00
|
|
|
this.socket.emit('authenticated');
|
2016-08-09 09:42:21 +00:00
|
|
|
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
|
|
|
|
done.resolve(this);
|
|
|
|
}
|
|
|
|
else {
|
2016-08-14 23:38:28 +00:00
|
|
|
this.authenticated = false;
|
2016-08-09 09:42:21 +00:00
|
|
|
this.socket.disconnect();
|
2017-07-07 20:02:19 +00:00
|
|
|
done.reject('not authenticated');
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
this.socket.emit('requestAuth');
|
2016-08-09 09:42:21 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
2016-08-12 03:56:40 +00:00
|
|
|
// listening -------------------------------
|
2016-08-09 09:42:21 +00:00
|
|
|
/**
|
|
|
|
* listen to function requests
|
|
|
|
*/
|
|
|
|
listenToFunctionRequests() {
|
2017-07-07 20:02:19 +00:00
|
|
|
let done = plugins.smartq.defer();
|
2016-08-09 09:42:21 +00:00
|
|
|
if (this.authenticated) {
|
2017-07-07 20:02:19 +00:00
|
|
|
this.socket.on('function', (dataArg) => {
|
2016-08-09 21:37:25 +00:00
|
|
|
// check if requested function is available to the socket's scope
|
2017-07-07 20:02:19 +00:00
|
|
|
plugins.beautylog.log('function request received');
|
2018-03-15 01:29:40 +00:00
|
|
|
let referencedFunction = this.role.allowedFunctions.find(socketFunctionArg => {
|
2016-08-09 21:37:25 +00:00
|
|
|
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
2016-08-09 09:42:21 +00:00
|
|
|
});
|
|
|
|
if (referencedFunction !== undefined) {
|
2017-07-07 20:02:19 +00:00
|
|
|
plugins.beautylog.ok('function in access scope');
|
2016-08-09 14:33:56 +00:00
|
|
|
let localSocketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
|
2017-07-07 20:02:19 +00:00
|
|
|
side: 'responding',
|
2016-08-09 21:37:25 +00:00
|
|
|
originSocketConnection: this,
|
|
|
|
shortId: dataArg.shortId,
|
|
|
|
funcCallData: dataArg.funcCallData
|
2016-08-09 14:33:56 +00:00
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
localSocketRequest.createResponse(); // takes care of creating response and sending it back
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-07-07 20:02:19 +00:00
|
|
|
plugins.beautylog.warn('function not existent or out of access scope');
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
this.socket.on('functionResponse', (dataArg) => {
|
2016-08-14 23:38:28 +00:00
|
|
|
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
|
2016-08-12 03:56:40 +00:00
|
|
|
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
|
|
|
|
targetSocketRequest.handleResponse(dataArg);
|
2016-08-09 14:33:56 +00:00
|
|
|
});
|
2016-08-14 23:38:28 +00:00
|
|
|
plugins.beautylog.log(`now listening to function requests for ${this.alias}`);
|
|
|
|
done.resolve(this);
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-08-14 23:38:28 +00:00
|
|
|
let errMessage;
|
|
|
|
plugins.beautylog.error(errMessage);
|
|
|
|
done.reject(errMessage);
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
|
|
|
return done.promise;
|
|
|
|
}
|
2016-08-08 16:20:00 +00:00
|
|
|
}
|
|
|
|
exports.SocketConnection = SocketConnection;
|
2018-03-15 01:29:40 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsaURBQWlEO0FBQ2pELGlEQUFpRDtBQUVqRCw2QkFBZ0M7QUFLaEMsMkZBSTZDO0FBa0M3QyxpQkFBaUI7QUFDTixRQUFBLG9CQUFvQixHQUFHLElBQUksZUFBUyxFQUFvQixDQUFDO0FBRXBFOztHQUVHO0FBQ0g7SUFPRSxZQUFZLFVBQStDO1FBSjNELGtCQUFhLEdBQVksS0FBSyxDQUFDO1FBSzdCLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQztRQUM5QixJQUFJLENBQUMsYUFBYSxHQUFHLFVBQVUsQ0FBQyxhQUFhLENBQUM7UUFDOUMsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFDO1FBQzVCLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsZUFBZSxHQUFHLFVBQVUsQ0FBQyxlQUFlLENBQUM7UUFDbEQsS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