-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
192 lines (155 loc) · 5.05 KB
/
app.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
// app.js
const express = require('express');
const multer = require('multer');
const uuid = require('uuid');
const fs = require('fs-extra');
const path = require('path');
const app = express();
const port = 3000;
// Data files
const adminDataPath = './admin.json';
const filesDataPath = './data/files.json';
const bannedIPsPath = './data/banned_ips.json';
// Load admin key
const adminData = fs.readJsonSync(adminDataPath);
const adminKey = adminData.adminKey;
// Helper functions to load and save data
const loadJSON = (path) => fs.readJsonSync(path, { throws: false }) || {};
const saveJSON = (path, data) => fs.writeJsonSync(path, data);
// Localization Middleware
const locales = {
en: require('./locales/en.json'),
de: require('./locales/de.json'),
ru: require('./locales/ru.json'),
uk: require('./locales/uk.json'),
};
app.use((req, res, next) => {
let lang = req.acceptsLanguages('en', 'de', 'ru', 'uk') || 'en';
req.locale = locales[lang] || locales['en'];
next();
});
// Serve essential static files for banned.html before IP detection
app.use('/css', express.static(path.join(__dirname, 'public', 'css')));
app.use('/js', express.static(path.join(__dirname, 'public', 'js')));
app.use('/locales', express.static(path.join(__dirname, 'locales')));
app.use('/images', express.static(path.join(__dirname, 'public', 'images')));
// IP Detection Middleware
app.set('trust proxy', true); // If behind a proxy like Nginx or a load balancer
app.use((req, res, next) => {
let clientIP = req.ip.replace('::ffff:', ''); // Handle IPv4-mapped IPv6 addresses
const bannedIPs = loadJSON(bannedIPsPath);
if (bannedIPs[clientIP]) {
// Allow access to banned.html and essential static assets
if (
req.path === '/banned.html' ||
req.path.startsWith('/css/') ||
req.path.startsWith('/js/') ||
req.path.startsWith('/locales/') ||
req.path.startsWith('/images/')
) {
return next();
} else {
return res.sendFile(path.join(__dirname, 'public', 'banned.html'));
}
}
next();
});
// Middleware for parsing JSON and URL-encoded data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve other static files
app.use(express.static('public'));
// Storage configuration for Multer
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'files/');
},
filename: (req, file, cb) => {
const uniqueName = uuid.v4() + path.extname(file.originalname);
cb(null, uniqueName);
},
});
const upload = multer({ storage });
// Routes
// Upload Route
app.post('/upload', upload.single('file'), (req, res) => {
const filesData = loadJSON(filesDataPath);
const fileID = uuid.v4();
const fileData = {
id: fileID,
originalName: req.file.originalname,
storedName: req.file.filename,
size: req.file.size,
uploadDate: new Date(),
};
filesData[fileID] = fileData;
saveJSON(filesDataPath, filesData);
res.json({ link: `/file.html?id=${fileID}` });
});
// File Details API
app.get('/file/:id/details', (req, res) => {
const filesData = loadJSON(filesDataPath);
const fileData = filesData[req.params.id];
if (!fileData) {
return res.json({ error: req.locale.file_not_found });
}
res.json(fileData);
});
// File Download Action
app.get('/download/:id', (req, res) => {
const filesData = loadJSON(filesDataPath);
const fileData = filesData[req.params.id];
if (!fileData) {
return res.status(404).send(req.locale.file_not_found);
}
const filePath = path.join(__dirname, 'files', fileData.storedName);
res.download(filePath, fileData.originalName);
});
// Serve rules.html when accessing /rules
app.get('/rules', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'rules.html'));
});
// Serve admin.html when accessing /admin
app.get('/admin', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin.html'));
});
// Admin Authentication Middleware
const authenticateAdmin = (req, res, next) => {
const key = req.headers['x-admin-key'] || req.query.key;
if (key === adminKey) {
next();
} else {
res.status(401).send('Unauthorized');
}
};
// Admin Routes
// Get All Files
app.get('/admin/files', authenticateAdmin, (req, res) => {
const filesData = loadJSON(filesDataPath);
res.json(filesData);
});
// Delete a File
app.delete('/admin/files/:id', authenticateAdmin, (req, res) => {
const filesData = loadJSON(filesDataPath);
const fileData = filesData[req.params.id];
if (fileData) {
fs.unlinkSync(`files/${fileData.storedName}`);
delete filesData[req.params.id];
saveJSON(filesDataPath, filesData);
res.json({ message: 'File deleted' });
} else {
res.status(404).json({ message: 'File not found' });
}
});
// Ban an IP
app.post('/admin/ban', authenticateAdmin, (req, res) => {
const bannedIPs = loadJSON(bannedIPsPath);
const ipToBan = req.body.ip;
bannedIPs[ipToBan] = true;
saveJSON(bannedIPsPath, bannedIPs);
res.json({ message: `IP ${ipToBan} has been banned` });
});
// Start the Server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});