-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs2manager.js
More file actions
248 lines (235 loc) · 8.43 KB
/
Copy pathcs2manager.js
File metadata and controls
248 lines (235 loc) · 8.43 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Import required modules
const express = require("express");
const bodyParser = require("body-parser");
const { Rcon } = require("rcon-client");
const fs = require("fs");
// Configuration
const CONFIG = {
rconHost: "127.0.0.1", // Change to server IP
rconPort: 27015, // Default CS2 RCON port
rconPassword: "passwd", // RCON password
webPort: 3136, // Port for web server
workshopFile: "workshop_maps.json",
};
// Initialize Express app
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
// Basic authentication
const authMiddleware = (req, res, next) => {
const auth = { username: "admin", password: "admin" }; // Change these credentials
const b64auth = (req.headers.authorization || "").split(" ")[1] || "";
const [login, pwd] = Buffer.from(b64auth, "base64").toString().split(":");
if (login && pwd && login === auth.username && pwd === auth.password) {
return next();
}
res.set("WWW-Authenticate", 'Basic realm="CS2 Manager"');
return res.status(401).send("Authentication required.");
};
app.use(authMiddleware);
// Load workshop maps
const loadWorkshopMaps = () => {
try {
const data = fs.readFileSync(CONFIG.workshopFile, "utf8");
return JSON.parse(data);
} catch (err) {
console.error("Error loading workshop maps:", err);
return [];
}
};
// RCON Command Function
const sendRconCommand = async (command) => {
const rcon = new Rcon({
host: CONFIG.rconHost,
port: CONFIG.rconPort,
password: CONFIG.rconPassword,
});
try {
await rcon.connect();
const response = await rcon.send(command);
await rcon.end();
return response;
} catch (err) {
console.error("RCON Error:", err);
return `Error: ${err.message}`;
}
};
// Routes
app.post("/change-map", async (req, res) => {
const map = req.body.map;
const command = `changelevel ${map}`;
const response = await sendRconCommand(command);
res.redirect(`/?message=Command executed: ${command} - ${response}`);
});
app.post("/change-workshop-map", async (req, res) => {
const workshopId = req.body.workshopId;
const command = `ds_workshop_changelevel ${workshopId}`;
const response = await sendRconCommand(command);
res.redirect(`/?message=Command executed: ${command} - ${response}`);
});
app.post("/send-command", async (req, res) => {
const command = req.body.command;
const response = await sendRconCommand(command);
res.redirect(`/?message=Command executed: ${command} - ${response}`);
});
app.get("/", (req, res) => {
const workshopMaps = loadWorkshopMaps();
const workshopOptions = workshopMaps
.map((map) => `<option value="${map}">${map}</option>`)
.join("");
const message = req.query.message || ""; // Fetch message from query string
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CS2 Server Manager</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
background-color: #121212;
color: #eee;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
position: relative;
}
h1 {
color: #4CAF50;
margin-bottom: 20px;
}
form {
background-color: #333;
padding: 20px;
margin: 10px;
border-radius: 8px;
width: 350px;
text-align: center;
}
form h3 {
margin-top: 0;
margin-bottom: 10px;
}
button, input, select {
padding: 10px;
margin-top: 10px;
width: 100%;
border: none;
border-radius: 5px;
background-color: #4e479d;
color: white;
font-size: 16px;
box-sizing: border-box;
}
button:hover {
background-color: #9171f8;
}
input, select {
margin-top: 10px;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
/* Floating GitHub Icon */
.github-icon {
position: fixed;
bottom: 20px;
left: 20px;
font-size: 40px;
color: white;
background-color: #333;
padding: 10px;
border-radius: 50%;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.3);
transition: background-color 0.3s ease;
}
.github-icon:hover {
background-color: #6cc644;
color: white;
}
/* Notification */
.notification {
position: absolute;
top: 20px;
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border-radius: 5px;
font-size: 16px;
z-index: 9999;
animation: slideIn 2s ease-out;
transition: opacity 1s ease-out;
}
@keyframes slideIn {
from {
top: -100px;
opacity: 0;
}
to {
top: 20px;
opacity: 1;
}
}
</style>
</head>
<body>
<!-- Display the notification -->
${message ? `<div class="notification">${message}</div>` : ""}
<div class="container">
<h1>CS2 Server Manager</h1>
<form action="/change-map" method="POST">
<h3>Change Local Map</h3>
<input type="text" name="map" placeholder="Map name (de_dust2)" required />
<button type="submit">Change Map</button>
</form>
<form action="/change-workshop-map" method="POST">
<h3>Change Workshop Map</h3>
<select name="workshopId" required>
${workshopOptions}
</select>
<button type="submit">Change Workshop Map</button>
</form>
<form action="/send-command" method="POST">
<h3>Quick Commands</h3>
<button type="submit" name="command" value="mp_restartgame 1">Restart Game</button>
</form>
<form action="/send-command" method="POST">
<h3>Send Custom Command</h3>
<input type="text" name="command" placeholder="Enter console command" required />
<button type="submit">Send Command</button>
</form>
</div>
<!-- GitHub -->
<a href="https://github.com/ParadoxLeon/cs2-server-manager" target="_blank" class="github-icon">
<i class="fab fa-github"></i>
</a>
<script>
// Check if notification exists
const notification = document.querySelector('.notification');
if (notification) {
// Fade out notification after 10 seconds
setTimeout(() => {
notification.style.opacity = '0';
setTimeout(() => {
notification.style.display = 'none';
}, 1000); // Allow time for fade-out
}, 10000); // Wait for 10 seconds
}
</script>
</body>
</html>
`);
});
// Start the web server
app.listen(CONFIG.webPort, () => {
console.log(`Web manager running at http://localhost:${CONFIG.webPort}`);
});