Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
228 lines (187 loc) · 5.26 KB
/
Copy pathserver.js
File metadata and controls
228 lines (187 loc) · 5.26 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
require("dotenv").config();
const express = require("express");
const http = require("http");
const cors = require("cors");
const { Server } = require("socket.io");
const { exec } = require("child_process");
const si = require("systeminformation");
const app = express();
const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
const TOKEN = process.env.TOKEN;
const MC_DIR = process.env.MC_DIR || "/home/takashi/server2";
const SESSION = process.env.TMUX_SESSION || "bedrock";
app.use(cors());
app.use(express.json());
const io = new Server(server, {
cors: {
origin: "*",
},
});
function run(cmd) {
return new Promise((resolve) => {
exec(cmd, { shell: "/bin/bash" }, (err, stdout, stderr) => {
resolve({
ok: !err,
stdout,
stderr,
});
});
});
}
function auth(req, res, next) {
const token = req.headers.authorization?.replace("Bearer ", "");
if (!TOKEN || token !== TOKEN) {
return res.status(401).json({
ok: false,
error: "Unauthorized",
});
}
next();
}
async function tmuxExists() {
const r = await run(`tmux has-session -t "${SESSION}" 2>/dev/null`);
return r.ok;
}
async function ensureTmuxSession() {
if (await tmuxExists()) return true;
// Buat tmux sebagai shell kosong, bukan langsung ./bedrock_server.
// Jadi kalau server stop, tmux tetap hidup.
const r = await run(`tmux new-session -d -s "${SESSION}" -c "${MC_DIR}"`);
return r.ok;
}
async function sendToTmux(command) {
await ensureTmuxSession();
return run(`tmux send-keys -t "${SESSION}" '${command}' C-m`);
}
async function isBedrockRunning() {
const r = await run(`pgrep -f "${MC_DIR}/bedrock_server|./bedrock_server"`);
return r.ok && r.stdout.trim().length > 0;
}
app.get("/", (req, res) => {
res.json({
ok: true,
message: "Minecraft Bedrock Web Control Backend aktif",
});
});
app.post("/api/start", auth, async (req, res) => {
await ensureTmuxSession();
if (await isBedrockRunning()) {
return res.json({
ok: true,
message: "Server sudah berjalan",
});
}
const r = await sendToTmux(`cd "${MC_DIR}" && ./bedrock_server`);
res.json({
ok: r.ok,
message: r.ok ? "Server berhasil dinyalakan" : "Gagal menyalakan server",
error: r.stderr,
});
});
app.post("/api/stop", auth, async (req, res) => {
if (!(await tmuxExists())) {
return res.json({
ok: true,
message: "Tmux server tidak berjalan",
});
}
if (await isBedrockRunning()) {
await run(`tmux send-keys -t "${SESSION}" 'stop' C-m`);
await new Promise((resolve) => setTimeout(resolve, 8000));
}
// STOP = matikan server DAN end task tmux/session.
await run(`tmux kill-session -t "${SESSION}" 2>/dev/null`);
res.json({
ok: true,
message: "Server distop dan tmux session dimatikan",
});
});
app.post("/api/restart", auth, async (req, res) => {
await ensureTmuxSession();
if (await isBedrockRunning()) {
await run(`tmux send-keys -t "${SESSION}" 'stop' C-m`);
await new Promise((resolve) => setTimeout(resolve, 8000));
}
// RESTART = jangan kill tmux. Jalankan ulang di tmux yang sama.
const r = await sendToTmux(`cd "${MC_DIR}" && ./bedrock_server`);
res.json({
ok: r.ok,
message: r.ok ? "Server berhasil direstart tanpa mematikan tmux" : "Gagal restart server",
error: r.stderr,
});
});
app.post("/api/list", auth, async (req, res) => {
if (!(await tmuxExists())) {
return res.json({
ok: false,
message: "Tmux/session belum berjalan",
});
}
if (!(await isBedrockRunning())) {
return res.json({
ok: false,
message: "Server belum berjalan",
});
}
await run(`tmux send-keys -t "${SESSION}" 'list' C-m`);
res.json({
ok: true,
message: "Perintah list dikirim",
});
});
app.get("/api/status", auth, async (req, res) => {
const tmux = await tmuxExists();
const running = await isBedrockRunning();
const mem = await si.mem();
const load = await si.currentLoad();
const fs = await si.fsSize();
const mainDisk = fs.find((d) => d.mount === "/") || fs[0];
res.json({
tmux,
running,
ram: {
total: mem.total,
used: mem.used,
free: mem.free,
percent: Number(((mem.used / mem.total) * 100).toFixed(1)),
},
cpu: {
load: Number(load.currentLoad.toFixed(1)),
cores: load.cpus.length,
},
storage: mainDisk
? {
mount: mainDisk.mount,
size: mainDisk.size,
used: mainDisk.used,
available: mainDisk.available,
percent: mainDisk.use,
}
: null,
});
});
io.on("connection", (socket) => {
socket.on("auth", async (token) => {
if (!TOKEN || token !== TOKEN) {
socket.emit("log", "Unauthorized\n");
socket.disconnect();
return;
}
socket.emit("log", "Backend tersambung...\n");
const interval = setInterval(async () => {
if (!(await tmuxExists())) {
socket.emit("log", "Tmux/session belum berjalan...\n");
return;
}
const r = await run(`tmux capture-pane -pt "${SESSION}" -S -200`);
socket.emit("log", r.stdout || "");
}, 1500);
socket.on("disconnect", () => {
clearInterval(interval);
});
});
});
server.listen(PORT, () => {
console.log(`Backend aktif di port ${PORT}`);
});