-
Notifications
You must be signed in to change notification settings - Fork 0
/
uplite.js
executable file
·274 lines (242 loc) · 9.12 KB
/
uplite.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
#!/usr/bin/env node
const express = require("express");
const multer = require("multer");
const basicAuth = require("basic-auth");
const path = require("path");
const fs = require("fs-extra");
const serveIndex = require("serve-index");
const minimist = require("minimist");
const os = require("os");
const morgan = require("morgan");
// Parse CLI arguments with defaults
const args = minimist(process.argv.slice(2), {
default: {
port: 58080,
user: "admin",
password: "password",
dir: "./",
"max-files": 10,
"max-size": 5 * 1024 * 1024 * 1024,
extensions: "",
},
});
const port = args.port;
const username = args.user;
let password = args.password;
const uploadDir = path.resolve(args.dir);
const maxFiles = args["max-files"];
const maxSize = parseInt(args["max-size"], 10);
const allowedExtensions = args.extensions
? args.extensions.split(",").map((ext) => ext.trim().toLowerCase())
: [];
// If user did not change the default password, generate a simple random one
if (password === "password") {
password = generateSimpleRandomPassword(8);
}
// Ensure upload directory exists
fs.ensureDirSync(uploadDir);
// Simple random password generator
function generateSimpleRandomPassword(length) {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
// Authentication middleware
function auth(req, res, next) {
const credentials = basicAuth(req);
if (!credentials || credentials.name !== username || credentials.pass !== password) {
res.set("WWW-Authenticate", 'Basic realm="uplite"');
return res.status(401).send("Access Denied");
}
next();
}
// Configure Multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir);
},
filename: (req, file, cb) => {
const timestamp = Date.now();
const cleanFileName = file.originalname
.replace(/[^a-zA-Z0-9_\-.]/g, "_") // Sanitize file names
.replace(/\s+/g, "_");
cb(null, `${timestamp}-${cleanFileName}`);
},
});
const upload = multer({
storage,
limits: { fileSize: maxSize },
fileFilter: (req, file, cb) => {
console.log("Processing file:", file.originalname);
if (allowedExtensions.length === 0) {
console.log("File accepted (no restrictions).");
return cb(null, true);
}
const fileExtension = path.extname(file.originalname).toLowerCase().replace(".", "");
if (allowedExtensions.includes(fileExtension)) {
console.log("File accepted:", file.originalname);
cb(null, true);
} else {
console.error("File rejected due to invalid extension:", file.originalname);
cb(new Error(`Invalid file type. Allowed extensions are: ${allowedExtensions.join(", ")}`));
}
},
});
// Initialize Express app
const app = express();
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use("/static", express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Set headers to improve security
app.use((req, res, next) => {
res.setHeader("Cache-Control", "no-store");
res.setHeader("X-Content-Type-Options", "nosniff");
res.removeHeader("X-Powered-By");
next();
});
// Enhanced logging middleware
app.use(
morgan((tokens, req, res) => {
const clientIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
const ipv4 = clientIP.includes("::ffff:") ? clientIP.split(":").pop() : clientIP;
return [
ipv4, // Client's IPv4 address
tokens.method(req, res), // HTTP method
tokens.url(req, res), // Requested URL
tokens.status(req, res), // Response status
tokens["response-time"](req, res), // Response time
"ms", // Time unit
].join(" ");
})
);
// Routes
app.get("/", auth, async (req, res) => {
try {
const files = await fs.readdir(uploadDir);
const fileDetails = await Promise.all(
files.map(async (file) => {
const filePath = path.join(uploadDir, file);
if (!(await fs.pathExists(filePath))) {
return null;
}
const stats = await fs.stat(filePath);
return {
name: file,
time: stats.mtimeMs,
};
})
);
const sortedFiles = fileDetails
.filter(Boolean)
.sort((a, b) => b.time - a.time)
.map((file) => file.name);
res.render("index", { files: sortedFiles });
} catch (err) {
console.error("Error reading files:", err);
res.status(500).send("Internal Server Error");
}
});
app.post("/upload", auth, upload.array("file", maxFiles), async (req, res) => {
try {
if (!req.files || req.files.length === 0) {
return res.status(400).send("No files were uploaded.");
}
console.log("Uploaded files:", req.files.map((f) => f.originalname));
res.redirect("/");
} catch (err) {
console.error("Upload error:", err.message);
res.status(500).send(err.message || "An error occurred during upload.");
}
});
app.get("/info/:filename", auth, async (req, res) => {
const fileName = path.basename(req.params.filename);
const filePath = path.join(uploadDir, fileName);
try {
if (!(await fs.pathExists(filePath))) {
return res.status(404).send("File not found.");
}
const stats = await fs.stat(filePath);
const fileInfo = {
name: fileName,
size: `${(stats.size / (1024 * 1024)).toFixed(2)} MB`,
modified: stats.mtime.toLocaleString(),
absolutePath: filePath,
os: os.type(),
arch: os.arch(),
host: os.hostname(),
userIP: req.ip || req.headers["x-forwarded-for"],
};
res.render("info", { fileInfo });
} catch (err) {
console.error("Error getting file info:", err);
res.status(500).send("Internal Server Error");
}
});
app.get("/delete/:filename", auth, async (req, res) => {
const fileName = path.basename(req.params.filename);
const filePath = path.join(uploadDir, fileName);
try {
if (!(await fs.pathExists(filePath))) {
console.warn(`Attempted to access deletion page for non-existent file: ${fileName}`);
return res.status(404).send("File not found.");
}
res.render("confirm-delete", { fileName });
} catch (err) {
console.error("Error checking file existence for deletion:", err);
res.status(500).send("Internal Server Error");
}
});
app.post("/delete/:filename", auth, async (req, res) => {
const fileName = path.basename(req.params.filename);
const filePath = path.join(uploadDir, fileName);
try {
if (await fs.pathExists(filePath)) {
await fs.remove(filePath);
console.log(`Deleted file: ${fileName}`);
} else {
console.warn(`Attempted to delete non-existent file: ${fileName}`);
}
res.redirect("/");
} catch (err) {
console.error("Error deleting file:", err);
res.status(500).send("Internal Server Error");
}
});
app.use("/files", auth, express.static(uploadDir), serveIndex(uploadDir, { icons: true }));
// Global Error Handler
app.use((err, req, res, next) => {
console.error("Unhandled Error:", err.message);
res.status(500).send(err.message || "An unexpected error occurred.");
});
// Start the server on all interfaces
app.listen(port, () => {
const networkInterfaces = os.networkInterfaces();
const uniqueAddresses = new Set();
console.log("\n=== uplite server is running ===\n");
// Add localhost explicitly
uniqueAddresses.add(`http://localhost:${port}`);
uniqueAddresses.add(`http://127.0.0.1:${port}`);
// Add network interfaces
Object.values(networkInterfaces).forEach((interfaces) => {
interfaces.forEach((iface) => {
if (iface.family === "IPv4") {
uniqueAddresses.add(`http://${iface.address}:${port}`);
}
});
});
console.log("Access the server at the following addresses:\n");
uniqueAddresses.forEach((address) => console.log(` - ${address}`));
console.log("\nServer Configuration:");
console.log(` - Shared Folder : ${uploadDir}`);
console.log(` - Username : ${username}`);
console.log(` - Password : ${password}`);
console.log(` - Allowed Extensions: ${allowedExtensions.length > 0 ? allowedExtensions.join(", ") : "All"}`);
console.log(` - Max Files/Upload : ${maxFiles}`);
console.log(` - Max File Size : ${(maxSize / (1024 * 1024)).toFixed(2)} MB\n`);
console.log("=================================\n");
});