Proxy/index.js

57 lines
1.5 KiB
JavaScript

const express = require('express');
const axios = require('axios');
const allowedDomains = [
"roblox.com"
]
const app = express();
const rateLimit = require("express-rate-limit");
app.enable("trust proxy");
app.use(rateLimit({
windowMs: 20000,
max: 10,
keyGenerator: (req) => req.headers["x-forwarded-for"] || req.connection.remoteAddress,
}));
app.get("/:domain/*", (req, res) => {
// get the domain and the rest of the path+args
const domain = req.params.domain;
const path = req.params[0];
const args = req.query;
// If domain has any characters that arent alphanumeric, a period, or -, return 400
if (!/^[a-zA-Z0-9.-]+$/.test(domain)) {
return res.status(400).send("Invalid domain");
}
// Check if domain is valid subdomain of allowedDomains
if (!allowedDomains.some((allowedDomain) => domain.endsWith(allowedDomain))) {
return res.status(400).send("Invalid domain");
}
// Make the request
axios.get(`https://${domain}/${path}`, {
params: args
}).then((response) => {
// Send the response back
res.status(response.status).send(response.data);
}).catch((error) => {
// Send the error back
if (!error.response) {
return res.status(500).send("An error occurred");
}
res.status(error.response.status).send(error.response.data);
});
});
app.get("/:domain", (req, res) => {
// redirect them to /:domain/
res.redirect(`/${req.params.domain}/`);
})
const port = process.env.SERVER_PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});