mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-23 14:21:04 +03:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3550d81dda |
No files matched your search
@@ -15,7 +15,7 @@ jobs:
|
||||
- run: corepack enable
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 20
|
||||
node-version: 16
|
||||
cache: 'pnpm'
|
||||
|
||||
- name: Install dependencies
|
||||
|
||||
@@ -18,7 +18,7 @@ jobs:
|
||||
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 20
|
||||
node-version: 16
|
||||
cache: 'pnpm'
|
||||
|
||||
- name: Get Playwright version
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
"@vueuse/router": "^10.0.0",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"change-case": "^4.1.2",
|
||||
"cidr-tools": "^7.0.4",
|
||||
"colord": "^2.9.3",
|
||||
"composerize-ts": "^0.6.2",
|
||||
"country-code-lookup": "^0.1.0",
|
||||
@@ -64,6 +65,12 @@
|
||||
"highlight.js": "^11.7.0",
|
||||
"iarna-toml-esm": "^3.0.5",
|
||||
"ibantools": "^4.3.3",
|
||||
"ip-address": "^9.0.5",
|
||||
"ip-bigint": "^8.0.2",
|
||||
"ip-cidr": "^4.0.0",
|
||||
"ip-matching": "^2.1.2",
|
||||
"is-cidr": "^5.0.3",
|
||||
"is-ip": "^5.0.1",
|
||||
"json5": "^2.2.3",
|
||||
"jwt-decode": "^3.1.2",
|
||||
"libphonenumber-js": "^1.10.28",
|
||||
|
||||
@@ -9,7 +9,7 @@ const useWebServer = process.env.NO_WEB_SERVER !== 'true';
|
||||
*/
|
||||
export default defineConfig({
|
||||
testDir: './src',
|
||||
testMatch: /\.e2e\.(spec\.)?ts$/,
|
||||
testMatch: /.*\.e2e\.(spec\.)?ts/,
|
||||
/* Run tests in files in parallel */
|
||||
fullyParallel: true,
|
||||
/* Fail the build on CI if you accidentally left test.only in the source code. */
|
||||
@@ -57,7 +57,7 @@ export default defineConfig({
|
||||
&& {
|
||||
webServer: {
|
||||
command: 'npm run preview',
|
||||
url: 'http://localhost:5050',
|
||||
url: 'http://127.0.0.1:5050',
|
||||
reuseExistingServer: !isCI,
|
||||
},
|
||||
}
|
||||
|
||||
+1
-10
@@ -6,7 +6,6 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
||||
|
||||
import { tool as textToUnicode } from './text-to-unicode';
|
||||
import { tool as safelinkDecoder } from './safelink-decoder';
|
||||
import { tool as option43Generator } from './option43-generator';
|
||||
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
|
||||
import { tool as numeronymGenerator } from './numeronym-generator';
|
||||
import { tool as macAddressGenerator } from './mac-address-generator';
|
||||
@@ -153,15 +152,7 @@ export const toolsByCategory: ToolCategory[] = [
|
||||
},
|
||||
{
|
||||
name: 'Network',
|
||||
components: [
|
||||
ipv4SubnetCalculator,
|
||||
ipv4AddressConverter,
|
||||
ipv4RangeExpander,
|
||||
macAddressLookup,
|
||||
macAddressGenerator,
|
||||
ipv6UlaGenerator,
|
||||
option43Generator,
|
||||
],
|
||||
components: [ipv4SubnetCalculator, ipv4AddressConverter, ipv4RangeExpander, macAddressLookup, macAddressGenerator, ipv6UlaGenerator],
|
||||
},
|
||||
{
|
||||
name: 'Math',
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { RouterOutlined } from '@vicons/material';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Option43 generator',
|
||||
path: '/option43-generator',
|
||||
description: 'Generate Option43 Wifi DHCP configuration',
|
||||
keywords: ['option43', 'wifi', 'dhcp', 'generator'],
|
||||
component: () => import('./option43-generator.vue'),
|
||||
icon: RouterOutlined,
|
||||
createdAt: new Date('2024-03-09'),
|
||||
});
|
||||
@@ -1,237 +0,0 @@
|
||||
// from option43.org
|
||||
|
||||
function IPToHexDigit(ip_addr: string) { // Ruckus
|
||||
const arr1 = [];
|
||||
// ip_addr = ip_addr.split('.').join('');
|
||||
for (let n = 0, l = ip_addr.length; n < l; n++) {
|
||||
const hex = Number(ip_addr.charCodeAt(n)).toString(16);
|
||||
arr1.push(hex);
|
||||
}
|
||||
return arr1.join('');
|
||||
}
|
||||
|
||||
function IPToHexNumber(ip_addr: string) {
|
||||
const arr1 = [];
|
||||
const ips = ip_addr.split('.');
|
||||
for (let n = 0, l = ips.length; n < l; n++) {
|
||||
const hex = (`0${Number(ips[n]).toString(16)}`).slice(-2);
|
||||
arr1.push(hex);
|
||||
}
|
||||
return arr1.join('');
|
||||
}
|
||||
|
||||
function IPCharCounter(ip_addr: string, ret: string) {
|
||||
if (ret === 'hex') {
|
||||
return (`0${ip_addr.length.toString(16)}`).slice(-2);
|
||||
}
|
||||
else {
|
||||
return ip_addr.length;
|
||||
}
|
||||
}
|
||||
|
||||
function splitIPs(ip_addr: string) {
|
||||
const validIPs = [];
|
||||
const rows = ip_addr.split('\n');
|
||||
for (let n = 0, l = rows.length; n < l; n++) {
|
||||
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(rows[n])) {
|
||||
validIPs.push(rows[n]);
|
||||
}
|
||||
}
|
||||
return validIPs;
|
||||
}
|
||||
|
||||
function IPNumberCounter(ip_addr: string[]) {
|
||||
return ip_addr.length;
|
||||
}
|
||||
|
||||
function renderSettings(
|
||||
dhcp_vendor: string,
|
||||
option43_type: string,
|
||||
option43_subtype: string,
|
||||
option43_value: string,
|
||||
option60_type: string | undefined,
|
||||
option60_value: string | undefined,
|
||||
vendor_link: string | undefined,
|
||||
diff_number: number | undefined) {
|
||||
let output;
|
||||
switch (dhcp_vendor) {
|
||||
case 'cisco_01':
|
||||
// option code [instance number] {ascii string | hex string | ip-address} // no quotation marks in option 43!
|
||||
output = '<p>Cisco CLI commands:</p><div class="cli"><p>option ';
|
||||
if (typeof diff_number !== 'undefined') {
|
||||
output = output + diff_number;
|
||||
}
|
||||
else { output = `${output}43`; }
|
||||
output = `${output} ${option43_type}`;
|
||||
output = `${output} ${option43_subtype}${option43_value}`;
|
||||
if (typeof option60_value !== 'undefined') {
|
||||
output = `${output}</p><p>option 60 ascii "${option60_value}"`;
|
||||
}
|
||||
output = `${output}</p></div><!--<p>More information: ${vendor_link}</p>-->`;
|
||||
return output;
|
||||
case 'juniper_01':
|
||||
// https://www.juniper.net/documentation/en_US/junos/topics/topic-map/dhcp-serever-options.html#id-configure-user-defined-dhcp-options
|
||||
|
||||
output = '<p>Juniper EX CLI commands:</p><div class="cli"><p>set system services dhcp option ';
|
||||
if (typeof diff_number !== 'undefined') {
|
||||
output = output + diff_number;
|
||||
}
|
||||
else { output = `${output}43`; }
|
||||
if (option43_type === 'ascii') {
|
||||
option43_type = 'string';
|
||||
}
|
||||
if (option60_type === 'ascii') {
|
||||
option60_type = 'string';
|
||||
}
|
||||
output = `${output} ${option43_type}`;
|
||||
output = `${output} ${option43_subtype}${option43_value}`;
|
||||
if (typeof option60_value !== 'undefined') {
|
||||
output = `${output}</p><p>set system services dhcp option 60 ascii "${option60_value}"`;
|
||||
}
|
||||
output = `${output}</p></div>`;
|
||||
return output;
|
||||
case 'juniper_02':
|
||||
// https://www.juniper.net/documentation/en_US/junos/topics/topic-map/dhcp-serever-options.html#id-configure-user-defined-dhcp-options
|
||||
|
||||
output = '<p>Juniper SRX CLI commands:</p><div class="cli"><p>set access address-assignment pool <i>AP_DHCP_POOL</i> family inet dhcp-attributes option ';
|
||||
if (typeof diff_number !== 'undefined') {
|
||||
output = output + diff_number;
|
||||
}
|
||||
else { output = `${output}43`; }
|
||||
if (option43_type === 'ascii') {
|
||||
option43_type = 'string';
|
||||
}
|
||||
if (option43_type === 'hex') {
|
||||
option43_type = 'byte-stream';
|
||||
}
|
||||
output = `${output} ${option43_type}`;
|
||||
if (option43_type === 'byte-stream') {
|
||||
output = `${output} "0x${(option43_subtype + option43_value).match(/.{1,2}/g)?.join(' 0x')}"`;
|
||||
}
|
||||
else {
|
||||
output = `${output} ${option43_subtype}${option43_value}`;
|
||||
}
|
||||
if (option60_type === 'ascii') {
|
||||
option60_type = 'string';
|
||||
}
|
||||
if (typeof option60_value !== 'undefined') {
|
||||
output = `${output}</p><p>set access address-assignment pool <i>AP_DHCP_POOL</i> family inet dhcp-attributes option 60 ascii "${option60_value}"`;
|
||||
}
|
||||
output = `${output}</p></div>`;
|
||||
return output;
|
||||
default:
|
||||
output = '<p>Raw values:</p><p><b>Option ';
|
||||
if (typeof diff_number !== 'undefined') {
|
||||
output = output + diff_number;
|
||||
}
|
||||
else {
|
||||
output = `${output}43`;
|
||||
}
|
||||
output = `${output}</b><br />Type: "`;
|
||||
if (option43_type === 'ascii') {
|
||||
option43_type = 'String/ASCII';
|
||||
}
|
||||
if (option43_type === 'hex') {
|
||||
option43_type = 'Hexadecimal';
|
||||
}
|
||||
if (option43_type === 'ip') {
|
||||
option43_type = 'IP-Address';
|
||||
}
|
||||
if (option60_type === 'ascii') {
|
||||
option60_type = 'String/ASCII';
|
||||
}
|
||||
output = output + option43_type;
|
||||
output = `${output}"<br />Value: ${option43_subtype}${option43_value}`;
|
||||
if (typeof option60_value !== 'undefined') {
|
||||
output = `${output}</p><p><b>Option 60</b><br /> Type: "${option60_type}"<br />Value: "${option60_value}"`;
|
||||
}
|
||||
output = `${output}</p></div>`;
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
export function getOption43Infos(ip_addr: string, wifi_vendor: string, dhcp_vendor: string) {
|
||||
const ip_addr_array = splitIPs(ip_addr);
|
||||
const ip_addr_hexDigit = IPToHexDigit (ip_addr_array.join(',')); // Ruckus, comma separation
|
||||
const ip_addr_hexNumber = IPToHexNumber (ip_addr_array.join('.')); // valid for Cisco, no separation
|
||||
const ip_addr_hexNumber_first = IPToHexNumber (ip_addr_array[0]);
|
||||
|
||||
const num_ips = IPNumberCounter (ip_addr_array);
|
||||
const num_ip_bytes = (`0${(num_ips * 4).toString(16)}`).slice(-2);
|
||||
|
||||
let option43_type = ''; // hex, ascii, ip
|
||||
let option43_subtype = ''; // vendor prefix
|
||||
let option43_value;
|
||||
let diff_number;
|
||||
let option60_type; // hex, ascii, ip
|
||||
let option60_value;
|
||||
let vendor_link;
|
||||
|
||||
switch (wifi_vendor) {
|
||||
case 'cisco_01':
|
||||
option43_type = 'hex';
|
||||
option43_subtype = 'f1';
|
||||
option43_value = num_ip_bytes + ip_addr_hexNumber;
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'ruckus_01':
|
||||
option43_type = 'hex';
|
||||
option43_subtype = '06';
|
||||
option43_value = IPCharCounter (ip_addr_array.join(','), 'hex') + ip_addr_hexDigit;
|
||||
option60_type = 'ascii';
|
||||
option60_value = 'Ruckus CPE';
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'ruckus_02':
|
||||
option43_type = 'hex';
|
||||
option43_subtype = '03';
|
||||
option43_value = IPCharCounter (ip_addr_array.join(','), 'hex') + ip_addr_hexDigit;
|
||||
option60_type = 'ascii';
|
||||
option60_value = 'Ruckus CPE';
|
||||
vendor_link = 'https://docs.commscope.com/bundle/zd-10.2-userguide/page/GUID-D5CF7FE0-D73F-4B4B-95C8-08CAB5B235D5.html';
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'aruba_01':
|
||||
option43_type = 'ip';
|
||||
option43_value = ip_addr;
|
||||
option60_type = 'ascii';
|
||||
option60_value = 'ArubaAP';
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'fortinet_01':
|
||||
option43_type = 'ip';
|
||||
option43_value = ip_addr_array.join(' ');
|
||||
// diff_number = 138;
|
||||
// option60_type = "ascii";
|
||||
// option60_value = "ArubaAP";
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'fortinet_02':
|
||||
option43_type = 'ip';
|
||||
option43_value = ip_addr_array.join(' ');
|
||||
diff_number = 138;
|
||||
// option60_type = "ascii";
|
||||
// option60_value = "ArubaAP";
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'ubiquiti_01':
|
||||
option43_type = 'hex';
|
||||
option43_subtype = '01';
|
||||
option43_value = `04${ip_addr_hexNumber_first}`;
|
||||
// option60_type = "ascii";
|
||||
// option60_value = "Ruckus CPE";
|
||||
// vendor_link = "https://docs.commscope.com/bundle/zd-10.2-userguide/page/GUID-D5CF7FE0-D73F-4B4B-95C8-08CAB5B235D5.html";
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'cambium_01':
|
||||
option43_type = 'ascii';
|
||||
// option43_subtype = "01";
|
||||
option43_value = `"https://${ip_addr[0]}"`;
|
||||
// option60_type = "ascii";
|
||||
// option60_value = "Ruckus CPE";
|
||||
// vendor_link = "https://docs.commscope.com/bundle/zd-10.2-userguide/page/GUID-D5CF7FE0-D73F-4B4B-95C8-08CAB5B235D5.html";
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
case 'linux_01':
|
||||
return `Linux:<br />Option 43 (IP-address): ${ip_addr}<br /> You also <b>need</b>:<br />Option 60 (String): "ArubaAP".`;
|
||||
case 'netgear_01':
|
||||
option43_type = 'hex';
|
||||
option43_subtype = '0204';
|
||||
option43_value = ip_addr_hexNumber_first;
|
||||
return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number);
|
||||
default:
|
||||
return 'Not implemented';
|
||||
}
|
||||
};
|
||||
@@ -1,91 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { useStorage } from '@vueuse/core';
|
||||
import { getOption43Infos } from './option43-generator.service';
|
||||
|
||||
const dhcpVendorOptions = [
|
||||
{ value: 'genuine', label: 'Native' },
|
||||
{ value: 'cisco_01', label: 'Cisco IOS device' },
|
||||
{ value: 'juniper_01', label: 'Juniper EX device' },
|
||||
{ value: 'juniper_02', label: 'Juniper SRX device' },
|
||||
{ value: 'linux_01', label: 'Linux DHCPD' },
|
||||
{ value: 'ruckus_01', label: 'Ruckus SmartCellGateway' },
|
||||
{ value: 'lancom_01', label: 'Lancom' },
|
||||
{ value: 'fortinet_01', label: 'Fortinet' },
|
||||
];
|
||||
const wifiVendorOptions = [
|
||||
{ value: 'cisco_01', label: 'Cisco Aironet' },
|
||||
{ value: 'aruba_01', label: 'Aruba' },
|
||||
{ value: 'ruckus_01', label: 'Ruckus SmartZone' },
|
||||
{ value: 'ruckus_02', label: 'Ruckus ZoneDirector' },
|
||||
{ value: 'ruckus_01', label: 'Ruckus SmartCellGateway' },
|
||||
{ value: 'ubiquiti_01', label: 'Ubiquiti' },
|
||||
{ value: 'fortinet_01', label: 'Fortinet (old)' },
|
||||
{ value: 'fortinet_02', label: 'Fortinet (new/CAPWAP)' },
|
||||
{ value: 'netgear_01', label: 'Netgear' },
|
||||
{ value: 'cambium_01', label: 'Cambium cnMaestro' },
|
||||
];
|
||||
|
||||
const dhcpVendor = useStorage('option43-generator:dhcp', 'genuine');
|
||||
const wifiVendor = useStorage('option43-generator:wifi', 'cisco_01');
|
||||
const ipAdresses = ref('192.168.0.15'); // NOSONAR
|
||||
|
||||
const option43Infos = computed(() => getOption43Infos(ipAdresses.value, wifiVendor.value, dhcpVendor.value));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div style="margin: 0 auto; max-width: 600px">
|
||||
<c-card>
|
||||
<c-select
|
||||
v-model:value="wifiVendor"
|
||||
label="Wifi Vendor:"
|
||||
label-position="left"
|
||||
label-width="120px"
|
||||
label-align="right"
|
||||
mb-2
|
||||
:options="wifiVendorOptions"
|
||||
w-full
|
||||
placeholder="Select a Wifi Vendor"
|
||||
/>
|
||||
|
||||
<c-select
|
||||
v-model:value="dhcpVendor"
|
||||
label="DHCP Vendor:"
|
||||
label-position="left"
|
||||
label-width="120px"
|
||||
label-align="right"
|
||||
mb-2
|
||||
:options="dhcpVendorOptions"
|
||||
w-full
|
||||
placeholder="Select a DHCP Vendor"
|
||||
/>
|
||||
|
||||
<c-input-text
|
||||
v-model:value="ipAdresses"
|
||||
label="IP Address(es) (one per line):"
|
||||
label-position="left"
|
||||
label-width="120px"
|
||||
label-align="right"
|
||||
multiline mb-2
|
||||
placeholder="Enter your IP Addresses (one per line)"
|
||||
/>
|
||||
</c-card>
|
||||
|
||||
<c-card title="Option 43 Result">
|
||||
<!-- //NOSONAR --><div v-html="option43Infos" />
|
||||
</c-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less">
|
||||
.highlight {
|
||||
color: orange;
|
||||
font-variant: small-caps;
|
||||
}
|
||||
.cli {
|
||||
background-color: black;
|
||||
color: #adfc03;
|
||||
font-family: monospace;
|
||||
padding: 10px;
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,232 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { getIPNetworkType, getNetworksCount, getSubnets, parseAsCIDR, to6to4Prefix, toARPA, toIPv4MappedAddress, toIPv4MappedAddressDecimal } from './ip';
|
||||
|
||||
describe('ipv4/6 util', () => {
|
||||
describe('parseAsCIDR', () => {
|
||||
it('returns cidr', () => {
|
||||
expect(parseAsCIDR('1.1.1.1/6')).to.eql('1.1.1.1/6');
|
||||
expect(parseAsCIDR('172.16.2.2/16')).to.eql('172.16.2.2/16');
|
||||
expect(parseAsCIDR('1a:b:c::d:e:f/ffff:ffff:f4ff:ffff:ffff:ff5f:ffff:ff00')).to.eql();
|
||||
expect(parseAsCIDR('10.1.2.3/255.255.255.252')).to.eql('10.1.2.0/30');
|
||||
expect(parseAsCIDR('10.*.0.*')).to.eql();
|
||||
expect(parseAsCIDR('10.1.0.*')).to.eql('10.1.0.0/24');
|
||||
expect(parseAsCIDR('10.2.*.*')).to.eql('10.2.0.0/16');
|
||||
expect(parseAsCIDR('a:b:0:8000::/ffff:ffff:ffff:8000::')).to.eql('a:b:0:8000::/49');
|
||||
expect(parseAsCIDR('::/::')).to.eql('::/0');
|
||||
expect(parseAsCIDR('10.20.30.64-10.20.30.127')).to.eql('10.20.30.64/26');
|
||||
expect(parseAsCIDR('10.0.128.0/255.0.128.0')).to.eql();
|
||||
expect(parseAsCIDR('a::bc:1234/128')).to.eql('a::bc:1234/128');
|
||||
expect(parseAsCIDR('a::bc:ff00-a::bc:ff0f')).to.eql('a::bc:ff00/124');
|
||||
expect(parseAsCIDR('10.0.1.1/255.255.1.0')).to.eql();
|
||||
expect(parseAsCIDR('10.0.0.0/255.255.0.0')).to.eql('10.0.0.0/16');
|
||||
});
|
||||
});
|
||||
describe('getSubnets', () => {
|
||||
it('returns subnets', () => {
|
||||
expect(getSubnets('1.1.1.1/1')).to.eql([
|
||||
'0.0.0.0/1',
|
||||
'128.0.0.0/1',
|
||||
]);
|
||||
expect(getSubnets('1.1.1.1/6')).to.eql([
|
||||
'0.0.0.0/6',
|
||||
'4.0.0.0/6',
|
||||
'8.0.0.0/6',
|
||||
'12.0.0.0/6',
|
||||
'16.0.0.0/6',
|
||||
'20.0.0.0/6',
|
||||
'24.0.0.0/6',
|
||||
'28.0.0.0/6',
|
||||
'32.0.0.0/6',
|
||||
'36.0.0.0/6',
|
||||
'40.0.0.0/6',
|
||||
'44.0.0.0/6',
|
||||
'48.0.0.0/6',
|
||||
'52.0.0.0/6',
|
||||
'56.0.0.0/6',
|
||||
'60.0.0.0/6',
|
||||
'64.0.0.0/6',
|
||||
'68.0.0.0/6',
|
||||
'72.0.0.0/6',
|
||||
'76.0.0.0/6',
|
||||
'80.0.0.0/6',
|
||||
'84.0.0.0/6',
|
||||
'88.0.0.0/6',
|
||||
'92.0.0.0/6',
|
||||
'96.0.0.0/6',
|
||||
'100.0.0.0/6',
|
||||
'104.0.0.0/6',
|
||||
'108.0.0.0/6',
|
||||
'112.0.0.0/6',
|
||||
'116.0.0.0/6',
|
||||
'120.0.0.0/6',
|
||||
'124.0.0.0/6',
|
||||
'128.0.0.0/6',
|
||||
'132.0.0.0/6',
|
||||
'136.0.0.0/6',
|
||||
'140.0.0.0/6',
|
||||
'144.0.0.0/6',
|
||||
'148.0.0.0/6',
|
||||
'152.0.0.0/6',
|
||||
'156.0.0.0/6',
|
||||
'160.0.0.0/6',
|
||||
'164.0.0.0/6',
|
||||
'168.0.0.0/6',
|
||||
'172.0.0.0/6',
|
||||
'176.0.0.0/6',
|
||||
'180.0.0.0/6',
|
||||
'184.0.0.0/6',
|
||||
'188.0.0.0/6',
|
||||
'192.0.0.0/6',
|
||||
'196.0.0.0/6',
|
||||
'200.0.0.0/6',
|
||||
'204.0.0.0/6',
|
||||
'208.0.0.0/6',
|
||||
'212.0.0.0/6',
|
||||
'216.0.0.0/6',
|
||||
'220.0.0.0/6',
|
||||
'224.0.0.0/6',
|
||||
'228.0.0.0/6',
|
||||
'232.0.0.0/6',
|
||||
'236.0.0.0/6',
|
||||
'240.0.0.0/6',
|
||||
'244.0.0.0/6',
|
||||
'248.0.0.0/6',
|
||||
'252.0.0.0/6',
|
||||
]);
|
||||
expect(getSubnets('1.1.1.1/8')).to.eql([]);
|
||||
expect(getSubnets('1.1.1.1/11')).to.eql([
|
||||
'1.0.0.0/11',
|
||||
'1.32.0.0/11',
|
||||
'1.64.0.0/11',
|
||||
'1.96.0.0/11',
|
||||
'1.128.0.0/11',
|
||||
'1.160.0.0/11',
|
||||
'1.192.0.0/11',
|
||||
'1.224.0.0/11',
|
||||
]);
|
||||
expect(getSubnets('172.16.2.2/16')).to.eql([]);
|
||||
expect(getSubnets('172.16.2.2/26')).to.eql([
|
||||
'172.16.2.0/26',
|
||||
'172.16.2.64/26',
|
||||
'172.16.2.128/26',
|
||||
'172.16.2.192/26',
|
||||
]);
|
||||
expect(getSubnets('172.16.2.2/31').length).to.eql(128);
|
||||
expect(getSubnets('255.255.255.0/32')).to.eql([]);
|
||||
expect(getSubnets('2001:db8:0:85a3::ac1f:8001/62')).to.eql([]);
|
||||
expect(getSubnets('2001:db8:0:85a3:0:0:ac1f:8001/62')).to.eql([]);
|
||||
expect(getSubnets('2001:db8:0:85a3::ac1f:8001/112')).to.eql([]);
|
||||
expect(getSubnets('2001:db8:0:85a3:0:0:ac1f:8001/112')).to.eql([]);
|
||||
});
|
||||
});
|
||||
describe('getNetworksCount', () => {
|
||||
it('returns networks count', () => {
|
||||
expect(getNetworksCount('1.1.1.1/1')).to.eql(2);
|
||||
expect(getNetworksCount('1.1.1.1/2')).to.eql(4);
|
||||
expect(getNetworksCount('1.1.1.1/3')).to.eql(8);
|
||||
expect(getNetworksCount('1.1.1.1/4')).to.eql(16);
|
||||
expect(getNetworksCount('1.1.1.1/5')).to.eql(32);
|
||||
expect(getNetworksCount('1.1.1.1/6')).to.eql(64);
|
||||
expect(getNetworksCount('1.1.1.1/7')).to.eql(128);
|
||||
expect(getNetworksCount('1.1.1.1/8')).to.eql(0);
|
||||
expect(getNetworksCount('1.1.1.1/9')).to.eql(2);
|
||||
expect(getNetworksCount('1.1.1.1/10')).to.eql(4);
|
||||
expect(getNetworksCount('1.1.1.1/11')).to.eql(8);
|
||||
expect(getNetworksCount('1.1.1.1/12')).to.eql(16);
|
||||
expect(getNetworksCount('1.1.1.1/13')).to.eql(32);
|
||||
expect(getNetworksCount('1.1.1.1/14')).to.eql(64);
|
||||
expect(getNetworksCount('1.1.1.1/15')).to.eql(128);
|
||||
expect(getNetworksCount('1.1.1.1/16')).to.eql(0);
|
||||
expect(getNetworksCount('1.1.1.1/17')).to.eql(2);
|
||||
expect(getNetworksCount('1.1.1.1/18')).to.eql(4);
|
||||
expect(getNetworksCount('1.1.1.1/19')).to.eql(8);
|
||||
expect(getNetworksCount('1.1.1.1/20')).to.eql(16);
|
||||
expect(getNetworksCount('1.1.1.1/21')).to.eql(32);
|
||||
expect(getNetworksCount('1.1.1.1/22')).to.eql(64);
|
||||
expect(getNetworksCount('1.1.1.1/23')).to.eql(128);
|
||||
expect(getNetworksCount('1.1.1.1/24')).to.eql(0);
|
||||
expect(getNetworksCount('1.1.1.1/25')).to.eql(2);
|
||||
expect(getNetworksCount('1.1.1.1/26')).to.eql(4);
|
||||
expect(getNetworksCount('1.1.1.1/27')).to.eql(8);
|
||||
expect(getNetworksCount('1.1.1.1/28')).to.eql(16);
|
||||
expect(getNetworksCount('1.1.1.1/29')).to.eql(32);
|
||||
expect(getNetworksCount('1.1.1.1/30')).to.eql(64);
|
||||
expect(getNetworksCount('1.1.1.1/31')).to.eql(128);
|
||||
expect(getNetworksCount('1.1.1.1/32')).to.eql(0);
|
||||
expect(getNetworksCount('2001:db8:0:85a3::ac1f:8001/32')).to.eql(4294967296n);
|
||||
expect(getNetworksCount('2001:db8:0:85a3::ac1f:8001/42')).to.eql(4194304n);
|
||||
expect(getNetworksCount('2001:db8:0:85a3:0:0:ac1f:8001/62')).to.eql(4n);
|
||||
expect(getNetworksCount('2001:db8:0:85a3::ac1f:8001/112')).to.eql(-1);
|
||||
expect(getNetworksCount('2001:db8:0:85a3:0:0:ac1f:8001/122')).to.eql(-1);
|
||||
});
|
||||
});
|
||||
describe('getIPNetworkType', () => {
|
||||
it('returns network type', () => {
|
||||
expect(getIPNetworkType('1.1.1.1')).to.eql('Public');
|
||||
expect(getIPNetworkType('10.10.1.1')).to.eql('Private Use');
|
||||
expect(getIPNetworkType('172.16.0.1')).to.eql('Private Use');
|
||||
expect(getIPNetworkType('192.168.1.1')).to.eql('Private Use');
|
||||
expect(getIPNetworkType('255.255.255.0')).to.eql('Reserved');
|
||||
expect(getIPNetworkType('224.0.0.1')).to.eql('Multicast');
|
||||
expect(getIPNetworkType('198.51.100.1')).to.eql('Documentation (TEST-NET-2)');
|
||||
expect(getIPNetworkType('198.18.0.1')).to.eql('Benchmarking');
|
||||
expect(getIPNetworkType('169.254.0.1')).to.eql('Link Local');
|
||||
expect(getIPNetworkType('127.0.0.1')).to.eql('Loopback');
|
||||
expect(getIPNetworkType('2001:db8:8:4::2')).to.eql('Documentation');
|
||||
expect(getIPNetworkType('FF02::2')).to.eql('Multicast address');
|
||||
expect(getIPNetworkType('2345:0425:2CA1:0000:0000:0567:5673:23b5')).to.eql('Public');
|
||||
expect(getIPNetworkType('fdf8:f53b:82e4::53')).to.eql('Unique-Local');
|
||||
expect(getIPNetworkType('::ffff:192.0.2.47')).to.eql('IPv4-mapped Address');
|
||||
expect(getIPNetworkType('::ffff:ac12:0a09')).to.eql('IPv4-mapped Address');
|
||||
expect(getIPNetworkType('::1')).to.eql('Loopback Address');
|
||||
expect(getIPNetworkType('fe80::200:5aee:feaa:20a2')).to.eql('Link-Local Unicast');
|
||||
expect(getIPNetworkType('2001:0002::6c::430')).to.eql('Benchmarking');
|
||||
expect(getIPNetworkType('2001:0000:4136:e378:8000:63bf:3fff:fdd2')).to.eql('TEREDO');
|
||||
expect(getIPNetworkType('2002:cb0a:3cdd:1::1')).to.eql('6to4');
|
||||
expect(getIPNetworkType('ff01:0:0:0:0:0:0:2')).to.eql('Multicast address');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toARPA', () => {
|
||||
it('returns ARPA address', () => {
|
||||
expect(toARPA('1.1.1.1')).to.eql('1.1.1.1.in-addr.arpa');
|
||||
expect(toARPA('10.10.1.1')).to.eql('1.1.10.10.in-addr.arpa');
|
||||
expect(toARPA('192.168.1.1')).to.eql('1.1.168.192.in-addr.arpa');
|
||||
expect(toARPA('255.255.255.0')).to.eql('0.255.255.255.in-addr.arpa');
|
||||
expect(toARPA('FF02::2')).to.eql('2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa.');
|
||||
expect(toARPA('2345:0425:2CA1:0000:0000:0567:5673:23b5')).to.eql('5.b.3.2.3.7.6.5.7.6.5.0.0.0.0.0.0.0.0.0.1.a.c.2.5.2.4.0.5.4.3.2.ip6.arpa.');
|
||||
expect(toARPA('fdf8:f53b:82e4::53')).to.eql('3.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.e.2.8.b.3.5.f.8.f.d.f.ip6.arpa.');
|
||||
expect(toARPA('::ffff:192.0.2.47')).to.eql('f.2.2.0.0.0.0.c.f.f.f.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.');
|
||||
expect(toARPA('::1')).to.eql('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.');
|
||||
});
|
||||
});
|
||||
describe('toIPv4MappedAddress', () => {
|
||||
it('returns IPv4MappedAddress', () => {
|
||||
expect(toIPv4MappedAddress('1.1.1.1')).to.eql('::ffff:0101:0101');
|
||||
expect(toIPv4MappedAddress('10.10.1.1')).to.eql('::ffff:0a0a:0101');
|
||||
expect(toIPv4MappedAddress('172.18.10.9')).to.eql('::ffff:ac12:0a09');
|
||||
expect(toIPv4MappedAddress('192.168.1.1')).to.eql('::ffff:c0a8:0101');
|
||||
expect(toIPv4MappedAddress('255.255.255.0')).to.eql('::ffff:ffff:ff00');
|
||||
});
|
||||
});
|
||||
describe('toIPv4MappedAddressDecimal', () => {
|
||||
it('returns networks count', () => {
|
||||
expect(toIPv4MappedAddressDecimal('1.1.1.1')).to.eql('::ffff:1.1.1.1');
|
||||
expect(toIPv4MappedAddressDecimal('10.10.1.1')).to.eql('::ffff:10.10.1.1');
|
||||
expect(toIPv4MappedAddressDecimal('192.168.1.1')).to.eql('::ffff:192.168.1.1');
|
||||
expect(toIPv4MappedAddressDecimal('172.18.10.9')).to.eql('::ffff:172.18.10.9');
|
||||
expect(toIPv4MappedAddressDecimal('255.255.255.0')).to.eql('::ffff:255.255.255.0');
|
||||
expect(toIPv4MappedAddressDecimal('2001:db8:0:85a3::ac1f:8001')).to.eql('');
|
||||
});
|
||||
});
|
||||
describe('to6to4Prefix', () => {
|
||||
it('returns networks count', () => {
|
||||
expect(to6to4Prefix('1.1.1.1')).to.eql('2002:01:0:1:01:01::/48');
|
||||
expect(to6to4Prefix('10.10.1.1')).to.eql('2002:0a:0:a:01:01::/48');
|
||||
expect(to6to4Prefix('172.18.10.9')).to.eql('2002:ac:1:2:0a:09::/48');
|
||||
expect(to6to4Prefix('192.168.1.1')).to.eql('2002:c0:a:8:01:01::/48');
|
||||
expect(to6to4Prefix('255.255.255.0')).to.eql('2002:ff:f:f:ff:00::/48');
|
||||
expect(to6to4Prefix('2001:db8:0:85a3::ac1f:8001')).to.eql('');
|
||||
});
|
||||
});
|
||||
});
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
import { isIPv4 } from 'is-ip';
|
||||
import { Address4, Address6 } from 'ip-address';
|
||||
import { contains as containsCidr } from 'cidr-tools';
|
||||
import type { IPMatch } from 'ip-matching';
|
||||
import { IPMask, IPSubnetwork, getMatch } from 'ip-matching';
|
||||
import isCidr from 'is-cidr';
|
||||
import ipv4registry from './ipv4registry.json';
|
||||
import ipv6registry from './ipv6registry.json';
|
||||
|
||||
const IPv4MAX = (BigInt(2) ** BigInt(32)) - BigInt(1);
|
||||
|
||||
// IP range specific information, see IANA allocations.
|
||||
// http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
||||
const _ipv4Registry = new Map(ipv4registry.map(v => [v[0] as string, v[1]]));
|
||||
|
||||
// https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
|
||||
const _ipv6Registry = new Map(ipv6registry.map(v => [v[0] as string, v[1]]));
|
||||
|
||||
export function parseAsCIDR(form: string) {
|
||||
if (isCidr(form)) {
|
||||
return form;
|
||||
}
|
||||
|
||||
const ipMatch: IPMatch = getMatch(form);
|
||||
if (ipMatch instanceof IPSubnetwork) {
|
||||
return (ipMatch as IPSubnetwork).toString();
|
||||
}
|
||||
if (ipMatch instanceof IPMask) {
|
||||
return (ipMatch as IPMask).convertToSubnet()?.toString();
|
||||
}
|
||||
return (ipMatch.convertToMasks() || [])[0]?.convertToSubnet()?.toString();
|
||||
}
|
||||
|
||||
export function getSubnets(cidr: string) {
|
||||
const [address, prefix] = cidr.split('/');
|
||||
if (isIPv4(address)) {
|
||||
const prefix4Int = Number(prefix || '32');
|
||||
const getMask = (prefix: number) => (IPv4MAX >> (BigInt(32) - BigInt(prefix))) << (BigInt(32) - BigInt(prefix));
|
||||
const bigInt = BigInt((new Address4(address)).bigInteger());
|
||||
|
||||
const subnets = [];
|
||||
let startNetwork;
|
||||
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);
|
||||
for (let netIndex = 0; netIndex < netCount; netIndex += 1) {
|
||||
const netAddr = Address4.fromBigInteger(startNetwork.toString()).correctForm();
|
||||
subnets.push(`${netAddr}/${prefix4Int}`);
|
||||
startNetwork += increment;
|
||||
}
|
||||
return subnets;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
export function getNetworksCount(cidr: string) {
|
||||
const [address, prefix] = cidr.split('/');
|
||||
if (isIPv4(address)) {
|
||||
const prefix4Int = Number(prefix || '32');
|
||||
|
||||
if (prefix4Int % 8 === 0) {
|
||||
return 0;
|
||||
}
|
||||
else if (prefix4Int < 8) {
|
||||
return 2 ** prefix4Int;
|
||||
}
|
||||
else if (prefix4Int < 16) {
|
||||
return 2 ** (prefix4Int - 8);
|
||||
}
|
||||
else if (prefix4Int < 24) {
|
||||
return 2 ** (prefix4Int - 16);
|
||||
}
|
||||
else {
|
||||
return 2 ** (prefix4Int - 24);
|
||||
}
|
||||
}
|
||||
|
||||
const prefix6Int = Number(prefix || '128');
|
||||
return prefix6Int <= 64 ? (BigInt(2) ** BigInt(64n - BigInt(prefix6Int))) : -1;
|
||||
}
|
||||
|
||||
export function getIPNetworkType(address: string) {
|
||||
const results = [];
|
||||
for (const [addr, info] of (isIPv4(address) ? _ipv4Registry : _ipv6Registry).entries()) {
|
||||
const found = containsCidr([`${addr}/${Number(info[0])}`], address);
|
||||
if (found) {
|
||||
results.unshift(info[1]);
|
||||
}
|
||||
}
|
||||
return results.length === 0 ? 'Public' : results[0]?.toString();
|
||||
}
|
||||
|
||||
export function toARPA(address: string) {
|
||||
if (isIPv4(address)) {
|
||||
const bigInt = BigInt((new Address4(address)).bigInteger());
|
||||
const reverseIP = (
|
||||
[(bigInt & BigInt(255)), (bigInt >> BigInt(8) & BigInt(255)),
|
||||
(bigInt >> BigInt(16) & BigInt(255)),
|
||||
(bigInt >> BigInt(24) & BigInt(255)),
|
||||
].join('.')
|
||||
);
|
||||
return `${reverseIP}.in-addr.arpa`;
|
||||
}
|
||||
|
||||
return (new Address6(address)).reverseForm();
|
||||
}
|
||||
|
||||
export function toIPv4MappedAddress(address: string) {
|
||||
if (!isIPv4(address)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const hexIP = (new Address4(address)).toHex().replace(/:/g, '');
|
||||
return `::ffff:${hexIP.substring(0, 4)}:${hexIP.substring(4)}`;
|
||||
}
|
||||
|
||||
export function toIPv4MappedAddressDecimal(address: string) {
|
||||
if (!isIPv4(address)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `::ffff:${address}`;
|
||||
}
|
||||
|
||||
export function to6to4Prefix(address: string) {
|
||||
if (!isIPv4(address)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const hexIP = (new Address4(address)).toHex();
|
||||
return `2002:${hexIP.substring(0, 4)}:${hexIP.substring(4)}::/48`;
|
||||
}
|
||||
|
||||
export function toMicrosoftTranscription(address: string) {
|
||||
if (!isIPv4(address)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (new Address6(address)).microsoftTranscription();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
[
|
||||
["0.0.0.0", [8, "This host on this network"]],
|
||||
["10.0.0.0", [8, "Private Use"]],
|
||||
["100.64.0.0", [10, "Shared Address Space"]],
|
||||
["127.0.0.0", [8, "Loopback"]],
|
||||
["169.254.0.0", [16, "Link Local"]],
|
||||
["172.16.0.0", [12, "Private Use"]],
|
||||
["192.0.0.0", [24, "IETF Protocol Assignments"]],
|
||||
["192.0.0.0", [29, "IPv4 Service Continuity Prefix"]],
|
||||
["192.0.0.8", [32, "IPv4 dummy address"]],
|
||||
["192.0.0.9", [32, "Port Control Protocol Anycast"]],
|
||||
["192.0.0.10", [32, "Traversal Using Relays around NAT Anycast"]],
|
||||
["192.0.0.170", [32, "NAT64/DNS64 Discovery"]],
|
||||
["192.0.0.171", [32, "NAT64/DNS64 Discovery"]],
|
||||
["192.0.2.0", [24, "Documentation (TEST-NET-1)"]],
|
||||
["192.31.196.0", [24, "AS112-v4"]],
|
||||
["192.52.193.0", [24, "AMT"]],
|
||||
["192.88.99.0", [24, "Deprecated (6to4 Relay Anycast)"]],
|
||||
["192.168.0.0", [16, "Private Use"]],
|
||||
["192.175.48.0", [24, "Direct Delegation AS112 Service"]],
|
||||
["198.18.0.0", [15, "Benchmarking"]],
|
||||
["198.51.100.0", [24, "Documentation (TEST-NET-2)"]],
|
||||
["203.0.113.0", [24, "Documentation (TEST-NET-3)"]],
|
||||
["224.0.0.0", [24, "Multicast"]],
|
||||
["240.0.0.0", [4, "Reserved"]],
|
||||
["255.255.255.255", [32, "Limited Broadcast"]]
|
||||
]
|
||||
@@ -0,0 +1,24 @@
|
||||
[
|
||||
["::1", [128, "Loopback Address"]],
|
||||
["::", [128, "Unspecified Address"]],
|
||||
["::ffff::", [96, "IPv4-mapped Address"]],
|
||||
["64:ff9b::", [96, "IPv4-IPv6 Translat."]],
|
||||
["64:ff9b:1::", [48, "IPv4-IPv6 Translat."]],
|
||||
["100::", [64, "Discard-Only Address Block"]],
|
||||
["2001::", [23, "IETF Protocol Assignments"]],
|
||||
["2001::", [32, "TEREDO"]],
|
||||
["2001:1::1", [128, "Port Control Protocol Anycast"]],
|
||||
["2001:1::2", [128, "Traversal Using Relays around NAT Anycast"]],
|
||||
["2001:2::", [48, "Benchmarking"]],
|
||||
["2001:3::", [32, "AMT"]],
|
||||
["2001:4:112::", [48, "AS112-v6"]],
|
||||
["2001:5::", [32, "EID Space for LISP (Managed by RIPE NCC)"]],
|
||||
["2001:10::", [28, "Deprecated (previously ORCHID)"]],
|
||||
["2001:20::", [28, "ORCHIDv2"]],
|
||||
["2001:db8::", [32, "Documentation"]],
|
||||
["2002::", [16, "6to4"]],
|
||||
["2620:4f:8000::", [48, "Direct Delegation AS112 Service"]],
|
||||
["fc00::", [7, "Unique-Local"]],
|
||||
["fe80::", [10, "Link-Local Unicast"]],
|
||||
["ff00::", [8, "Multicast address"]]
|
||||
]
|
||||
Reference in new issue
Block a user