diff --git a/src/database/database.ts b/src/database/database.ts index 24580d2..b332af5 100644 --- a/src/database/database.ts +++ b/src/database/database.ts @@ -64,7 +64,7 @@ export default class Database { constructor() { this.db = new SQLiteDatabase(`${DATA_DIR}/db.sqlite`); - this.db.pragma("journal_mode = WAL"); + this.db.pragma("journal_mode = WAL"); // Enable WAL mode for better performance logger.info("Ensuring tables exist"); this.db.exec(CREATE_PINGS_TABLE); // Ensure the pings table exists diff --git a/src/index.ts b/src/index.ts index fc2f4ac..bbc0d31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,8 +20,12 @@ export const serverManager = new ServerManager(); */ export const websocketServer = new WebsocketServer(Config.websocket.port); -// The scanner is responsible for scanning all servers -new Scanner(); +(async () => { + await serverManager.init(); + + // The scanner is responsible for scanning all servers + new Scanner(); +})(); // The websocket server is responsible for // sending data to the client in real time diff --git a/src/scanner/scanner.ts b/src/scanner/scanner.ts index 52d39a8..9464f93 100644 --- a/src/scanner/scanner.ts +++ b/src/scanner/scanner.ts @@ -9,7 +9,6 @@ export default class Scanner { constructor() { logger.info("Loading scanner database"); - logger.info("Starting server scan"); cron.schedule(Config.scanner.updateCron, () => { this.scanServers(); }); @@ -41,7 +40,7 @@ export default class Scanner { let online = false; try { - response = await server.pingServer(server); + response = await server.pingServer(); if (response == undefined) { return; // Server is offline } diff --git a/src/server/server.ts b/src/server/server.ts index 2051a4e..b4ff802 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -80,19 +80,20 @@ 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(server: Server): Promise { - switch (server.getType()) { + public pingServer(): Promise { + switch (this.getType()) { case "PC": { - return this.pingPCServer(server); + return this.pingPCServer(); } case "PE": { - return this.pingPEServer(server); + return this.pingPEServer(); } default: { throw new Error( - `Unknown server type ${server.getType()} for ${server.getName()}` + `Unknown server type ${this.getType()} for ${this.getName()}` ); } } @@ -104,10 +105,10 @@ export default class Server { * @param server the server to ping * @returns the ping response or undefined if the server is offline */ - private async pingPCServer(server: Server): Promise { + private async pingPCServer(): Promise { if (this.dnsInfo.resolvedServer == undefined && !this.dnsInfo.hasResolved) { try { - const resolvedServer = await resolveDns(server.getIP()); + const resolvedServer = await resolveDns(this.getIP()); this.dnsInfo = { hasResolved: true, @@ -125,7 +126,7 @@ export default class Server { ip = resolvedServer.ip; port = resolvedServer.port; } else { - ip = server.getIP(); + ip = this.getIP(); port = 25565; // The default port } @@ -139,7 +140,7 @@ export default class Server { this.favicon = res.favicon; // Set the favicon resolve({ - id: server.getID(), + id: this.getID(), timestamp: Date.now(), ip: ip, playerCount: res.players.online, @@ -154,20 +155,20 @@ export default class Server { * @param server the server to ping * @returns the ping response or undefined if the server is offline */ - private async pingPEServer(server: Server): Promise { + private async pingPEServer(): Promise { return new Promise((resolve, reject) => { bedrockPing( - server.getIP(), - server.getPort() || 19132, + this.getIP(), + this.getPort() || 19132, (err: any, res: any) => { if (err || res == undefined) { return reject(err); } resolve({ - id: server.getID(), + id: this.getID(), timestamp: Date.now(), - ip: server.getIP(), + ip: this.getIP(), playerCount: res.currentPlayers, }); } diff --git a/src/server/serverManager.ts b/src/server/serverManager.ts index 6a07202..bc186b1 100644 --- a/src/server/serverManager.ts +++ b/src/server/serverManager.ts @@ -5,16 +5,23 @@ import Servers from "../../data/servers.json"; export default class ServerManager { private servers: Server[] = []; - constructor() { - for (const server of Servers) { - this.servers.push( - new Server({ - id: server.id, - ip: server.ip, - name: server.name, - type: server.type as ServerType, - }) - ); + constructor() {} + + /** + * Loads the servers from the config file. + */ + async init() { + for (const configServer of Servers) { + const server = new Server({ + id: configServer.id, + ip: configServer.ip, + name: configServer.name, + type: configServer.type as ServerType, + }); + try { + await server.pingServer(); + } catch (err) {} + this.servers.push(server); } } diff --git a/src/websocket/websocket.ts b/src/websocket/websocket.ts index d0491f0..be3b7f2 100644 --- a/src/websocket/websocket.ts +++ b/src/websocket/websocket.ts @@ -1,4 +1,5 @@ -import { Server as SocketServer } from "socket.io"; +import { Socket, Server as SocketServer } from "socket.io"; +import { serverManager } from ".."; import Server, { ServerStatus } from "../server/server"; import { Ping } from "../types/ping"; import { logger } from "../utils/logger"; @@ -12,11 +13,31 @@ export default class WebsocketServer { this.server.on("connection", (socket) => { logger.debug("ws: Client connected"); - - // todo: send ping data to client + this.sendServerList(socket); }); } + /** + * Sends the server list to the given socket. + * + * @param socket the socket to send the server list to + */ + public sendServerList(socket: Socket): void { + logger.debug(`ws: Sending server list to ${socket.id}`); + + const servers = []; + for (const server of serverManager.getServers()) { + servers.push({ + id: server.getID(), + name: server.getName(), + ip: server.getIP(), + port: server.getPort(), + favicon: server.getFavicon(), + }); + } + socket.emit("serverList", servers); + } + /** * Sends the latest ping data for the given server to all clients. *