-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (144 loc) · 4.36 KB
/
server.js
File metadata and controls
172 lines (144 loc) · 4.36 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
160
161
162
163
164
165
166
167
168
169
170
171
172
const path = require("path");
const { createServer } = require("http");
const { parse } = require("url");
const fs = require("fs");
const { WebSocketServer } = require("ws");
const crypto = require("crypto");
const dir = path.join(__dirname);
const nextDir = path.join(dir, ".next");
process.env.NODE_ENV = "production";
process.chdir(dir);
const currentPort = parseInt(process.env.PORT, 10) || 3000;
const hostname = process.env.HOSTNAME || "0.0.0.0";
let keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
if (
Number.isNaN(keepAliveTimeout) ||
!Number.isFinite(keepAliveTimeout) ||
keepAliveTimeout < 0
) {
keepAliveTimeout = undefined;
}
const sessionsFilePath = path.join(dir, "data", "users", "sessions.json");
function readSessions() {
try {
const content = fs.readFileSync(sessionsFilePath, "utf-8");
return JSON.parse(content) || {};
} catch {
return {};
}
}
function parseCookies(cookieHeader) {
const cookies = {};
if (!cookieHeader) return cookies;
cookieHeader.split(";").forEach((pair) => {
const idx = pair.indexOf("=");
if (idx < 0) return;
const key = pair.substring(0, idx).trim();
const val = pair.substring(idx + 1).trim();
cookies[key] = val;
});
return cookies;
}
function authenticateWs(req) {
const cookies = parseCookies(req.headers.cookie);
const isHttps = process.env.HTTPS === "true";
const sessionId = isHttps
? cookies["__Host-session"]
: cookies["session"];
if (!sessionId) return null;
const sessions = readSessions();
const username = sessions[sessionId];
return username || null;
}
const connectedClients = new Map();
const wss = new WebSocketServer({ noServer: true });
wss.on("connection", (ws, req) => {
const connectionId = crypto.randomUUID();
const username = req._wsUsername;
connectedClients.set(ws, { connectionId, username });
ws.send(JSON.stringify({ type: "connected", connectionId }));
ws.isAlive = true;
ws.on("pong", () => {
ws.isAlive = true;
});
ws.on("close", () => {
connectedClients.delete(ws);
});
});
const heartbeat = setInterval(() => {
wss.clients.forEach((ws) => {
if (!ws.isAlive) {
connectedClients.delete(ws);
ws.terminate();
return;
}
ws.isAlive = false;
ws.ping();
});
}, 30000);
setInterval(() => {
for (const [ws] of connectedClients) {
if (ws.readyState >= 2) connectedClients.delete(ws);
}
}, 60000);
wss.on("close", () => {
clearInterval(heartbeat);
});
globalThis.__jottyBroadcast = (event) => {
const payload = JSON.stringify(event);
wss.clients.forEach((client) => {
if (client.readyState === 1) {
client.send(payload);
}
});
};
globalThis.__jottyHasConnectedClients = () => connectedClients.size > 0;
const nextConfigStr = fs.readFileSync(
path.join(nextDir, "required-server-files.json"),
"utf-8"
);
const { config: nextConfig } = JSON.parse(nextConfigStr);
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig);
const next = require("next");
const app = next({ dev: false, dir, hostname, port: currentPort, conf: nextConfig });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer(async (req, res) => {
const parsedUrl = parse(req.url, true);
await handle(req, res, parsedUrl);
});
server.on("upgrade", (req, socket, head) => {
const { pathname } = parse(req.url);
if (pathname === "/_ws") {
const username = authenticateWs(req);
if (!username) {
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
socket.destroy();
return;
}
req._wsUsername = username;
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req);
});
} else {
socket.destroy();
}
});
if (keepAliveTimeout) {
server.keepAliveTimeout = keepAliveTimeout;
}
server.listen(currentPort, hostname, () => {
console.log(`
jjjj . .
jjj .tt .tt
jjj .ooooo. .tttttt .ttttt yyyy yyy
jjj ooo' 'ooo ttt ttt 'yy. .y'
jjj ooo ooo ttt ttt 'yy..y'
jjj ooo ooo ttt . ttt . 'yyY'
.J. jjj 'OoooooO' 'ttt' 'ttt' 'y'
'JJJJJ 'y..y'
'YyY'
`);
console.log(`> Ready on http://${hostname}:${currentPort}`);
});
});