100 lines
8.8 KiB
JavaScript
100 lines
8.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const plugins = require("./smartsocket.plugins");
|
|
const helpers = require("./smartsocket.helpers");
|
|
const lik_1 = require("lik");
|
|
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
|
|
// export classes
|
|
exports.allSocketConnections = new lik_1.Objectmap();
|
|
/**
|
|
* class SocketConnection represents a websocket connection
|
|
*/
|
|
class SocketConnection {
|
|
constructor(optionsArg) {
|
|
this.authenticated = false;
|
|
this.alias = optionsArg.alias;
|
|
this.authenticated = optionsArg.authenticated;
|
|
this.role = optionsArg.role;
|
|
this.side = optionsArg.side;
|
|
this.smartsocketHost = optionsArg.smartsocketHost;
|
|
this.socket = optionsArg.socket;
|
|
// standard behaviour that is always true
|
|
exports.allSocketConnections.add(this);
|
|
this.socket.on('disconnect', () => {
|
|
plugins.beautylog.info(`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`);
|
|
this.socket.disconnect();
|
|
exports.allSocketConnections.remove(this);
|
|
});
|
|
}
|
|
// authenticating --------------------------
|
|
/**
|
|
* authenticate the socket
|
|
*/
|
|
authenticate() {
|
|
let done = plugins.smartq.defer();
|
|
this.socket.on('dataAuth', (dataArg) => {
|
|
plugins.beautylog.log('received authentication data. now hashing and comparing...');
|
|
this.socket.removeListener('dataAuth', () => { });
|
|
if (helpers.checkPasswordForRole(dataArg, this.smartsocketHost)) {
|
|
// TODO: authenticate password
|
|
this.alias = dataArg.alias;
|
|
this.authenticated = true;
|
|
this.role = helpers.getSocketRoleByName(dataArg.role, this.smartsocketHost);
|
|
this.socket.emit('authenticated');
|
|
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
|
|
done.resolve(this);
|
|
}
|
|
else {
|
|
this.authenticated = false;
|
|
this.socket.disconnect();
|
|
done.reject('not authenticated');
|
|
}
|
|
});
|
|
this.socket.emit('requestAuth');
|
|
return done.promise;
|
|
}
|
|
// listening -------------------------------
|
|
/**
|
|
* listen to function requests
|
|
*/
|
|
listenToFunctionRequests() {
|
|
let done = plugins.smartq.defer();
|
|
if (this.authenticated) {
|
|
this.socket.on('function', (dataArg) => {
|
|
// check if requested function is available to the socket's scope
|
|
plugins.beautylog.log('function request received');
|
|
let referencedFunction = this.role.allowedFunctions.find(socketFunctionArg => {
|
|
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
|
});
|
|
if (referencedFunction !== undefined) {
|
|
plugins.beautylog.ok('function in access scope');
|
|
let localSocketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
|
|
side: 'responding',
|
|
originSocketConnection: this,
|
|
shortId: dataArg.shortId,
|
|
funcCallData: dataArg.funcCallData
|
|
});
|
|
localSocketRequest.createResponse(); // takes care of creating response and sending it back
|
|
}
|
|
else {
|
|
plugins.beautylog.warn('function not existent or out of access scope');
|
|
}
|
|
});
|
|
this.socket.on('functionResponse', (dataArg) => {
|
|
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
|
|
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
|
|
targetSocketRequest.handleResponse(dataArg);
|
|
});
|
|
plugins.beautylog.log(`now listening to function requests for ${this.alias}`);
|
|
done.resolve(this);
|
|
}
|
|
else {
|
|
let errMessage;
|
|
plugins.beautylog.error(errMessage);
|
|
done.reject(errMessage);
|
|
}
|
|
return done.promise;
|
|
}
|
|
}
|
|
exports.SocketConnection = SocketConnection;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsaURBQWlEO0FBQ2pELG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