2020-04-22 15:52:17 +02:00
|
|
|
import ApplicationComponent from "../ApplicationComponent";
|
2020-07-11 11:46:16 +02:00
|
|
|
import {Express, Request} from "express";
|
2020-04-22 15:52:17 +02:00
|
|
|
import WebSocket, {Server as WebSocketServer} from "ws";
|
|
|
|
import Logger from "../Logger";
|
|
|
|
import cookie from "cookie";
|
|
|
|
import cookieParser from "cookie-parser";
|
|
|
|
import config from "config";
|
|
|
|
import ExpressAppComponent from "./ExpressAppComponent";
|
|
|
|
import Application from "../Application";
|
|
|
|
import RedisComponent from "./RedisComponent";
|
|
|
|
import WebSocketListener from "../WebSocketListener";
|
2020-09-23 16:11:51 +02:00
|
|
|
import NunjucksComponent from "./NunjucksComponent";
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 22:03:22 +02:00
|
|
|
export default class WebSocketServerComponent extends ApplicationComponent {
|
2020-04-22 15:52:17 +02:00
|
|
|
private wss?: WebSocket.Server;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(
|
2020-09-23 16:11:51 +02:00
|
|
|
private readonly application: Application,
|
|
|
|
private readonly expressAppComponent: ExpressAppComponent,
|
|
|
|
private readonly storeComponent: RedisComponent,
|
|
|
|
private readonly nunjucksComponent?: NunjucksComponent,
|
|
|
|
) {
|
2020-04-22 15:52:17 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public async start(_app: Express): Promise<void> {
|
|
|
|
const listeners: { [p: string]: WebSocketListener<Application> } = this.application.getWebSocketListeners();
|
2020-04-22 15:52:17 +02:00
|
|
|
this.wss = new WebSocketServer({
|
|
|
|
server: this.expressAppComponent.getServer(),
|
|
|
|
}, () => {
|
|
|
|
Logger.info(`Websocket server started over webserver.`);
|
|
|
|
}).on('error', (err) => {
|
|
|
|
Logger.error(err, 'An error occurred in the websocket server.');
|
|
|
|
}).on('connection', (socket, request) => {
|
|
|
|
const listener = request.url ? listeners[request.url] : null;
|
|
|
|
|
|
|
|
if (!listener) {
|
|
|
|
socket.close(1002, `Path not found ${request.url}`);
|
|
|
|
return;
|
|
|
|
} else if (!request.headers.cookie) {
|
2020-07-19 17:37:10 +02:00
|
|
|
listener.handle(socket, request, null).catch(err => {
|
|
|
|
Logger.error(err, 'Error in websocket listener.');
|
|
|
|
});
|
2020-04-22 15:52:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(`Websocket on ${request.url}`);
|
|
|
|
|
|
|
|
const cookies = cookie.parse(request.headers.cookie);
|
|
|
|
const sid = cookieParser.signedCookie(cookies['connect.sid'], config.get('session.secret'));
|
|
|
|
|
|
|
|
if (!sid) {
|
|
|
|
socket.close(1002);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const store = this.storeComponent.getStore();
|
|
|
|
store.get(sid, (err, session) => {
|
|
|
|
if (err || !session) {
|
|
|
|
Logger.error(err, 'Error while initializing session in websocket.');
|
|
|
|
socket.close(1011);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
session.id = sid;
|
|
|
|
|
|
|
|
store.createSession(<Request>request, session);
|
2020-09-23 08:55:35 +02:00
|
|
|
listener.handle(socket, request, <Express.Session>session).catch(err => {
|
2020-04-22 15:52:17 +02:00
|
|
|
Logger.error(err, 'Error in websocket listener.');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-09-23 16:11:51 +02:00
|
|
|
|
|
|
|
const env = this.nunjucksComponent?.getEnv();
|
|
|
|
if (env) {
|
|
|
|
env.addGlobal('websocketUrl', config.get('public_websocket_url'));
|
|
|
|
}
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async stop(): Promise<void> {
|
2020-09-25 23:42:15 +02:00
|
|
|
const wss = this.wss;
|
|
|
|
if (wss) {
|
|
|
|
await this.close('WebSocket server', callback => wss.close(callback));
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|