|
| 1 | +<script setup lang="ts"> |
| 2 | +import { merge, parse } from 'cidr-tools'; |
| 3 | +import { isIP, isIPv6 } from 'is-ip'; |
| 4 | +import { Exchange } from '@vicons/tabler'; |
| 5 | +import { stringifyIp } from 'ip-bigint'; |
| 6 | +import { useValidation } from '@/composable/validation'; |
| 7 | +
|
| 8 | +const rawStartAddress = useStorage('ip-range-to-cidr:startAddress', '192.168.1.1'); |
| 9 | +const rawEndAddress = useStorage('ip-range-to-cidr:endAddress', '192.168.6.255'); |
| 10 | +
|
| 11 | +const isReversed = ref<boolean>(false); |
| 12 | +const isNotSameVersion = ref<boolean>(false); |
| 13 | +
|
| 14 | +const result = computed(() => { |
| 15 | + try { |
| 16 | + isNotSameVersion.value = isIPv6(rawEndAddress.value) !== isIPv6(rawStartAddress.value); |
| 17 | + isReversed.value = false; |
| 18 | + if (isNotSameVersion.value) { |
| 19 | + return []; |
| 20 | + } |
| 21 | +
|
| 22 | + const startIp = parse(rawStartAddress.value).start; |
| 23 | + const endIp = parse(rawEndAddress.value).end; |
| 24 | + isReversed.value = startIp > endIp; |
| 25 | + if (isReversed.value) { |
| 26 | + return []; |
| 27 | + } |
| 28 | +
|
| 29 | + const version = isIPv6(rawStartAddress.value) ? 6 : 4; |
| 30 | + const ipRangeLength = endIp - startIp + 1n; |
| 31 | + const allIps = new BigInt64Array(Number(ipRangeLength)); |
| 32 | + let iterIp = startIp; |
| 33 | + for (let i = 0; i < ipRangeLength; i++) { |
| 34 | + allIps[i] = iterIp++; |
| 35 | + } |
| 36 | +
|
| 37 | + return merge(Array.from(allIps, ip => stringifyIp({ number: ip, version }))); |
| 38 | + } |
| 39 | + catch (e) { |
| 40 | + return []; |
| 41 | + } |
| 42 | +}); |
| 43 | +
|
| 44 | +const startIpValidation = useValidation({ |
| 45 | + source: rawStartAddress, |
| 46 | + rules: [{ message: 'Invalid ipv4/6 address', validator: ip => isIP(ip) }], |
| 47 | +}); |
| 48 | +const endIpValidation = useValidation({ |
| 49 | + source: rawEndAddress, |
| 50 | + rules: [{ message: 'Invalid ipv4/6 address', validator: ip => isIP(ip) }], |
| 51 | +}); |
| 52 | +
|
| 53 | +const showResult = computed(() => endIpValidation.isValid && startIpValidation.isValid && result.value.length > 0); |
| 54 | +
|
| 55 | +function onSwitchStartEndClicked() { |
| 56 | + const tmpStart = rawStartAddress.value; |
| 57 | + rawStartAddress.value = rawEndAddress.value; |
| 58 | + rawEndAddress.value = tmpStart; |
| 59 | +} |
| 60 | +</script> |
| 61 | + |
| 62 | +<template> |
| 63 | + <div> |
| 64 | + <n-grid cols="4" x-gap="12" mb-6 w-full> |
| 65 | + <n-gi span="2"> |
| 66 | + <c-input-text |
| 67 | + v-model:value="rawStartAddress" |
| 68 | + label="Start address" |
| 69 | + placeholder="Start IPv4/6 address..." |
| 70 | + :validation="startIpValidation" |
| 71 | + clearable |
| 72 | + /> |
| 73 | + </n-gi> |
| 74 | + <n-gi span="2"> |
| 75 | + <c-input-text |
| 76 | + v-model:value="rawEndAddress" |
| 77 | + label="End address" |
| 78 | + placeholder="End IPv4/6 address..." |
| 79 | + :validation="endIpValidation" |
| 80 | + clearable |
| 81 | + /> |
| 82 | + </n-gi> |
| 83 | + </n-grid> |
| 84 | + |
| 85 | + <c-card v-if="showResult" title="CIDR" data-test-id="result"> |
| 86 | + <ul style="list-item-type: none"> |
| 87 | + <li v-for="cidr in result" :key="cidr"> |
| 88 | + {{ cidr }} |
| 89 | + </li> |
| 90 | + </ul> |
| 91 | + </c-card> |
| 92 | + <n-alert |
| 93 | + v-else-if="startIpValidation.isValid && endIpValidation.isValid && isReversed" |
| 94 | + title="Invalid combination of start and end IPv4/6 address" |
| 95 | + type="error" |
| 96 | + > |
| 97 | + <div my-3 op-70> |
| 98 | + The end IPv4/6 address is lower than the start IPv4/6 address. This is not valid and no result could be calculated. |
| 99 | + In the most cases the solution to solve this problem is to change start and end address. |
| 100 | + </div> |
| 101 | + |
| 102 | + <c-button @click="onSwitchStartEndClicked"> |
| 103 | + <n-icon mr-2 :component="Exchange" depth="3" size="22" /> |
| 104 | + Switch start and end IPv4/6 address |
| 105 | + </c-button> |
| 106 | + </n-alert> |
| 107 | + <n-alert |
| 108 | + v-else-if="isNotSameVersion" |
| 109 | + title="Invalid combination of IP version 4/6" |
| 110 | + type="error" |
| 111 | + > |
| 112 | + <div my-3 op-70> |
| 113 | + Start IP and End IP must be of same version: IPv4 or IPv6 |
| 114 | + </div> |
| 115 | + </n-alert> |
| 116 | + </div> |
| 117 | +</template> |
0 commit comments