-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
159 lines (132 loc) · 5.03 KB
/
proxy.ts
File metadata and controls
159 lines (132 loc) · 5.03 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import https from "https";
import httpProxy from "http-proxy";
import { IncomingMessage, ServerResponse } from "http";
import { Socket } from "net";
import cron from "node-cron";
import {
Logger,
I18nService,
backupService
} from "kage-library";
import { config } from "./app.config.js";
import getEnv from "./src/_common/helpers/getEnv.js";
import terminateApp from "./src/_common/helpers/terminateApp.js";
const log = new Logger({
path: "/logs",
useNerdFonts: config.useNerdFonts
});
/*
————————————————————————————————————————————————————————————————
Setup server
————————————————————————————————————————————————————————————————
*/
const proxy = httpProxy.createProxyServer({
ws: true,
secure: config.isProduction
});
// Map domains to ports
const serverMap: Record<string, string> = {};
for (const [key, domain] of Object.entries(config.domains)) {
const port = config.ports[key as keyof typeof config.ports];
if (!domain) {
log.proxy.warn(`Missing domain for "${key}" key in app.config.ts`).save();
continue;
}
if (!port) {
log.proxy.warn(`Missing port for "${key}" key in app.config.ts`).save();
continue;
}
serverMap[domain.toLowerCase()] = `https://localhost:${port}`;
}
// HTTPS server
const local = https.createServer(
getEnv("SSL"),
async (req: IncomingMessage, res: ServerResponse) => {
if (req.url!.startsWith("/favicon.ico")) {
res.writeHead(302, {
Location: `https://${config.domains.cdn}${config.metadata.assets.icon}`
});
return res.end();
}
const hostname = req.headers.host?.split(":")[0].toLowerCase();
const target = hostname ? serverMap[hostname] : undefined;
if (!target) {
log.proxy.warn(`Host '${hostname}' is not mapped to a server`).save();
const i18n = await I18nService.load(
{
localesPath: "/public/locales",
locale: "en",
defaultLocale: config.metadata.locale
}
);
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.writeHead(502);
return res.end(i18n.t("messages.badGateway"));
}
proxy.web(
req,
res,
{
target,
secure: config.isProduction,
changeOrigin: false,
headers: {
host: hostname as string
}
},
async (error: Error) => {
log.proxy.error(`Error for '${hostname}':`, error).save();
const i18n = await I18nService.load(
{
localesPath: "/public/locales",
locale: "en",
defaultLocale: config.metadata.locale
}
);
res.writeHead(502);
res.end(i18n.t("messages.badGateway"));
}
);
}
);
// WebSocket handling
local.on(
"upgrade",
(req: IncomingMessage, socket: Socket, head: Buffer) => {
const hostname = req.headers.host?.split(":")[0].toLowerCase();
const target = hostname ? serverMap[hostname] : undefined;
if (!target) {
socket.destroy();
return;
}
proxy.ws(req, socket, head, {
target,
secure: config.isProduction,
headers: {
host: hostname as string
}
});
}
);
/*
————————————————————————————————————————————————————————————————
Start server
————————————————————————————————————————————————————————————————
*/
const port = 443
local.listen(port, () => {
log.proxy.info(`Proxy online at https://localhost:${port}`);
});
process.once("SIGTERM", () => terminateApp(log)); // Host
process.once("SIGINT", () => terminateApp(log)); // Ctrl+C
/*
————————————————————————————————————————————————————————————————
Scheduled events
————————————————————————————————————————————————————————————————
*/
// Run everyday at midnight
cron.schedule("0 0 * * *", () => {
log.cron.info("Running daily tasks...");
log.cleanLogs();
backupService(config.folders.data, config.folders.backups);
});