-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.ts
More file actions
33 lines (31 loc) · 1.02 KB
/
server.ts
File metadata and controls
33 lines (31 loc) · 1.02 KB
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
32
33
import { serve } from "https://deno.land/std@0.74.0/http/server.ts";
import { isUrlAllowed } from "./helpers/allowed-urls-helper.ts";
export async function run(
port: number,
route: string,
allowedUrls: string,
allowedOrigins: string,
) {
const server = serve({ port });
console.log(`CORS proxy server listening at port ${port}.`);
for await (const req of server) {
try {
if (req.url.startsWith(route)) {
const url = req.url.slice(route.length);
if (!isUrlAllowed(url, allowedUrls)) {
req.respond({ status: 403, body: "403 Forbidden" });
continue;
}
const response = await fetch(url);
const text = await response.text();
const headers = new Headers();
headers.set("Access-Control-Allow-Origin", allowedOrigins);
req.respond({ body: text, headers });
} else {
req.respond({ status: 404, body: "404 Not Found" });
}
} catch {
req.respond({ status: 500, body: "500 Internal Server Error" });
}
}
}