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`);
|
|
|
|
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)) {
|
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');
|
2016-08-09 09:42:21 +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;
|
2017-07-07 20:02:19 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsaURBQWdEO0FBQ2hELGlEQUFnRDtBQUVoRCw2QkFBK0I7QUFLL0IsMkZBQWdIO0FBK0JoSCxpQkFBaUI7QUFDTixRQUFBLG9CQUFvQixHQUFHLElBQUksZUFBUyxFQUFvQixDQUFBO0FBRW5FOztHQUVHO0FBQ0g7SUFPRSxZQUFhLFVBQStDO1FBSjVELGtCQUFhLEdBQVksS0FBSyxDQUFBO1FBSzVCLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQTtRQUM3QixJQUFJLENBQUMsYUFBYSxHQUFHLFVBQVUsQ0FBQyxhQUFhLENBQUE7UUFDN0MsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFBO1FBQzNCLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQTtRQUMzQixJQUFJLENBQUMsZUFBZSxHQUFHLFVBQVUsQ0FBQyxlQUFlLENBQUE7UUFDakQsSUFBSSxDQUFDLE1BQU0sR0FBRyxVQUFVLENBQUMsTUFBTSxDQUFBO1FBRS9CLHlDQUF5QztRQUN6Qyw0QkFBb0IsQ0FB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