mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
1 parent
7955f5013d
commit
99f4be60ec
4 files changed
+125
-10
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import { RouterOutlined } from '@vicons/material';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'IPv4 Subnets Lister',
|
||||
path: '/ipv4-subnets-list',
|
||||
description: 'List all possible subnets of a given CIDR',
|
||||
keywords: ['ipv4', 'subnet', 'bitmask'],
|
||||
component: () => import('./ipv4-subnets-list.vue'),
|
||||
icon: RouterOutlined,
|
||||
createdAt: new Date('2025-12-06'),
|
||||
category: 'Network',
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
<script setup lang="ts">
|
||||
import { Netmask } from 'netmask';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { isNotThrowing } from '@/utils/boolean';
|
||||
import SpanCopyable from '@/components/SpanCopyable.vue';
|
||||
import type { SubnetInfo } from '@/utils/ip';
|
||||
import { getSubnetsInfos, parseAsCIDR } from '@/utils/ip';
|
||||
import { useITStorage } from '@/composable/queryParams';
|
||||
|
||||
const ip = useITStorage('ipv4-subnets-list:ip', '192.168.0.0/24');
|
||||
|
||||
const getNetworkInfo = (address: string) => new Netmask(parseAsCIDR(address.trim()) || address.trim());
|
||||
|
||||
const networkInfo = computed(() => withDefaultOnError(() => getNetworkInfo(ip.value), undefined));
|
||||
|
||||
const allSubnetsInfos = computed(() => withDefaultOnError(() => {
|
||||
let bitmask = networkInfo.value?.bitmask;
|
||||
if (!bitmask || bitmask < 16) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const base = networkInfo.value?.base;
|
||||
const subnets: { bitmask: number; hostsPerSubnet: number; subnets: SubnetInfo[] }[] = [];
|
||||
for (;bitmask <= 30; bitmask++) {
|
||||
const bitmaskSubnets = getSubnetsInfos(`${base}/${bitmask}`);
|
||||
subnets.push({ bitmask, hostsPerSubnet: bitmaskSubnets[0]?.hostsCount, subnets: bitmaskSubnets });
|
||||
}
|
||||
return subnets;
|
||||
}, undefined));
|
||||
|
||||
const ipValidationRules = [
|
||||
{
|
||||
message: 'We cannot parse this address, check the format',
|
||||
validator: (value: string) => isNotThrowing(() => getNetworkInfo(value)),
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<c-input-text
|
||||
v-model:value="ip"
|
||||
label="An IPv4 address with or without mask (CIDR/IP Range/Wildcard IP/IP Mask)"
|
||||
placeholder="IPv4 or cidr..."
|
||||
:validation-rules="ipValidationRules"
|
||||
mb-4
|
||||
/>
|
||||
|
||||
<div v-if="networkInfo && allSubnetsInfos?.length">
|
||||
<c-card v-for="{ bitmask, hostsPerSubnet, subnets } in allSubnetsInfos" :key="bitmask" :title="`${networkInfo.base}/${bitmask} (${subnets.length} subnets; ${hostsPerSubnet} hosts)`" mb-2>
|
||||
<n-table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Network</th>
|
||||
<th>First IP</th>
|
||||
<th>Last IP</th>
|
||||
<th>Broadcast</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="subnet in subnets" :key="subnet.netAddress">
|
||||
<td font-bold>
|
||||
<SpanCopyable :value="subnet.netAddress" />
|
||||
</td>
|
||||
<td>
|
||||
<SpanCopyable :value="subnet.firstIP" />
|
||||
</td>
|
||||
<td>
|
||||
<SpanCopyable :value="subnet.lastIP" />
|
||||
</td>
|
||||
<td>
|
||||
<SpanCopyable :value="subnet.broadcastIP" />
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</n-table>
|
||||
</c-card>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -93,7 +93,9 @@ describe('ipv4/6 util', () => {
|
||||
'248.0.0.0/6', // NOSONAR
|
||||
'252.0.0.0/6', // NOSONAR
|
||||
]); // NOSONAR
|
||||
expect(getSubnets('1.1.1.1/8')).to.eql([]); // NOSONAR
|
||||
expect(getSubnets('1.1.1.1/8')).to.eql([
|
||||
'1.0.0.0/8', // NOSONAR
|
||||
]); // NOSONAR
|
||||
expect(getSubnets('1.1.1.1/11')).to.eql([ // NOSONAR
|
||||
'1.0.0.0/11', // NOSONAR
|
||||
'1.32.0.0/11', // NOSONAR
|
||||
@@ -104,7 +106,9 @@ describe('ipv4/6 util', () => {
|
||||
'1.192.0.0/11', // NOSONAR
|
||||
'1.224.0.0/11', // NOSONAR
|
||||
]); // NOSONAR
|
||||
expect(getSubnets('172.16.2.2/16')).to.eql([]); // NOSONAR
|
||||
expect(getSubnets('172.16.2.2/16')).to.eql([
|
||||
'172.16.0.0/16', // NOSONAR
|
||||
]); // NOSONAR
|
||||
expect(getSubnets('172.16.2.2/26')).to.eql([ // NOSONAR
|
||||
'172.16.2.0/26', // NOSONAR
|
||||
'172.16.2.64/26', // NOSONAR
|
||||
@@ -112,7 +116,9 @@ describe('ipv4/6 util', () => {
|
||||
'172.16.2.192/26', // NOSONAR
|
||||
]); // NOSONAR
|
||||
expect(getSubnets('172.16.2.2/31').length).to.eql(128); // NOSONAR
|
||||
expect(getSubnets('255.255.255.0/32')).to.eql([]); // NOSONAR
|
||||
expect(getSubnets('255.255.255.0/32')).to.eql([
|
||||
'255.255.255.0/32', // NOSONAR
|
||||
]); // NOSONAR
|
||||
expect(getSubnets('2001:db8:0:85a3::ac1f:8001/62')).to.eql([]); // NOSONAR
|
||||
expect(getSubnets('2001:db8:0:85a3:0:0:ac1f:8001/62')).to.eql([]); // NOSONAR
|
||||
expect(getSubnets('2001:db8:0:85a3::ac1f:8001/112')).to.eql([]); // NOSONAR
|
||||
|
||||
+23
-7
@@ -31,7 +31,16 @@ export function parseAsCIDR(form: string) {
|
||||
return (ipMatch.convertToMasks() || [])[0]?.convertToSubnet()?.toString() || '';
|
||||
}
|
||||
|
||||
export function getSubnets(cidr: string) {
|
||||
export interface SubnetInfo {
|
||||
netAddress: string
|
||||
firstIP: string
|
||||
lastIP: string
|
||||
broadcastIP: string
|
||||
prefix: number
|
||||
hostsCount: number
|
||||
}
|
||||
|
||||
export function getSubnetsInfos(cidr: string): SubnetInfo[] {
|
||||
const [address, prefix] = cidr.split('/');
|
||||
if (isIPv4(address)) {
|
||||
const prefix4Int = Number(prefix || '32');
|
||||
@@ -43,15 +52,18 @@ export function getSubnets(cidr: string) {
|
||||
if (prefix4Int < 8) {
|
||||
startNetwork = 0;
|
||||
}
|
||||
if (prefix4Int % 8 === 0) {
|
||||
return [];
|
||||
}
|
||||
startNetwork = bigInt & getMask(prefix4Int);
|
||||
const increment = BigInt(2) ** BigInt(32 - prefix4Int);
|
||||
const netCount = getNetworksCount(cidr);
|
||||
const netCount = getNetworksCount(cidr) || 1;
|
||||
for (let netIndex = 0; netIndex < netCount; netIndex += 1) {
|
||||
const netAddr = Address4.fromBigInt(startNetwork).correctForm();
|
||||
subnets.push(`${netAddr}/${prefix4Int}`);
|
||||
subnets.push({
|
||||
netAddress: Address4.fromBigInt(startNetwork).correctForm(),
|
||||
firstIP: Address4.fromBigInt(startNetwork + 1n).correctForm(),
|
||||
lastIP: Address4.fromBigInt(startNetwork + increment - 2n).correctForm(),
|
||||
broadcastIP: Address4.fromBigInt(startNetwork + increment - 1n).correctForm(),
|
||||
prefix: prefix4Int,
|
||||
hostsCount: Number(increment - 2n),
|
||||
});
|
||||
startNetwork += increment;
|
||||
}
|
||||
return subnets;
|
||||
@@ -60,6 +72,10 @@ export function getSubnets(cidr: string) {
|
||||
return [];
|
||||
}
|
||||
|
||||
export function getSubnets(cidr: string) {
|
||||
return getSubnetsInfos(cidr).map(({ netAddress, prefix }) => `${netAddress}/${prefix}`);
|
||||
}
|
||||
|
||||
export function getNetworksCount(cidr: string) {
|
||||
const [address, prefix] = cidr.split('/');
|
||||
if (isIPv4(address)) {
|
||||
|
||||
Reference in new issue
Block a user