mirror of
https://github.com/louislam/uptime-kuma.git
synced 2026-05-17 08:26:56 +03:00
fix: Gamedig unable to resolve SRV records by removing redundant DNS resolution in GameDigMonitorType (#6923)
Co-authored-by: louislam <1336778+louislam@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
const { MonitorType } = require("./monitor-type");
|
||||
const { UP } = require("../../src/util");
|
||||
const { GameDig } = require("gamedig");
|
||||
const dns = require("dns").promises;
|
||||
const net = require("net");
|
||||
|
||||
class GameDigMonitorType extends MonitorType {
|
||||
name = "gamedig";
|
||||
@@ -11,15 +9,10 @@ class GameDigMonitorType extends MonitorType {
|
||||
* @inheritdoc
|
||||
*/
|
||||
async check(monitor, heartbeat, server) {
|
||||
let host = monitor.hostname;
|
||||
if (net.isIP(host) === 0) {
|
||||
host = await this.resolveHostname(host);
|
||||
}
|
||||
|
||||
try {
|
||||
const state = await GameDig.query({
|
||||
type: monitor.game,
|
||||
host: host,
|
||||
host: monitor.hostname,
|
||||
port: monitor.port,
|
||||
givenPortOnly: Boolean(monitor.gamedigGivenPortOnly),
|
||||
});
|
||||
@@ -31,21 +24,6 @@ class GameDigMonitorType extends MonitorType {
|
||||
throw new Error(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a domain name to its IPv4 address.
|
||||
* @param {string} hostname - The domain name to resolve (e.g., "example.dyndns.org").
|
||||
* @returns {Promise<string>} - The resolved IP address.
|
||||
* @throws Will throw an error if the DNS resolution fails.
|
||||
*/
|
||||
async resolveHostname(hostname) {
|
||||
try {
|
||||
const result = await dns.lookup(hostname);
|
||||
return result.address;
|
||||
} catch (err) {
|
||||
throw new Error(`DNS resolution failed for ${hostname}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -2,7 +2,6 @@ const { describe, test, mock } = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const { GameDigMonitorType } = require("../../../server/monitor-types/gamedig");
|
||||
const { UP, PENDING } = require("../../../src/util");
|
||||
const net = require("net");
|
||||
const { GameDig } = require("gamedig");
|
||||
|
||||
describe("GameDig Monitor", () => {
|
||||
@@ -40,11 +39,13 @@ describe("GameDig Monitor", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("check() resolves hostname to IP address when hostname is not an IP", async () => {
|
||||
test("check() passes hostname directly to GameDig when hostname is not an IP", async () => {
|
||||
const gamedigMonitor = new GameDigMonitorType();
|
||||
|
||||
let capturedOptions = null;
|
||||
|
||||
mock.method(GameDig, "query", async (options) => {
|
||||
assert.ok(net.isIP(options.host) !== 0, `Expected IP address, got ${options.host}`);
|
||||
capturedOptions = options;
|
||||
return {
|
||||
name: "Test Server",
|
||||
ping: 50,
|
||||
@@ -66,6 +67,7 @@ describe("GameDig Monitor", () => {
|
||||
try {
|
||||
await gamedigMonitor.check(monitor, heartbeat, {});
|
||||
|
||||
assert.strictEqual(capturedOptions.host, "localhost");
|
||||
assert.strictEqual(heartbeat.status, UP);
|
||||
assert.strictEqual(heartbeat.msg, "Test Server");
|
||||
assert.strictEqual(heartbeat.ping, 50);
|
||||
@@ -74,7 +76,7 @@ describe("GameDig Monitor", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("check() uses IP address directly without DNS resolution when hostname is IPv4", async () => {
|
||||
test("check() passes IPv4 address directly to GameDig", async () => {
|
||||
const gamedigMonitor = new GameDigMonitorType();
|
||||
|
||||
let capturedOptions = null;
|
||||
@@ -109,7 +111,7 @@ describe("GameDig Monitor", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("check() uses IP address directly without DNS resolution when hostname is IPv6", async () => {
|
||||
test("check() passes IPv6 address directly to GameDig", async () => {
|
||||
const gamedigMonitor = new GameDigMonitorType();
|
||||
|
||||
let capturedOptions = null;
|
||||
@@ -233,21 +235,4 @@ describe("GameDig Monitor", () => {
|
||||
|
||||
await assert.rejects(gamedigMonitor.check(monitor, heartbeat, {}), /Error/);
|
||||
});
|
||||
|
||||
test("resolveHostname() returns IP address when given valid hostname", async () => {
|
||||
const gamedigMonitor = new GameDigMonitorType();
|
||||
|
||||
const resolvedIP = await gamedigMonitor.resolveHostname("localhost");
|
||||
|
||||
assert.ok(net.isIP(resolvedIP) !== 0, `Expected valid IP address, got ${resolvedIP}`);
|
||||
});
|
||||
|
||||
test("resolveHostname() rejects when DNS resolution fails for invalid hostname", async () => {
|
||||
const gamedigMonitor = new GameDigMonitorType();
|
||||
|
||||
await assert.rejects(
|
||||
gamedigMonitor.resolveHostname("this-domain-definitely-does-not-exist-12345.invalid"),
|
||||
/DNS resolution failed/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user