Skip to content

Commit

Permalink
feat(IPs): Parsing and searching for IPs and Subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
EntraptaJ committed Jan 23, 2021
1 parent 7172cad commit 2cde5e9
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 5 deletions.
44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"fast-xml-parser": "^3.17.6",
"fluent-json-schema": "^2.0.3",
"got": "^11.8.1",
"ip-address": "^7.1.0",
"js-yaml": "^3.14.1",
"reflect-metadata": "^0.1.13",
"typedi": "^0.10.0",
Expand Down
12 changes: 9 additions & 3 deletions src/Modules/IPAM/IPAMConfigController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { PathLike } from 'fs';
import { readFile } from 'fs/promises';
import { load } from 'js-yaml';
import { Service } from 'typedi';
import Container, { Service } from 'typedi';
import { logger, LogMode } from '../../Library/Logger';
import { setContainer } from '../../Utils/Containers';
import { isObjectType } from '../../Utils/isTypes';
Expand Down Expand Up @@ -166,6 +166,8 @@ export class IPAMConfigController {
const circuits = this.processCircuits(ipamConfigFile.circuits);
const networks = this.processNetworks(ipamConfigFile.networks);

Container.set('networks', networks);

const ipam = new IPAM({
circuitLocations,
circuits,
Expand All @@ -174,8 +176,12 @@ export class IPAMConfigController {
networks,
});

for (const network of ipam.networks) {
network.hosts.map((networkHost) => console.log(networkHost.coreDevice));
const dns1Network = ipam.networks.find(
({ prefix }) => prefix === '64.184.193.0/30',
);

if (dns1Network) {
console.log(dns1Network.IPv4);
}

return;
Expand Down
5 changes: 5 additions & 0 deletions src/Modules/Networks/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import { Circuit } from '../Circuits/Circuit';
import { Contact } from '../CommunityContacts/CommunityContact';
import { Network as IPAMNetwork } from '../IPAM/IPAMConfig.gen';
import { NetworkHost } from './NetworkHost';
import { Address4 } from 'ip-address';

@Service()
export class Network implements IPAMNetwork {
public prefix: string;

public get IPv4(): Address4 {
return new Address4(this.prefix);
}

public description: string;

public circuitId?: string;
Expand Down
16 changes: 16 additions & 0 deletions src/Modules/Networks/NetworkController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// src/Modules/Networks/NetworkController.ts
import { Inject, Service } from 'typedi';
import { Network } from './Network';
import { Address4 } from 'ip-address';

@Service()
export class NetworkController {
@Inject('networks')
public networks: Network[];

public findIP(ipAddress: Address4): Network[] {
return this.networks.filter((network) =>
ipAddress.isInSubnet(network.IPv4),
);
}
}
15 changes: 15 additions & 0 deletions src/Utils/Networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// src/Utils/Networks.ts
import { Network } from '../Modules/Networks/Network';

/**
* Returns the smallest child subnet of an array of networks, intended to be used
* on an array containing a tree to retrieve the intended subnet
* @param networks Array of networks. Intended to be an array of networks within a "tree"
*
* @returns The Network with the smallest subnet size
*/
export function getSmallestSubnet(networks: Network[]): Network {
return networks.reduce((previous, current) =>
current.IPv4.subnetMask >= previous.IPv4.subnetMask ? current : previous,
);
}
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
// src/index.ts
import 'reflect-metadata';
import { timeout } from './Utils/timeout';
import { logger, LogMode } from './Library/Logger';
import { ipamConfigController } from './Modules/IPAM/IPAMConfigController';
import Container from 'typedi';
import { NetworkController } from './Modules/Networks/NetworkController';
import { Address4 } from 'ip-address';
import { getSmallestSubnet } from './Utils/Networks';

const config = ipamConfigController.loadFile('IPAM.yaml');
await ipamConfigController.loadFile('IPAM.yaml');

console.log(config);
const networkController = Container.get(NetworkController);

console.log('Parsing IP');

const ddosIP = new Address4('66.165.222.177/32');
const networks = networkController.findIP(ddosIP);

console.log('Lowest Network: ', getSmallestSubnet(networks));

/* interface Country {
name: string;
Expand Down

0 comments on commit 2cde5e9

Please sign in to comment.