swaf/dist/components/WebSocketServerComponent.js
2020-04-22 17:49:58 +02:00

73 lines
11 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import ApplicationComponent from "../ApplicationComponent";
import { Server as WebSocketServer } from "ws";
import Logger from "../Logger";
import cookie from "cookie";
import cookieParser from "cookie-parser";
import config from "config";
export default class WebSocketServerComponent extends ApplicationComponent {
constructor(application, expressAppComponent, storeComponent) {
super();
this.expressAppComponent = expressAppComponent;
this.application = application;
this.storeComponent = storeComponent;
}
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
const listeners = this.application.getWebSocketListeners();
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) {
socket.close(1002, `Can't process request without cookies.`);
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, session);
listener.handle(socket, request, session).catch(err => {
Logger.error(err, 'Error in websocket listener.');
});
});
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (this.wss) {
yield this.close('WebSocket server', this.wss, this.wss.close);
}
});
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiV2ViU29ja2V0U2VydmVyQ29tcG9uZW50LmpzIiwic291cmNlUm9vdCI6Ii4vIiwic291cmNlcyI6WyJjb21wb25lbnRzL1dlYlNvY2tldFNlcnZlckNvbXBvbmVudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFBQSxPQUFPLG9CQUFvQixNQUFNLHlCQUF5QixDQUFDO0FBRTNELE9BQWtCLEVBQUMsTUFBTSxJQUFJLGVBQWUsRUFBQyxNQUFNLElBQUksQ0FBQztBQUN4RCxPQUFPLE1BQU0sTUFBTSxXQUFXLENBQUM7QUFDL0IsT0FBTyxNQUFNLE1BQU0sUUFBUSxDQUFDO0FBQzVCLE9BQU8sWUFBWSxNQUFNLGVBQWUsQ0FBQztBQUN6QyxPQUFPLE1BQU0sTUFBTSxRQUFRLENBQUM7QUFNNUIsTUFBTSxDQUFDLE9BQU8sT0FBTyx3QkFBeUIsU0FBUSxvQkFBMEI7SUFPNUUsWUFBWSxXQUF3QixFQUFFLG1CQUF3QyxFQUFFLGNBQThCO1FBQzFHLEtBQUssRUFBRSxDQUFDO1FBQ1IsSUFBSSxDQUFDLG1CQUFtQixHQUFHLG1CQUFtQixDQUFDO1FBQy9DLElBQUksQ0FBQyxXQUFXLEdBQUcsV0FBVyxDQUFDO1FBQy9CLElBQUksQ0FBQyxjQUFjLEdBQUcsY0FBYyxDQUFDO0lBQ3pDLENBQUM7SUFFWSxLQUFLLENBQUMsR0FBWSxFQUFFLE1BQWM7O1lBQzNDLE1BQU0sU0FBUyxHQUF1QyxJQUFJLENBQUMsV0FBVyxDQUFDLHFCQUFxQixFQUFFLENBQUM7WUFDL0YsSUFBSSxDQUFDLEdBQUcsR0FBRyxJQUFJLGVBQWUsQ0FBQztnQkFDM0IsTUFBTSxFQUFFLElBQUks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vic2VydmVyLmApO1xuICAgICAgICB9KS5vbignZXJyb3InLCAoZXJyKSA9PiB7XG4gICAgICAgICAgICBMb2dnZXIuZXJyb3IoZXJyLCAnQW4gZXJyb3Igb2NjdXJyZWQgaW4gdGhlIHdlYnNvY2tldCBzZXJ2ZXIuJyk7XG4gICAgICAgIH0pLm9uKCdjb25uZWN0aW9uJywgKHNvY2tldCwgcmVxdWVzdCkgPT4ge1xuICAgICAgICAgICAgY29uc3QgbGlzdGVuZXIgPSByZXF1ZXN0LnVybCA/IGxpc3RlbmVyc1tyZXF1ZXN0LnVybF0gOiBudWxsO1xuXG4gICAgICAgICAgICBpZiAoIWxpc3RlbmVyKSB7XG4gICAgICAgICAgICAgICAgc29ja2V0LmNsb3NlKDEwMDIsIGBQYXRoIG5vdCBmb3VuZCAke3JlcXVlc3QudXJsfWApO1xuICAgICAgICAgICAgICAgIHJldHVybjtcbiAgICAgICAgICAgIH0gZWxzZSBpZiAoIXJlcXVlc3QuaGVhZGVycy5jb29raWUpIHtcbiAgICAgICAgICAgICAgICBzb2NrZXQuY2xvc2UoMTAwMiwgYENhbid0IHByb2Nlc3MgcmVxdWVzdCB3aXRob3V0IGNvb2tpZXMuYCk7XG4gICAgICAgICAgICAgICAgcmV0dXJuO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBMb2dnZXIuZGVidWcoYFdlYnNvY2tldCBvbiAke3JlcXVlc3QudXJsfWApO1xuXG4gICAgICAgICAgICBjb25zdCBjb29raWVzID0gY29va2llLnBhcnNlKHJlcXVlc3QuaGVhZGVycy5jb29raWUpO1xuICAgICAgICAgICAgY29uc3Qgc2lkID0gY29va2llUGFyc2VyLnNpZ25lZENvb2tpZShjb29raWVzWydjb25uZWN0LnNpZCddLCBjb25maWcuZ2V0KCdzZXNzaW9uLnNlY3JldCcpKTtcblxuICAgICAgICAgICAgaWYgKCFzaWQpIHtcbiAgICAgICAgICAgICAgICBzb2NrZXQuY2xvc2UoMTAwMik7XG4gICAgICAgICAgICAgICAgcmV0dXJuO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBjb25zdCBzdG9yZSA9IHRoaXMuc3RvcmVDb21wb25lbnQuZ2V0U3RvcmUoKTtcbiAgICAgICAgICAgIHN0b3JlLmdldChzaWQsIChlcnIsIHNlc3Npb24pID0+IHtcbiAgICAgICAgICAgICAgICBpZiAoZXJyIHx8ICFzZXNzaW9uKSB7XG4gICAgICAgICAgICAgICAgICAgIExvZ2dlci5lcnJvcihlcnIsICdFcnJvciB3aGlsZSBpbml0aWFsaXppbmcgc2Vzc2lvbiBpbiB3ZWJzb2NrZXQuJyk7XG4gICAgICAgICAgICAgICAgICAgIHNvY2tldC5jbG9zZSgxMDExKTtcbiAgICAgICAgICAgICAgICAgICAgcmV0dXJuO1xuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIHNlc3Npb24uaWQgPSBzaWQ7XG5cbiAgICAgICAgICAgICAgICBzdG9yZS5jcmVhdGVTZXNzaW9uKDxSZXF1ZXN0PnJlcXVlc3QsIHNlc3Npb24pO1xuICAgICAgICAgICAgICAgIGxpc3RlbmVyLmhhbmRsZShzb2NrZXQsIHJlcXVlc3QsIHNlc3Npb24pLmNhdGNoKGVyciA9PiB7XG4gICAgICAgICAgICAgICAgICAgIExvZ2dlci5lcnJvcihlcnIsICdFcnJvciBpbiB3ZWJzb2NrZXQgbGlzdGVuZXIuJyk7XG4gICAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICB9KTtcbiAgICAgICAgfSk7XG4gICAgfVxuXG4gICAgcHVibGljIGFzeW5jIHN0b3AoKTogUHJvbWlzZTx2b2lkPiB7XG4gICAgICAgIGlmICh0aGlzLndzcykge1xuICAgICAgICAgICAgYXdhaXQgdGhpcy5jbG9zZSgnV2ViU29ja2V0IHNlcnZlcicsIHRoaXMud3NzLCB0aGlzLndzcy5jbG9zZSk7XG4gICAgICAgIH1cbiAgICB9XG59Il19