2018-04-13 13:45:48 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
2019-08-13 13:48:20 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2018-04-13 13:45:48 +00:00
|
|
|
|
2018-05-26 11:44:32 +00:00
|
|
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
|
|
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
2019-08-13 13:48:20 +00:00
|
|
|
import { UniverseConnection } from './smartuniverse.classes.universeconnection';
|
|
|
|
import { Universe } from './smartuniverse.classes.universe';
|
2018-04-29 12:17:26 +00:00
|
|
|
|
2018-04-13 13:45:48 +00:00
|
|
|
/**
|
2018-04-29 12:17:26 +00:00
|
|
|
* enables messages to stay within a certain scope.
|
2018-04-13 13:45:48 +00:00
|
|
|
*/
|
|
|
|
export class UniverseChannel {
|
2018-05-24 14:55:24 +00:00
|
|
|
// ======
|
|
|
|
// STATIC
|
|
|
|
// ======
|
2018-05-23 21:50:45 +00:00
|
|
|
|
2018-05-07 16:50:07 +00:00
|
|
|
/**
|
2018-05-19 22:41:59 +00:00
|
|
|
* creates new channels
|
|
|
|
* @param channelArg the name of the topic
|
|
|
|
* @param passphraseArg the secret thats used for a certain topic.
|
2018-05-07 16:50:07 +00:00
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static createChannel(
|
2019-08-13 13:48:20 +00:00
|
|
|
universeArg: Universe,
|
2018-05-26 11:44:32 +00:00
|
|
|
channelNameArg: string,
|
|
|
|
passphraseArg: string
|
|
|
|
) {
|
2019-08-13 13:48:20 +00:00
|
|
|
const newChannel = new UniverseChannel(universeArg, channelNameArg, passphraseArg);
|
|
|
|
universeArg.universeCache.channelMap.add(newChannel);
|
2018-05-19 22:41:59 +00:00
|
|
|
return newChannel;
|
2018-05-26 11:44:32 +00:00
|
|
|
}
|
2018-05-23 21:50:45 +00:00
|
|
|
|
2018-05-24 14:55:24 +00:00
|
|
|
/**
|
|
|
|
* returns boolean wether certain channel exists
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
|
2018-05-28 10:07:25 +00:00
|
|
|
const channel = universeCacheArg.channelMap.find(channelArg => {
|
2018-05-24 14:55:24 +00:00
|
|
|
return channelArg.name === channelNameArg;
|
|
|
|
});
|
2018-05-26 11:44:32 +00:00
|
|
|
if (channel) {
|
2018-05-24 14:55:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:59:37 +00:00
|
|
|
/**
|
|
|
|
* a static message authorization function that takes the UniverseCache
|
|
|
|
* (where messages and channels are stored and their lifetime is managed)
|
|
|
|
* and the universemessage to find a fitting channel for the message
|
|
|
|
* @param universeCacheArg
|
|
|
|
* @param universeMessageArg
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static authorizeAMessageForAChannel(
|
|
|
|
universeCacheArg: UniverseCache,
|
|
|
|
universeMessageArg: UniverseMessage
|
2019-08-12 13:10:40 +00:00
|
|
|
): UniverseChannel {
|
2018-05-28 10:07:25 +00:00
|
|
|
const foundChannel = universeCacheArg.channelMap.find(universeChannel => {
|
2018-05-26 11:44:32 +00:00
|
|
|
const result = universeChannel.authenticate(universeMessageArg);
|
|
|
|
return result;
|
2018-05-23 22:14:57 +00:00
|
|
|
});
|
2018-05-26 11:44:32 +00:00
|
|
|
if (foundChannel) {
|
|
|
|
universeMessageArg.authenticated = true;
|
|
|
|
universeMessageArg.universeChannelList.add(foundChannel);
|
2019-08-13 13:55:01 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('ok', 'message authorized');
|
2018-05-23 22:14:57 +00:00
|
|
|
return foundChannel;
|
|
|
|
} else {
|
2018-05-26 11:44:32 +00:00
|
|
|
universeMessageArg.authenticated = false;
|
|
|
|
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
|
2019-08-13 13:55:01 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('warn', 'message not valid');
|
2019-08-12 13:10:40 +00:00
|
|
|
return null;
|
2018-05-23 22:14:57 +00:00
|
|
|
}
|
2018-05-26 11:44:32 +00:00
|
|
|
}
|
2018-05-23 22:14:57 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
public static getUniverseChannelByName(universeRef: Universe, universeChannelName: string) {
|
|
|
|
return universeRef.universeCache.channelMap.find(channelArg => {
|
|
|
|
return channelArg.name === universeChannelName;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-24 14:55:24 +00:00
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2018-05-19 22:41:59 +00:00
|
|
|
/**
|
|
|
|
* the name of the channel
|
|
|
|
*/
|
|
|
|
public name: string;
|
2019-08-13 13:48:20 +00:00
|
|
|
public universeRef: Universe;
|
2018-04-29 12:17:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-19 22:41:59 +00:00
|
|
|
* the passphrase for the channel
|
2018-04-29 12:17:26 +00:00
|
|
|
*/
|
2018-05-19 22:41:59 +00:00
|
|
|
public passphrase: string;
|
2018-04-29 12:17:26 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
constructor(universeArg: Universe, channelNameArg: string, passphraseArg: string) {
|
|
|
|
this.universeRef = universeArg;
|
2018-05-19 22:41:59 +00:00
|
|
|
this.name = channelNameArg;
|
|
|
|
this.passphrase = passphraseArg;
|
|
|
|
}
|
2018-04-13 13:45:48 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-12 12:59:37 +00:00
|
|
|
* authenticates a client on the server side by matching
|
|
|
|
* # the messages channelName against the unverseChannel's name
|
|
|
|
* # the messages password against the universeChannel's password
|
2018-04-13 13:45:48 +00:00
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public authenticate(universeMessageArg: UniverseMessage): boolean {
|
|
|
|
return (
|
2019-04-22 21:11:51 +00:00
|
|
|
this.name === universeMessageArg.targetChannelName &&
|
|
|
|
this.passphrase === universeMessageArg.passphrase
|
2018-05-26 11:44:32 +00:00
|
|
|
);
|
2018-05-19 22:41:59 +00:00
|
|
|
}
|
2018-05-28 10:07:25 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
/**
|
|
|
|
* pushes a message to clients
|
|
|
|
* @param messageArg
|
|
|
|
*/
|
|
|
|
public async pushToClients(messageArg: UniverseMessage) {
|
|
|
|
const universeConnectionsWithChannelAccess: UniverseConnection[] = [];
|
|
|
|
this.universeRef.universeCache.connectionMap.forEach(async socketConnection => {
|
|
|
|
if (socketConnection.authenticatedChannels.includes(this)) {
|
|
|
|
universeConnectionsWithChannelAccess.push(socketConnection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (const universeConnection of universeConnectionsWithChannelAccess) {
|
2019-09-01 15:04:25 +00:00
|
|
|
const smartsocket = universeConnection.socketConnection
|
|
|
|
.smartsocketRef as plugins.smartsocket.Smartsocket;
|
2019-08-13 13:48:20 +00:00
|
|
|
const universeMessageToSend: interfaces.IUniverseMessage = {
|
|
|
|
id: messageArg.id,
|
|
|
|
timestamp: messageArg.timestamp,
|
|
|
|
passphrase: messageArg.passphrase,
|
|
|
|
targetChannelName: this.name,
|
|
|
|
messageText: messageArg.messageText,
|
|
|
|
payload: messageArg.payload,
|
|
|
|
payloadStringType: messageArg.payloadStringType
|
|
|
|
};
|
2019-09-01 15:04:25 +00:00
|
|
|
smartsocket.clientCall(
|
|
|
|
'processMessage',
|
|
|
|
universeMessageToSend,
|
|
|
|
universeConnection.socketConnection
|
|
|
|
);
|
2019-08-13 13:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 13:45:48 +00:00
|
|
|
}
|