|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useStorage } from '@vueuse/core'; |
| 3 | +import { Address6 } from 'ip-address'; |
| 4 | +import { getIPNetworkType, parseAsCIDR } from '@/utils/ip'; |
| 5 | +import { withDefaultOnError } from '@/utils/defaults'; |
| 6 | +import SpanCopyable from '@/components/SpanCopyable.vue'; |
| 7 | +import { isNotThrowing } from '@/utils/boolean'; |
| 8 | +
|
| 9 | +const ip = useStorage('ipv6-subnet-calculator:ip', '2001:db8:0:85a3:0:0:ac1f:8001/32'); // NOSONAR |
| 10 | +
|
| 11 | +const getNetworkInfo = (address: string) => new Address6(parseAsCIDR(address.trim()) || address.trim()); |
| 12 | +const networkInfo = computed(() => withDefaultOnError(() => getNetworkInfo(ip.value), undefined)); |
| 13 | +
|
| 14 | +const ipValidationRules = [ |
| 15 | + { |
| 16 | + message: 'We cannot parse this address, check the format', |
| 17 | + validator: (value: string) => isNotThrowing(() => getNetworkInfo(value)), |
| 18 | + }, |
| 19 | +]; |
| 20 | +
|
| 21 | +const sections: { |
| 22 | + label: string |
| 23 | + getValue: (blocks: Address6) => string | undefined |
| 24 | + undefinedFallback?: string |
| 25 | +}[] = [ |
| 26 | + { |
| 27 | + label: 'Full address', |
| 28 | + getValue: (block: Address6) => block.canonicalForm(), |
| 29 | + }, |
| 30 | + { |
| 31 | + label: 'Short address', |
| 32 | + getValue: (block: Address6) => block.correctForm(), |
| 33 | + }, |
| 34 | + { |
| 35 | + label: 'Address as binary', |
| 36 | + getValue: (block: Address6) => (block.binaryZeroPad()).match(/.{8}/g)?.join(':') ?? '', |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'Address as integer', |
| 40 | + getValue: (block: Address6) => BigInt(`0x${block.getBitsBase16(0, 128)}`).toString(), |
| 41 | + }, |
| 42 | + { |
| 43 | + label: 'Address as decimal', |
| 44 | + getValue: (block: Address6) => block.decimal(), |
| 45 | + }, |
| 46 | + { |
| 47 | + label: 'Address as hex', |
| 48 | + getValue: (block: Address6) => (block.getBitsBase16(0, 128)), |
| 49 | + }, |
| 50 | + { |
| 51 | + label: 'Network mask size', |
| 52 | + getValue: (block: Address6) => block.subnetMask.toString(), |
| 53 | + }, |
| 54 | + { |
| 55 | + label: 'Network mask', |
| 56 | + getValue: (block: Address6) => BigInt(`0b${'1'.repeat(block.subnetMask).padEnd(128, '0')}`).toString(16).match(/.{4}/g)?.join(':') ?? '', |
| 57 | + }, |
| 58 | + { |
| 59 | + label: 'Network mask as integer', |
| 60 | + getValue: (block: Address6) => BigInt(`0b${'1'.repeat(block.subnetMask).padEnd(128, '0')}`).toString(), |
| 61 | + }, |
| 62 | + { |
| 63 | + label: 'Network mask as binary', |
| 64 | + getValue: (block: Address6) => '1'.repeat(block.subnetMask).padEnd(128, '0').match(/.{8}/g)?.join(':') ?? '', |
| 65 | + }, |
| 66 | + { |
| 67 | + label: 'Total IP addresses', |
| 68 | + getValue: (block: Address6) => { |
| 69 | + const totalAddresses = BigInt(2) ** BigInt(128 - block.subnetMask); |
| 70 | + return totalAddresses.toString(); |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + label: 'Total networks', |
| 75 | + getValue: ({ subnetMask }) => subnetMask <= 64 ? (BigInt(2) ** BigInt(64 - subnetMask)).toString() : '', |
| 76 | + }, |
| 77 | + { |
| 78 | + label: 'First address', |
| 79 | + getValue: (block: Address6) => block.startAddress().correctForm(), |
| 80 | + }, |
| 81 | + { |
| 82 | + label: 'Last address', |
| 83 | + getValue: (block: Address6) => block.endAddress().correctForm(), |
| 84 | + }, |
| 85 | + { |
| 86 | + label: 'Scope', |
| 87 | + getValue: (block: Address6) => block.getScope(), |
| 88 | + }, |
| 89 | + { |
| 90 | + label: '6to4 Properties', |
| 91 | + getValue: (block: Address6) => JSON.stringify(block.inspect6to4()), |
| 92 | + }, |
| 93 | + { |
| 94 | + label: 'Teredo Properties', |
| 95 | + getValue: (block: Address6) => JSON.stringify(block.inspectTeredo()), |
| 96 | + }, |
| 97 | + { |
| 98 | + label: 'ARPA', |
| 99 | + getValue: (block: Address6) => block.reverseForm(), |
| 100 | + }, |
| 101 | + { |
| 102 | + label: 'Microsoft UNC Transcription', |
| 103 | + getValue: (block: Address6) => block.microsoftTranscription(), |
| 104 | + }, |
| 105 | + { |
| 106 | + label: 'Type', |
| 107 | + getValue: (block: Address6) => getIPNetworkType(block.correctForm()), |
| 108 | + }, |
| 109 | +]; |
| 110 | +</script> |
| 111 | + |
| 112 | +<template> |
| 113 | + <div w-600> |
| 114 | + <c-input-text |
| 115 | + v-model:value="ip" |
| 116 | + label="An IPv6 address with or without mask" |
| 117 | + placeholder="The ipv6 address..." |
| 118 | + :validation-rules="ipValidationRules" |
| 119 | + mb-4 |
| 120 | + /> |
| 121 | + |
| 122 | + <div v-if="networkInfo"> |
| 123 | + <n-table> |
| 124 | + <tbody> |
| 125 | + <tr v-for="{ getValue, label, undefinedFallback } in sections" :key="label"> |
| 126 | + <td font-bold> |
| 127 | + {{ label }} |
| 128 | + </td> |
| 129 | + <td> |
| 130 | + <SpanCopyable v-if="networkInfo" :value="getValue(networkInfo)" /> |
| 131 | + <span v-else op-70> |
| 132 | + {{ undefinedFallback }} |
| 133 | + </span> |
| 134 | + </td> |
| 135 | + </tr> |
| 136 | + </tbody> |
| 137 | + </n-table> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | +</template> |
0 commit comments