This commit is contained in:
Lee
2024-01-03 07:25:17 +00:00
parent 4ed5d2af84
commit e603f65fdc
18 changed files with 1350 additions and 639 deletions

View File

@ -2,8 +2,11 @@ import javaPing from "mcping-js";
import { ResolvedServer, resolveDns } from "../utils/dnsResolver";
const bedrockPing = require("mcpe-ping-fixed"); // Doesn't have typescript definitions
import { Point } from "@influxdata/influxdb-client";
import { influx } from "..";
import Config from "../../data/config.json";
import { Ping } from "../types/ping";
import { logger } from "../utils/logger";
/**
* The type of server.
@ -79,23 +82,43 @@ export default class Server {
/**
* Pings a server and gets the response.
*
* @param server the server to ping
* @param insertPing whether to insert the ping into the database
* @returns the ping response or undefined if the server is offline
*/
public pingServer(): Promise<Ping | undefined> {
switch (this.getType()) {
case "PC": {
return this.pingPCServer();
public async pingServer(): Promise<Ping | undefined> {
try {
let response;
switch (this.getType()) {
case "PC": {
response = await this.pingPCServer();
break;
}
case "PE": {
response = await this.pingPEServer();
break;
}
}
case "PE": {
return this.pingPEServer();
if (!response) {
return Promise.resolve(undefined);
}
default: {
throw new Error(
`Unknown server type ${this.getType()} for ${this.getName()}`
try {
influx.writePoint(
new Point("playerCount")
.tag("id", this.getID().toString())
.tag("ip", this.getIP().toLowerCase())
.intField("playerCount", response.playerCount)
.timestamp(response.timestamp)
);
} catch (err) {
logger.warn(`Failed to write ping to influxdb`, err);
}
return Promise.resolve(response);
} catch (err) {
logger.warn(`Failed to ping ${this.getIP()}`, err);
return Promise.resolve(undefined);
}
}
@ -133,7 +156,7 @@ export default class Server {
const serverPing = new javaPing.MinecraftServer(ip, port);
return new Promise((resolve, reject) => {
serverPing.ping(Config.scanner.timeout, 700, (err, res) => {
serverPing.ping(Config.scanner.timeout, 765, (err, res) => {
if (err || res == undefined) {
return reject(err);
}

View File

@ -1,6 +1,7 @@
import Server, { ServerType } from "./server";
import Servers from "../../data/servers.json";
import { logger } from "../utils/logger";
export default class ServerManager {
private servers: Server[] = [];
@ -11,6 +12,7 @@ export default class ServerManager {
* Loads the servers from the config file.
*/
async init() {
logger.info("Loading servers");
for (const configServer of Servers) {
const server = new Server({
id: configServer.id,
@ -18,11 +20,17 @@ export default class ServerManager {
name: configServer.name,
type: configServer.type as ServerType,
});
try {
await server.pingServer();
} catch (err) {}
this.servers.push(server);
}
// do an inital ping of all servers to load data from them
await Promise.all(
this.servers.map((server) => {
try {
server.pingServer();
} catch (err) {}
})
);
logger.info(`Loaded ${this.servers.length} servers`);
}
/**