File tree Expand file tree Collapse file tree 7 files changed +104
-5
lines changed Expand file tree Collapse file tree 7 files changed +104
-5
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 22import { PathLike } from 'fs' ;
33import { readFile } from 'fs/promises' ;
44import { load } from 'js-yaml' ;
5- import { Service } from 'typedi' ;
5+ import Container , { Service } from 'typedi' ;
66import { logger , LogMode } from '../../Library/Logger' ;
77import { setContainer } from '../../Utils/Containers' ;
88import { 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 ;
Original file line number Diff line number Diff line change @@ -5,11 +5,16 @@ import { Circuit } from '../Circuits/Circuit';
55import { Contact } from '../CommunityContacts/CommunityContact' ;
66import { Network as IPAMNetwork } from '../IPAM/IPAMConfig.gen' ;
77import { NetworkHost } from './NetworkHost' ;
8+ import { Address4 } from 'ip-address' ;
89
910@Service ( )
1011export 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 ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11// src/index.ts
2+ import 'reflect-metadata' ;
23import { timeout } from './Utils/timeout' ;
34import { logger , LogMode } from './Library/Logger' ;
45import { 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;
You can’t perform that action at this time.
0 commit comments