Skip to content

Commit 2cde5e9

Browse files
committed
feat(IPs): Parsing and searching for IPs and Subnets
1 parent 7172cad commit 2cde5e9

File tree

7 files changed

+104
-5
lines changed

7 files changed

+104
-5
lines changed

package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"fast-xml-parser": "^3.17.6",
4848
"fluent-json-schema": "^2.0.3",
4949
"got": "^11.8.1",
50+
"ip-address": "^7.1.0",
5051
"js-yaml": "^3.14.1",
5152
"reflect-metadata": "^0.1.13",
5253
"typedi": "^0.10.0",

src/Modules/IPAM/IPAMConfigController.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { PathLike } from 'fs';
33
import { readFile } from 'fs/promises';
44
import { load } from 'js-yaml';
5-
import { Service } from 'typedi';
5+
import Container, { Service } from 'typedi';
66
import { logger, LogMode } from '../../Library/Logger';
77
import { setContainer } from '../../Utils/Containers';
88
import { isObjectType } from '../../Utils/isTypes';
@@ -166,6 +166,8 @@ export class IPAMConfigController {
166166
const circuits = this.processCircuits(ipamConfigFile.circuits);
167167
const networks = this.processNetworks(ipamConfigFile.networks);
168168

169+
Container.set('networks', networks);
170+
169171
const ipam = new IPAM({
170172
circuitLocations,
171173
circuits,
@@ -174,8 +176,12 @@ export class IPAMConfigController {
174176
networks,
175177
});
176178

177-
for (const network of ipam.networks) {
178-
network.hosts.map((networkHost) => console.log(networkHost.coreDevice));
179+
const dns1Network = ipam.networks.find(
180+
({ prefix }) => prefix === '64.184.193.0/30',
181+
);
182+
183+
if (dns1Network) {
184+
console.log(dns1Network.IPv4);
179185
}
180186

181187
return;

src/Modules/Networks/Network.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import { Circuit } from '../Circuits/Circuit';
55
import { Contact } from '../CommunityContacts/CommunityContact';
66
import { Network as IPAMNetwork } from '../IPAM/IPAMConfig.gen';
77
import { NetworkHost } from './NetworkHost';
8+
import { Address4 } from 'ip-address';
89

910
@Service()
1011
export class Network implements IPAMNetwork {
1112
public prefix: string;
1213

14+
public get IPv4(): Address4 {
15+
return new Address4(this.prefix);
16+
}
17+
1318
public description: string;
1419

1520
public circuitId?: string;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// src/Modules/Networks/NetworkController.ts
2+
import { Inject, Service } from 'typedi';
3+
import { Network } from './Network';
4+
import { Address4 } from 'ip-address';
5+
6+
@Service()
7+
export class NetworkController {
8+
@Inject('networks')
9+
public networks: Network[];
10+
11+
public findIP(ipAddress: Address4): Network[] {
12+
return this.networks.filter((network) =>
13+
ipAddress.isInSubnet(network.IPv4),
14+
);
15+
}
16+
}

src/Utils/Networks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// src/Utils/Networks.ts
2+
import { Network } from '../Modules/Networks/Network';
3+
4+
/**
5+
* Returns the smallest child subnet of an array of networks, intended to be used
6+
* on an array containing a tree to retrieve the intended subnet
7+
* @param networks Array of networks. Intended to be an array of networks within a "tree"
8+
*
9+
* @returns The Network with the smallest subnet size
10+
*/
11+
export function getSmallestSubnet(networks: Network[]): Network {
12+
return networks.reduce((previous, current) =>
13+
current.IPv4.subnetMask >= previous.IPv4.subnetMask ? current : previous,
14+
);
15+
}

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
// src/index.ts
2+
import 'reflect-metadata';
23
import { timeout } from './Utils/timeout';
34
import { logger, LogMode } from './Library/Logger';
45
import { ipamConfigController } from './Modules/IPAM/IPAMConfigController';
6+
import Container from 'typedi';
7+
import { NetworkController } from './Modules/Networks/NetworkController';
8+
import { Address4 } from 'ip-address';
9+
import { getSmallestSubnet } from './Utils/Networks';
510

6-
const config = ipamConfigController.loadFile('IPAM.yaml');
11+
await ipamConfigController.loadFile('IPAM.yaml');
712

8-
console.log(config);
13+
const networkController = Container.get(NetworkController);
14+
15+
console.log('Parsing IP');
16+
17+
const ddosIP = new Address4('66.165.222.177/32');
18+
const networks = networkController.findIP(ddosIP);
19+
20+
console.log('Lowest Network: ', getSmallestSubnet(networks));
921

1022
/* interface Country {
1123
name: string;

0 commit comments

Comments
 (0)