update structure

This commit is contained in:
Philipp Kunz 2016-08-08 18:20:00 +02:00
parent fc530ba37e
commit 8d58e0b2f3
16 changed files with 220 additions and 99 deletions

View File

@ -1,12 +1,6 @@
/// <reference types="socket.io" /> /// <reference types="socket.io" />
import { Objectmap } from "lik"; import { Objectmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketObject {
alias?: string;
authenticated: boolean;
role?: string;
socket: SocketIO.Socket;
}
export interface ISmartsocketConstructorOptions { export interface ISmartsocketConstructorOptions {
port: number; port: number;
} }
@ -14,13 +8,13 @@ export declare class Smartsocket {
options: ISmartsocketConstructorOptions; options: ISmartsocketConstructorOptions;
io: SocketIO.Server; io: SocketIO.Server;
openSockets: Objectmap; openSockets: Objectmap;
registeredRoles: Objectmap; registeredFunctions: Objectmap;
constructor(optionsArg: ISmartsocketConstructorOptions); constructor(optionsArg: ISmartsocketConstructorOptions);
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocket(socket); private _handleSocket(socketArg);
registerFunctions(socketRoleArg: SocketRole): void; registerFunction(socketRoleArg: SocketRole): void;
/** /**
* starts listening to incling sockets: * starts listening to incling sockets:
*/ */

View File

@ -3,12 +3,12 @@ const plugins = require("./smartsocket.plugins");
const helpers = require("./smartsocket.helpers"); const helpers = require("./smartsocket.helpers");
// classes // classes
const lik_1 = require("lik"); const lik_1 = require("lik");
; const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
; ;
class Smartsocket { class Smartsocket {
constructor(optionsArg) { constructor(optionsArg) {
this.openSockets = new lik_1.Objectmap(); this.openSockets = new lik_1.Objectmap();
this.registeredRoles = new lik_1.Objectmap(); this.registeredFunctions = new lik_1.Objectmap();
/** /**
* starts listening to incling sockets: * starts listening to incling sockets:
*/ */
@ -32,20 +32,20 @@ class Smartsocket {
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
_handleSocket(socket) { _handleSocket(socketArg) {
let socketObject = { let socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
socket: socket, authenticated: false,
authenticated: false socket: socketArg
}; });
plugins.beautylog.log("Socket connected. Trying to authenticate..."); plugins.beautylog.log("Socket connected. Trying to authenticate...");
this.openSockets.add(socketObject); this.openSockets.add(socketConnection);
helpers.authenticateSocket(socketObject) helpers.authenticateSocket(socketConnection)
.then(); .then();
} }
registerFunctions(socketRoleArg) { registerFunction(socketRoleArg) {
this.registeredRoles.add(socketRoleArg); this.registeredFunctions.add(socketRoleArg);
} }
; ;
} }
exports.Smartsocket = Smartsocket; exports.Smartsocket = Smartsocket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFDakQsTUFBWSxPQUFPLFdBQU0sdUJBQXVCLENBQUMsQ0FBQTtBQUVqRCxVQUFVO0FBQ1Ysc0JBQTBCLEtBQUssQ0FBQyxDQUFBO0FBVS9CLENBQUM7QUFLRCxDQUFDO0FBRUY7SUFLSSxZQUFZLFVBQTBDO1FBRnRELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQUUsQ0FBQztRQUM5QixvQkFBZSxHQUFHLElBQUksZUFBUyxFQUFFLENBQUM7UUF3QmxDOztXQUVHO1FBRUgsZ0JBQVcsR0FBRztZQUNWLElBQUksQ0FBQyxFQUFFLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQzlDLElBQUksQ0FBQyxFQUFFLENBQUMsRUFBRSxD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
role?: string;
socket: SocketIO.Socket;
}
export declare class SocketConnection {
alias?: string;
authenticated: boolean;
role?: string;
socket: SocketIO.Socket;
constructor(optionsArg: ISocketConnectionOptions);
}

View File

@ -0,0 +1,13 @@
"use strict";
;
class SocketConnection {
constructor(optionsArg) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
}
exports.SocketConnection = SocketConnection;
;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFPQyxDQUFDO0FBRUY7SUFLSSxZQUFZLFVBQW1DO1FBQzNDLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQztRQUM5QixJQUFJLENBQUMsYUFBYSxHQUFHLFVBQVUsQ0FBQyxhQUFhLENBQUM7UUFDOUMsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFDO1FBQzVCLElBQUksQ0FBQyxNQUFNLEdBQUcsVUFBVSxDQUFDLE1BQU0sQ0FBQztJQUNwQyxDQUFDO0FBQ0wsQ0FBQztBQVhZLHdCQUFnQixtQkFXNUIsQ0FBQTtBQUFBLENBQUMifQ==

View File

@ -1,4 +1,9 @@
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketFunctionData {
functionName: string;
functionData: any;
responseTimeout?: number;
}
export interface SocketFunctionOptions { export interface SocketFunctionOptions {
name: string; name: string;
func: any; func: any;
@ -9,4 +14,6 @@ export declare class SocketFunction {
func: any; func: any;
roles: SocketRole[]; roles: SocketRole[];
constructor(optionsArg: SocketFunctionOptions); constructor(optionsArg: SocketFunctionOptions);
private _notifyRole(socketRoleArg);
functionRequest(dataArg: ISocketFunctionData): void;
} }

View File

@ -1,12 +1,21 @@
"use strict"; "use strict";
; ;
;
class SocketFunction { class SocketFunction {
constructor(optionsArg) { constructor(optionsArg) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.func = optionsArg.func; this.func = optionsArg.func;
this.roles = optionsArg.roles; this.roles = optionsArg.roles;
for (let socketRoleArg of this.roles) {
this._notifyRole(socketRoleArg);
}
} }
; ;
_notifyRole(socketRoleArg) {
socketRoleArg.addSocketFunction(this);
}
functionRequest(dataArg) {
}
} }
exports.SocketFunction = SocketFunction; exports.SocketFunction = SocketFunction;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRmdW5jdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc29ja2V0ZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQVlDLENBQUM7QUFFRjtJQUlJLFlBQVksVUFBaUM7UUFDekMsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFDO1FBQzVCLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsS0FBSyxHQUFHLFVBQVUsQ0FBQyxLQUFLLENBQUM7SUFDbEMsQ0FBQzs7QUFDTCxDQUFDO0FBVFksc0JBQWMsaUJBUzFCLENBQUEifQ== //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRmdW5jdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc29ja2V0ZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQVVDLENBQUM7QUFNRCxDQUFDO0FBRUY7SUFJSSxZQUFZLFVBQWlDO1FBQ3pDLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLEtBQUssR0FBRyxVQUFVLENBQUMsS0FBSyxDQUFDO1FBQzlCLEdBQUcsQ0FBQyxDQUFDLElBQUksYUFBYSxJQUFJLElBQUksQ0FBQyxLQUFLLENBQUMsQ0FBQSxDQUFDO1lBQ2xDLElBQUksQ0FBQyxXQUFXLENBQUMsYUFBYSxDQUFDLENBQUM7UUFDcEMsQ0FBQztJQUNMLENBQUM7O0lBQ08sV0FBVyxDQUFDLGFBQXdCO1FBQ3hDLGFBQWEsQ0FBQyxpQkFBaUIsQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUMxQyxDQUFDO0lBQ0QsZUFBZSxDQUFDLE9BQTJCO0lBRTNDLENBQUM7QUFDTCxDQUFDO0FBbEJZLHNCQUFjLGlCQWtCMUIsQ0FBQSJ9

View File

@ -1,3 +1,4 @@
import { SocketFunction } from "./smartsocket.classes.socketfunction";
/** /**
* interface for class SocketRole * interface for class SocketRole
*/ */
@ -11,5 +12,7 @@ export interface SocketRoleOptions {
export declare class SocketRole { export declare class SocketRole {
name: string; name: string;
passwordHash: string; passwordHash: string;
allowedFunctions: SocketFunction[];
constructor(optionsArg: SocketRoleOptions); constructor(optionsArg: SocketRoleOptions);
addSocketFunction(socketFunctionArg: SocketFunction): void;
} }

View File

@ -7,6 +7,10 @@ class SocketRole {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
} }
;
addSocketFunction(socketFunctionArg) {
this.allowedFunctions.push(socketFunctionArg);
}
} }
exports.SocketRole = SocketRole; exports.SocketRole = SocketRole;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFjQTs7R0FFRztBQUNIO0lBR0ksWUFBWSxVQUE0QjtRQUNwQyxJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO0lBQ2hELENBQUM7QUFDTCxDQUFDO0FBUFksa0JBQVUsYUFPdEIsQ0FBQSJ9 //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFjQTs7R0FFRztBQUNIO0lBSUksWUFBWSxVQUE2QjtRQUNyQyxJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO0lBQ2hELENBQUM7O0lBQ0QsaUJBQWlCLENBQUMsaUJBQWdDO1FBQzlDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxJQUFJLENBQUMsaUJBQWlCLENBQUMsQ0FBQztJQUNsRCxDQUFDO0FBQ0wsQ0FBQztBQVhZLGtCQUFVLGFBV3RCLENBQUEifQ==

View File

@ -1,7 +1,8 @@
/// <reference types="q" /> /// <reference types="q" />
import * as plugins from "./smartsocket.plugins"; import * as plugins from "./smartsocket.plugins";
import { ISocketObject } from "./smartsocket.classes.smartsocket"; import { SocketConnection } from "./smartsocket.classes.socketconnection";
/** /**
* authenticate a socket * authenticate a socket
*/ */
export declare let authenticateSocket: (socketObjectArg: ISocketObject) => plugins.q.Promise<{}>; export declare let authenticateSocket: (socketConnectionArg: SocketConnection) => plugins.q.Promise<{}>;
export declare let listenToReadySocket: (socketConnectionArg: SocketConnection) => plugins.q.Promise<{}>;

View File

@ -3,26 +3,31 @@ const plugins = require("./smartsocket.plugins");
/** /**
* authenticate a socket * authenticate a socket
*/ */
exports.authenticateSocket = (socketObjectArg) => { exports.authenticateSocket = (socketConnectionArg) => {
let done = plugins.q.defer(); let done = plugins.q.defer();
socketObjectArg.socket.on("dataAuth", dataArg => { socketConnectionArg.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing..."); plugins.beautylog.log("received authentication data. now hashing and comparing...");
socketObjectArg.socket.removeListener("dataAuth", () => { }); socketConnectionArg.socket.removeListener("dataAuth", () => { });
if ((true)) { if ((true)) {
socketObjectArg.alias = dataArg.alias; socketConnectionArg.alias = dataArg.alias;
socketObjectArg.authenticated = true; socketConnectionArg.authenticated = true;
socketObjectArg.role = dataArg.role; socketConnectionArg.role = dataArg.role;
socketObjectArg.socket.emit("authenticated"); socketConnectionArg.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`); plugins.beautylog.ok(`socket with >>alias ${socketConnectionArg.alias} >>role ${socketConnectionArg.role} is authenticated!`);
done.resolve(socketObjectArg); done.resolve(socketConnectionArg);
} }
else { else {
socketObjectArg.socket.disconnect(); socketConnectionArg.socket.disconnect();
done.reject("not authenticated"); done.reject("not authenticated");
} }
; ;
}); });
socketObjectArg.socket.emit("requestAuth"); socketConnectionArg.socket.emit("requestAuth");
return done.promise; return done.promise;
}; };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFRakQ7O0dBRUc7QUFDUSwwQkFBa0IsR0FBRyxDQUFDLGVBQThCO0lBQzNELElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7SUFDN0IsZUFBZSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxFQUFFLE9BQU87UUFDekMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsNERBQTRELENBQUMsQ0FBQztRQUNwRixlQUFlLENBQUMsTUFBTSxDQUFDLGNBQWMsQ0FBQyxVQUFVLEVBQUUsUUFBUSxDQUFDLENBQUMsQ0FBQztRQUM3RCxFQUFFLENBQUEsQ0FBQyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUEsQ0FBQztZQUNQLGVBQWUsQ0FBQyxLQUFLLEdBQUcsT0FBTyxDQUFDLEtBQUssQ0FBQTtZQUNyQyxlQUFlLENBQUMsYUFBYSxHQUFHLElBQUksQ0FBQztZQUNyQyxlQUFlLENBQUMsSUFBSSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUM7WUFDcEMsZUFBZSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUM7WUFDN0MsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsdUJBQXVCLGVBQWUsQ0FBQyxLQUFLLFdBQVcsZUFBZSxDQUFDLElBQUksb0JBQW9CLENBQUMsQ0FBQTtZQUNySCxJQUFJLENBQUMsT0FBTyxDQUFDLGVBQWUsQ0FBQyxDQUFDO1FBQ2xDLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNKLGVBQWUsQ0FBQyxNQUFNLENBQUMsVUFBVSxFQUFFLENBQUM7WUFDcEMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxtQkFBbUIsQ0FBQyxDQUFDO1FBQ3JDLENBQUM7UUFBQSxDQUFDO0lBQ04sQ0FBQyxDQUFDLENBQUM7SUFDSCxlQUFlLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxhQUFhLENBQUMsQ0FBQztJQUMzQyxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUMifQ== exports.listenToReadySocket = (socketConnectionArg) => {
let done = plugins.q.defer();
// socketConnectionArg.socket.listen("function", )
return done.promise;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFRakQ7O0dBRUc7QUFDUSwwQkFBa0IsR0FBRyxDQUFDLG1CQUFxQztJQUNsRSxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzdCLG1CQUFtQixDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxFQUFFLE9BQU87UUFDN0MsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsNERBQTRELENBQUMsQ0FBQztRQUNwRixtQkFBbUIsQ0FBQyxNQUFNLENBQUMs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O0lBQ2xELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hCLENBQUMsQ0FBQSJ9

View File

@ -19,11 +19,13 @@
"homepage": "https://gitlab.com/pushrocks/smartsocket#README", "homepage": "https://gitlab.com/pushrocks/smartsocket#README",
"dependencies": { "dependencies": {
"@types/q": "0.x.x", "@types/q": "0.x.x",
"@types/shortid": "0.0.27",
"@types/socket.io": "^1.4.26", "@types/socket.io": "^1.4.26",
"@types/socket.io-client": "^1.4.26", "@types/socket.io-client": "^1.4.26",
"beautylog": "^5.0.20", "beautylog": "^5.0.20",
"lik": "^1.0.10", "lik": "^1.0.15",
"q": "^1.4.1", "q": "^1.4.1",
"shortid": "^2.2.6",
"socket.io": "^1.4.8", "socket.io": "^1.4.8",
"socket.io-client": "^1.4.8", "socket.io-client": "^1.4.8",
"taskbuffer": "^1.0.7" "taskbuffer": "^1.0.7"

View File

@ -5,14 +5,7 @@ import * as helpers from "./smartsocket.helpers";
import { Objectmap } from "lik"; import { Objectmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
export interface ISocketObject {
alias?:string;
authenticated: boolean
role?:string,
socket: SocketIO.Socket,
};
export interface ISmartsocketConstructorOptions { export interface ISmartsocketConstructorOptions {
port: number; port: number;
@ -22,8 +15,7 @@ export interface ISmartsocketConstructorOptions {
export class Smartsocket { export class Smartsocket {
options: ISmartsocketConstructorOptions options: ISmartsocketConstructorOptions
io: SocketIO.Server; io: SocketIO.Server;
openSockets = new Objectmap(); openSockets = new Objectmap<SocketConnection>();
registeredRoles = new Objectmap();
constructor(optionsArg: ISmartsocketConstructorOptions) { constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg; this.options = optionsArg;
}; };
@ -31,22 +23,17 @@ export class Smartsocket {
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocket(socket) { private _handleSocket(socketArg) {
let socketObject: ISocketObject = { let socketConnection: SocketConnection = new SocketConnection({
socket: socket, authenticated:false,
authenticated: false socket:socketArg
}; });
plugins.beautylog.log("Socket connected. Trying to authenticate...") plugins.beautylog.log("Socket connected. Trying to authenticate...")
this.openSockets.add(socketObject); this.openSockets.add(socketConnection);
helpers.authenticateSocket(socketObject) socketConnection.authenticate()
.then(); .then(socketConnection.listenToFunctionRequests);
}
registerFunctions(socketRoleArg:SocketRole){
this.registeredRoles.add(socketRoleArg);
}; };
/** /**
* starts listening to incling sockets: * starts listening to incling sockets:
*/ */
@ -58,7 +45,7 @@ export class Smartsocket {
}); });
} }
closeServer = () => { closeServer = () => {
this.openSockets.forEach((socketObjectArg: ISocketObject) => { this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`); plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
socketObjectArg.socket.disconnect(); socketObjectArg.socket.disconnect();
}); });

View File

@ -0,0 +1,64 @@
import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers";
// import classes
import { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction, ISocketFunctionData } from "./smartsocket.classes.socketfunction";
export interface ISocketConnectionOptions {
alias?: string;
authenticated: boolean;
role?: SocketRole;
socket: SocketIO.Socket;
};
export class SocketConnection {
alias?: string;
authenticated: boolean;
role?: SocketRole;
socket: SocketIO.Socket;
constructor(optionsArg: ISocketConnectionOptions) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.q.defer();
this.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
this.socket.removeListener("dataAuth", () => { });
if ((true)) { // TODO: authenticate password
this.alias = dataArg.alias
this.authenticated = true;
this.role = helpers.findSocketRoleByString(dataArg.role);
this.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
} else {
this.socket.disconnect();
done.reject("not authenticated");
};
});
this.socket.emit("requestAuth");
return done.promise;
};
/**
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.q.defer();
if(this.authenticated){
this.socket.on("function", (dataArg:ISocketFunctionData) => {
this.role.allowedFunctions
})
} else {
done.reject("socket needs to be authenticated first");
};
return done.promise;
}
};

View File

@ -4,7 +4,11 @@ import * as plugins from "./smartsocket.plugins";
import { Stringmap } from "lik"; import { Stringmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketFunctionData {
functionName:string,
functionData:any,
responseTimeout?:number
};
export interface SocketFunctionOptions { export interface SocketFunctionOptions {
name: string; name: string;
@ -16,9 +20,31 @@ export class SocketFunction {
name: string; name: string;
func: any; func: any;
roles: SocketRole[]; roles: SocketRole[];
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: SocketFunctionOptions) { constructor(optionsArg: SocketFunctionOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.func = optionsArg.func; this.func = optionsArg.func;
this.roles = optionsArg.roles; this.roles = optionsArg.roles;
}; for (let socketRoleArg of this.roles){
this._notifyRole(socketRoleArg);
}
};
/**
* notifies a role about access to this SocketFunction
*/
private _notifyRole(socketRoleArg:SocketRole){
socketRoleArg.addSocketFunction(this);
}
/**
* handles a function request to this SocketFunction
*/
functionRequest(dataArg:ISocketFunctionData){
};
} }

View File

@ -1,7 +1,12 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from "./smartsocket.plugins";
// import classes // import classes
import { Stringmap } from "lik"; import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
export let allSocketRoles = new Objectmap<SocketRole>();
/** /**
* interface for class SocketRole * interface for class SocketRole
@ -11,15 +16,19 @@ export interface SocketRoleOptions {
passwordHash: string; passwordHash: string;
} }
/** /**
* A socketrole defines access to certain routines. * A socketrole defines access to certain routines.
*/ */
export class SocketRole { export class SocketRole {
name: string; name: string;
passwordHash: string; passwordHash: string;
allowedFunctions = new Objectmap<SocketFunction>();
constructor(optionsArg: SocketRoleOptions) { constructor(optionsArg: SocketRoleOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
allSocketRoles.add(this);
};
addSocketFunction(socketFunctionArg:SocketFunction){
this.allowedFunctions.add(socketFunctionArg);
} }
} }

View File

@ -1,31 +1,14 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from "./smartsocket.plugins";
// interfaces // classes
import {ISocketObject} from "./smartsocket.classes.smartsocket"; import { Smartsocket } from "./smartsocket.classes.smartsocket";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole";
// SocketRole helpers
export let findSocketRoleByString = (socketRoleNameArg: string): SocketRole => {
/** return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg })
* authenticate a socket
*/
export let authenticateSocket = (socketObjectArg: ISocketObject) => {
let done = plugins.q.defer();
socketObjectArg.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
socketObjectArg.socket.removeListener("dataAuth", () => { });
if((true)){ // TODO: authenticate password
socketObjectArg.alias = dataArg.alias
socketObjectArg.authenticated = true;
socketObjectArg.role = dataArg.role;
socketObjectArg.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`)
done.resolve(socketObjectArg);
} else {
socketObjectArg.socket.disconnect();
done.reject("not authenticated");
};
});
socketObjectArg.socket.emit("requestAuth");
return done.promise;
}; };