-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
527 lines (461 loc) · 13.2 KB
/
server.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
log("Preparing...");
import express from "express";
import { Server } from "socket.io";
import { config } from "dotenv";
import { createServer } from "http";
config();
import { fileURLToPath } from "url";
import path from "path";
import { existsSync, mkdirSync, readFileSync, readdirSync } from "fs";
log("Successfully loaded necessary modules.");
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.resolve(path.dirname(__filename));
// Local imports to help set up the server
import * as Config from "./config.js";
import {
encodedLengths,
generateResponse,
getIp,
getSocketioIp,
log,
} from "./api.js";
// Create the directories to store logs and conversation history
if (!existsSync(__dirname + "/logs")) {
mkdirSync(__dirname + "/logs");
}
if (!existsSync(__dirname + "/convos")) {
mkdirSync(__dirname + "/convos");
}
function rateLimit(req) {
let ip = req;
if (typeof req !== "string") ip = getIp(req);
// Get the number of requests made by this IP address in the last hour
const numRequests = requestsMap.get(ip) || 0;
let plan = { limit: MAX_REQS, label: "free" };
if (PlansLookup.has(ip)) plan = PlansLookup.get(ip);
// If the number of requests is greater than the maximum allowed, return an error
if (numRequests >= plan.limit) {
return true;
}
// Increment the number of requests made by this IP address and store it in the Map
requestsMap.set(ip, numRequests + 1);
if (numRequests === 0) {
// Set the expiry time for this IP address's entry in the Map to 1 hour from now
const expiryTime = 60 * 60 * 1000;
const expiryTimeDate = Date.now() + expiryTime;
requestsMap.set(ip + "e", expiryTimeDate);
setTimeout(() => {
requestsMap.delete(ip);
}, expiryTime);
return false;
}
}
const app = express();
const server = createServer(app);
const io = new Server(server);
// Some server configuration
const MAX_REQS = Config.default.options.defaultRateLimit;
const requestsMap = new Map();
const prompts = Config.default.prompts.init();
const PlansLookup = new Map();
try {
let h = [];
h = Config.default.options.plans;
for (let index = 0; index < h.length; index++) {
let item = h[index];
if (item.ips && Array.isArray(item.ips)) {
for (let z = 0; z < item.ips.length; z++) {
PlansLookup.set(item.ips[z], {
limit: item.limit,
label: item.label,
});
}
} else {
PlansLookup.set(item.ip, {
limit: item.limit,
label: item.label,
});
}
}
} catch (e) {
log("[ERROR]", e);
}
// Global variables
globalThis.requestsMap = requestsMap;
globalThis.prompts = prompts;
globalThis.MAX_REQS = MAX_REQS;
globalThis.PlansLookup = PlansLookup;
globalThis.Config = Config;
globalThis.dirname = __dirname;
const finalApiPrompts = [];
prompts.forEach((p, k) => {
if (
p.type &&
p.type === "community" &&
Config.default.prompts.allowCommunity !== true
)
return;
finalApiPrompts.push({
label: p.label,
id: k,
greeting: p.greeting || "No greeting set.",
hint: p.description || "This is a custom chat bot prompt.",
type: p.type || "builtIn",
avatar: p.avatar || null,
displayName: p.displayName || null,
// Changed as of v0.5.3
prompt:
Config.default.options?.api &&
Config.default.options?.api?.exposePrompts &&
Config.default.options?.api?.exposePrompts === true
? p.prompt || null
: null,
greetingMessages: p.greetingMessages || null,
});
});
app.get("/api/prompts", (_req, res) => {
res.json(finalApiPrompts);
});
// Debug header testing, still leaving it in for debugging/testing purposes.
app.get("/hdr", (req, res) => {
res.json(req.headers);
});
app.use("/api", express.json({
limit: '5mb'
}));
app.get("/api/usage", (req, res) => {
const ip = getIp(req);
let plan = { limit: MAX_REQS, label: "free" };
if (PlansLookup.has(ip)) plan = PlansLookup.get(ip);
res.status(200).json({
used: requestsMap.get(ip),
remaining: plan.limit - requestsMap.get(ip),
total: plan.limit,
expires: new Date(requestsMap.get(ip + "e")).toJSON(),
plan: plan.label,
});
});
const ver = "v0.6.4";
const sub = "(2023-12-22)";
import { marked } from "marked";
import { gfmHeadingId } from "marked-gfm-heading-id";
marked.use(gfmHeadingId());
let changelogData = marked(
readFileSync(__dirname + "/CHANGELOG.md").toString(),
{
mangle: false,
gfm: true,
async: false,
}
);
const [major, minor] = ver.slice(1).split(".").map(Number);
const verString = `v${major}.${minor}`;
app.get("/api/version", (req, res) => {
// You can set any message or whatever if you make codebase changes
res.json({
version: ver,
substring: sub,
changelog: changelogData,
footerNote: `<p class="mt-0">ChatGPT-Chatify ${ver} ${sub}.<br><b>This instance uses the Dashboard.</b><br>See our <a target="_blank" href="/usage-terms">usage policy</a>.</p>`,
});
});
app.get("/usage-terms", (_req, res) => {
res.sendFile(__dirname + "/public/usage-terms.html");
});
// Entrypoint
app.use(Config.default.options.entryPoint, express.static("public/chatify"));
// Dashboard
const ipData = Config.default.options.dashboard.access.reduce((map, entry) => {
entry.ips.forEach((ip) => {
map[ip] = { allowed: entry.allowed, limit: entry.limit };
});
return map;
}, {});
const validateIP = (req, res, next) => {
const requestIP = getIp(req);
const data = ipData[requestIP];
if (data) {
req.data = data;
next();
} else {
res.status(403).send("Forbidden.");
}
};
// Serve /dash only to allowed IPs
app.use("/dash", validateIP, express.static("public/dashboard"));
app.post("/api/generate", (req, res) => {
if (rateLimit(getIp(req)) === true) {
return res.status(429).json({
error: true,
errorMessage: "Too Many Requests",
errorCode: "too_many_requests",
});
}
let result = "";
let onKill = null;
generateResponse(
JSON.stringify(req.body),
(m) => {
if (m.data !== undefined) {
result += m.data;
} else if (m.error && m.error === true) {
res.status(500).json(m);
} else if (m.done && m.done === true) {
res.json(Object.assign({}, m, { result }));
}
},
(m) => {
res.status(500).json({ error: true, errorMessage: m });
hasUserRequested = false;
},
(m) => {
onKill = m;
req.once("error", (_) => {
typeof onKill === "function" && onKill();
});
},
true,
getIp(req)
);
});
app.get("/api/dash/logs", validateIP, (req, res) => {
const { data } = req;
if (!data.allowed.includes("logHistory")) {
res.status(403).send("Forbidden");
return;
}
res.json(
readdirSync(__dirname + "/logs/").filter((e) =>
data.limit !== undefined ? e.includes(data.limit) : true
)
);
});
app.get("/api/dash/logs/:log", validateIP, (req, res) => {
const { data } = req;
if (data.limit && !req.params.log.includes(data.limit))
return res.status(403).send("No");
if (!data.allowed.includes("logHistory")) {
res.status(403).send("Forbidden");
return;
}
res.sendFile(__dirname + "/logs/" + req.params.log);
});
app.get("/api/dash/convos", validateIP, (req, res) => {
const { data } = req;
if (!data.allowed.includes("convoHistory")) {
res.status(403).send("Forbidden");
return;
}
res.json(
readdirSync(__dirname + "/convos/").filter((e) =>
data.limit !== undefined ? e.includes(data.limit) : true
)
);
});
app.get("/api/dash/convos/:convo", validateIP, (req, res) => {
const { data } = req;
if (data.limit && !req.params.convo.includes(data.limit))
return res.status(403).send("No");
if (!data.allowed.includes("convoHistory")) {
res.status(403).send("Forbidden");
return;
}
res.sendFile(__dirname + "/convos/" + req.params.convo);
});
// Middleware function to handle a 404 error
const handle404 = (req, res, next) => {
const err = new Error("Not Found");
err.status = 404;
next(err);
};
// Error handling middleware function
const errorHandler = (err, req, res, next) => {
if (err.status === 404) {
// Send custom 404 page here
res.status(404).sendFile(__dirname + "/public/404.html");
} else {
// Handle other errors here
res
.status(err.status || 500)
.send("Server error: " + err.message || "Internal Server Error");
}
};
function rateLimitSocket(sock) {
if (rateLimit(getSocketioIp(sock)) === true) {
setTimeout(() => {
sock.emit("rateLimitReached");
sock.disconnect();
}, 1000);
return true;
}
}
// This code was recently ported from "express-ws", so it may not be entirely optimized for Socket.IO.
io.on("connection", (sock) => {
let hasUserRequested = false;
sock.on("error", (err) => {
console.error("Socket error:", err);
});
sock.on("connect_error", (err) => {
console.error("Socket connection error:", err);
});
sock.on("connect_timeout", () => {
console.error("Socket connection timed out");
});
sock.on("begin", async (e) => {
if (rateLimitSocket(sock) === true) {
return;
}
if (hasUserRequested) return;
hasUserRequested = true;
generateResponse(
JSON.stringify(e),
(m) => {
if (m.data !== undefined) {
sock.emit("recv", m);
} else if (m.error && m.error === true) {
sock.emit("err", m);
} else if (m.done && m.done === true) {
sock.emit("done");
}
},
(m) => {
sock.emit("err", { error: true, errorMessage: m });
hasUserRequested = false;
},
(m) => {
try {
sock.on("error", m);
} catch (e) {
console.log("[ERR] Failed to stop request..\n", e);
}
},
true,
getSocketioIp(sock)
);
});
});
log("Starting server...");
function randomTime() {
return Math.floor(Math.random() * 15 + 20);
}
function sendPartialData(res, currentIndex, prewrittenMessage) {
const endIndex = Math.min(
currentIndex + Math.floor(Math.random() * (5 - 3 + 1) + 3),
prewrittenMessage.length
);
const partialMessage = JSON.stringify({
type: "inc",
data: prewrittenMessage.slice(currentIndex, endIndex),
});
res.write(`data: ${partialMessage}\n
`);
return endIndex;
}
import { inspect } from "util";
app.post("/api/stream", (req, res) => {
if (
req.body &&
req.body.userSettings &&
req.body.userSettings.testMode &&
req.body.userSettings?.testMode === true
) {
1;
// Fake a server-side message instead of actually calling the AI
// (for event streaming testing)
let prewrittenMessage = `Hello there, this is a pre-written message to test event streaming.
Here's a trimmed summary of your request body:
\`\`\`js
${inspect(req.body, undefined, 1)}
\`\`\`
# Markdown testing
*One*, **two**, ***three***! Hey, check [this](https://google.com) out!
## Markdown again
\`\`\`
Long code block testing
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
\`\`\`
### This concludes our markdown test
:)
`;
let currentIndex = 0;
const intervalId = setInterval(() => {
currentIndex = sendPartialData(res, currentIndex, prewrittenMessage);
if (currentIndex === prewrittenMessage.length) {
clearInterval(intervalId);
res.write(`data: {"type":"done"}\n
`);
res.end();
}
}, randomTime());
return;
}
if (rateLimit(getIp(req)) === true) {
return res.status(429).json({
error: true,
errorMessage: "Too Many Requests",
errorCode: "too_many_requests",
});
}
res.set({
"Cache-Control": "no-cache",
"Content-Type": "text/event-stream",
Connection: "keep-alive",
});
let onKill = null;
generateResponse(
JSON.stringify(req.body),
(m) => {
if (m.data !== undefined) {
// result += m.data;
const partialMessage = JSON.stringify({ type: "inc", data: m.data });
res.write(`data: ${partialMessage}\n
`);
} else if (m.error && m.error === true) {
res.write(`data: {"type":"error","data":"${m.errorMessage.replace(
/"/g,
'"'
)}"}\n
`);
} else if (m.done && m.done === true) {
res.write(`data: {"type":"done"}\n
`);
res.end();
}
},
(m) => {
res.status(500).json({ error: true, errorMessage: m });
},
(m) => {
onKill = m;
req.once("error", (_) => {
typeof onKill === "function" && onKill();
});
},
true,
getIp(req)
);
});
// Middleware goes at the bottom, below all the actual routes
app.use(handle404);
app.use(errorHandler);
server.listen(
Config.default.options.server.port,
Config.default.options.server.host,
(_) => {
log(
`Listening on http://${Config.default.options.server.host}:${Config.default.options.server.port} !`
);
}
);