-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
293 lines (257 loc) · 7.86 KB
/
index.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
// Imports (native)
const http = require("http")
const fs = require("fs");
const path = require("path");
// Imports (external)
const formidable = require("formidable");
const mime = require("mime-types");
const open = require("open");
const arg = require("arg");
const qr = require("qrcode");
const mv = require("mv");
// Helper funcs
const say = (msg, v = 1) => {
if (v >= verbosity) console.log(msg);
};
const bool = () => true;
const ip = () => {
const { networkInterfaces } = require("os");
const networks = networkInterfaces();
const res = {};
for (const name of Object.keys(networks)) {
for (const net of networks[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family == 4 && !net.internal) {
if (!res[name]) res[name] = [];
res[name].push(net.address);
}
}
}
let best = null;
for (const name of Object.keys(res)) best = best || res[name];
return best;
};
// Constants (args)
const args = arg({
// long
"--host": String,
"--port": Number,
"--quiet": arg.flag(bool),
"--silent": arg.flag(bool),
"--no-wipe": arg.flag(bool),
"--no-open": arg.flag(bool),
// short
"-p": "--port",
"-h": "--host",
"-q": "--quiet",
"-s": "--silent",
"-w": "--no-wipe",
"-o": "--no-open"
});
while (args._.length < 2) args._.push(null);
const host =
process.env.PASTEBIN_HOST ||
args["--host"] ||
args._[0] ||
ip() ||
"localhost";
const port = process.env.PASTEBIN_PORT ||
args["--port"] ||
args._[1] ||
8080;
const quiet = args["--quiet"] || false;
const silent = args["--silent"] || false;
const verbosity = silent ? 2 : quiet ? 1 : 0;
let wipe = !args["--no-wipe"];
let auto = !args["--no-open"];
if (wipe === null) wipe = true;
if (auto === null) auto = true;
// If uploads/ directory doesn't exist, create it
// Otherwise, wipe the directory
if (!fs.existsSync("./uploads")) fs.mkdirSync("./uploads");
else if (wipe) {
say("For security reasons, wiping uploads/ directory...");
fs.readdirSync("./uploads").forEach(file =>
fs.unlinkSync(`./uploads/${file}`)
);
}
// Start the server
http
.createServer((req, res) => {
say(`${req.method} ${req.url}`, 0);
// If request url is not '/',
// host the file
if (req.url == "/fileupload") {
const form = new formidable.IncomingForm();
form.parse(req, (_err, _fields, files) => {
const oldpath = files.filetoupload.filepath;
const newpath = "./uploads/" + files.filetoupload.originalFilename;
mv(oldpath, newpath, err => {
if (err) throw err;
// Generate QR code
const fullURL =
"http://" +
host +
":" +
port +
"/uploads/" +
files.filetoupload.originalFilename;
const shortURL =
host +
":" +
port +
"/uploads/" +
files.filetoupload.originalFilename;
qr.toFile("./qrcode.png", fullURL);
const html =
"<img src='/qrcode.png'/><br>" +
"<a href='/uploads/" +
`${files.filetoupload.originalFilename}'>` +
`${shortURL}</a><br>` +
"<p>File uploaded and moved!</p>" +
"<a href='/'>Back to home</a>";
// Read upload.html
fs.readFile("./templates/upload.html", "utf8", (err, data) => {
if (err) throw err;
const content = data.replace("%CONTENT%", html);
res.write(content);
res.end();
});
});
});
} else if (req.url == "/paste") {
const form = new formidable.IncomingForm();
form.parse(req, (_err, fields, _files) => {
const bin = fields.bin;
const filename = "uploads/_bin.txt";
fs.writeFile(filename, bin, err => {
if (err) throw err;
});
// Generate QR code
const fullURL = "http://" + host + ":" + port + "/pasted";
qr.toFile("./qrcode.png", fullURL);
const html =
"<img src='/qrcode.png'/><br>" +
"<a href='/pasted'>" +
"View upload</a><br>" +
"<p>Content pasted and ready!</p>" +
"<a href='/'>Back to home</a>";
// Read upload.html
fs.readFile("./templates/upload.html", "utf8", (err, data) => {
if (err) throw err;
const content = data.replace("%CONTENT%", html);
res.write(content);
res.end();
});
});
} else if (req.url == "/download") {
// If request url is '/download',
// display links to download files
let html = "<ul>";
fs.readdir("./uploads", (err, files) => {
if (err) throw err;
files.forEach(file => {
html +=
`<li><a href="/uploads/${file}" download>${file}` + "</a><br></li>";
});
});
html += "</ul>";
// Read download.html file
fs.readFile("./templates/download.html", "utf8", (err, data) => {
if (err) throw err;
let content = data;
content = content.replace("%CONTENT%", html);
res.write(content);
res.end();
});
} else if (req.url == "/pasted") {
// If request url is '/pasted',
// display the pasted content
let html = "";
fs.readFile("./uploads/_bin.txt", "utf8", (err, data) => {
if (err) throw err;
html += data;
});
// Read pasted.html file
fs.readFile("./templates/pasted.html", "utf8", (err, data) => {
if (err) throw err;
let content = data;
content = content.replace("%CONTENT%", html);
res.write(content);
res.end();
});
} else if (req.url != "/") {
// parse URL
const parsedURL = new URL(req.url, "http://" + host + ":" + port);
// extract URL path
const pathName = `.${parsedURL.pathname}`;
// based on the URL path, extract the file extension.
// e.g. .js, .doc, ...
const ext = path.parse(pathName).ext;
if (fs.existsSync(pathName)) {
// Read file from system and prompt user to download the file
fs.readFile(pathName, (err, data) => {
if (err) {
res.statusCode = 500;
res.end(
`Error retrieving the file: ${err}.` +
`(Error: ${res.statusCode})`
);
} else {
// If the file is found,
// set Content-type and send data
res.setHeader("Content-type", mime.lookup(ext) || "text/plain");
res.end(data);
}
});
} else {
// if the file is not found, return 404
res.statusCode = 404;
res.end(`File ${pathName} not found! ` + `(Error: ${res.statusCode})`);
}
} else {
res.writeHead(200, { "Content-Type": "text/html" });
// Read template.html file synchronously
fs.readFile("./templates/home.html", "utf8", (err, data) => {
if (err) throw err;
const content = data.replace(
"%CONTENT%",
`
<form class="paste" action="paste" method="post" >
<textarea id="bin" name="bin"
placeholder="Paste here with Ctrl+V">
</textarea>
<input id="go" type="submit" value="Go">
</form>
<br><br>
<p id="sep">
or
</p>
<div>
<form class="file" action="fileupload" method="post"
enctype="multipart/form-data">
<div class="input">
<input id="file" type="file" name="filetoupload">
</div>
<span id="label" onclick="showInfo()">
Click to upload
</span>
<br>
<input type="submit">
</form>
</div>
`
);
res.write(content);
res.end();
});
}
})
.listen(parseInt(port), host);
say(`Server listening on port http://${host}:${port}`);
if (auto) open(`http://${host}:${port}`);
// End the server
process.on("SIGINT", () => {
say("\nCaught interrupt signal; aborting...", 0);
process.exit();
});