-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.js
151 lines (120 loc) · 3.3 KB
/
data.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
import { requestOpFwApi } from "./http.js";
import { loadOnDutyData } from "./duty.js";
import { decompressPlayers } from "./decompressor.js";
import { HistoryBin } from "./history-bin.js";
import { getServers, promises as dns } from "node:dns";
export async function updateWorldJSON(server) {
const dutyMap = await loadOnDutyData(server);
const data = decompressPlayers(await requestOpFwApi(`${server.url}/op-framework/world.json?compress=1`, server.token));
const clientData = {};
for (let x = 0; x < data.players.length; x++) {
const player = data.players[x];
clientData[player.source] = cleanupPlayer(player, dutyMap);
}
try {
const bin = HistoryBin.getInstance(server.server);
await bin.writeAll(data.players);
} catch (err) {
console.warn(`Failed to write to history bin: ${err.message}`);
}
server.players = data.players;
server.world = data.world;
return {
players: clientData,
instance: data.world?.instance,
};
}
export async function checkIfServerIsUp(server) {
let uptime = false,
name = false,
logo = false,
success = false;
try {
if (server.down) {
// To test if we can do authorized requests
await requestOpFwApi(`${server.url}/op-framework/auth.json`, server.token);
}
const data = await requestOpFwApi(`${server.url}/op-framework/variables.json`, server.token);
if (typeof data.serverUptimeMilliseconds === "number") {
uptime = data.serverUptimeMilliseconds;
}
if (typeof data.communityName === "string") {
name = data.communityName;
}
if (typeof data.communityLogo === "string") {
logo = data.communityLogo;
}
success = data.serverReady === true;
} catch (e) {
console.warn(`Failed to check if server is up (${String(server.url)}): ${e.message}`);
await canResolveServerDNS(server.url);
if (!server.url) {
console.error("Server URL not found, waiting for restart...");
process.exit(1);
}
}
if (success && server.down) {
console.info(`Server ${server.url} is up again! (uptime=${uptime}, name=${name})`);
}
if (!success) {
return false;
}
return {
uptime: uptime,
name: name,
logo: logo,
};
}
async function canResolveServerDNS(url) {
if (!url || url.match(/\d+\.\d+\.\d+\.\d+/)) {
return;
}
const uri = new URL(url),
host = uri.host,
servers = getServers();
try {
const result = await dns.lookup(host);
if (!result || !result.address) {
throw new Error("no address found");
}
console.log(`Resolved ${host} to: ${result.address}`);
} catch (e) {
console.info(`Active DNS servers: ${servers.join(", ")}`);
console.warn(`Failed to resolve ${host}: ${e.message}`);
}
}
function cleanupPlayer(player, dutyMap) {
const character = player.character,
vehicle = player.vehicle;
const license = player.licenseIdentifier,
duty = license in dutyMap ? dutyMap[license] : false;
return {
source: player.source,
license: license,
name: player.name,
coords: player.coords,
speed: player.speed,
flags: player.flags,
instance: player.instanceId,
character: character
? {
id: character.id,
name: character.fullName,
flags: character.flags,
}
: false,
vehicle: vehicle
? {
id: vehicle.id,
model: vehicle.model,
driving: vehicle.driving,
}
: false,
duty: duty
? {
type: duty.type,
department: duty.department,
}
: false,
};
}