-
Notifications
You must be signed in to change notification settings - Fork 1
/
slave-handler.js
207 lines (165 loc) · 4.38 KB
/
slave-handler.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
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
import { getServerByName } from "./server.js";
import { getLogs } from "./console.js";
import { isValidLicense } from "./auth.js";
import { getAverage } from "./average.js";
import { HistoryBin } from "./history-bin.js";
import { formatUptime } from "./functions.js";
const startup = new Date();
export class SlaveHandler {
constructor() {
process.on("message", message => {
this.handle(message);
});
}
async handle(message) {
// Special handling for termination message
if (message === "terminate") {
console.info(info("Received terminate message, terminating..."));
const storage = await HistoryBin.getInstance();
await storage.close();
process.exit(0);
}
const { id, server, func, options } = message;
const srv = getServerByName(server);
if (!srv) {
this.respond(id, "Server not found");
return;
}
switch (func) {
case "players":
this.get_players(id, srv);
return;
case "online":
this.get_online(id, srv, options);
return;
case "server":
this.get_server(id, srv);
return;
case "count":
this.get_count(id, srv);
return;
case "health":
this.get_health(id, srv);
return;
}
this.respond(id, {
status: false,
error: "Invalid function",
});
}
respond(id, data) {
process.send({
id: id,
type: "request",
data: data,
});
}
static routes() {
return {
// Data routes require authentication
data: ["/players", "/online"],
// Static routes require no authentication
static: ["/server", "/count", "/health"],
};
}
// Get all players (cached)
get_players(id, srv) {
const players = srv.players.map(player => {
const character = player.character;
return {
source: player.source,
name: player.name,
license: player.licenseIdentifier,
flags: player.flags,
character: character
? {
id: character.id,
name: character.fullName,
flags: character.flags,
}
: false,
};
});
this.respond(id, {
status: true,
data: players,
});
}
// Get online status of 1 or more players (cached)
get_online(id, srv, options) {
const players = options?.split(",")?.filter(player => isValidLicense(player));
if (!players || !players.length) {
this.respond(id, {
status: false,
error: "No players specified",
});
return;
}
if (players.length > 50) {
this.respond(id, {
status: false,
error: "Too many players specified (max 50)",
});
}
const online = {};
for (const license of players) {
const player = srv.players.find(player => player.licenseIdentifier === license);
online[license] = player
? {
source: player.source,
character: player.character ? player.character.id : false,
}
: false;
}
this.respond(id, {
status: true,
data: online,
});
}
// Get server info
get_server(id, srv) {
const data = {
baseTime: srv.world?.baseTime || 0,
uptime: srv.info?.uptime || 0,
name: srv.info?.name || "",
logo: srv.info?.logo || "",
};
this.respond(id, {
status: true,
data: data,
});
}
// Get server player count
get_count(id, srv) {
const count = srv.players?.length || 0;
this.respond(id, {
status: true,
data: count,
});
}
// Get slave health
async get_health(id, srv) {
const avgWorld = getAverage("world"),
avgStaff = getAverage("staff");
const logs = [];
logs.push(srv ? "+ server object found" : "- server object not found");
logs.push(srv && !srv.failed ? "+ server object startup successful" : "- server object startup failed");
logs.push(srv?.token ? "+ server.token is set" : "- server.token is not set");
logs.push(srv && !srv.down ? "+ server is up" : `- server is down (${srv?.downError || "Unknown error"})`);
logs.push(srv?.info ? "+ server.info is set" : "- server.info is not set");
logs.push(`+ worker pid is ${process.pid}`);
logs.push(avgWorld ? `+ world.json API average is ${avgWorld}ms` : "- world.json API average is not set");
logs.push(avgStaff ? `+ staff.json API average is ${avgStaff}ms` : "- staff.json API average is not set");
logs.push(`+ startup was ${startup.toUTCString()}`);
logs.push(`+ uptime is ${formatUptime(startup)}`);
logs.push("");
logs.push((srv?.info ? "+ server.info = " : "- server.info = ") + JSON.stringify(srv?.info));
this.respond(id, {
status: true,
data: {
info: logs.join("\n"),
logs: getLogs().join("\n"),
},
});
}
}