feat(My IP): show IPv4, IPv6 and country

Fix #1535
This commit is contained in:
ShareVB committed 2025-03-17 23:19:14 +01:00
1 parent 91541b8f7c
commit f3814e9a5b
5 files changed
+248 -32

No files matched your search

+3 -6
View File
@@ -64,6 +64,7 @@ declare module '@vue/runtime-core' {
'CModalValue.demo': typeof import('./src/ui/c-modal-value/c-modal-value.demo.vue')['default']
CMonacoEditor: typeof import('./src/ui/c-monaco-editor/c-monaco-editor.vue')['default']
CodeHighlighter: typeof import('./src/tools/code-highlighter/code-highlighter.vue')['default']
CoinFlipper: typeof import('./src/tools/coin-flipper/coin-flipper.vue')['default']
CollapsibleToolMenu: typeof import('./src/components/CollapsibleToolMenu.vue')['default']
ColorConverter: typeof import('./src/tools/color-converter/color-converter.vue')['default']
ColoredCard: typeof import('./src/components/ColoredCard.vue')['default']
@@ -128,6 +129,7 @@ declare module '@vue/runtime-core' {
FolderStructureDiagram: typeof import('./src/tools/folder-structure-diagram/folder-structure-diagram.vue')['default']
ForceConverter: typeof import('./src/tools/force-converter/force-converter.vue')['default']
FormatTransformer: typeof import('./src/components/FormatTransformer.vue')['default']
FortuneWheel: typeof import('./src/tools/fortune-wheel/fortune-wheel.vue')['default']
GeoDistanceCalculator: typeof import('./src/tools/geo-distance-calculator/geo-distance-calculator.vue')['default']
GitMemo: typeof import('./src/tools/git-memo/git-memo.vue')['default']
'GitMemo.content': typeof import('./src/tools/git-memo/git-memo.content.md')['default']
@@ -255,24 +257,19 @@ declare module '@vue/runtime-core' {
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NFormItem: typeof import('naive-ui')['NFormItem']
NginxFormatter: typeof import('./src/tools/nginx-formatter/nginx-formatter.vue')['default']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NSlider: typeof import('naive-ui')['NSlider']
NSpace: typeof import('naive-ui')['NSpace']
NSwitch: typeof import('naive-ui')['NSwitch']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OcrImage: typeof import('./src/tools/ocr-image/ocr-image.vue')['default']
Option43Generator: typeof import('./src/tools/option43-generator/option43-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
OutlookParser: typeof import('./src/tools/outlook-parser/outlook-parser.vue')['default']
ParquetsReader: typeof import('./src/tools/parquets-reader/parquets-reader.vue')['default']
PassphraseGenerator: typeof import('./src/tools/passphrase-generator/passphrase-generator.vue')['default']
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
PasteAsMarkdown: typeof import('./src/tools/paste-as-markdown/paste-as-markdown.vue')['default']
+6 -5
View File
@@ -39,11 +39,12 @@
"dependencies": {
"@chenfengyuan/vue-barcode": "^2.0.2",
"@date-fns/utc": "^1.2.0",
"@dice-roller/rpg-dice-roller": "^5.5.1",
"@github/paste-markdown": "^1.5.3",
"@guolao/vue-monaco-editor": "^1.5.4",
"@guyplusplus/turndown-plugin-gfm": "^1.0.7",
"@huggingface/transformers": "3.0.0-alpha.14",
"@dice-roller/rpg-dice-roller": "^5.5.1",
"@iplookup/country-extra": "^1.0.20250317",
"@it-tools/bip39": "^0.0.4",
"@it-tools/oggen": "^1.3.0",
"@kenjiuno/decompressrtf": "^0.1.4",
@@ -71,17 +72,17 @@
"@vueuse/core": "^10.11.1",
"@vueuse/head": "^1.0.0",
"@vueuse/router": "^10.0.0",
"@younestouati/playing-cards-standard-deck": "^6.0.1",
"@xmldom/xmldom": "^0.8.10",
"@younestouati/playing-cards-standard-deck": "^6.0.1",
"@zxing/library": "^0.21.3",
"aircodes": "^1.3.16",
"ansible-vault": "^1.3.0",
"apache-md5": "^1.1.8",
"arr-diff": "^4.0.0",
"bcryptjs": "^2.4.3",
"byte-data": "^19.0.1",
"big.js": "^6.2.2",
"bwip-js": "^4.5.1",
"byte-data": "^19.0.1",
"change-case": "^5.4.4",
"chatgpt-prompt-splitter": "^1.0.5",
"chinesegen": "^0.3.3",
@@ -280,9 +281,9 @@
"@types/dompurify": "^3.2.0",
"@types/emscripten": "^1.40.0",
"@types/figlet": "^1.7.0",
"@types/js-beautify": "^1.14.3",
"@types/hex-array": "^1.0.2",
"@types/har-format": "^1.2.16",
"@types/hex-array": "^1.0.2",
"@types/js-beautify": "^1.14.3",
"@types/jsdom": "^21.1.7",
"@types/jsonpath": "^0.2.4",
"@types/libmime": "^5.0.3",
+7
View File
@@ -26,6 +26,9 @@ dependencies:
'@huggingface/transformers':
specifier: 3.0.0-alpha.14
version: 3.0.0-alpha.14
'@iplookup/country-extra':
specifier: ^1.0.20250317
version: 1.0.20250317
'@it-tools/bip39':
specifier: ^0.0.4
version: 0.0.4
@@ -3645,6 +3648,10 @@ packages:
- supports-color
dev: true
/@iplookup/country-extra@1.0.20250317:
resolution: {integrity: sha512-pKzMGz7oAX1GmFqLw8hLS4A66iouzY6Mjk5HoI0mVgFFnxYgmi2YZQHRuBMvUlFg40gMo7dWeCfMfxd5HSWeQg==}
dev: false
/@isaacs/cliui@8.0.2:
resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==}
engines: {node: '>=12'}
+168
View File
@@ -0,0 +1,168 @@
window.IpLookup = (function () {
'use strict';
const aton4 = (a) => {
a = a.split(/\./);
return (a[0] << 24 | a[1] << 16 | a[2] << 8 | a[3]) >>> 0
};
const aton6Start = (a) => {
if(a.includes('.')){
return aton4(a.split(':').pop())
}
a = a.split(/:/);
const l = a.length - 1;
var i, r = 0n;
if (l < 7) {
const omitStart = a.indexOf('');
if(omitStart < 4){
const omitted = 8 - a.length, omitEnd = omitStart + omitted;
for (i = 7; i >= omitStart; i--) {
a[i] = i > omitEnd ? a[i - omitted] : 0;
}
}
}
for (i = 0; i < 4; i++) {
if(a[i]) r += BigInt(parseInt(a[i], 16)) << BigInt(16 * (3 - i));
}
return r
};
const getUnderberFill = (num, len) => {
if(num.length > len) return num
return '_'.repeat(len - num.length) + num
};
const numberToDir = (num) => {
return getUnderberFill(num.toString(36), 2)
};
const TOP_URL = 'https://cdn.jsdelivr.net/npm/@iplookup/country-extra/';
const MAIN_RECORD_SIZE = 2 ;
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const downloadArrayBuffer = async(url, retry = 3) => {
return fetch(url, {cache: 'no-cache'}).then(async (res) => {
if(!res.ok) {
if(res.status === 404) return null
if(retry) {
await sleep(100 * (4-retry) * (4-retry));
return downloadArrayBuffer(url, retry - 1)
}
return null
}
return res.arrayBuffer()
})
};
const downloadIdx = downloadArrayBuffer;
const Idx = {}, Url = {4: TOP_URL, 6: TOP_URL};
const Preload = {
4: downloadIdx(TOP_URL + '4.idx').then(buf => {
if(!buf){
// console.log('ipv6 file cannot download')
return
}
return Idx[4] = new Uint32Array(buf)
}),
6: downloadIdx(TOP_URL + '4.idx').then(buf => {
if(!buf){
// console.log('ipv6 file cannot download')
return
}
return Idx[6] = new BigUint64Array(buf)
})
};
var ip_lookup = async (ipString) => {
var ip, version, isv4 = true;
if(ipString.includes(':')) {
ip = aton6Start(ipString);
version = ip.constructor === BigInt ? 6 : 4;
if(version === 6) isv4 = false;
} else {
ip = aton4(ipString);
version = 4;
}
const ipIndexes = Idx[version] || (await Preload[version]);
if(!ipIndexes) {
// console.log('Cannot download idx file')
return null
}
if(!(ip >= ipIndexes[0])) return null
var fline = 0, cline = ipIndexes.length-1, line;
for(;;){
line = (fline + cline) >> 1;
if(ip < ipIndexes[line]){
if(cline - fline < 2) return null
cline = line - 1;
} else {
if(fline === line) {
if(cline > line && ip >= ipIndexes[cline]){
line = cline;
}
break;
}
fline = line;
}
}
const fileName = numberToDir(line);
const dataBuffer = await downloadArrayBuffer(Url[version] + version + '/' + fileName);
if(!dataBuffer) {
// console.log('Cannot download data file')
return null
}
const ipSize = (version - 2) * 2;
const recordSize = MAIN_RECORD_SIZE + ipSize * 2;
const recordCount = dataBuffer.byteLength / recordSize;
const startList = isv4 ? new Uint32Array(dataBuffer.slice(0, 4 * recordCount)) : new BigUint64Array(dataBuffer.slice(0, 8 * recordCount));
fline = 0, cline = recordCount - 1;
for(;;){
line = fline + cline >> 1;
if(ip < startList[line]){
if(cline - fline < 2) return null
cline = line - 1;
} else {
if(fline === line) {
if(cline > line && ip >= startList[cline]){
line = cline;
}
break;
}
fline = line;
}
}
const endIp = isv4 ? new Uint32Array( dataBuffer.slice((recordCount+line)*ipSize , (recordCount+line+1)*ipSize))[0]
: new BigUint64Array(dataBuffer.slice((recordCount+line)*ipSize , (recordCount+line+1)*ipSize))[0];
if(ip >= startList[line] && ip <= endIp){
{
const ccCode = new Uint16Array(dataBuffer.slice(recordCount*ipSize*2+line*MAIN_RECORD_SIZE, recordCount*ipSize*2+(line+1)*MAIN_RECORD_SIZE))[0];
return {country: String.fromCharCode(ccCode&255, ccCode>>8)}
}
}
return null
};
/*! countries-list v3.1.1 by Annexare | MIT */
var a={AD:{name:"Andorra",native:"Andorra",phone:[376],continent:"EU",capital:"Andorra la Vella",currency:["EUR"],languages:["ca"]},AE:{name:"United Arab Emirates",native:"\u062F\u0648\u0644\u0629 \u0627\u0644\u0625\u0645\u0627\u0631\u0627\u062A \u0627\u0644\u0639\u0631\u0628\u064A\u0629 \u0627\u0644\u0645\u062A\u062D\u062F\u0629",phone:[971],continent:"AS",capital:"Abu Dhabi",currency:["AED"],languages:["ar"]},AF:{name:"Afghanistan",native:"\u0627\u0641\u063A\u0627\u0646\u0633\u062A\u0627\u0646",phone:[93],continent:"AS",capital:"Kabul",currency:["AFN"],languages:["ps","uz","tk"]},AG:{name:"Antigua and Barbuda",native:"Antigua and Barbuda",phone:[1268],continent:"NA",capital:"Saint John's",currency:["XCD"],languages:["en"]},AI:{name:"Anguilla",native:"Anguilla",phone:[1264],continent:"NA",capital:"The Valley",currency:["XCD"],languages:["en"]},AL:{name:"Albania",native:"Shqip\xEBria",phone:[355],continent:"EU",capital:"Tirana",currency:["ALL"],languages:["sq"]},AM:{name:"Armenia",native:"\u0540\u0561\u0575\u0561\u057D\u057F\u0561\u0576",phone:[374],continent:"AS",capital:"Yerevan",currency:["AMD"],languages:["hy","ru"]},AO:{name:"Angola",native:"Angola",phone:[244],continent:"AF",capital:"Luanda",currency:["AOA"],languages:["pt"]},AQ:{name:"Antarctica",native:"Antarctica",phone:[672],continent:"AN",capital:"",currency:[],languages:[]},AR:{name:"Argentina",native:"Argentina",phone:[54],continent:"SA",capital:"Buenos Aires",currency:["ARS"],languages:["es","gn"]},AS:{name:"American Samoa",native:"American Samoa",phone:[1684],continent:"OC",capital:"Pago Pago",currency:["USD"],languages:["en","sm"]},AT:{name:"Austria",native:"\xD6sterreich",phone:[43],continent:"EU",capital:"Vienna",currency:["EUR"],languages:["de"]},AU:{name:"Australia",native:"Australia",phone:[61],continent:"OC",capital:"Canberra",currency:["AUD"],languages:["en"]},AW:{name:"Aruba",native:"Aruba",phone:[297],continent:"NA",capital:"Oranjestad",currency:["AWG"],languages:["nl","pa"]},AX:{name:"Aland",native:"\xC5land",phone:[358],continent:"EU",capital:"Mariehamn",currency:["EUR"],languages:["sv"],partOf:"FI"},AZ:{name:"Azerbaijan",native:"Az\u0259rbaycan",phone:[994],continent:"AS",continents:["AS","EU"],capital:"Baku",currency:["AZN"],languages:["az"]},BA:{name:"Bosnia and Herzegovina",native:"Bosna i Hercegovina",phone:[387],continent:"EU",capital:"Sarajevo",currency:["BAM"],languages:["bs","hr","sr"]},BB:{name:"Barbados",native:"Barbados",phone:[1246],continent:"NA",capital:"Bridgetown",currency:["BBD"],languages:["en"]},BD:{name:"Bangladesh",native:"Bangladesh",phone:[880],continent:"AS",capital:"Dhaka",currency:["BDT"],languages:["bn"]},BE:{name:"Belgium",native:"Belgi\xEB",phone:[32],continent:"EU",capital:"Brussels",currency:["EUR"],languages:["nl","fr","de"]},BF:{name:"Burkina Faso",native:"Burkina Faso",phone:[226],continent:"AF",capital:"Ouagadougou",currency:["XOF"],languages:["fr","ff"]},BG:{name:"Bulgaria",native:"\u0411\u044A\u043B\u0433\u0430\u0440\u0438\u044F",phone:[359],continent:"EU",capital:"Sofia",currency:["BGN"],languages:["bg"]},BH:{name:"Bahrain",native:"\u200F\u0627\u0644\u0628\u062D\u0631\u064A\u0646",phone:[973],continent:"AS",capital:"Manama",currency:["BHD"],languages:["ar"]},BI:{name:"Burundi",native:"Burundi",phone:[257],continent:"AF",capital:"Bujumbura",currency:["BIF"],languages:["fr","rn"]},BJ:{name:"Benin",native:"B\xE9nin",phone:[229],continent:"AF",capital:"Porto-Novo",currency:["XOF"],languages:["fr"]},BL:{name:"Saint Barthelemy",native:"Saint-Barth\xE9lemy",phone:[590],continent:"NA",capital:"Gustavia",currency:["EUR"],languages:["fr"]},BM:{name:"Bermuda",native:"Bermuda",phone:[1441],continent:"NA",capital:"Hamilton",currency:["BMD"],languages:["en"]},BN:{name:"Brunei",native:"Negara Brunei Darussalam",phone:[673],continent:"AS",capital:"Bandar Seri Begawan",currency:["BND"],languages:["ms"]},BO:{name:"Bolivia",native:"Bolivia",phone:[591],continent:"SA",capital:"Sucre",currency:["BOB","BOV"],languages:["es","ay","qu"]},BQ:{name:"Bonaire",native:"Bonaire",phone:[5997],continent:"NA",capital:"Kralendijk",currency:["USD"],languages:["nl"]},BR:{name:"Brazil",native:"Brasil",phone:[55],continent:"SA",capital:"Bras\xEDlia",currency:["BRL"],languages:["pt"]},BS:{name:"Bahamas",native:"Bahamas",phone:[1242],continent:"NA",capital:"Nassau",currency:["BSD"],languages:["en"]},BT:{name:"Bhutan",native:"\u02BCbrug-yul",phone:[975],continent:"AS",capital:"Thimphu",currency:["BTN","INR"],languages:["dz"]},BV:{name:"Bouvet Island",native:"Bouvet\xF8ya",phone:[47],continent:"AN",capital:"",currency:["NOK"],languages:["no","nb","nn"]},BW:{name:"Botswana",native:"Botswana",phone:[267],continent:"AF",capital:"Gaborone",currency:["BWP"],languages:["en","tn"]},BY:{name:"Belarus",native:"\u0411\u0435\u043B\u0430\u0440\u0443\u0301\u0441\u044C",phone:[375],continent:"EU",capital:"Minsk",currency:["BYN"],languages:["be","ru"]},BZ:{name:"Belize",native:"Belize",phone:[501],continent:"NA",capital:"Belmopan",currency:["BZD"Line truncated
var browserExtra = async(ip) => {
const geodata = await ip_lookup(ip);
if(geodata && a[geodata.country]){
const h = a[geodata.country];
geodata.country_name = h.name;
geodata.country_native = h.native;
geodata.continent = h.continent;
geodata.capital = h.capital;
geodata.phone = h.phone;
geodata.currency = h.currency;
geodata.languages = h.languages;
}
return geodata
};
return browserExtra;
})();
+64 -21
View File
@@ -1,42 +1,85 @@
<script setup lang="ts">
import { useScriptTag } from '@vueuse/core';
import { computedRefreshableAsync } from '@/composable/computedRefreshable';
import { useCopy } from '@/composable/copy';
const [clientIP, refreshClientIP] = computedRefreshableAsync(async () => {
declare global {
interface Window {
IpLookup: (ip: string) => Promise<{
country: string
country_name: string
country_native: string
continent: string
continent_name: string
}>
}
}
const { load: loadIpLookup } = useScriptTag('/iplookup.js', undefined, { type: 'module', manual: true });
const [clientIPDetails, refreshClientIP] = computedRefreshableAsync(async () => {
const ipv4 = { ip: '', error: '' };
const ipv6 = { ip: '', error: '' };
const location = {
country: {
country: '',
country_name: '',
country_native: '',
continent: '',
continent_name: '',
},
error: '',
};
try {
return (await (await fetch('https://api64.ipify.org?format=json', {
ipv4.ip = (await (await fetch('https://api4.ipify.org?format=json', {
mode: 'cors',
})).json()).ip?.toString();
}
catch (e: any) {
return e.toString();
ipv4.error = `Not detected (${e.toString()})`;
}
try {
ipv6.ip = (await (await fetch('https://api6.ipify.org?format=json', {
mode: 'cors',
})).json()).ip?.toString();
}
catch (e: any) {
ipv6.error = `Not detected (${e.toString()})`;
}
});
const { copy } = useCopy({ source: clientIP, text: 'Client IP copied to the clipboard' });
await loadIpLookup();
try {
location.country = await window.IpLookup(ipv6.ip || ipv4.ip);
}
catch (e: any) {
location.error = e.toString();
}
return {
ipv4: ipv4.ip || ipv4.error,
ipv6: ipv6.ip || ipv6.error,
location: {
country: location.country?.country || `ERROR: ${location.error || 'Unknown'}`,
country_name: location.country ? `${location.country?.country_name}/${location.country?.country_native}` : '',
continent: location.country ? location.country?.continent : '',
},
};
});
</script>
<template>
<c-card title="Your IPv4/6 address">
<div class="ip">
{{ clientIP }}
<c-card title="Your IPv4/6 address details">
<div v-if="clientIPDetails">
<input-copyable v-model:value="clientIPDetails.ipv4" label-position="left" label-width="100px" label-align="right" readonly label="IPv4:" placeholder="Your IPv4" />
<input-copyable v-model:value="clientIPDetails.ipv6" label-position="left" label-width="100px" label-align="right" readonly label="IPv6:" placeholder="Your IPv6" />
<input-copyable v-model:value="clientIPDetails.location.country" label-position="left" label-width="100px" label-align="right" readonly label="Country:" />
<input-copyable v-model:value="clientIPDetails.location.country_name" label-position="left" label-width="100px" label-align="right" readonly label="Country Name:" />
<input-copyable v-model:value="clientIPDetails.location.continent" label-position="left" label-width="100px" label-align="right" readonly label="Continent Name:" />
</div>
<div flex justify-center gap-3>
<c-button @click="copy()">
Copy
</c-button>
<c-button @click="refreshClientIP">
Refresh
</c-button>
</div>
</c-card>
</template>
<style lang="less" scoped>
.ip {
text-align: center;
font-size: 26px;
font-weight: 400;
margin: 10px 0 25px;
}
</style>