2022-02-28 22:04:36 -07:00
|
|
|
import { WebSocketServer } from 'ws';
|
2022-01-12 08:02:30 -07:00
|
|
|
import cookie from 'cookie';
|
|
|
|
import httpServer from 'http';
|
|
|
|
import log4js from 'log4js';
|
2022-03-21 04:56:42 -06:00
|
|
|
import authenticationController from '../../controllers/authentication';
|
2022-03-20 17:59:37 -06:00
|
|
|
import athenaRealtime from '../athena';
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-03-20 17:59:37 -06:00
|
|
|
import controlsFunction from './controls';
|
|
|
|
import realtimeCommands from './commands';
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-03-22 09:14:08 -06:00
|
|
|
const logger = log4js.getLogger();
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-08 15:32:22 -07:00
|
|
|
let server;
|
|
|
|
let wss;
|
2022-01-12 08:02:30 -07:00
|
|
|
let controls;
|
2022-01-08 15:32:22 -07:00
|
|
|
|
2022-01-08 15:00:08 -07:00
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
2022-01-03 09:34:46 -07:00
|
|
|
function __server() {
|
|
|
|
server = httpServer.createServer();
|
|
|
|
|
2022-01-12 08:02:30 -07:00
|
|
|
wss = new WebSocketServer({ server }, { path: '/realtime/v1', handshakeTimeout: 500 });
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-02-28 22:04:36 -07:00
|
|
|
server.listen(process.env.CLIENT_SOCKET_PORT, process.env.CLIENT_SOCKET_HOST, () => {
|
|
|
|
logger.info(`Web(Server) - UP @ ${process.env.CLIENT_SOCKET_HOST}:${process.env.CLIENT_SOCKET_PORT}`);
|
2022-01-07 18:35:55 -07:00
|
|
|
});
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-07 18:35:55 -07:00
|
|
|
wss.on('connection', manageConnection);
|
|
|
|
wss.on('close', () => {
|
|
|
|
logger.info('Web(Websocket) - DOWN');
|
2022-01-03 09:34:46 -07:00
|
|
|
});
|
|
|
|
return wss;
|
|
|
|
}
|
|
|
|
|
2022-01-08 16:28:15 -07:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-01-03 09:34:46 -07:00
|
|
|
function buildResponse(ws, success, msg, data) {
|
2022-01-07 18:35:55 -07:00
|
|
|
ws.send(JSON.stringify({
|
2022-01-08 13:43:57 -07:00
|
|
|
success, msg, data, timestamp: Date.now(),
|
2022-01-07 18:35:55 -07:00
|
|
|
}));
|
2022-01-03 09:34:46 -07:00
|
|
|
}
|
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
async function authenticateUser(ws, req) {
|
|
|
|
// if (req.headers.Authorization) {
|
|
|
|
// account = await authenticationController.getAccountFromJWT(req.headers.Authorization)
|
|
|
|
// }
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
if (!req.headers.cookie) {
|
|
|
|
// TODO: send error
|
|
|
|
ws.terminate();
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
const cookies = cookie.parse(req.headers.cookie);
|
|
|
|
if (!cookies.jwt) {
|
|
|
|
// TODO: send error
|
|
|
|
ws.terminate();
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
const account = await authenticationController.getAccountFromJWT(cookies.jwt);
|
|
|
|
if (!account) {
|
|
|
|
// TODO: send error
|
|
|
|
ws.terminate();
|
|
|
|
return false;
|
2022-01-03 09:34:46 -07:00
|
|
|
}
|
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
ws.account = account;
|
|
|
|
return true;
|
2022-01-07 18:35:55 -07:00
|
|
|
}
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
async function manageConnection(ws, req) {
|
2022-01-07 18:35:55 -07:00
|
|
|
logger.info(`Web(Websocket) - New Connection ${ws._socket.remoteAddress}`);
|
2022-01-03 09:34:46 -07:00
|
|
|
|
2022-01-08 15:57:55 -07:00
|
|
|
await authenticateUser(ws, req);
|
2022-01-03 09:34:46 -07:00
|
|
|
|
|
|
|
console.log(ws.account);
|
|
|
|
|
2022-01-07 18:35:55 -07:00
|
|
|
ws.on('message', async (message) => {
|
|
|
|
console.log(message);
|
2022-01-03 09:34:46 -07:00
|
|
|
const msg = JSON.parse(message.toString('utf8'));
|
|
|
|
|
|
|
|
switch (msg.command) {
|
2022-01-07 18:35:55 -07:00
|
|
|
case 'is_dongle_online':
|
2022-01-03 09:34:46 -07:00
|
|
|
return realtimeCommands.isDongleOnline(ws, msg);
|
2022-01-07 18:35:55 -07:00
|
|
|
case 'reboot_dongle':
|
|
|
|
return realtimeCommands.rebootDongle(ws, msg);
|
|
|
|
case 'take_snapshot':
|
|
|
|
return realtimeCommands.takeSnapshot(ws, msg);
|
2022-01-03 09:34:46 -07:00
|
|
|
default:
|
2022-01-07 18:35:55 -07:00
|
|
|
return ws.send(JSON.stringify({
|
2022-01-08 13:43:57 -07:00
|
|
|
error: true, id: msg.id || null, msg: 'VERIFY_DATA', data: { msg },
|
2022-01-07 18:35:55 -07:00
|
|
|
}));
|
2022-01-03 09:34:46 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return wss;
|
|
|
|
|
2022-01-07 18:35:55 -07:00
|
|
|
// ws.send(JSON.stringify(await commandBuilder('reboot')))
|
2022-01-03 09:34:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const websocketServer = __server();
|
|
|
|
|
2022-01-12 08:02:30 -07:00
|
|
|
controls = controlsFunction(websocketServer);
|
2022-01-03 09:34:46 -07:00
|
|
|
|
|
|
|
athenaRealtime.realtimeCallback(controls);
|
|
|
|
|
2022-01-12 08:02:30 -07:00
|
|
|
export default {
|
2022-01-03 09:34:46 -07:00
|
|
|
controls,
|
2022-01-08 13:43:57 -07:00
|
|
|
websocketServer,
|
2022-01-07 18:35:55 -07:00
|
|
|
};
|