inital commit

This commit is contained in:
Lee
2023-11-14 17:56:44 +00:00
commit 161486bf49
38 changed files with 5094 additions and 0 deletions

34
apps/node/src/index.ts Normal file
View File

@ -0,0 +1,34 @@
import dotenv from "dotenv";
import { RouteManager, RouteMessages, createServer } from "server";
import { checkEnvironmentVariables } from "utils";
import ProxyRoute from "./routes/proxy";
import { initSecrets } from "./secrets";
dotenv.config(); // load .env file
// Check environment variables
const envVarsValid = checkEnvironmentVariables("API_PORT", "INFISICAL_TOKEN");
if (!envVarsValid) {
process.exit(1);
}
const server = createServer({
port: process.env.API_PORT || 3000,
onLoaded: async () => {
await initSecrets(process.env.INFISICAL_TOKEN!); // Load the infisical secrets
const routeManager = new RouteManager();
routeManager.addRoute(new ProxyRoute());
server.all("*", (req, res) => {
const route = routeManager.getRoute(req.path);
if (route) {
route.handle(req, res);
return;
}
// Handle unknown routes
res.json(RouteMessages.unknownRoute());
});
},
});

View File

@ -0,0 +1,55 @@
import axios from "axios";
import { Request, Response } from "express";
import { Route, RouteMessages } from "server";
import { PROXY_SECRET } from "../secrets";
export default class ProxyRoute extends Route {
constructor() {
super({ path: "/proxy" });
}
async handle(req: Request, res: Response) {
const json = req.body;
const secret = json.secret;
if (!secret) {
res.status(401).json(RouteMessages.badRequest("No secret provided"));
return;
}
if (secret !== PROXY_SECRET) {
res.status(401).json(RouteMessages.badRequest("Invalid secret"));
return;
}
const url = json.url;
if (!url) {
res.status(400).json(RouteMessages.badRequest("No URL provided"));
return;
}
// TODO: handle rate limiting? and/or caching?
const response = await axios.get(url, {
headers: {
"Content-Type": "application/json",
},
});
const data = response.data;
const headers = response.headers;
// Is delete the best way to do this??
// Remove CORS headers
delete headers["access-control-allow-origin"];
delete headers["access-control-allow-credentials"];
delete headers["access-control-allow-headers"];
delete headers["access-control-allow-methods"];
// Cloudflare headers
delete headers["server"];
delete headers["nel"];
delete headers["report-to"];
delete headers["cf-cache-status"];
delete headers["cf-ray"];
delete headers["alt-svc"];
// Return the JSON response
res.status(response.status).set(headers).json(data);
}
}

20
apps/node/src/secrets.ts Normal file
View File

@ -0,0 +1,20 @@
import { createInfisicalClient } from "utils";
export let PROXY_SECRET: string;
/**
* Initialize the secrets from Infisical
*/
export async function initSecrets(token: string) {
console.log("Initializing secrets...");
const infisicalClient = createInfisicalClient(token);
const proxySecret = (await infisicalClient.getSecret("PROXY_SECRET"))
.secretValue;
if (!proxySecret) {
throw new Error("PROXY_SECRET not set in Infisical");
}
PROXY_SECRET = proxySecret;
}