From ef354340d6a8eb2721816e18c9f56e957fb603bb Mon Sep 17 00:00:00 2001 From: James Allardice Date: Tue, 11 Aug 2020 15:38:33 +0100 Subject: [PATCH 1/2] Add original network to Network instance This will allow consumers to access the original network on the parsed instance. --- src/network.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/network.ts b/src/network.ts index bdda096..6d5f441 100644 --- a/src/network.ts +++ b/src/network.ts @@ -8,9 +8,12 @@ const AFTER = 1; export class Network { public readonly addr = new Address(); + public readonly network?: string; private netbits = -1; public constructor(network?: string, throwErrors?: boolean) { + this.network = network; + if (network) { var net = parse.network(network, throwErrors); if (net) { From aac37a47ca6e38ab915683d3c4c05a5ce0fdddf9 Mon Sep 17 00:00:00 2001 From: James Allardice Date: Tue, 11 Aug 2020 15:39:13 +0100 Subject: [PATCH 2/2] Return Network instance instead of boolean This allows consumers to access additional information on a matched network. --- src/match.ts | 12 +++++------ src/tests/match.test.ts | 44 +++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/match.ts b/src/match.ts index 4d3c692..283ba05 100644 --- a/src/match.ts +++ b/src/match.ts @@ -36,16 +36,16 @@ export class Matcher { * * @param network - An address or subnet * - * @returns A boolean + * @returns A Network or null */ public has(network: string) { var net = shared.parseBaseNetwork(network, false, false); - if (!net || !net.isValid()) return false; + if (!net || !net.isValid()) return null; var idx = sort.binarySearchForInsertionIndex(net, this.sorted); - if (idx < 0) return false; - if (idx < this.sorted.length && this.sorted[idx].contains(net)) return true; - if (idx - 1 >= 0 && this.sorted[idx - 1].contains(net)) return true; - return false; + if (idx < 0) return null; + if (idx < this.sorted.length && this.sorted[idx].contains(net)) return this.sorted[idx]; + if (idx - 1 >= 0 && this.sorted[idx - 1].contains(net)) return this.sorted[idx - 1]; + return null; } /** diff --git a/src/tests/match.test.ts b/src/tests/match.test.ts index 2abf85a..900f29f 100644 --- a/src/tests/match.test.ts +++ b/src/tests/match.test.ts @@ -16,7 +16,7 @@ test("sanity check Matcher #1", () => { "192.168.0.255/32" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.254")).toEqual(false); + expect(matcher.has("192.168.0.254")).toEqual(null); }); test("sanity check Matcher #2", () => { @@ -35,7 +35,11 @@ test("sanity check Matcher #2", () => { "192.168.0.255/32" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.254")).toEqual(true); + expect(matcher.has("192.168.0.254")).toEqual({ + addr: { arr: [192, 168, 0, 0] }, + netbits: 24, + network: "192.168.0.0/24" + }); }); test("sanity check Matcher #3", () => { @@ -54,7 +58,11 @@ test("sanity check Matcher #3", () => { "192.168.0.255/32" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.254")).toEqual(true); + expect(matcher.has("192.168.0.254")).toEqual({ + addr: { arr: [192, 168, 0, 0] }, + netbits: 24, + network: "192.168.0.0/24" + }); }); test("sanity check Matcher #4", () => { @@ -73,7 +81,7 @@ test("sanity check Matcher #4", () => { "192.168.0.255/32" ]; const matcher = new match.Matcher(input); - expect(matcher.has("foobar")).toEqual(false); + expect(matcher.has("foobar")).toEqual(null); }); test("sanity check Matcher #5", () => { @@ -92,7 +100,11 @@ test("sanity check Matcher #5", () => { "192.168.0.255/32" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.255")).toEqual(true); + expect(matcher.has("192.168.0.255")).toEqual({ + addr: { arr: [192, 168, 0, 255] }, + netbits: 32, + network: "192.168.0.255/32" + }); }); test("sanity check Matcher #6", () => { @@ -111,7 +123,7 @@ test("sanity check Matcher #6", () => { "192.168.0.254/31" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.25")).toEqual(false); + expect(matcher.has("192.168.0.25")).toEqual(null); }); test("sanity check Matcher #6", () => { @@ -131,7 +143,7 @@ test("sanity check Matcher #6", () => { ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.0")).toEqual(false); + expect(matcher.has("192.168.0.0")).toEqual(null); }); test("sanity check Matcher #7", () => { @@ -150,7 +162,11 @@ test("sanity check Matcher #7", () => { "192.168.0.254/32" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.124/32")).toEqual(true); + expect(matcher.has("192.168.0.124/32")).toEqual({ + addr: { arr: [192, 168, 0, 123] }, + netbits: 31, + network: "192.168.0.123/32" + }); }); test("sanity check Matcher #8", () => { @@ -169,7 +185,11 @@ test("sanity check Matcher #8", () => { "192.168.0.254/31" ]; const matcher = new match.Matcher(input); - expect(matcher.has("192.168.0.255")).toEqual(true); + expect(matcher.has("192.168.0.255")).toEqual({ + addr: { arr: [192, 168, 0, 254] }, + netbits: 31, + network: "192.168.0.254/31" + }); }); test("sanity check Matcher #9", () => { @@ -191,5 +211,9 @@ test("sanity check Matcher #9", () => { input.forEach((s: string) => { matcher.add(s); }); - expect(matcher.has("192.168.0.255")).toEqual(true); + expect(matcher.has("192.168.0.255")).toEqual({ + addr: { arr: [192, 168, 0, 254] }, + netbits: 31, + network: "192.168.0.254/31" + }); });