Skip to content

Commit 7fab4e4

Browse files
committed
Merge branch 'feat/ip-in-range' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents e3fdf35 + f9cc935 commit 7fab4e4

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
"ip-matching": "^2.1.2",
206206
"is-cidr": "^5.0.3",
207207
"is-ip": "^5.0.1",
208+
"ip-matching": "^2.1.2",
208209
"json5": "^2.2.3",
209210
"jsonpath": "^1.1.1",
210211
"jsonar-mod": "^1.9.0",

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ import { tool as manyUnitsConverter } from './many-units-converter';
200200
import { tool as powerConverter } from './power-converter';
201201
import { tool as dockerComposeConverter } from './docker-compose-converter';
202202
import { tool as imageExifReader } from './image-exif-reader';
203+
import { tool as ipInRange } from './ip-in-range';
203204
import { tool as yamlViewer } from './yaml-viewer';
204205
import { tool as barcodeReader } from './barcode-reader';
205206
import { tool as barcodeGenerator } from './barcode-generator';
@@ -404,6 +405,7 @@ export const toolsByCategory: ToolCategory[] = [
404405
ipv4SubnetCalculator,
405406
ipv4AddressConverter,
406407
// ipv6AddressConverter,
408+
ipInRange,
407409
ipv4RangeExpander,
408410
macAddressLookup,
409411
macAddressGenerator,

src/tools/ip-in-range/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { UnfoldMoreOutlined } from '@vicons/material';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'IP in Range/CIDR/Mask',
6+
path: '/ip-in-range',
7+
description: 'Given a CIDR/IP Range/Wildcard IP/IP Mask, tell if a given IP is in subnet range',
8+
keywords: ['ip', 'cidr', 'range'],
9+
component: () => import('./ip-in-range.vue'),
10+
icon: UnfoldMoreOutlined,
11+
createdAt: new Date('2024-03-09'),
12+
});

src/tools/ip-in-range/ip-in-range.vue

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<script setup lang="ts">
2+
import { useStorage } from '@vueuse/core';
3+
import { Check as CheckIcon, LetterX as CrossIcon } from '@vicons/tabler';
4+
import { getMatch } from 'ip-matching';
5+
import { withDefaultOnError } from '@/utils/defaults';
6+
import { isNotThrowing } from '@/utils/boolean';
7+
import SpanCopyable from '@/components/SpanCopyable.vue';
8+
9+
const range = useStorage('ip-in-range:range', '192.168.0.1/24'); // NOSONAR
10+
const ip = useStorage('ip-in-range:ip', '192.168.0.1'); // NOSONAR
11+
12+
const match = computed(() => withDefaultOnError(() => getMatch(range.value), undefined));
13+
const subnets = computed(() => {
14+
return (match.value?.convertToMasks() || []).map((mask) => {
15+
const subnet = mask.convertToSubnet();
16+
if (!subnet) {
17+
return { cidr: '', start: '', end: '' };
18+
}
19+
return {
20+
cidr: subnet.toString(),
21+
start: subnet.getFirst().toString(),
22+
end: subnet.getLast().toString(),
23+
};
24+
}).filter(subnet => subnet.cidr !== '');
25+
});
26+
const ipIsInMatch = computed(() => match.value?.matches(ip.value));
27+
28+
const ipValidationRules = [
29+
{
30+
message: 'We cannot parse this CIDR/IP Range/Mask/Wildcard, check the format',
31+
validator: (value: string) => isNotThrowing(() => getMatch(value)) && getMatch(range.value),
32+
},
33+
];
34+
</script>
35+
36+
<template>
37+
<div>
38+
<c-input-text
39+
v-model:value="range"
40+
label="An IPv4/6 CIDR/Range/Mask/Wildcard"
41+
placeholder="The ipv4/6 CIDR..."
42+
:validation-rules="ipValidationRules"
43+
mb-4
44+
/>
45+
46+
<c-input-text
47+
v-model:value="ip"
48+
label="An IPv4/6 address"
49+
placeholder="The ipv4/6 address..."
50+
:validation-rules="ipValidationRules"
51+
mb-4
52+
/>
53+
54+
<n-divider />
55+
56+
<div flex justify-center>
57+
<span v-if="ipIsInMatch">
58+
<n-icon color="green">
59+
<CheckIcon />
60+
</n-icon>
61+
Match
62+
</span>
63+
<span v-else>
64+
<n-icon color="red">
65+
<CrossIcon />
66+
</n-icon>
67+
Not Match
68+
</span>
69+
</div>
70+
71+
<n-divider />
72+
73+
<c-card title="Subnets">
74+
<n-table>
75+
<tbody>
76+
<tr v-for="{ cidr, start, end } in subnets" :key="cidr">
77+
<td font-bold>
78+
<SpanCopyable :value="cidr" />
79+
</td>
80+
<td>
81+
<SpanCopyable :value="start" />
82+
</td>
83+
<td>
84+
<SpanCopyable :value="end" />
85+
</td>
86+
</tr>
87+
</tbody>
88+
</n-table>
89+
</c-card>
90+
</div>
91+
</template>

0 commit comments

Comments
 (0)