Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (118 loc) · 4.87 KB
/
Copy pathserver.js
File metadata and controls
140 lines (118 loc) · 4.87 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
const express = require('express');
const http = require('http');
const path = require('path');
const os = require('os');
const fs = require('fs');
const rateLimit = require('express-rate-limit');
const validator = require('validator');
const simpleGit = require('simple-git');
const { execFile } = require('child_process');
const shellQuote = require('shell-quote');
const app = express();
const server = http.createServer(app);
const git = simpleGit();
let port = 1024;
const findFreePort = (startingPort) => {
return new Promise((resolve) => {
const server = http.createServer();
server.listen(startingPort, () => {
server.once('close', () => resolve(startingPort));
server.close();
});
server.on('error', () => {
resolve(findFreePort(startingPort + 1));
});
});
};
const startServer = async (plugins) => {
port = await findFreePort(port);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bootstrap', express.static(path.join(__dirname, 'node_modules/bootstrap/dist')));
app.use('/bootstrap-icons/font', express.static(path.join(__dirname, 'node_modules/bootstrap-icons/font')));
app.use('/bootstrap-icons/icons', express.static(path.join(__dirname, 'node_modules/bootstrap-icons/icons')));
// Basic route
app.get('/', (req, res) => {
res.render('index');
});
app.get('/windows/settings', (req, res) => {
res.render('windows/settings');
});
// User home directory route
app.get('/user-home-dir', (req, res) => {
const homeDir = os.homedir();
res.json({ homeDir });
});
// Command execution limiter
const commandLimiter = rateLimit({
windowMs: 60 * 1000,
max: 10,
message: { success: false, message: 'Too many requests, please try again later.' }
});
// Clone project route
app.post('/create-project', async (req, res) => {
const { projectName, folderPath } = req.body;
if (!projectName || !folderPath) {
return res.status(400).json({ success: false, message: 'Missing project name or folder path.' });
}
const targetPath = path.join(folderPath, projectName);
try {
await git.clone('https://github.com/ScriptBlocks/example-project.git', targetPath);
res.json({ success: true });
} catch (error) {
console.error('Git clone error:', error);
res.status(500).json({ success: false, message: 'Failed to create project.' });
}
});
// Command execution route
app.post('/execute-command', commandLimiter, (req, res) => {
let { command, currentDir } = req.body;
let workingDir = currentDir === '~' ? os.homedir() : path.resolve(os.homedir(), currentDir);
// Validate that workingDir is inside the home directory
if (!workingDir.startsWith(os.homedir()) || !fs.existsSync(workingDir) || !fs.lstatSync(workingDir).isDirectory()) {
return res.status(400).json({ success: false, message: 'Invalid working directory.' });
}
// Sanitize and validate the command
command = command.trim();
if (!validator.isAscii(command)) {
return res.status(400).json({ success: false, message: 'Invalid command.' });
}
if (command.startsWith('cd ')) {
const targetDir = path.resolve(workingDir, command.slice(3).trim());
try {
process.chdir(targetDir);
workingDir = process.cwd();
res.json({ success: true, currentDir: workingDir, output: '' });
} catch (error) {
res.json({ success: false, currentDir: workingDir, output: `cd: ${error.message}` });
}
} else {
const parsedCommand = shellQuote.parse(command);
const cmd = parsedCommand[0];
const args = parsedCommand.slice(1);
execFile(cmd, args, { cwd: workingDir }, (error, stdout, stderr) => {
if (error) {
res.json({ success: false, currentDir: workingDir, output: stderr || error.message });
} else {
res.json({ success: true, currentDir: workingDir, output: stdout || stderr });
}
});
}
});
// Apply server hooks from plugins
if (plugins) {
plugins.forEach(plugin => {
if (typeof plugin.serverHook === 'function') {
plugin.serverHook(app); // Register plugin routes/functions
}
});
}
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
return { server, port };
};
module.exports = { startServer };