bigbluebutton-Github/bigbluebutton-html5/imports/startup/server/index.js

194 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-10-22 00:27:47 +08:00
import { Meteor } from 'meteor/meteor';
2019-03-29 23:47:07 +08:00
import { WebAppInternals } from 'meteor/webapp';
2017-12-21 00:10:01 +08:00
import Langmap from 'langmap';
2018-01-10 20:43:45 +08:00
import fs from 'fs';
2019-07-02 03:00:27 +08:00
import Users from '/imports/api/users';
import './settings';
import { lookup as lookupUserAgent } from 'useragent';
2016-10-18 20:03:51 +08:00
import Logger from './logger';
import Redis from './redis';
import setMinBrowserVersions from './minBrowserVersion';
2019-07-02 03:00:27 +08:00
import userLeaving from '/imports/api/users/server/methods/userLeaving';
const parse = Npm.require('url').parse;
2019-07-02 03:00:27 +08:00
const INTERVAL_TIME = 10000;
const AVAILABLE_LOCALES = fs.readdirSync('assets/app/locales');
2016-10-18 20:03:51 +08:00
Meteor.startup(() => {
const APP_CONFIG = Meteor.settings.public.app;
2018-03-22 03:16:07 +08:00
const env = Meteor.isDevelopment ? 'development' : 'production';
2019-03-29 23:47:07 +08:00
const CDN_URL = APP_CONFIG.cdn;
2019-03-30 01:33:23 +08:00
if (CDN_URL.trim()) {
2019-03-29 23:47:07 +08:00
// Add CDN
BrowserPolicy.content.disallowEval();
BrowserPolicy.content.allowInlineScripts();
BrowserPolicy.content.allowInlineStyles();
BrowserPolicy.content.allowImageDataUrl(CDN_URL);
BrowserPolicy.content.allowFontDataUrl(CDN_URL);
BrowserPolicy.content.allowOriginForAll(CDN_URL);
WebAppInternals.setBundledJsCssPrefix(CDN_URL + APP_CONFIG.basename);
const fontRegExp = /\.(eot|ttf|otf|woff|woff2)$/;
2019-03-29 23:47:07 +08:00
WebApp.rawConnectHandlers.use('/', (req, res, next) => {
2019-03-29 23:47:07 +08:00
if (fontRegExp.test(req._parsedUrl.pathname)) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Vary', 'Origin');
res.setHeader('Pragma', 'public');
res.setHeader('Cache-Control', '"public"');
}
return next();
});
}
setMinBrowserVersions();
2019-07-02 03:00:27 +08:00
Meteor.setInterval(() => {
const currentTime = Date.now();
Logger.info('Checking for inactive users');
const users = Users.find({
connectionStatus: 'online',
lastPing: {
$lt: (currentTime - INTERVAL_TIME), // get user who has not pinged in the last 10 seconds
},
}).fetch();
if (!users.length) return Logger.info('No inactive users');
Logger.info('Removing inactive users');
users.forEach((user) => {
2019-07-03 01:32:16 +08:00
const loginTimeDelta = currentTime - user.loginTime;
const lastPingDelta = currentTime - user.lastPing;
2019-07-02 03:00:27 +08:00
user.requesterUserId = user.userId;
2019-07-03 01:32:16 +08:00
return loginTimeDelta >= INTERVAL_TIME
&& lastPingDelta >= INTERVAL_TIME
2019-07-02 03:00:27 +08:00
&& userLeaving(user, user.userId, user.connectionId);
});
return Logger.info('All inactive user have been removed');
}, INTERVAL_TIME);
Logger.warn(`SERVER STARTED.\nENV=${env},\nnodejs version=${process.version}\nCDN=${CDN_URL}\n`, APP_CONFIG);
});
2017-10-06 20:50:01 +08:00
WebApp.connectHandlers.use('/check', (req, res) => {
2017-06-03 03:25:02 +08:00
const payload = { html5clientStatus: 'running' };
2016-06-14 01:56:41 +08:00
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(JSON.stringify(payload));
});
WebApp.connectHandlers.use('/locale', (req, res) => {
2017-03-10 03:50:21 +08:00
const APP_CONFIG = Meteor.settings.public.app;
const fallback = APP_CONFIG.defaultSettings.application.fallbackLocale;
2018-05-28 19:35:24 +08:00
const browserLocale = req.query.locale.split(/[-_]/g);
const localeList = [fallback];
2018-05-30 20:39:02 +08:00
const usableLocales = AVAILABLE_LOCALES
2018-05-28 19:35:24 +08:00
.map(file => file.replace('.json', ''))
.reduce((locales, locale) => (locale.match(browserLocale[0])
? [...locales, locale]
: locales), []);
2018-05-30 20:39:02 +08:00
const regionDefault = usableLocales.find(locale => browserLocale[0] === locale);
2018-05-28 19:35:24 +08:00
if (regionDefault) localeList.push(regionDefault);
2018-05-30 21:09:33 +08:00
if (!regionDefault && usableLocales.length) localeList.push(usableLocales[0]);
2018-05-28 19:35:24 +08:00
let normalizedLocale;
2017-03-10 03:50:21 +08:00
let messages = {};
2017-10-27 19:36:27 +08:00
2018-05-28 19:35:24 +08:00
if (browserLocale.length > 1) {
normalizedLocale = `${browserLocale[0]}_${browserLocale[1].toUpperCase()}`;
2017-10-27 19:36:27 +08:00
localeList.push(normalizedLocale);
2017-03-10 03:50:21 +08:00
}
2018-05-28 19:35:24 +08:00
2017-10-27 19:36:27 +08:00
localeList.forEach((locale) => {
2017-03-10 03:50:21 +08:00
try {
const data = Assets.getText(`locales/${locale}.json`);
messages = Object.assign(messages, JSON.parse(data));
2017-12-21 00:10:01 +08:00
normalizedLocale = locale;
2017-03-10 03:50:21 +08:00
} catch (e) {
Logger.warn(`'Could not process locale ${locale}:${e}`);
// Getting here means the locale is not available in the current locale files.
2017-03-10 03:50:21 +08:00
}
});
res.setHeader('Content-Type', 'application/json');
2017-10-27 19:36:27 +08:00
res.end(JSON.stringify({ normalizedLocale, messages }));
});
2017-04-06 20:36:59 +08:00
WebApp.connectHandlers.use('/locales', (req, res) => {
let locales = [];
2017-12-21 00:10:01 +08:00
try {
locales = AVAILABLE_LOCALES
2017-12-21 00:10:01 +08:00
.map(file => file.replace('.json', ''))
.map(file => file.replace('_', '-'))
.map(locale => ({
locale,
name: Langmap[locale].nativeName,
}));
} catch (e) {
Logger.warn(`'Could not process locales error: ${e}`);
2017-04-06 20:36:59 +08:00
}
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(JSON.stringify(locales));
2017-04-06 20:36:59 +08:00
});
WebApp.connectHandlers.use('/feedback', (req, res) => {
req.on('data', Meteor.bindEnvironment((data) => {
const body = JSON.parse(data);
const {
meetingId,
userId,
authToken,
} = body;
const user = Users.findOne({
meetingId,
userId,
connectionStatus: 'offline',
authToken,
});
2019-07-07 06:48:33 +08:00
if (!user) {
Logger.error(`Feedback failed, user with id=${userId} wasn't found`);
res.setHeader('Content-Type', 'application/json');
res.writeHead(500);
res.end(JSON.stringify({ status: 'ok' }));
return;
}
const feedback = {
userName: user.name,
...body,
};
Logger.info('FEEDBACK LOG:', feedback);
}));
req.on('end', Meteor.bindEnvironment(() => {
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(JSON.stringify({ status: 'ok' }));
}));
});
WebApp.connectHandlers.use('/useragent', (req, res) => {
const userAgent = req.headers['user-agent'];
let response = 'No user agent found in header';
if (userAgent) {
2019-05-03 00:47:40 +08:00
response = lookupUserAgent(userAgent).toString();
}
Logger.info(`The requesting user agent is ${response}`);
// res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(response);
});
2016-10-18 20:03:51 +08:00
export const eventEmitter = Redis.emitter;
2017-06-03 03:25:02 +08:00
export const redisPubSub = Redis;