2020-04-03 15:07:12 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const isValidDomain = require('is-valid-domain');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
async uploadProxy(ctx) {
|
|
|
|
try {
|
|
|
|
const url = new URL(ctx.query.url);
|
|
|
|
|
|
|
|
if (!['http:', 'https:'].includes(url.protocol)) {
|
2020-05-25 10:09:19 +02:00
|
|
|
throw new Error('Unexpected url protocol');
|
2020-04-03 15:07:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidDomain(url.hostname)) {
|
2020-05-25 10:09:19 +02:00
|
|
|
throw new Error('Invalid url hostname');
|
2020-04-03 15:07:12 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = 'Invalid URL';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(ctx.query.url, {
|
|
|
|
headers: _.omit(ctx.request.headers, ['origin', 'host', 'authorization']),
|
|
|
|
});
|
|
|
|
|
2020-05-25 16:05:12 +02:00
|
|
|
for (const [key, value] of res.headers.entries()) {
|
2020-04-03 15:07:12 +02:00
|
|
|
ctx.set(key, value);
|
2020-05-25 16:05:12 +02:00
|
|
|
}
|
2020-04-03 15:07:12 +02:00
|
|
|
|
|
|
|
ctx.status = res.status;
|
|
|
|
ctx.body = res.body;
|
|
|
|
} catch (err) {
|
|
|
|
strapi.log.error(err);
|
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = 'Internal Server Error';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|