|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { CKeyValueListItems } from '@/ui/c-key-value-list/c-key-value-list.types'; |
| 3 | +
|
| 4 | +const ipOrDomain = ref('8.8.8.8'); |
| 5 | +const errorMessage = ref(''); |
| 6 | +
|
| 7 | +const fields: Array<{ field: string; name: string }> = [ |
| 8 | + { field: 'message', name: 'Message' }, |
| 9 | + { field: 'continent', name: 'Continent Name' }, |
| 10 | + { field: 'continentCode', name: 'Continent code' }, |
| 11 | + { field: 'country', name: 'Country Name' }, |
| 12 | + { field: 'countryCode', name: 'Country Code' }, |
| 13 | + { field: 'region', name: 'Region/state Code' }, |
| 14 | + { field: 'regionName', name: 'Region/state Name' }, |
| 15 | + { field: 'city', name: 'City' }, |
| 16 | + { field: 'district', name: 'District' }, |
| 17 | + { field: 'zip', name: 'Zip Code' }, |
| 18 | + { field: 'lat', name: 'Latitude' }, |
| 19 | + { field: 'lon', name: 'Longitude' }, |
| 20 | + { field: 'timezone', name: 'Timezone' }, |
| 21 | + { field: 'offset', name: 'Timezone UTC DST offset (in seconds)' }, |
| 22 | + { field: 'currency', name: 'National Currency' }, |
| 23 | + { field: 'isp', name: 'ISP Name' }, |
| 24 | + { field: 'org', name: 'Organization Name' }, |
| 25 | + { field: 'as', name: 'AS Number and Organization' }, |
| 26 | + { field: 'asname', name: 'AS name (RIR)' }, |
| 27 | + { field: 'reverse', name: 'Reverse DNS of the IP' }, |
| 28 | + { field: 'mobile', name: 'Mobile (cellular) connection' }, |
| 29 | + { field: 'proxy', name: 'Proxy, VPN or Tor exit address' }, |
| 30 | + { field: 'hosting', name: 'Hosting, colocated or data center' }, |
| 31 | + { field: 'query', name: 'IP used for the query' }, |
| 32 | +]; |
| 33 | +
|
| 34 | +const geoInfos = ref<CKeyValueListItems>([]); |
| 35 | +const geoInfosData = ref<any>({}); |
| 36 | +const status = ref<'pending' | 'error' | 'success'>('pending'); |
| 37 | +
|
| 38 | +const openStreetMapUrl = computed( |
| 39 | + () => { |
| 40 | + const gpsLatitude = geoInfosData.value.lat; |
| 41 | + const gpsLongitude = geoInfosData.value.lon; |
| 42 | + return gpsLatitude && gpsLongitude ? `https://www.openstreetmap.org/?mlat=${gpsLatitude}&mlon=${gpsLongitude}#map=18/${gpsLatitude}/${gpsLongitude}` : undefined; |
| 43 | + }, |
| 44 | +); |
| 45 | +
|
| 46 | +async function onGetInfos() { |
| 47 | + try { |
| 48 | + status.value = 'pending'; |
| 49 | +
|
| 50 | + const geoInfoQueryResponse = await fetch(`http://ip-api.com/json/${ipOrDomain.value}`); |
| 51 | + if (!geoInfoQueryResponse.ok) { |
| 52 | + throw geoInfoQueryResponse.statusText; |
| 53 | + } |
| 54 | +
|
| 55 | + const data = await geoInfoQueryResponse.json(); |
| 56 | +
|
| 57 | + const allGeoInfos = []; |
| 58 | + for (const field of fields) { |
| 59 | + if (data[field.field]) { |
| 60 | + allGeoInfos.push({ |
| 61 | + label: field.name, |
| 62 | + value: data[field.field], |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + status.value = 'success'; |
| 68 | + geoInfos.value = allGeoInfos; |
| 69 | + geoInfosData.value = data; |
| 70 | + } |
| 71 | + catch (e: any) { |
| 72 | + errorMessage.value = e.toString(); |
| 73 | + status.value = 'error'; |
| 74 | + return []; |
| 75 | + } |
| 76 | +} |
| 77 | +</script> |
| 78 | + |
| 79 | +<template> |
| 80 | + <div> |
| 81 | + <div flex items-center gap-2> |
| 82 | + <c-input-text |
| 83 | + v-model:value="ipOrDomain" |
| 84 | + placeholder="Enter an IPv4/6 or a domain name" |
| 85 | + @update:value="() => { status = 'pending' }" |
| 86 | + /> |
| 87 | + <c-button align-center @click="onGetInfos"> |
| 88 | + Get GEO Location Infos |
| 89 | + </c-button> |
| 90 | + </div> |
| 91 | + |
| 92 | + <n-divider /> |
| 93 | + |
| 94 | + <c-card v-if="status === 'pending'" mt-5> |
| 95 | + Click on button above to get latest infos |
| 96 | + </c-card> |
| 97 | + |
| 98 | + <c-card v-if="status === 'success' && openStreetMapUrl" mt-4> |
| 99 | + <c-button :href="openStreetMapUrl" target="_blank"> |
| 100 | + Localize on Open Street Map |
| 101 | + </c-button> |
| 102 | + </c-card> |
| 103 | + |
| 104 | + <c-card v-if="status === 'success'" mt-5> |
| 105 | + <c-key-value-list :items="geoInfos" /> |
| 106 | + </c-card> |
| 107 | + |
| 108 | + <n-alert v-if="status === 'error'" title="Errors occured" type="error" mt-5> |
| 109 | + {{ errorMessage }} |
| 110 | + </n-alert> |
| 111 | + </div> |
| 112 | +</template> |
0 commit comments