-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.ts
69 lines (55 loc) · 1.78 KB
/
app.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { serve } from "https://deno.land/std@0.173.0/http/server.ts";
import { serveDir } from "https://deno.land/std@0.173.0/http/file_server.ts";
import { getCookies } from "https://deno.land/std@0.173.0/http/cookie.ts";
import Proxy from "https://cdn.jsdelivr.net/gh/NebulaServices/aero-backends/proxy.ts";
const proxy = new Proxy("/fetch", "/fetchWs");
import "https://deno.land/std@0.173.0/dotenv/load.ts";
import config from "./config.json" assert { type: "json" };
import { prefix } from "https://cdn.jsdelivr.net/gh/NebulaServices/aero/config.js";
await Deno.run({
cmd: ["./deps.sh"],
stderr: "piped",
});
await serve(
async (req: Request): Promise<Response> => {
const { key } = getCookies(req.headers);
const url = new URL(req.url);
const unlocked = key === config.key;
const code = url.search.startsWith("?unlock");
const usingCrOS =
req.headers.get("user-agent")?.includes("CrOS") ?? false;
const allowed = !config.requireUnlock || unlocked || code || usingCrOS;
if (!allowed) {
const resp = await serveDir(req, {
fsRoot: config.blockedDir,
showIndex: true,
quiet: true,
});
// Only block html files
if (resp.headers.get("content-type")?.includes("text/html"))
return resp;
}
const path = url.pathname;
if (proxy.route(path)) return await proxy.handle(req);
else if (proxy.routeWs(path)) return await proxy.handleWs(req);
else {
if (path.startsWith(prefix))
return new Response("Failed to start the service worker", {
status: 404,
});
const resp = await serveDir(req, {
fsRoot: config.dir,
showDirListing: true,
showIndex: true,
quiet: true,
});
if (code)
resp.headers.set(
"set-cookie",
"key=unlock; SameSite=None; Secure"
);
return resp;
}
},
{ port: config.port }
);