-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Carson Moore <[email protected]> Co-authored-by: Cameron Waterman <[email protected]>
- Loading branch information
1 parent
9ba4835
commit fa5c1f5
Showing
5 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
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,14 @@ | ||
// constants.ts | ||
export const defaultProjection = [ | ||
'id', | ||
'alias', | ||
'connected.data.state', | ||
'grains.data.minion_blackout as locked', | ||
'grains.data.boottime as systemStartTime', | ||
'grains.data.productname as model', | ||
'grains.data.manufacturer as vendor', | ||
'grains.data.osfullname as osFullName', | ||
'grains.data.ip4_interfaces as ip4Interfaces', | ||
'grains.data.ip6_interfaces as ip6Interfaces', | ||
'workspace' | ||
] |
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,67 @@ | ||
/** | ||
* Utility functions for parsing IP, MAC addresses, identify IP and hostname | ||
*/ | ||
export class NetworkUtils { | ||
static getIpAddressFromInterfaces(ipInterfaces: { [key: string]: string[] }): string | null { | ||
if (ipInterfaces) { | ||
const firstConnectedInterface = NetworkUtils.getFirstConnectedInterface(ipInterfaces); | ||
if (firstConnectedInterface) { | ||
return firstConnectedInterface.address; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static getFirstConnectedInterface(ipInterfaces: { [key: string]: string[] }): { name: string, address: string } | null { | ||
for (const ipInterfaceName in ipInterfaces) { | ||
if (!ipInterfaceName) { | ||
continue; | ||
} | ||
const ipInterfaceAddresses = ipInterfaces[ipInterfaceName]; | ||
if (ipInterfaceName !== 'lo' && this.isInterfaceConnected(ipInterfaceAddresses)) { | ||
return { | ||
name: ipInterfaceName, | ||
address: ipInterfaceAddresses[0] | ||
}; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static getMacAddressFromInterfaceName(ipInterfaceName: string, hwaddrInterfaces: { [key: string]: string }): string | null { | ||
if (!hwaddrInterfaces) { | ||
return null; | ||
} | ||
return hwaddrInterfaces[ipInterfaceName] || null; | ||
} | ||
|
||
static isValidIp(hostnameOrIpAddress: string): boolean { | ||
const validIpAddressRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/; | ||
return validIpAddressRegex.test(hostnameOrIpAddress); | ||
} | ||
|
||
static isValidHostname(hostnameOrIpAddress: string): boolean { | ||
const validHostnameRegex = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/; | ||
return validHostnameRegex.test(hostnameOrIpAddress); | ||
} | ||
|
||
private static isInterfaceConnected(ipInterfaceAddresses: string[]): boolean { | ||
if (!ipInterfaceAddresses || ipInterfaceAddresses.length === 0) { | ||
return false; | ||
} | ||
if (ipInterfaceAddresses.length > 1) { | ||
return true; | ||
} | ||
|
||
const ipInterfaceAddress = ipInterfaceAddresses[0]; | ||
if (ipInterfaceAddress === '0.0.0.0' || ipInterfaceAddress === '::') { | ||
// https://tools.ietf.org/html/rfc3513#section-2.5.2 | ||
// These addresses are used as non-routable meta-addresses | ||
// to designate an invalid, unknown, or non-applicable address. | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
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