generated from K-FOSS/TS-Core-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IPs): Parsing and searching for IPs and Subnets
- Loading branch information
Showing
7 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters