Repository: louislam/uptime-kuma
File: server/monitor-types/dns.js (JavaScript)

Code before:
        if (!conditionsResult) {
            throw new Error(dnsMessage);
        }

        heartbeat.msg = dnsMessage;
        heartbeat.status = UP;
    }

    /**
     * Parses a comma-separated list of DNS resolver servers and resolves any hostnames
     * to their corresponding IPv4 and/or IPv6 addresses.
     *
     * We are primarily doing this to support hostnames of docker containers like adguard.
     *
     * - Whitespace is removed from the input string
     * - Empty entries are ignored
     * - IP literals (IPv4 / IPv6) are accepted as-is
     * - Hostnames are resolved to both A and AAAA records in parallel
     * - Invalid or unresolvable entries are logged and skipped
     * @param {string} dnsResolveServer - Comma-separated list of resolver servers (IPs or hostnames)
     * @returns {Promise<Array<string>>} A set of resolved IP addresses
     * @throws {Error} If no valid resolver servers could be parsed or resolved
     */
    async resolveDnsResolverServers(dnsResolveServer) {
        // Remove all spaces, split into array, remove all elements that are empty
        const addresses = dnsResolveServer.replace(/\s/g, "").split(",").filter((x) => x !== "");
        const resolver = new Resolver();
        const parsed = new Set();

        for (const e of addresses) {
            if (net.isIP(e)) { // If IPv4 or IPv6 addr, just add and go to next element
                parsed.add(e);
                continue;
            }

            // Otherwise, attempt to resolve hostname
            const [ v4, v6 ] = await Promise.allSettled([
                resolver.resolve4(e),
                resolver.resolve6(e),
            ]);

            const addrs = [
                ...(v4.status === "fulfilled" ? v4.value : []),
                ...(v6.status === "fulfilled" ? v6.value : []),
            ];

            if (!addrs.length) {
                log.error("DNS", `Invalid resolver server ${e}`);
                continue;
            }


Review comment (suggestion):
```suggestion
        const addresses = dnsResolveServer.replace(/\s/g, "").split(",").filter((x) => x !== "");
        if (!addresses.length) {
            throw new Error("No Resolver Servers specified. Please specifiy at least one resolver server like 1.1.1.1 or a hostname");
        }
```

Code after:
        if (!conditionsResult) {
            throw new Error(dnsMessage);
        }

        heartbeat.msg = dnsMessage;
        heartbeat.status = UP;
    }

    /**
     * Parses a comma-separated list of DNS resolver servers and resolves any hostnames
     * to their corresponding IPv4 and/or IPv6 addresses.
     *
     * We are primarily doing this to support hostnames of docker containers like adguard.
     *
     * - Whitespace is removed from the input string
     * - Empty entries are ignored
     * - IP literals (IPv4 / IPv6) are accepted as-is
     * - Hostnames are resolved to both A and AAAA records in parallel
     * - Invalid or unresolvable entries are logged and skipped
     * @param {string} dnsResolveServer - Comma-separated list of resolver servers (IPs or hostnames)
     * @returns {Promise<Array<string>>} Array of resolved IP addresses
     * @throws {Error} If no valid resolver servers could be parsed or resolved
     */
    async resolveDnsResolverServers(dnsResolveServer) {
        // Remove all spaces, split into array, remove all elements that are empty
        const addresses = dnsResolveServer.replace(/\s/g, "").split(",").filter((x) => x !== "");
        if (!addresses.length) {
            throw new Error("No Resolver Servers specified. Please specifiy at least one resolver server like 1.1.1.1 or a hostname");
        }
        const resolver = new Resolver();

        // Make promises to be resolved concurrently
        const promises = addresses.map(async (e) => {
            if (net.isIP(e)) { // If IPv4 or IPv6 addr, immediately return
                return [ e ];
            }

            // Otherwise, attempt to resolve hostname
            const [ v4, v6 ] = await Promise.allSettled([
                resolver.resolve4(e),
                resolver.resolve6(e),
            ]);

            const addrs = [
                ...(v4.status === "fulfilled" ? v4.value : []),
                ...(v6.status === "fulfilled" ? v6.value : []),
            ];

            if (!addrs.length) {
                log.error("DNS", `Invalid resolver server ${e}`);
            }