-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
102 lines (84 loc) · 3.81 KB
/
Copy pathserver.ts
File metadata and controls
102 lines (84 loc) · 3.81 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import http from "node:http";
import path from "node:path";
import fs from "node:fs";
import mime from "mime-types";
import process from "node:process";
import load_downloads, { html as d2html } from "./downloads.ts";
const base_dir = import.meta.dirname;
if (base_dir === undefined) throw new Error("dirname is undefined");
process.on("uncaughtException", console.log);
process.on("unhandledRejection", console.log);
const downloads = load_downloads();
const server = http.createServer(async (req, res) => {
if (req.url == undefined) req.url = "/";
let req_url;
try {
req_url = new URL(req.url, "http://localhost");
} catch {
res.writeHead(302, "FOUND", {
location: "/"
});
res.end();
return;
}
if (req_url.pathname == "/") req_url.pathname = "/index.html";
const req_path = path.join(base_dir, req_url.pathname);
const relative_path = path.relative(base_dir, req_path);
if (relative_path.startsWith("..")) { res.writeHead(404); res.end(); return; }
const special = ["/d/", "/d", "/downloads/", "/downloads"];
if ((!fs.existsSync(req_path) ||
!(fs.statSync(req_path).isFile())) && !special.includes(req_url.pathname)) { res.writeHead(404); res.end(); return; }
if (req_url.pathname == "/index.html") {
let html = fs.readFileSync(req_path, "utf-8");
// inline css
html = html.replace(/<link(?:(?:.*rel="stylesheet".*href="([^"]+?)".*)|(?:.*href="([^"]+?)".*rel="stylesheet".*))>/g, (_link_tag, filename) => {
return `<style>\n${fs.readFileSync(path.join(base_dir, filename), "utf-8")}\n</style>`;
});
// inline js
html = html.replace(/<script (?:.*src="([^"]+?)".*?)><\/script>/g, (_script_tag, filename) => {
return `<script>\n${fs.readFileSync(path.join(base_dir, filename), "utf-8")}\n</script>`;
});
// inline profile picture
html = html.replace(`src="images/FurriousFox.webp"`, `src="data:image/webp;base64,${fs.readFileSync(path.join(base_dir, "images/FurriousFox.webp"), "base64")}"`);
// calculate age
let age = "";
{
const birthday = new Date(1193612400000);
const now = new Date();
let years = now.getUTCFullYear() - birthday.getUTCFullYear();
birthday.setUTCFullYear(now.getUTCFullYear());
if (birthday <= now) { birthday.setUTCFullYear(now.getUTCFullYear() + 1); years++; }
const birthday_year = birthday.getUTCFullYear();
years -= ((+birthday) - (+now)) / (3600000 * 24 * (((birthday_year % 4 === 0 && birthday_year % 100 !== 0) || (birthday_year % 400 === 0)) ? 366 : 365));
age = Math.floor(years).toString();
}
html = html.replace("{{age}}", age);
res.writeHead(200, "OK", {
"content-type": `${mime.lookup(req_path) || 'application/octet-stream'}`,
"content-length": html.length,
});
res.end(html);
return;
} else if (req_url.pathname == "/d" || req_url.pathname == "/d/") {
res.writeHead(302, "FOUND", {
location: "/downloads",
"content-type": "text/html",
});
res.end(`<meta http-equiv="refresh" content="0;URL='/downloads'" /><a href="/downloads">found</a>`);
return;
} else if (req_url.pathname == "/downloads" || req_url.pathname == "/downloads/") {
res.writeHead(200, "OK", {
"content-type": "text/html"
});
res.end(d2html(await downloads));
return;
} else {
res.writeHead(200, "OK", {
"content-type": `${mime.lookup(req_path) || 'application/octet-stream'}`,
"content-length": fs.statSync(req_path).size,
});
fs.createReadStream(req_path).pipe(res);
return;
}
});
server.listen(5113, "127.0.0.1");