lintageddon 2
This commit is contained in:
parent
cd876ccfcb
commit
e9d7650ffe
@ -43,7 +43,7 @@ class ChannelManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addClientToChannel(client: PublicClient|PrivateClient|CustomClient, channel_id: string) {
|
addClientToChannel(client: PublicClient|PrivateClient|CustomClient, channel_id: string) {
|
||||||
let channel: PrivateChannel|PrivateChannel|CustomChannel|null = this.channelExists(channel_id);
|
const channel: PrivateChannel|PrivateChannel|CustomChannel|null = this.channelExists(channel_id);
|
||||||
|
|
||||||
if (channel) {
|
if (channel) {
|
||||||
channel.addClient(client);
|
channel.addClient(client);
|
||||||
@ -75,7 +75,7 @@ class ChannelManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
removeClientFromChannel(client_id: number, channel_id: string) {
|
removeClientFromChannel(client_id: number, channel_id: string) {
|
||||||
for (let channel of this.channels) {
|
for (const channel of this.channels) {
|
||||||
if (channel.id == channel_id) {
|
if (channel.id == channel_id) {
|
||||||
if (channel.removeClient(client_id)) {
|
if (channel.removeClient(client_id)) {
|
||||||
logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`);
|
logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`);
|
||||||
@ -111,6 +111,6 @@ class ChannelManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export default ChannelManager;
|
export default ChannelManager;
|
||||||
|
@ -14,7 +14,7 @@ class ChannelBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
broadcastMessage(from: PublicClient|PrivateClient|CustomClient, message: object) {
|
broadcastMessage(from: PublicClient|PrivateClient|CustomClient, message: object) {
|
||||||
for (let to of this.clients) {
|
for (const to of this.clients) {
|
||||||
if (this.messageTransactionPossible(from, to)) {
|
if (this.messageTransactionPossible(from, to)) {
|
||||||
to.ws.send(JSON.stringify(message));
|
to.ws.send(JSON.stringify(message));
|
||||||
logger.accessLog.info(`sent to ${to.id}`, { data: { message: message }});
|
logger.accessLog.info(`sent to ${to.id}`, { data: { message: message }});
|
||||||
@ -23,7 +23,7 @@ class ChannelBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {'status': 'success', 'message': `message broadcast complete`};
|
return {status: 'success', message: `message broadcast complete`};
|
||||||
}
|
}
|
||||||
|
|
||||||
messageTransactionPossible(_from: PublicClient|PrivateClient|CustomClient, _to: PublicClient|PrivateClient|CustomClient) {
|
messageTransactionPossible(_from: PublicClient|PrivateClient|CustomClient, _to: PublicClient|PrivateClient|CustomClient) {
|
||||||
@ -33,11 +33,11 @@ class ChannelBase {
|
|||||||
addClient(client: PublicClient|PrivateClient|CustomClient) {
|
addClient(client: PublicClient|PrivateClient|CustomClient) {
|
||||||
if (this.clientExists(client.id)) {
|
if (this.clientExists(client.id)) {
|
||||||
logger.errorLog.info('Client already exits in channel', {channelId: this.id, clientId: client.id});
|
logger.errorLog.info('Client already exits in channel', {channelId: this.id, clientId: client.id});
|
||||||
return {'status': 'notice', 'message': 'client already exists in channel'};
|
return {status: 'notice', message: 'client already exists in channel'};
|
||||||
} else {
|
} else {
|
||||||
this.clients.push(client);
|
this.clients.push(client);
|
||||||
logger.accessLog.info('Added client to channel', {channelId: this.id, clientId: client.id});
|
logger.accessLog.info('Added client to channel', {channelId: this.id, clientId: client.id});
|
||||||
return {'status': 'success', 'message': 'client added'};
|
return {status: 'success', message: 'client added'};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,12 +7,12 @@ class CustomChannel extends ChannelBase {
|
|||||||
|
|
||||||
constructor(id: string, custom: any) {
|
constructor(id: string, custom: any) {
|
||||||
super(id);
|
super(id);
|
||||||
this.custom = custom
|
this.custom = custom;
|
||||||
}
|
}
|
||||||
|
|
||||||
messageTransactionPossible(from: CustomClient, to: CustomClient) {
|
messageTransactionPossible(from: CustomClient, to: CustomClient) {
|
||||||
return eval(this.custom.broadcastConditions)
|
return eval(this.custom.broadcastConditions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
export default CustomChannel;
|
export default CustomChannel;
|
||||||
|
@ -4,7 +4,7 @@ import PrivateClient from './clients/types/privateClient';
|
|||||||
import CustomClient from './clients/types/customClient';
|
import CustomClient from './clients/types/customClient';
|
||||||
import ChannelManager from './channelManager';
|
import ChannelManager from './channelManager';
|
||||||
|
|
||||||
var logger = require('./logger');
|
const logger = require('./logger');
|
||||||
|
|
||||||
class ClientManager {
|
class ClientManager {
|
||||||
clients: any[] = [];
|
clients: any[] = [];
|
||||||
@ -15,7 +15,7 @@ class ClientManager {
|
|||||||
|
|
||||||
addClient(data: any, channelManager: ChannelManager, ws: WebSocket) {
|
addClient(data: any, channelManager: ChannelManager, ws: WebSocket) {
|
||||||
if (data.client_type && !this.clientExists(data.user_id)) {
|
if (data.client_type && !this.clientExists(data.user_id)) {
|
||||||
let client = this.getClientType(data, channelManager, ws);
|
const client = this.getClientType(data, channelManager, ws);
|
||||||
this.clients.push(client);
|
this.clients.push(client);
|
||||||
logger.accessLog.info(`client added to client manager: ${data.user_id}`);
|
logger.accessLog.info(`client added to client manager: ${data.user_id}`);
|
||||||
return client;
|
return client;
|
||||||
@ -30,7 +30,7 @@ class ClientManager {
|
|||||||
let result: PublicClient[]|PrivateClient[]|CustomClient[] = [];
|
let result: PublicClient[]|PrivateClient[]|CustomClient[] = [];
|
||||||
|
|
||||||
for (let client of this.clients) {
|
for (let client of this.clients) {
|
||||||
if (client.type() == client_type) {
|
if (client.type() === client_type) {
|
||||||
result.push(client);
|
result.push(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ class ClientManager {
|
|||||||
|
|
||||||
clientExists(id: number) {
|
clientExists(id: number) {
|
||||||
for (let client of this.clients) {
|
for (let client of this.clients) {
|
||||||
if (client.id == id) {
|
if (client.id === id) {
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,9 +71,9 @@ class ClientManager {
|
|||||||
try {
|
try {
|
||||||
logger.accessLog.info(`attempting to create client of type ${data.client_type}, client id: ${data.user_id}...`);
|
logger.accessLog.info(`attempting to create client of type ${data.client_type}, client id: ${data.user_id}...`);
|
||||||
|
|
||||||
if (data.client_type == 'public') {
|
if (data.client_type === 'public') {
|
||||||
return new PublicClient(data, ws, channelManager, this)
|
return new PublicClient(data, ws, channelManager, this)
|
||||||
} else if (data.channel_type == 'private') {
|
} else if (data.channel_type === 'private') {
|
||||||
return new PrivateClient(data, ws, channelManager, this)
|
return new PrivateClient(data, ws, channelManager, this)
|
||||||
} else {
|
} else {
|
||||||
return new CustomClient(data, ws, channelManager, this)
|
return new CustomClient(data, ws, channelManager, this)
|
||||||
|
@ -28,7 +28,7 @@ class ClientBase {
|
|||||||
this.roles = ['receiver']
|
this.roles = ['receiver']
|
||||||
this.messageListener = (data: any) => {
|
this.messageListener = (data: any) => {
|
||||||
if (this.channel) {
|
if (this.channel) {
|
||||||
logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {data: data});
|
logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {data});
|
||||||
data = messageManager.prepareMessage(data, this.channel, this);
|
data = messageManager.prepareMessage(data, this.channel, this);
|
||||||
this.messageTransaction(data);
|
this.messageTransaction(data);
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,15 @@ import ClientBase from '../clientBase';
|
|||||||
import ClientManager from '../../clientManager';
|
import ClientManager from '../../clientManager';
|
||||||
import ChannelManager from '../../channelManager';
|
import ChannelManager from '../../channelManager';
|
||||||
import * as WebSocket from 'ws';
|
import * as WebSocket from 'ws';
|
||||||
import * as Joi from 'joi';
|
|
||||||
|
|
||||||
var logger = require('../../logger');
|
const logger = require('../../logger');
|
||||||
|
|
||||||
class CustomClient extends ClientBase {
|
class CustomClient extends ClientBase {
|
||||||
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
||||||
super(data, ws, channelManager, clientManager);
|
super(data, ws, channelManager, clientManager);
|
||||||
this.roles = data.user_roles
|
this.roles = data.user_roles;
|
||||||
logger.accessLog.info('Custom Client Created', {data: data});
|
logger.accessLog.info('Custom Client Created', {data: data});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export default CustomClient;
|
export default CustomClient;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import ClientBase from '../clientBase';
|
|||||||
import ClientManager from '../../clientManager';
|
import ClientManager from '../../clientManager';
|
||||||
import ChannelManager from '../../channelManager';
|
import ChannelManager from '../../channelManager';
|
||||||
|
|
||||||
var logger = require('../../logger');
|
const logger = require('../../logger');
|
||||||
|
|
||||||
class PrivateClient extends ClientBase {
|
class PrivateClient extends ClientBase {
|
||||||
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
||||||
@ -14,4 +14,3 @@ class PrivateClient extends ClientBase {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default PrivateClient;
|
export default PrivateClient;
|
||||||
|
|
||||||
|
@ -3,4 +3,3 @@ import ClientBase from '../clientBase';
|
|||||||
class PublicClient extends ClientBase {};
|
class PublicClient extends ClientBase {};
|
||||||
|
|
||||||
export default PublicClient;
|
export default PublicClient;
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ module.exports = {
|
|||||||
issuer: 'Yardstick Software',
|
issuer: 'Yardstick Software',
|
||||||
subject: 'Braid JWT',
|
subject: 'Braid JWT',
|
||||||
audience: 'internal',
|
audience: 'internal',
|
||||||
algorithm: ["HS256"]
|
algorithm: ['HS256']
|
||||||
},
|
},
|
||||||
messageTypes : ['broadcast', 'direct', 'changeChannel']
|
messageTypes : ['broadcast', 'direct', 'changeChannel']
|
||||||
}
|
};
|
||||||
|
@ -8,4 +8,4 @@ module.exports = {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
var {winston, transports, createLogger, format} = require('winston');
|
const {winston, transports, createLogger, format} = require('winston');
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
// Set this to whatever, by default the path of the script.
|
// Set this to whatever, by default the path of the script.
|
||||||
var logPath = './logs/';
|
const logPath = './logs/';
|
||||||
|
|
||||||
const tsFormat = () => (new Date().toISOString());
|
const tsFormat = () => (new Date().toISOString());
|
||||||
const logFormat = format.combine(format.timestamp(), format.json());
|
const logFormat = format.combine(format.timestamp(), format.json());
|
||||||
@ -15,7 +15,7 @@ function loggerTransports(logName: string, logLevel: string) {
|
|||||||
level: logLevel
|
level: logLevel
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
};
|
}
|
||||||
|
|
||||||
const errorLog = createLogger({
|
const errorLog = createLogger({
|
||||||
format: logFormat,
|
format: logFormat,
|
||||||
|
@ -4,13 +4,13 @@ import Validations from './services/validations';
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
prepareMessage: (message: string) => {
|
prepareMessage: (message: string) => {
|
||||||
let validations = new Validations(message)
|
let validations = new Validations(message)
|
||||||
let parsed = JSON.parse(message)
|
let parsed = JSON.parse(message);
|
||||||
const result = Joi.validate(parsed, validations.MessageConditions);
|
const result = Joi.validate(parsed, validations.MessageConditions);
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
return result
|
return result;
|
||||||
} else {
|
} else {
|
||||||
return parsed
|
return parsed;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import * as cors from 'cors';
|
import * as cors from 'cors';
|
||||||
import * as express from 'express';
|
import * as express from 'express';
|
||||||
|
|
||||||
var app = require('./config/app');
|
const app = require('./config/app');
|
||||||
var corsOptions = {
|
const corsOptions = {
|
||||||
origin: (origin: string, callback: Function) => {
|
origin: (origin: string, callback: Function) => {
|
||||||
if (app.whitelist.indexOf(origin) !== -1) {
|
if (app.whitelist.indexOf(origin) !== -1) {
|
||||||
callback(null, true);
|
callback(null, true);
|
||||||
@ -12,14 +12,14 @@ var corsOptions = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
//application
|
//application
|
||||||
var appController = require('./controllers/appController');
|
const appController = require('./controllers/appController');
|
||||||
router.route(['/', '/home']).get(appController.home)
|
router.route(['/', '/home']).get(appController.home)
|
||||||
|
|
||||||
//auth
|
//auth
|
||||||
var authController = require('./controllers/authController');
|
const authController = require('./controllers/authController');
|
||||||
router.route('/auth/user').post(cors(), authController.confirmToken);
|
router.route('/auth/user').post(cors(), authController.confirmToken);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
@ -4,7 +4,7 @@ import * as jwt from 'jsonwebtoken';
|
|||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
|
|
||||||
// internal imports
|
// internal imports
|
||||||
var app = require('./config/app');
|
const app = require('./config/app');
|
||||||
const logger = require('./logger');
|
const logger = require('./logger');
|
||||||
|
|
||||||
import ClientManager from './clientManager';
|
import ClientManager from './clientManager';
|
||||||
@ -13,14 +13,14 @@ import PublicClient from './clients/types/publicClient';
|
|||||||
import PrivateClient from './clients/types/privateClient';
|
import PrivateClient from './clients/types/privateClient';
|
||||||
import CustomClient from './clients/types/customClient';
|
import CustomClient from './clients/types/customClient';
|
||||||
|
|
||||||
var wss = new WebSocket.Server({ maxPayload:250000, port: app.port });
|
const wss = new WebSocket.Server({ maxPayload:250000, port: app.port });
|
||||||
|
|
||||||
let clientManager = new ClientManager();
|
const clientManager = new ClientManager();
|
||||||
let channelManager = new ChannelManager();
|
const channelManager = new ChannelManager();
|
||||||
|
|
||||||
function connectionManager() {
|
function connectionManager() {
|
||||||
wss.on('connection', (ws: WebSocket, request: any, args: string) => {
|
wss.on('connection', (ws: WebSocket, request: any, args: string) => {
|
||||||
let result = JSON.parse(validateJWT(request));
|
const result = JSON.parse(validateJWT(request));
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
ws.send(`Unable to validate JWT, please try again or contact support...`);
|
ws.send(`Unable to validate JWT, please try again or contact support...`);
|
||||||
@ -59,7 +59,7 @@ function connectionManager() {
|
|||||||
function validateJWT(request: any) {
|
function validateJWT(request: any) {
|
||||||
try {
|
try {
|
||||||
let query = url.parse(request.url, true).query;
|
let query = url.parse(request.url, true).query;
|
||||||
let token = query.token || (app.environment == 'development' ? app.devToken : '');
|
let token = query.token || (app.environment === 'development' ? app.devToken : '');
|
||||||
return JSON.stringify(jwt.verify(token, app.secret, app.signOptions));
|
return JSON.stringify(jwt.verify(token, app.secret, app.signOptions));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
import PublicChannel from '../channels/types/publicChannel';
|
|
||||||
import PrivateChannel from '../channels/types/privateChannel';
|
|
||||||
import CustomChannel from '../channels/types/customChannel';
|
|
||||||
import * as Joi from 'joi'
|
import * as Joi from 'joi'
|
||||||
|
|
||||||
var app = require('../config/app');
|
const app = require('../config/app');
|
||||||
|
|
||||||
class Validations {
|
class Validations {
|
||||||
MessageConditions = {
|
MessageConditions = {
|
||||||
@ -14,11 +11,11 @@ class Validations {
|
|||||||
user_id: Joi.number().integer(),
|
user_id: Joi.number().integer(),
|
||||||
message: Joi.alternatives().try(Joi.string(), Joi.object()),
|
message: Joi.alternatives().try(Joi.string(), Joi.object()),
|
||||||
custom: Joi.object()
|
custom: Joi.object()
|
||||||
}
|
};
|
||||||
|
|
||||||
constructor(message: any) {
|
constructor(message: any) {
|
||||||
if (message.channel_type === 'custom') {
|
if (message.channel_type === 'custom') {
|
||||||
let conditions = message.conditions;
|
const conditions = message.conditions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user