|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useCopy } from '@/composable/copy'; |
| 3 | +import { queryAllDns, queryWhois, formatDnsRecords, getTypeName, defaultRecordTypes } from './dns-query.service'; |
| 4 | +import type { DnsAnswer, WhoisInfo } from './dns-query.service'; |
| 5 | +
|
| 6 | +const domain = ref('example.com'); |
| 7 | +const isLoading = ref(false); |
| 8 | +const errorMessage = ref(''); |
| 9 | +const answers = ref<DnsAnswer[]>([]); |
| 10 | +const whoisInfo = ref<WhoisInfo | null>(null); |
| 11 | +const whoisLoading = ref(false); |
| 12 | +const hasQueried = ref(false); |
| 13 | +
|
| 14 | +const domainValidationRules = [ |
| 15 | + { |
| 16 | + message: 'Please enter a valid domain name', |
| 17 | + validator: (value: string) => /^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/.test(value.trim()), |
| 18 | + }, |
| 19 | +]; |
| 20 | +
|
| 21 | +const groupedAnswers = computed(() => { |
| 22 | + const groups: { type: string; records: DnsAnswer[] }[] = []; |
| 23 | + const seen = new Map<string, DnsAnswer[]>(); |
| 24 | +
|
| 25 | + for (const answer of answers.value) { |
| 26 | + const typeName = getTypeName(answer.type); |
| 27 | + if (!seen.has(typeName)) { |
| 28 | + const records: DnsAnswer[] = []; |
| 29 | + seen.set(typeName, records); |
| 30 | + groups.push({ type: typeName, records }); |
| 31 | + } |
| 32 | + seen.get(typeName)!.push(answer); |
| 33 | + } |
| 34 | +
|
| 35 | + return groups; |
| 36 | +}); |
| 37 | +
|
| 38 | +const formattedResult = computed(() => formatDnsRecords(answers.value)); |
| 39 | +
|
| 40 | +const { copy } = useCopy({ source: formattedResult, text: 'DNS records copied to the clipboard' }); |
| 41 | +
|
| 42 | +async function doQuery() { |
| 43 | + const trimmed = domain.value.trim(); |
| 44 | + if (!trimmed) { |
| 45 | + return; |
| 46 | + } |
| 47 | +
|
| 48 | + isLoading.value = true; |
| 49 | + errorMessage.value = ''; |
| 50 | + answers.value = []; |
| 51 | + whoisInfo.value = null; |
| 52 | + hasQueried.value = true; |
| 53 | +
|
| 54 | + try { |
| 55 | + answers.value = await queryAllDns(trimmed, defaultRecordTypes); |
| 56 | + } |
| 57 | + catch (err: unknown) { |
| 58 | + errorMessage.value = err instanceof Error ? err.message : 'DNS query failed'; |
| 59 | + } |
| 60 | + finally { |
| 61 | + isLoading.value = false; |
| 62 | + } |
| 63 | +
|
| 64 | + whoisLoading.value = true; |
| 65 | + try { |
| 66 | + whoisInfo.value = await queryWhois(trimmed); |
| 67 | + } |
| 68 | + catch { |
| 69 | + // WHOIS is best-effort |
| 70 | + } |
| 71 | + finally { |
| 72 | + whoisLoading.value = false; |
| 73 | + } |
| 74 | +} |
| 75 | +</script> |
| 76 | + |
| 77 | +<template> |
| 78 | + <div> |
| 79 | + <c-input-text |
| 80 | + v-model:value="domain" |
| 81 | + label="Domain name" |
| 82 | + placeholder="e.g. example.com" |
| 83 | + clearable |
| 84 | + autocomplete="off" |
| 85 | + autocorrect="off" |
| 86 | + autocapitalize="off" |
| 87 | + spellcheck="false" |
| 88 | + :validation-rules="domainValidationRules" |
| 89 | + mb-4 |
| 90 | + /> |
| 91 | + |
| 92 | + <div flex justify-center mb-4> |
| 93 | + <c-button :disabled="!domain.trim() || isLoading" @click="doQuery()"> |
| 94 | + {{ isLoading ? 'Querying...' : 'Query DNS' }} |
| 95 | + </c-button> |
| 96 | + </div> |
| 97 | + |
| 98 | + <n-alert v-if="errorMessage" type="error" mb-4> |
| 99 | + {{ errorMessage }} |
| 100 | + </n-alert> |
| 101 | + |
| 102 | + <div v-if="hasQueried && !isLoading && !errorMessage"> |
| 103 | + <div v-if="groupedAnswers.length > 0"> |
| 104 | + <div v-for="group in groupedAnswers" :key="group.type" mb-4> |
| 105 | + <div mb-2 font-bold text-15px> |
| 106 | + {{ group.type }} |
| 107 | + </div> |
| 108 | + <n-table :bordered="true" :single-line="false" size="small"> |
| 109 | + <thead> |
| 110 | + <tr> |
| 111 | + <th scope="col">Name</th> |
| 112 | + <th scope="col">TTL</th> |
| 113 | + <th scope="col">Data</th> |
| 114 | + </tr> |
| 115 | + </thead> |
| 116 | + <tbody> |
| 117 | + <tr v-for="(answer, index) in group.records" :key="index"> |
| 118 | + <td>{{ answer.name }}</td> |
| 119 | + <td>{{ answer.TTL }}s</td> |
| 120 | + <td style="word-break: break-all;"> |
| 121 | + {{ answer.data }} |
| 122 | + </td> |
| 123 | + </tr> |
| 124 | + </tbody> |
| 125 | + </n-table> |
| 126 | + </div> |
| 127 | + |
| 128 | + <div flex justify-center mb-5> |
| 129 | + <c-button @click="copy()"> |
| 130 | + Copy results |
| 131 | + </c-button> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + |
| 135 | + <c-card v-else mb-4> |
| 136 | + <div italic op-60> |
| 137 | + No records found for this domain. |
| 138 | + </div> |
| 139 | + </c-card> |
| 140 | + </div> |
| 141 | + |
| 142 | + <div v-if="whoisLoading" mt-2 op-60 italic> |
| 143 | + Loading WHOIS info... |
| 144 | + </div> |
| 145 | + |
| 146 | + <div v-if="whoisInfo" mt-2> |
| 147 | + <n-divider /> |
| 148 | + <div mb-3 font-bold text-16px> |
| 149 | + WHOIS |
| 150 | + </div> |
| 151 | + |
| 152 | + <n-table :bordered="true" :single-line="false" size="small"> |
| 153 | + <tbody> |
| 154 | + <tr> |
| 155 | + <td font-500 w-180px> |
| 156 | + Domain Name |
| 157 | + </td> |
| 158 | + <td>{{ whoisInfo.domainName }}</td> |
| 159 | + </tr> |
| 160 | + <tr v-if="whoisInfo.registrar"> |
| 161 | + <td font-500> |
| 162 | + Registrar |
| 163 | + </td> |
| 164 | + <td>{{ whoisInfo.registrar }}</td> |
| 165 | + </tr> |
| 166 | + <tr v-if="whoisInfo.registrationDate"> |
| 167 | + <td font-500> |
| 168 | + Registration Date |
| 169 | + </td> |
| 170 | + <td>{{ whoisInfo.registrationDate }}</td> |
| 171 | + </tr> |
| 172 | + <tr v-if="whoisInfo.expirationDate"> |
| 173 | + <td font-500> |
| 174 | + Expiration Date |
| 175 | + </td> |
| 176 | + <td>{{ whoisInfo.expirationDate }}</td> |
| 177 | + </tr> |
| 178 | + <tr v-if="whoisInfo.updatedDate"> |
| 179 | + <td font-500> |
| 180 | + Updated Date |
| 181 | + </td> |
| 182 | + <td>{{ whoisInfo.updatedDate }}</td> |
| 183 | + </tr> |
| 184 | + <tr v-if="whoisInfo.registrantCountry"> |
| 185 | + <td font-500> |
| 186 | + Registrant |
| 187 | + </td> |
| 188 | + <td>{{ [whoisInfo.registrantProvince, whoisInfo.registrantCountry].filter(Boolean).join(', ') }}</td> |
| 189 | + </tr> |
| 190 | + <tr v-if="whoisInfo.nameServers.length > 0"> |
| 191 | + <td font-500> |
| 192 | + Name Servers |
| 193 | + </td> |
| 194 | + <td>{{ whoisInfo.nameServers.join(', ') }}</td> |
| 195 | + </tr> |
| 196 | + <tr v-if="whoisInfo.status.length > 0"> |
| 197 | + <td font-500> |
| 198 | + Domain Status |
| 199 | + </td> |
| 200 | + <td> |
| 201 | + <div v-for="s in whoisInfo.status" :key="s"> |
| 202 | + {{ s }} |
| 203 | + </div> |
| 204 | + </td> |
| 205 | + </tr> |
| 206 | + <tr v-if="whoisInfo.dnssec"> |
| 207 | + <td font-500> |
| 208 | + DNSSEC |
| 209 | + </td> |
| 210 | + <td>{{ whoisInfo.dnssec }}</td> |
| 211 | + </tr> |
| 212 | + </tbody> |
| 213 | + </n-table> |
| 214 | + </div> |
| 215 | + </div> |
| 216 | +</template> |
0 commit comments