-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
132 lines (106 loc) · 3.69 KB
/
Copy pathapp.js
File metadata and controls
132 lines (106 loc) · 3.69 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
const express = require('express');
const crypto = require('crypto');
const helmet = require('helmet');
const escapeHtml = require('escape-html');
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
app.use(helmet());
app.set('view engine', 'ejs');
const messages = new Map();
const MAX_MESSAGE_LENGTH = 10000;
function hashPin(pin) {
return crypto.createHash('sha256').update(pin).digest('hex');
}
app.get('/', (req, res) => {
res.render('index');
});
app.post('/share', (req, res) => {
const { text, pin } = req.body;
if (!text || text.trim() === '') {
return res.status(400).render('error', { error: 'Text is required' });
}
if (text.length > MAX_MESSAGE_LENGTH) {
return res.status(400).render('error', { error: `Message too long. Maximum length is ${MAX_MESSAGE_LENGTH} characters.` });
}
const id = crypto.randomBytes(8).toString('hex');
const sanitizedText = escapeHtml(text);
messages.set(id, { text: sanitizedText, pin: pin ? hashPin(pin) : null });
const link = `${req.protocol}://${req.get('host')}/view/${id}`;
res.render('index', { link });
});
app.get('/view/:id', (req, res) => {
const { id } = req.params;
const message = messages.get(id);
if (!message) {
return res.status(404).render('error', { error: 'Message not found or already viewed' });
}
if (message.pin) {
res.render('view', { id, pinProtected: true });
} else {
messages.delete(id);
res.render('view', { message: message.text, pinProtected: false, sanitized: true });
}
});
app.post('/view/:id', (req, res) => {
const { id } = req.params;
const { pin } = req.body;
const message = messages.get(id);
if (!message) {
return res.status(404).render('error', { error: 'Message not found or already viewed' });
}
if (message.pin !== hashPin(pin)) {
return res.status(400).render('view', { id, pinProtected: true, error: 'Incorrect PIN' });
}
const responseMessage = message.text;
messages.delete(id);
res.render('view', { message: responseMessage, pinProtected: false, sanitized: true });
});
app.post('/api/share', (req, res) => {
const { text, pin } = req.body;
if (!text || text.trim() === '') {
return res.status(400).json({ error: 'Text is required' });
}
if (text.length > MAX_MESSAGE_LENGTH) {
return res.status(400).json({ error: `Message too long. Maximum length is ${MAX_MESSAGE_LENGTH} characters.` });
}
const id = crypto.randomBytes(8).toString('hex');
const sanitizedText = escapeHtml(text);
messages.set(id, { text: sanitizedText, pin: pin ? hashPin(pin) : null });
res.json({ id, link: `${req.protocol}://${req.get('host')}/view/${id}` });
});
app.get('/api/view/:id', (req, res) => {
const { id } = req.params;
const message = messages.get(id);
if (!message) {
return res.status(404).json({ error: 'Message not found or already viewed' });
}
if (message.pin) {
return res.json({ pinProtected: true });
} else {
messages.delete(id);
return res.json({ message: message.text });
}
});
app.post('/api/view/:id', (req, res) => {
const { id } = req.params;
const { pin } = req.body;
const message = messages.get(id);
if (!message) {
return res.status(404).json({ error: 'Message not found or already viewed' });
}
if (message.pin && message.pin !== hashPin(pin)) {
return res.status(400).json({ error: 'Incorrect PIN' });
}
const responseMessage = message.text;
messages.delete(id);
res.json({ message: responseMessage });
});
if (require.main === module) {
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
}
module.exports = app;