-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (126 loc) · 3.75 KB
/
Copy pathserver.js
File metadata and controls
148 lines (126 loc) · 3.75 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
const express = require("express");
const bodyParser = require("body-parser");
const { spawn } = require("child_process");
const path = require("path");
const fs = require("fs");
const app = express();
const PORT = 3001;
app.use(bodyParser.json());
app.use(express.static("public"));
const scriptsConfigPath = path.join(__dirname, "scripts-config.json");
let scriptsConfig = [];
const loadScriptsConfig = () => {
try {
const data = fs.readFileSync(scriptsConfigPath, "utf-8");
scriptsConfig = JSON.parse(data);
} catch (err) {
console.error("Error loading scripts-config.json:", err);
}
};
app.post("/execute", (req, res) => {
let scriptsConfig;
try {
const data = fs.readFileSync(scriptsConfigPath, "utf-8");
scriptsConfig = JSON.parse(data);
} catch (err) {
return res.status(500).json({
message: "Error loading scripts-config.json",
error: err.message,
});
}
const { script, fps } = req.body;
const scriptConfig = scriptsConfig.find((s) => s.name === script);
if (!scriptConfig) {
return res.status(400).json({ message: "Script not allowed or not found" });
}
if (scriptConfig.name === "global_fps_custom.bat" && fps) {
const vbsContent = `Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c ${path.join(
__dirname,
"scripts",
"global_fps_custom.bat"
)} ${fps}", 0, False`;
const tempVbsPath = path.join(__dirname, "scripts", "temp_fps.vbs");
fs.writeFileSync(tempVbsPath, vbsContent);
const proceso = spawn("wscript.exe", [tempVbsPath], {
windowsHide: true,
stdio: "pipe",
});
let output = "";
proceso.stdout.on("data", (data) => {
output += data.toString();
});
proceso.stderr.on("data", (data) => {
output += data.toString();
});
proceso.on("error", (error) => {
console.error(`Error executing script: ${error.message}`);
res
.status(500)
.json({ message: "Error executing script", error: error.message });
});
proceso.on("close", (code) => {
fs.unlinkSync(tempVbsPath);
if (code === 0) {
res.json({
message: `Script "${scriptConfig.description}" executed successfully`,
output,
});
} else {
res.status(500).json({
message: `Script finished with errors (code ${code})`,
output,
});
}
});
return;
}
const vbsName = scriptConfig.name.replace(/\.bat$/i, ".vbs");
const vbsScriptPath = path.join(__dirname, "scripts", vbsName);
if (!fs.existsSync(vbsScriptPath)) {
return res
.status(404)
.json({ message: "VBScript file for the script does not exist" });
}
const proceso = spawn("wscript.exe", [vbsScriptPath], {
windowsHide: true,
stdio: "pipe",
});
let output = "";
proceso.stdout.on("data", (data) => {
output += data.toString();
});
proceso.stderr.on("data", (data) => {
output += data.toString();
});
proceso.on("error", (error) => {
console.error(`Error executing script: ${error.message}`);
res
.status(500)
.json({ message: "Error executing script", error: error.message });
});
proceso.on("close", (code) => {
if (code === 0) {
res.json({
message: `Script "${scriptConfig.description}" executed successfully`,
output,
});
} else {
res.status(500).json({
message: `Script finished with errors (code ${code})`,
output,
});
}
});
});
app.get("/scripts", (req, res) => {
res.json(scriptsConfig);
});
loadScriptsConfig();
fs.watchFile(scriptsConfigPath, (curr, prev) => {
console.log("scripts-config.json has changed. Reloading...");
loadScriptsConfig();
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});