2018-03-13 05:15:40 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
2019-01-31 01:52:18 +00:00
|
|
|
import { Handler, Route, Server } from '@pushrocks/smartexpress';
|
2018-05-28 10:07:25 +00:00
|
|
|
import { UniverseCache, UniverseChannel, UniverseMessage } from './';
|
2018-04-13 13:45:48 +00:00
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
import * as paths from './smartuniverse.paths';
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2019-04-30 17:16:03 +00:00
|
|
|
import { UniverseConnection } from './smartuniverse.classes.universeconnection';
|
2019-04-11 15:52:01 +00:00
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
export interface ISmartUniverseConstructorOptions {
|
|
|
|
messageExpiryInMilliseconds: number;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:14:57 +00:00
|
|
|
/**
|
2019-08-12 12:59:37 +00:00
|
|
|
* main class that setups a Universe
|
2018-05-23 22:14:57 +00:00
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
export class Universe {
|
|
|
|
// subinstances
|
2018-05-26 11:44:32 +00:00
|
|
|
public universeCache: UniverseCache;
|
2018-03-13 05:15:40 +00:00
|
|
|
|
|
|
|
// options
|
|
|
|
private options: ISmartUniverseConstructorOptions;
|
2018-03-20 07:16:54 +00:00
|
|
|
|
2019-04-30 17:16:03 +00:00
|
|
|
/**
|
|
|
|
* the smartexpress server used
|
|
|
|
*/
|
|
|
|
private smartexpressServer: plugins.smartexpress.Server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the smartsocket used
|
|
|
|
*/
|
|
|
|
private smartsocket: plugins.smartsocket.Smartsocket;
|
|
|
|
|
|
|
|
constructor(optionsArg: ISmartUniverseConstructorOptions) {
|
|
|
|
this.options = optionsArg;
|
2019-08-13 13:48:20 +00:00
|
|
|
this.universeCache = new UniverseCache(this, this.options.messageExpiryInMilliseconds);
|
2019-04-30 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
/**
|
|
|
|
* stores the version of the universe server running
|
|
|
|
* this is done since the version is exposed through the api and multiple fs actions are avoided this way.
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
private universeVersionStore: string;
|
2019-04-11 15:52:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the currently running version of smartuniverse
|
|
|
|
*/
|
2019-04-30 17:16:03 +00:00
|
|
|
public getUniverseVersion() {
|
2018-03-13 05:15:40 +00:00
|
|
|
if (this.universeVersionStore) {
|
|
|
|
return this.universeVersionStore;
|
|
|
|
} else {
|
|
|
|
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
|
|
|
|
this.universeVersionStore = packageJson.version;
|
|
|
|
return this.universeVersionStore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:07:25 +00:00
|
|
|
/**
|
|
|
|
* adds a channel to the Universe
|
|
|
|
*/
|
|
|
|
public async addChannel(nameArg: string, passphraseArg: string) {
|
2019-08-13 13:48:20 +00:00
|
|
|
const newChannel = UniverseChannel.createChannel(this, nameArg, passphraseArg);
|
2018-05-28 10:07:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
/**
|
|
|
|
* initiates a server
|
|
|
|
*/
|
2019-04-22 22:28:57 +00:00
|
|
|
public async start(portArg: number) {
|
2019-04-11 15:52:01 +00:00
|
|
|
// lets create the base smartexpress server
|
2018-03-13 05:15:40 +00:00
|
|
|
this.smartexpressServer = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
2019-01-31 01:52:18 +00:00
|
|
|
defaultAnswer: async () => {
|
2019-06-06 20:22:45 +00:00
|
|
|
return `smartuniverse server ${this.getUniverseVersion()}`;
|
2019-01-31 01:52:18 +00:00
|
|
|
},
|
2018-03-13 05:15:40 +00:00
|
|
|
forceSsl: false,
|
|
|
|
port: portArg
|
|
|
|
});
|
|
|
|
|
2018-04-13 13:45:48 +00:00
|
|
|
// add websocket upgrade
|
2019-04-24 16:20:31 +00:00
|
|
|
this.smartsocket = new plugins.smartsocket.Smartsocket({});
|
2018-04-13 13:45:48 +00:00
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// add a role for the clients
|
2018-05-30 14:34:06 +00:00
|
|
|
const ClientRole = new plugins.smartsocket.SocketRole({
|
2019-04-24 16:20:31 +00:00
|
|
|
name: 'UniverseClient',
|
|
|
|
passwordHash: plugins.smarthash.sha256FromStringSync('UniverseClient') // authentication happens on another level
|
2018-05-30 14:34:06 +00:00
|
|
|
});
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// add the role to smartsocket
|
2018-05-30 14:34:06 +00:00
|
|
|
this.smartsocket.addSocketRoles([ClientRole]);
|
|
|
|
|
2019-08-13 11:04:49 +00:00
|
|
|
const socketFunctionSubscription = new plugins.smartsocket.SocketFunction({
|
2019-06-06 21:23:37 +00:00
|
|
|
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
|
2018-05-30 14:34:06 +00:00
|
|
|
funcName: 'channelSubscription',
|
2019-08-12 12:59:37 +00:00
|
|
|
funcDef: async (
|
|
|
|
dataArg: interfaces.IServerCallSubscribeActionPayload,
|
|
|
|
socketConnectionArg
|
|
|
|
) => {
|
2019-06-06 21:23:37 +00:00
|
|
|
// run in "this context" of this class
|
2019-08-12 12:59:37 +00:00
|
|
|
await (async () => {
|
2019-06-06 21:23:37 +00:00
|
|
|
const universeConnection = new UniverseConnection({
|
2019-06-10 15:46:06 +00:00
|
|
|
socketConnection: socketConnectionArg,
|
2019-08-13 13:48:20 +00:00
|
|
|
authenticationRequests: [dataArg]
|
2019-08-12 12:59:37 +00:00
|
|
|
});
|
2019-08-13 13:48:20 +00:00
|
|
|
await UniverseConnection.addConnectionToCache(this, universeConnection);
|
2019-08-12 12:59:37 +00:00
|
|
|
return {
|
|
|
|
'subscription status': 'success'
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-13 11:04:49 +00:00
|
|
|
const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({
|
2019-08-12 12:59:37 +00:00
|
|
|
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
|
|
|
|
funcName: 'processMessage',
|
|
|
|
funcDef: async (dataArg: interfaces.IUniverseMessage, socketConnectionArg) => {
|
|
|
|
// run in "this" context of this class
|
|
|
|
await (async () => {
|
|
|
|
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
|
|
|
|
this.universeCache,
|
|
|
|
socketConnectionArg
|
|
|
|
);
|
|
|
|
if (universeConnection) {
|
2019-08-13 13:55:01 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('ok', 'found UniverseConnection for socket for incoming message');
|
2019-08-12 12:59:37 +00:00
|
|
|
} else {
|
2019-08-13 13:55:01 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('warn', 'found no Authorized channel for incoming message');
|
2019-08-12 15:23:10 +00:00
|
|
|
return {
|
|
|
|
error: 'You need to authenticate for a channel'
|
|
|
|
};
|
2019-08-12 12:59:37 +00:00
|
|
|
}
|
2019-08-13 13:48:20 +00:00
|
|
|
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(socketConnectionArg, dataArg);
|
2019-08-12 13:10:40 +00:00
|
|
|
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
|
2019-08-12 12:59:37 +00:00
|
|
|
this.universeCache,
|
2019-08-12 13:10:40 +00:00
|
|
|
unauthenticatedMessage
|
2019-08-12 12:59:37 +00:00
|
|
|
);
|
2019-08-12 13:12:31 +00:00
|
|
|
if (foundChannel && unauthenticatedMessage.authenticated) {
|
2019-08-12 13:10:40 +00:00
|
|
|
const authenticatedMessage = unauthenticatedMessage;
|
|
|
|
await this.universeCache.addMessage(authenticatedMessage);
|
|
|
|
}
|
2019-06-06 20:22:45 +00:00
|
|
|
})();
|
2019-04-30 17:16:03 +00:00
|
|
|
}
|
2018-05-30 14:34:06 +00:00
|
|
|
});
|
|
|
|
|
2019-08-13 11:04:49 +00:00
|
|
|
// add socket functions
|
|
|
|
this.smartsocket.addSocketFunction(socketFunctionSubscription);
|
|
|
|
this.smartsocket.addSocketFunction(socketFunctionProcessMessage);
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// add smartsocket to the running smartexpress app
|
2019-04-24 16:20:31 +00:00
|
|
|
this.smartsocket.setExternalServer('smartexpress', this.smartexpressServer as any);
|
|
|
|
// start everything
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.start();
|
2019-04-24 16:20:31 +00:00
|
|
|
await this.smartsocket.start();
|
2019-08-13 13:55:01 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('success', 'started universe');
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
/**
|
|
|
|
* stop everything
|
|
|
|
*/
|
2018-03-20 07:16:54 +00:00
|
|
|
public async stopServer() {
|
2018-03-27 22:38:32 +00:00
|
|
|
await this.smartsocket.stop();
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.stop();
|
|
|
|
}
|
|
|
|
}
|