Skip to content

Commit

Permalink
fix ping response issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aelew committed Feb 6, 2024
1 parent 38a8f3c commit b49404c
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/server/api/routers/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import isCloudflare from '@authentication/cloudflare-ip';
// @ts-expect-error package has no types
import { getAllRecords } from '@layered/dns-records';
import ky from 'ky';
import ping, { PingResponse } from 'ping';
import ping from 'ping';

import { domainSchema, ipSchema } from '@/app/(tools)/schema';
import { getIPData } from '@/lib/ip';
Expand Down Expand Up @@ -66,26 +66,29 @@ export const lookupRouter = createTRPCRouter({
.json<CertificateInfo[]>();

// Remove duplicates with a set, then sort the subdomains by root domain first and everything else alphabetically
const hosts = [...new Set(certs.map((c) => c.common_name))].sort((a, b) => {
if (a === input.domain) {
return -1;
}
if (b === input.domain) {
return 1;
}
return a.localeCompare(b);
});
const hosts = [...new Set(certs.map((c) => c.common_name))]
.filter((h) => h !== 'sni.cloudflaressl.com')
.sort((a, b) => {
if (a === input.domain) {
return -1;
}
if (b === input.domain) {
return 1;
}
return a.localeCompare(b);
});

const pings = await Promise.allSettled(
hosts.map((h) => ping.promise.probe(h))
);

return pings.filter(assertFulfilled).map((p) => ({
subdomain: p.value.inputHost,
ip: p.value.numeric_host,
cloudflare: p.value.numeric_host
? isCloudflare(p.value.numeric_host)
: false
}));
return pings.filter(assertFulfilled).map((p) => {
const ip = p.value.numeric_host?.replace(')', '');
return {
ip,
subdomain: p.value.inputHost,
cloudflare: ip ? isCloudflare(ip) : false
};
});
})
});

0 comments on commit b49404c

Please sign in to comment.