-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
153 lines (131 loc) · 4.51 KB
/
server.js
File metadata and controls
153 lines (131 loc) · 4.51 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
const express = require('express');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const DATA_DIR = process.env.DATA_DIR || './data';
// Создаём директорию для данных если её нет
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
// Middleware
app.use(express.json({ limit: '50mb' }));
app.use(express.static('public'));
// API Endpoints
// Получить все сохранённые бункеры
app.get('/api/bunkers', (req, res) => {
try {
const files = fs.readdirSync(DATA_DIR).filter(f => f.endsWith('.json'));
const bunkers = files.map(file => {
const content = fs.readFileSync(path.join(DATA_DIR, file), 'utf8');
const data = JSON.parse(content);
return {
id: file.replace('.json', ''),
name: data.name,
created: data.created,
itemCount: data.items.length
};
});
res.json(bunkers);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Создать новый бункер
app.post('/api/bunkers', (req, res) => {
try {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: 'Name is required' });
}
const id = crypto.randomBytes(8).toString('hex');
const bunker = {
id,
name,
created: new Date().toISOString(),
items: [],
password: crypto.randomBytes(16).toString('hex')
};
fs.writeFileSync(path.join(DATA_DIR, `${id}.json`), JSON.stringify(bunker, null, 2));
res.json(bunker);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Получить конкретный бункер
app.get('/api/bunkers/:id', (req, res) => {
try {
const filePath = path.join(DATA_DIR, `${req.params.id}.json`);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'Bunker not found' });
}
const content = fs.readFileSync(filePath, 'utf8');
const bunker = JSON.parse(content);
// Не отправляем пароль на фронтенд
delete bunker.password;
res.json(bunker);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Добавить элемент в бункер
app.post('/api/bunkers/:id/items', (req, res) => {
try {
const { label, value } = req.body;
if (!label || !value) {
return res.status(400).json({ error: 'Label and value are required' });
}
const filePath = path.join(DATA_DIR, `${req.params.id}.json`);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'Bunker not found' });
}
const content = fs.readFileSync(filePath, 'utf8');
const bunker = JSON.parse(content);
const item = {
id: crypto.randomBytes(4).toString('hex'),
label,
value: crypto.createHash('sha256').update(value).digest('hex'), // хешируем значение
created: new Date().toISOString(),
revealed: false
};
bunker.items.push(item);
fs.writeFileSync(filePath, JSON.stringify(bunker, null, 2));
res.json({ id: item.id, label, created: item.created });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Удалить элемент из бункера
app.delete('/api/bunkers/:id/items/:itemId', (req, res) => {
try {
const filePath = path.join(DATA_DIR, `${req.params.id}.json`);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'Bunker not found' });
}
const content = fs.readFileSync(filePath, 'utf8');
const bunker = JSON.parse(content);
bunker.items = bunker.items.filter(item => item.id !== req.params.itemId);
fs.writeFileSync(filePath, JSON.stringify(bunker, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Удалить бункер
app.delete('/api/bunkers/:id', (req, res) => {
try {
const filePath = path.join(DATA_DIR, `${req.params.id}.json`);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'Bunker not found' });
}
fs.unlinkSync(filePath);
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`🚀 Bunkr Storage Server запущен на http://localhost:${PORT}`);
console.log(`📁 Данные сохраняются в: ${path.resolve(DATA_DIR)}`);
});