mirror of
https://github.com/Ride-The-Lightning/RTL
synced 2024-10-31 09:20:27 +00:00
9c59954205
Offers QR Code bug fix Websocket Authcheck csrf cookie validation Bug Fix: Wrong year in Date #918 Improved INFO & DEBUG Logging LND: Bug fix Color Setting in Config #925 2FA button toggle #906 Bug Fix: HTLC viewing #924 All Tooltips on form controls are updated with mat-icon:info Co-authored-by: saiy2k <saiy2k@gmail.com>
87 lines
4.7 KiB
JavaScript
87 lines
4.7 KiB
JavaScript
import express from 'express';
|
|
import sessions from 'express-session';
|
|
import cookieParser from 'cookie-parser';
|
|
import bodyParser from 'body-parser';
|
|
import { join, dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import CORS from './cors.js';
|
|
import CSRF from './csrf.js';
|
|
import sharedRoutes from '../routes/shared/index.js';
|
|
import lndRoutes from '../routes/lnd/index.js';
|
|
import clRoutes from '../routes/c-lightning/index.js';
|
|
import eclRoutes from '../routes/eclair/index.js';
|
|
import { Common } from './common.js';
|
|
import { Logger } from './logger.js';
|
|
import { Config } from './config.js';
|
|
import { CLWSClient } from '../controllers/c-lightning/webSocketClient.js';
|
|
import { ECLWSClient } from '../controllers/eclair/webSocketClient.js';
|
|
import { LNDWSClient } from '../controllers/lnd/webSocketClient.js';
|
|
const ONE_DAY = 1000 * 60 * 60 * 24;
|
|
export class ExpressApplication {
|
|
constructor() {
|
|
this.app = express();
|
|
this.logger = Logger;
|
|
this.common = Common;
|
|
this.config = Config;
|
|
this.eclWsClient = ECLWSClient;
|
|
this.clWsClient = CLWSClient;
|
|
this.lndWsClient = LNDWSClient;
|
|
this.directoryName = dirname(fileURLToPath(import.meta.url));
|
|
this.getApp = () => this.app;
|
|
this.loadConfiguration = () => {
|
|
this.config.setServerConfiguration();
|
|
};
|
|
this.setCORS = () => { CORS.mount(this.app); };
|
|
this.setCSRF = () => { CSRF.mount(this.app); };
|
|
this.setApplicationRoutes = () => {
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'App', msg: 'Setting up Application Routes..' });
|
|
this.app.use(this.common.baseHref + '/api', sharedRoutes);
|
|
this.app.use(this.common.baseHref + '/api/lnd', lndRoutes);
|
|
this.app.use(this.common.baseHref + '/api/cl', clRoutes);
|
|
this.app.use(this.common.baseHref + '/api/ecl', eclRoutes);
|
|
this.app.use(this.common.baseHref, express.static(join(this.directoryName, '../..', 'frontend')));
|
|
this.app.use((req, res, next) => {
|
|
res.cookie('XSRF-TOKEN', req.csrfToken ? req.csrfToken() : '');
|
|
res.sendFile(join(this.directoryName, '../..', 'frontend', 'index.html'));
|
|
});
|
|
this.app.use((err, req, res, next) => this.handleApplicationErrors(err, res));
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'App', msg: 'Application Routes Set' });
|
|
};
|
|
this.handleApplicationErrors = (err, res) => {
|
|
switch (err.code) {
|
|
case 'EACCES':
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'ERROR', fileName: 'App', msg: 'Server requires elevated privileges' });
|
|
res.status(406).send('Server requires elevated privileges.');
|
|
break;
|
|
case 'EADDRINUSE':
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'ERROR', fileName: 'App', msg: 'Server is already in use' });
|
|
res.status(409).send('Server is already in use.');
|
|
break;
|
|
case 'ECONNREFUSED':
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'ERROR', fileName: 'App', msg: 'Server is down/locked' });
|
|
res.status(401).send('Server is down/locked.');
|
|
break;
|
|
case 'EBADCSRFTOKEN':
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'ERROR', fileName: 'App', msg: 'Invalid CSRF token. Form tempered.' });
|
|
res.status(403).send('Invalid CSRF token, form tempered.');
|
|
break;
|
|
default:
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'ERROR', fileName: 'App', msg: 'DEFUALT ERROR', error: err });
|
|
res.status(400).send(JSON.stringify(err));
|
|
break;
|
|
}
|
|
};
|
|
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'App', msg: 'Starting Express Application..' });
|
|
this.app.set('trust proxy', true);
|
|
this.app.use(sessions({ secret: this.common.secret_key, saveUninitialized: true, cookie: { secure: false, maxAge: ONE_DAY }, resave: false }));
|
|
this.app.use(cookieParser(this.common.secret_key));
|
|
this.app.use(bodyParser.json({ limit: '25mb' }));
|
|
this.app.use(bodyParser.urlencoded({ extended: false, limit: '25mb' }));
|
|
this.loadConfiguration();
|
|
this.setCORS();
|
|
this.setCSRF();
|
|
this.setApplicationRoutes();
|
|
}
|
|
}
|
|
export default ExpressApplication;
|