-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
151 lines (118 loc) · 3.67 KB
/
Copy pathserver.js
File metadata and controls
151 lines (118 loc) · 3.67 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
const http = require("http");
const path = require("path");
const express = require("express");
const { getRandomPhoneticKey, loadLimiter, createLimiter } = require("./utils/utils.js");
const { createPaste, getPaste, Paste } = require("./utils/paste-utils.js");
const config = require("./config.json");
if (config.password === "") {
console.error("Password must be not empty or 'null'.");
process.exit(1);
}
if (config.master_password === "") {
console.error("Master password must be not empty or 'null'.");
process.exit(1);
}
const filenameRegex = new RegExp(config.paste.filename_regex);
const app = express();
const server = http.createServer(app);
app.set("view engine", "ejs");
app.use(express.json({ limit: "10mb" }));
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
app.use("/public", express.static(path.join(__dirname, "./public")));
app.get("/", (req, res) => {
res.render("create", {
max_length: config.paste.max_length,
});
});
app.post("/save", createLimiter, async (req, res) => {
const { filename, content, dpassword, password } = req.body;
if (!content) {
res.status(400).json({
error: "No content provided.",
});
return;
}
if (content.length > config.paste.max_length) {
res.status(413).json({
error: "Content is too long.",
});
return;
}
if (password !== config.password && password !== config.master_password) {
res.status(403).json({
error: "Invalid password.",
});
return;
}
let pastename = getRandomPhoneticKey(config.paste.filename_length);
if (filename && filename !== "") {
if (config.master_password && password !== config.master_password) {
res.status(400).json({
error: "Master password is required for custom filenames.",
});
return;
}
if (!filenameRegex.test(filename)) {
res.status(400).json({
error: `Invalid filename. Filename must match the regular expression:\n${config.paste.filename_regex}`,
});
return;
}
pastename = filename;
}
const existing = await getPaste(pastename);
if (existing) {
res.status(409).json({
error: "Paste with that name already exists.",
});
return;
}
const paste = new Paste(pastename, content, dpassword);
await createPaste(paste);
res.status(201).json({
filename: paste.filename,
});
});
app.get("/:filename", loadLimiter, async (req, res) => {
sendDocument(req, res, false);
});
app.get("/raw/:filename", loadLimiter, async (req, res) => {
sendDocument(req, res, true);
});
const sendDocument = async (req, res, raw) => {
const { filename } = req.params;
const { password } = req.query;
let syntax = "";
let name = filename;
if (filename.includes(".")) {
[name, syntax] = filename.split(".");
}
console.log(`Requested paste ${filename}.`);
const paste = await getPaste(name);
if (!paste) {
res.redirect("/");
return;
}
if (paste.password && paste.password !== password) {
res.render("password", {
filename: paste.filename,
raw: raw,
});
return;
}
if (raw) {
res.set({
"Content-Type": "text/plain",
});
res.send(paste.content);
} else {
res.render("view", {
filename: paste.filename,
content: paste.content,
syntax: syntax,
});
}
};
server.listen(config.port, () => {
console.log(`Listening on port ${config.port}.`);
});