diff --git a/src/infra_version.js b/src/infra_version.js index 9e0bca878..1e19c0a3d 100644 --- a/src/infra_version.js +++ b/src/infra_version.js @@ -22,6 +22,6 @@ exports = module.exports = { 'redis': { repo: 'cloudron/redis', tag: 'cloudron/redis:2.3.0@sha256:0e31ec817e235b1814c04af97b1e7cf0053384aca2569570ce92bef0d95e94d2' }, 'mail': { repo: 'cloudron/mail', tag: 'cloudron/mail:2.9.4@sha256:0e169b97a0584a76197d2bbc039d8698bf93f815588b3b43c251bd83dd545465' }, 'graphite': { repo: 'cloudron/graphite', tag: 'cloudron/graphite:2.3.0@sha256:b7bc1ca4f4d0603a01369a689129aa273a938ce195fe43d00d42f4f2d5212f50' }, - 'sftp': { repo: 'cloudron/sftp', tag: 'cloudron/sftp:1.1.0@sha256:0c1fe4dd6121900624dcb383251ecb0084c3810e095064933de671409d8d6d7b' } + 'sftp': { repo: 'cloudron/sftp', tag: 'cloudron/sftp:2.0.0@sha256:fcf7d51718c8cbfea7bd7ba49c3214d8fc2c06e0920594e288452085de432773' } } }; diff --git a/src/routes/filemanager.js b/src/routes/filemanager.js new file mode 100644 index 000000000..17dee7fcc --- /dev/null +++ b/src/routes/filemanager.js @@ -0,0 +1,41 @@ +'use strict'; + +exports = module.exports = { + proxy +}; + +var assert = require('assert'), + BoxError = require('../boxerror.js'), + docker = require('../docker.js'), + middleware = require('../middleware/index.js'), + HttpError = require('connect-lastmile').HttpError, + safe = require('safetydance'), + url = require('url'); + +function proxy(req, res, next) { + assert.strictEqual(typeof req.params.id, 'string'); + + const appId = req.params.id; + + docker.inspect('sftp', function (error, result) { + if (error)return next(BoxError.toHttpError(error)); + + const ip = safe.query(result, 'NetworkSettings.Networks.cloudron.IPAddress', null); + if (!ip) return next(new BoxError(BoxError.INACTIVE, 'Error getting IP of sftp service')); + + req.url = req.originalUrl.replace(`/api/v1/apps/${appId}/files`, `/files/${appId}`); + + const proxyOptions = url.parse(`https://${ip}:3000`); + proxyOptions.rejectUnauthorized = false; + const fileManagerProxy = middleware.proxy(proxyOptions); + + fileManagerProxy(req, res, function (error) { + if (!error) return next(); + + if (error.code === 'ECONNREFUSED') return next(new HttpError(424, 'Unable to connect to filemanager server')); + if (error.code === 'ECONNRESET') return next(new HttpError(424, 'Unable to query filemanager server')); + + next(new HttpError(500, error)); + }); + }); +}