allow http proxied requests
This commit is contained in:
parent
520b04a44d
commit
1f58d4be23
1 changed files with 52 additions and 16 deletions
|
@ -1,4 +1,5 @@
|
|||
import https from "https";
|
||||
import http from "http";
|
||||
|
||||
function httpsRequest(params) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
@ -22,6 +23,28 @@ function httpsRequest(params) {
|
|||
});
|
||||
}
|
||||
|
||||
function httpRequest(params) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var request = http.request(params, function (response) {
|
||||
let data = "";
|
||||
|
||||
response.on("data", (chunk) => {
|
||||
data = data + chunk.toString();
|
||||
});
|
||||
|
||||
response.on("end", () => {
|
||||
resolve([response.statusCode, data]);
|
||||
});
|
||||
});
|
||||
|
||||
request.on("error", (error) => {
|
||||
reject([500, error]);
|
||||
});
|
||||
|
||||
request.end();
|
||||
});
|
||||
}
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const headers = ["X-API-Key", "Content-Type", "Authorization"].reduce((obj, key) => {
|
||||
if (req.headers && req.headers.hasOwnProperty(key.toLowerCase())) {
|
||||
|
@ -30,23 +53,36 @@ export default async function handler(req, res) {
|
|||
return obj;
|
||||
}, {});
|
||||
|
||||
// this agent allows us to bypass the certificate check
|
||||
// which is required for most self-signed certificates
|
||||
const httpsAgent = new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
|
||||
const url = new URL(req.query.url);
|
||||
|
||||
const [status, data] = await httpsRequest({
|
||||
hostname: url.hostname,
|
||||
path: url.pathname,
|
||||
port: url.port,
|
||||
agent: httpsAgent,
|
||||
method: req.method,
|
||||
headers: headers,
|
||||
body: req.method == "GET" || req.method == "HEAD" ? null : req.body,
|
||||
});
|
||||
if (url.protocol === "https:") {
|
||||
// this agent allows us to bypass the certificate check
|
||||
// which is required for most self-signed certificates
|
||||
const httpsAgent = new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
|
||||
res.status(status).send(data);
|
||||
const [status, data] = await httpsRequest({
|
||||
hostname: url.hostname,
|
||||
path: url.pathname,
|
||||
port: url.port,
|
||||
agent: httpsAgent,
|
||||
method: req.method,
|
||||
headers: headers,
|
||||
body: req.method == "GET" || req.method == "HEAD" ? null : req.body,
|
||||
});
|
||||
|
||||
return res.status(status).send(data);
|
||||
} else {
|
||||
const [status, data] = await httpRequest({
|
||||
hostname: url.hostname,
|
||||
path: url.pathname,
|
||||
port: url.port,
|
||||
method: req.method,
|
||||
headers: headers,
|
||||
body: req.method == "GET" || req.method == "HEAD" ? null : req.body,
|
||||
});
|
||||
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue