-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer Configuration.js
53 lines (43 loc) · 1.67 KB
/
Server Configuration.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const CryptoJS = require('crypto-js');
const helmet = require('helmet');
const { check, validationResult } = require('express-validator');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(helmet());
// Encryption key from environment variables
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || 'DefaultSecretKey!';
// Route to handle command execution
app.post('/execute', [
// Input validation
check('command')
.trim()
.notEmpty()
.withMessage('Command cannot be empty')
.isAlphanumeric('en-US', { ignore: ' ' })
.withMessage('Command contains invalid characters')
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { command } = req.body;
// Encrypt the command
const encryptedCommand = CryptoJS.AES.encrypt(command, ENCRYPTION_KEY).toString();
// Log encrypted command securely
console.log(`Encrypted Command: ${encryptedCommand}`);
// Simulate command execution
setTimeout(() => {
const responseMessage = `Command "${command}" executed successfully.`;
const encryptedResponse = CryptoJS.AES.encrypt(responseMessage, ENCRYPTION_KEY).toString();
// Return encrypted response
res.json({ message: encryptedResponse });
}, 1000);
});
// Start the server
app.listen(PORT, () => {
console.log(`BF UUI Server is running on http://localhost:${PORT}`);
});