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