clean up more prototype code:

* remove commented code
* serialize errors sent from http workers to the main process
* drop support for custom http headers set via config.js#httpHeaders
* websockets: only listen on localhost, respect websocketPort config in workers' proxy config
This commit is contained in:
ansuz 2022-12-20 18:03:52 +05:30
parent 24274e6c9b
commit 953c817c5b
2 changed files with 23 additions and 32 deletions

View File

@ -47,9 +47,8 @@ Logger.levels.forEach(level => {
info: info,
}, (err) => {
if (err) {
return void console.error(err); // XXX
return void console.error(new Error(err));
}
//console.log("Log statement received"); // XXX
});
};
});
@ -57,9 +56,12 @@ Logger.levels.forEach(level => {
const EVENTS = {};
var Env = JSON.parse(process.env.Env);
EVENTS.ENV_UPDATE = function (data /*, cb */) { // XXX
// XXX log?
Env = JSON.parse(data);
EVENTS.ENV_UPDATE = function (data /*, cb */) {
try {
Env = JSON.parse(data);
} catch (err) {
Log.error('HTTP_WORKER_ENV_UPDATE', Util.serializeError(err));
}
};
EVENTS.FLUSH_CACHE = function (data /*, cb */) { // XXX
@ -86,9 +88,6 @@ process.on('message', msg => {
});
*/
if (!(msg && msg.txid)) { return; }
//console.log('MESSAGE_RECEIVED', msg);
if (msg.type === 'REPLY') {
var txid = msg.txid;
return void response.handle(txid, [msg.error, msg.value]);
@ -100,9 +99,7 @@ process.on('message', msg => {
return void ev(msg.data, () => {});
}
}
console.error("UNHANDLED_MESSAGE", msg);
// XXX unhandled
});
@ -120,24 +117,15 @@ var EXEMPT = [
];
var cacheHeaders = function (Env, key, headers) {
if (Env.DEV_MODE) { return; } // XXX clustering
if (Env.DEV_MODE) { return; }
Env[key] = headers;
};
var getHeaders = function (Env, type) { // XXX clustering
var getHeaders = function (Env, type) {
var key = type + 'HeadersCache';
if (Env[key]) { return Env[key]; }
var headers = {};
var custom; // = undefined; //config.httpHeaders; // XXX drop support for this?
// if the admin provided valid http headers then use them
if (custom && typeof(custom) === 'object' && !Array.isArray(custom)) {
headers = Util.clone(custom);
} else {
// otherwise use the default
headers = Default.httpHeaders(Env);
}
var headers = Default.httpHeaders(Env);
headers['Content-Security-Policy'] = type === 'office'?
Default.padContentSecurity(Env):
@ -195,11 +183,14 @@ app.head(/^\/common\/feedback\.html/, function (req, res, next) {
}());
const { createProxyMiddleware } = require("http-proxy-middleware");
var proxyTarget = new URL('', 'ws:localhost');
proxyTarget.port = Env.websocketPort;
const wsProxy = createProxyMiddleware({
target: 'ws://localhost:3003', // XXX
target: proxyTarget.href,
ws: true,
logLevel: 'error', ///silent',
//pathFilter: '/cryptpad_websocket',
logLevel: 'error',
});
app.use('/cryptpad_websocket', wsProxy);
@ -309,7 +300,7 @@ var makeRouteCache = function (template, cacheName) {
};
};
var serveConfig = makeRouteCache(function (/* host */) { // XXX
var serveConfig = makeRouteCache(function () {
return [
'define(function(){',
'return ' + JSON.stringify({
@ -340,7 +331,7 @@ var serveConfig = makeRouteCache(function (/* host */) { // XXX
].join(';\n');
}, 'configCache');
var serveBroadcast = makeRouteCache(function (/* host */) { // XXX
var serveBroadcast = makeRouteCache(function () {
var maintenance = Env.maintenance;
if (maintenance && maintenance.end && maintenance.end < (+new Date())) {
maintenance = undefined;
@ -365,7 +356,7 @@ var Define = function (obj) {
});`;
};
app.get('/api/instance', function (req, res) { // XXX use caching?
app.get('/api/instance', function (req, res) {
res.setHeader('Content-Type', 'text/javascript');
res.send(Define({
name: Env.instanceName,
@ -402,9 +393,9 @@ app.get('/api/updatequota', function (req, res) {
res.status(404);
return void send404(res);
}
sendMessage({ // XXX test this
sendMessage({
command: 'UPDATE_QUOTA',
}, (err /*, value */) => {
}, (err) => {
if (err) {
res.status(500);
return void send500(res);
@ -415,7 +406,7 @@ app.get('/api/updatequota', function (req, res) {
app.get('/api/profiling', function (req, res) {
if (!Env.enableProfiling) { return void send404(res); }
sendMessage({ // XXX test this
sendMessage({
command: 'GET_PROFILING_DATA',
}, (err, value) => {
if (err) {

View File

@ -82,7 +82,7 @@ nThen(function (w) {
}
}).nThen(function (w) {
Env.httpServer = Http.createServer(app);
Env.httpServer.listen(Env.websocketPort, '::', w(function () {
Env.httpServer.listen(Env.websocketPort, 'localhost', w(function () {
Env.Log.info('WEBSOCKET_LISTENING', {
port: Env.websocketPort,
});