The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/app/file-upload/server/lib/proxy.js

101 lines
2.4 KiB

import http from 'http';
import URL from 'url';
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { UploadFS } from 'meteor/jalik:ufs';
import { InstanceStatus } from 'meteor/konecty:multiple-instances-status';
import { Logger } from '../../../logger';
import { isDocker } from '../../../utils';
const logger = new Logger('UploadProxy');
WebApp.connectHandlers.stack.unshift({
route: '',
handle: Meteor.bindEnvironment(function (req, res, next) {
// Quick check to see if request should be catch
if (!req.url.includes(`/${UploadFS.config.storesPath}/`)) {
return next();
}
logger.debug({ msg: 'Upload URL:', url: req.url });
if (req.method !== 'POST') {
return next();
}
// Remove store path
const parsedUrl = URL.parse(req.url);
const path = parsedUrl.pathname.substr(UploadFS.config.storesPath.length + 1);
// Get store
const regExp = new RegExp('^/([^/?]+)/([^/?]+)$');
const match = regExp.exec(path);
// Request is not valid
if (match === null) {
res.writeHead(400);
res.end();
return;
}
// Get store
const store = UploadFS.getStore(match[1]);
if (!store) {
res.writeHead(404);
res.end();
return;
}
// Get file
const fileId = match[2];
const file = store.getCollection().findOne({ _id: fileId });
if (file === undefined) {
res.writeHead(404);
res.end();
return;
}
if (file.instanceId === InstanceStatus.id()) {
logger.debug('Correct instance');
return next();
}
// Proxy to other instance
const instance = InstanceStatus.getCollection().findOne({ _id: file.instanceId });
if (instance == null) {
res.writeHead(404);
res.end();
return;
}
Convert rocketchat-file-upload to main module structure (#13094) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix missed export * Fix canBeDeleted and canBeCreated function, remove async
7 years ago
if (instance.extraInformation.host === process.env.INSTANCE_IP && isDocker() === false) {
instance.extraInformation.host = 'localhost';
}
logger.debug(`Wrong instance, proxing to ${instance.extraInformation.host}:${instance.extraInformation.port}`);
const options = {
hostname: instance.extraInformation.host,
port: instance.extraInformation.port,
path: req.originalUrl,
method: 'POST',
};
logger.warn(
'UFS proxy middleware is deprecated as this upload method is not being used by Web/Mobile Clients. See this: https://docs.rocket.chat/api/rest-api/methods/rooms/upload',
);
const proxy = http.request(options, function (proxy_res) {
proxy_res.pipe(res, {
end: true,
});
});
req.pipe(proxy, {
end: true,
});
}),
});