2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let ProxyManager
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-11-01 12:30:59 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2019-05-29 05:21:06 -04:00
|
|
|
const request = require('request')
|
2021-10-20 06:17:59 -04:00
|
|
|
const { URL, URLSearchParams } = require('url')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = ProxyManager = {
|
|
|
|
apply(publicApiRouter) {
|
|
|
|
return (() => {
|
|
|
|
const result = []
|
2021-10-26 04:08:56 -04:00
|
|
|
for (const proxyUrl in settings.proxyUrls) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const target = settings.proxyUrls[proxyUrl]
|
|
|
|
result.push(
|
2021-04-14 09:17:21 -04:00
|
|
|
(function (target) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const method =
|
|
|
|
(target.options != null ? target.options.method : undefined) ||
|
|
|
|
'get'
|
|
|
|
return publicApiRouter[method](
|
|
|
|
proxyUrl,
|
|
|
|
ProxyManager.createProxy(target)
|
|
|
|
)
|
|
|
|
})(target)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
})()
|
|
|
|
},
|
|
|
|
|
|
|
|
createProxy(target) {
|
2021-04-14 09:17:21 -04:00
|
|
|
return function (req, res, next) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const targetUrl = makeTargetUrl(target, req)
|
|
|
|
logger.log({ targetUrl, reqUrl: req.url }, 'proxying url')
|
|
|
|
|
|
|
|
const options = { url: targetUrl }
|
|
|
|
if (req.headers != null ? req.headers.cookie : undefined) {
|
|
|
|
options.headers = { Cookie: req.headers.cookie }
|
|
|
|
}
|
|
|
|
if ((target != null ? target.options : undefined) != null) {
|
|
|
|
Object.assign(options, target.options)
|
|
|
|
}
|
|
|
|
if (['post', 'put'].includes(options.method)) {
|
|
|
|
options.form = req.body
|
|
|
|
}
|
|
|
|
const upstream = request(options)
|
|
|
|
upstream.on('error', error =>
|
|
|
|
logger.error({ err: error }, 'error in ProxyManager')
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: better handling of status code
|
|
|
|
// see https://github.com/overleaf/write_latex/wiki/Streams-and-pipes-in-Node.js
|
|
|
|
return upstream.pipe(res)
|
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// make a URL from a proxy target.
|
|
|
|
// if the query is specified, set/replace the target's query with the given query
|
2021-10-26 04:08:56 -04:00
|
|
|
function makeTargetUrl(target, req) {
|
2021-10-20 06:17:59 -04:00
|
|
|
const targetUrl = new URL(parseSettingUrl(target, req))
|
2019-05-29 05:21:06 -04:00
|
|
|
if (req.query != null && Object.keys(req.query).length > 0) {
|
2021-10-20 06:17:59 -04:00
|
|
|
targetUrl.search = new URLSearchParams(req.query).toString()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-10-20 06:17:59 -04:00
|
|
|
return targetUrl.href
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-10-26 04:08:56 -04:00
|
|
|
function parseSettingUrl(target, { params }) {
|
2019-05-29 05:21:06 -04:00
|
|
|
let path
|
|
|
|
if (typeof target === 'string') {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
if (typeof target.path === 'function') {
|
|
|
|
path = target.path(params)
|
|
|
|
} else {
|
|
|
|
;({ path } = target)
|
|
|
|
}
|
|
|
|
return `${target.baseUrl}${path || ''}`
|
|
|
|
}
|