forked from metavannier/ProjectTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
285 lines (247 loc) · 8.89 KB
/
Copy pathserver.js
File metadata and controls
285 lines (247 loc) · 8.89 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
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
const express = require("express");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const session = require("express-session");
require('dotenv').config();
const app = express();
const PORT = 80;
// Validate that required env vars are set
if (!process.env.PASSWORD) {
console.error("ERROR: PASSWORD is not set in .env file. Server aborted.");
process.exit(1);
}
if (!process.env.SESSION_SECRET) {
console.error("ERROR: SESSION_SECRET is not set in .env file. Server aborted.");
process.exit(1);
}
const DATA_FILE = path.join(__dirname, "projects.json");
const PROJECT_DIR = path.join(__dirname, "project");
// Ensure project directory exists
if (!fs.existsSync(PROJECT_DIR)) {
fs.mkdirSync(PROJECT_DIR, { recursive: true });
}
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Session middleware — authentication stored on the server
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
sameSite: 'strict',
maxAge: 8 * 60 * 60 * 1000
}
}));
// Protective middleware — blocks everything without a valid session
function requireAuth(req, res, next) {
if (req.session && req.session.authenticated) {
return next();
}
// API request → 401, otherwise redirect to login
if (req.path.startsWith('/api/')) {
return res.status(401).json({ error: "Unauthorized" });
}
res.redirect('/');
}
// Redirect / to the login page
app.get("/", (req, res) => {
if (req.session && req.session.authenticated) {
return res.redirect('/index.html');
}
res.sendFile(path.join(__dirname, "login.html"));
});
// index.html protected first before express.static
app.get("/index.html", requireAuth, (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// index:false to not serve index.html automatically
app.use(express.static(path.join(__dirname), {index:false}));
// Route login — verifies the password and creates the session
app.post("/api/auth", (req, res) => {
const { password } = req.body;
if (!password) {
return res.status(400).json({ success: false, error: "No password provided" });
}
// Constant-time comparison to avoid timing attacks
const expected = Buffer.from(process.env.PASSWORD);
const received = Buffer.from(password);
const match =
expected.length === received.length &&
crypto.timingSafeEqual(expected, received);
if (match) {
req.session.authenticated = true;
res.json({ success: true });
} else {
// Random delay to slow down brute-force attacks
const delay = 100 + Math.floor(Math.random() * 200);
setTimeout(() => {
res.status(401).json({ success: false, error: "Invalid password" });
}, delay);
}
});
// Route logout — session destroyed
app.get("/logout", (req, res) => {
req.session.destroy(() => {
res.redirect('/');
});
});
// ─── API Routes — all protected by requireAuth ───────────────────────────
// Helper to get individual project filename
function getProjectFilename(project) {
const safeTitle = project.title
.replace(/[^a-zA-Z0-9]/g, "_")
.replace(/_+/g, "_")
.substring(0, 50);
return `${project.id}_${safeTitle}.json`;
}
// Helper to save individual project file
function saveIndividualProject(project) {
try {
const filename = getProjectFilename(project);
const filePath = path.join(PROJECT_DIR, filename);
fs.writeFileSync(filePath, JSON.stringify(project, null, 2));
console.log(`✓ Saved individual project: ${filename}`);
} catch (err) {
console.error("Error saving individual project:", err);
}
}
// Helper to delete individual project file by ID
function deleteIndividualProjectById(projectId) {
try {
const files = fs.readdirSync(PROJECT_DIR)
.filter(f => f.endsWith('.json'));
for (const file of files) {
const match = file.match(/^(\d+)_/);
if (match && parseInt(match[1]) === projectId) {
const filePath = path.join(PROJECT_DIR, file);
fs.unlinkSync(filePath);
console.log(`✓ Deleted individual project file: ${file}`);
return true;
}
}
console.log(`No individual file found for project ID: ${projectId}`);
return false;
} catch (err) {
console.error("Error deleting project file:", err);
return false;
}
}
// Load projects from individual files
app.get("/api/projects", requireAuth, (req, res) => {
try {
const files = fs.readdirSync(PROJECT_DIR).filter(f => f.endsWith('.json'));
const projects = files.map(file => {
return JSON.parse(fs.readFileSync(path.join(PROJECT_DIR, file), "utf-8"));
});
projects.sort((a, b) => (a.order ?? a.id) - (b.order ?? b.id));
res.json(projects);
} catch (err) {
if (fs.existsSync(DATA_FILE)) {
res.json(JSON.parse(fs.readFileSync(DATA_FILE, "utf-8")));
} else {
res.redirect('/');
}
}
});
// Save projects
app.post("/api/projects", requireAuth, (req, res) => {
const newProjects = req.body;
let existingIds = [];
try {
const files = fs.readdirSync(PROJECT_DIR).filter(f => f.endsWith('.json'));
existingIds = files
.map(f => {
const match = f.match(/^(\d+)_/);
return match ? parseInt(match[1]) : null;
})
.filter(id => id !== null);
} catch (err) {
console.error("Error reading project directory for diff:", err);
}
const newIds = newProjects.map(p => p.id);
const deletedIds = existingIds.filter(id => !newIds.includes(id));
deletedIds.forEach(id => deleteIndividualProjectById(id));
fs.writeFileSync(DATA_FILE, JSON.stringify(newProjects, null, 2));
res.json({ status: "ok" });
});
// Save a single project file
app.post("/api/project/:id", requireAuth, (req, res) => {
const project = req.body;
try {
saveIndividualProject(project);
res.json({ status: "ok" });
} catch (err) {
res.status(500).json({ error: "Failed to save project" });
}
});
// Delete a project
app.delete("/api/project/:id", requireAuth, (req, res) => {
const projectId = parseInt(req.params.id);
if (deleteIndividualProjectById(projectId)) {
res.json({ status: "ok", message: "Project file deleted" });
} else {
res.status(404).json({ error: "Project file not found" });
}
});
// Clean up orphaned files
app.post("/api/projects/cleanup", requireAuth, (req, res) => {
try {
let existingProjects = [];
if (fs.existsSync(DATA_FILE)) {
existingProjects = JSON.parse(fs.readFileSync(DATA_FILE, "utf-8"));
}
const existingIds = existingProjects.map(p => p.id);
const files = fs.readdirSync(PROJECT_DIR)
.filter(f => f.endsWith('.json'));
let deletedCount = 0;
files.forEach(file => {
const match = file.match(/^(\d+)_/);
if (match) {
const fileId = parseInt(match[1]);
if (!existingIds.includes(fileId)) {
const filePath = path.join(PROJECT_DIR, file);
fs.unlinkSync(filePath);
console.log(`Cleaned up orphaned file: ${file}`);
deletedCount++;
}
}
});
res.json({ status: "ok", deleted: deletedCount });
} catch (err) {
console.error("Error during cleanup:", err);
res.status(500).json({ error: "Cleanup failed" });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`Individual project files stored in: ${PROJECT_DIR}`);
// Perform initial cleanup on startup
try {
let existingProjects = [];
if (fs.existsSync(DATA_FILE)) {
existingProjects = JSON.parse(fs.readFileSync(DATA_FILE, "utf-8"));
}
const existingIds = existingProjects.map(p => p.id);
const files = fs.readdirSync(PROJECT_DIR)
.filter(f => f.endsWith('.json'));
let orphanedCount = 0;
files.forEach(file => {
const match = file.match(/^(\d+)_/);
if (match) {
const fileId = parseInt(match[1]);
if (!existingIds.includes(fileId)) {
orphanedCount++;
}
}
});
if (orphanedCount > 0) {
console.log(`Found ${orphanedCount} orphaned project files. Run POST /api/projects/cleanup to remove them.`);
}
} catch (err) {
console.error("Error checking for orphaned files:", err);
}
});