summaryrefslogtreecommitdiff
path: root/src/web/service/proxy/proxy.ts
blob: 48c9fa4a53ef59d4a188d3172a7bd48bbc0759fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import * as URL from 'url';
import * as express from 'express';
import * as request from 'request';
import * as isUrl from 'is-url';

module.exports = (req: express.Request, res: express.Response) => {
	const url = req.params.url;

	if (!url || !isUrl(url)) {
		return;
	}

	request({
		url: url + URL.parse(req.url, true).search,
		encoding: null
	}, (err, response, content) => {
		if (err) {
			console.error(err);
			return;
		}

		const contentType = response.headers['content-type'];

		if (/^text\//.test(contentType) || contentType === 'application/javascript') {
			content = content.toString().replace(/http:\/\//g, `${config.secondary_scheme}://proxy.${config.secondary_host}/http://`);
		}

		res.header('Content-Type', contentType);
		res.send(content);
	});
};