update structure

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

View File

@ -3,16 +3,9 @@ import * as helpers from "./smartsocket.helpers";
// classes
import { Objectmap } from "lik";
import {SocketRole} from "./smartsocket.classes.socketrole";
import {SocketFunction} from "./smartsocket.classes.socketfunction";
export interface ISocketObject {
alias?:string;
authenticated: boolean
role?:string,
socket: SocketIO.Socket,
};
import { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
export interface ISmartsocketConstructorOptions {
port: number;
@ -20,10 +13,9 @@ export interface ISmartsocketConstructorOptions {
};
export class Smartsocket {
options:ISmartsocketConstructorOptions
options: ISmartsocketConstructorOptions
io: SocketIO.Server;
openSockets = new Objectmap();
registeredRoles = new Objectmap();
openSockets = new Objectmap<SocketConnection>();
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
};
@ -31,22 +23,17 @@ export class Smartsocket {
/**
* the standard handler for new socket connections
*/
private _handleSocket(socket) {
let socketObject: ISocketObject = {
socket: socket,
authenticated: false
};
private _handleSocket(socketArg) {
let socketConnection: SocketConnection = new SocketConnection({
authenticated:false,
socket:socketArg
});
plugins.beautylog.log("Socket connected. Trying to authenticate...")
this.openSockets.add(socketObject);
helpers.authenticateSocket(socketObject)
.then();
}
registerFunctions(socketRoleArg:SocketRole){
this.registeredRoles.add(socketRoleArg);
this.openSockets.add(socketConnection);
socketConnection.authenticate()
.then(socketConnection.listenToFunctionRequests);
};
/**
* starts listening to incling sockets:
*/
@ -58,7 +45,7 @@ export class Smartsocket {
});
}
closeServer = () => {
this.openSockets.forEach((socketObjectArg: ISocketObject) => {
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
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 { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketFunctionData {
functionName:string,
functionData:any,
responseTimeout?:number
};
export interface SocketFunctionOptions {
name: string;
@ -16,9 +20,31 @@ export class SocketFunction {
name: string;
func: any;
roles: SocketRole[];
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: SocketFunctionOptions) {
this.name = optionsArg.name;
this.func = optionsArg.func;
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,25 +1,34 @@
import * as plugins from "./smartsocket.plugins";
// 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
*/
export interface SocketRoleOptions {
name:string;
passwordHash:string;
name: string;
passwordHash: string;
}
/**
* A socketrole defines access to certain routines.
*/
export class SocketRole {
name:string;
passwordHash:string;
constructor(optionsArg:SocketRoleOptions){
name: string;
passwordHash: string;
allowedFunctions = new Objectmap<SocketFunction>();
constructor(optionsArg: SocketRoleOptions) {
this.name = optionsArg.name;
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";
// interfaces
import {ISocketObject} from "./smartsocket.classes.smartsocket";
// classes
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;
};