diff options
Diffstat (limited to 'master/server/src/networking.ts')
| -rw-r--r-- | master/server/src/networking.ts | 38 | 
1 files changed, 38 insertions, 0 deletions
| diff --git a/master/server/src/networking.ts b/master/server/src/networking.ts new file mode 100644 index 0000000..5a58fcd --- /dev/null +++ b/master/server/src/networking.ts @@ -0,0 +1,38 @@ +import { filter_object } from "utility"; + +import { NetworkInterfaceInfo, networkInterfaces } from "os"; + +import { IPSubnetwork, IPv4 } from "ip-matching"; +import { promise as ping_promise } from "ping"; + +const probe = ping_promise.probe; + +export function get_network_interfaces() { +	const invalid_network_interfaces = [ "lo" ]; + +	const nw_interfaces = +		networkInterfaces() as Record<string, NetworkInterfaceInfo[] | undefined>; + +	return filter_object( +		nw_interfaces, +		(name, opt_info) => { +			return !invalid_network_interfaces.includes(name) && opt_info !== undefined; +		} +	) as Record<string, NetworkInterfaceInfo[]>; +} + +export async function ping_subnetwork(subnetwork: IPSubnetwork) { +	const first_ip = subnetwork.getFirst() as IPv4; + +	let current_ip = first_ip.getNext(); + +	while(current_ip !== undefined && subnetwork.matches(current_ip)) { +		const current_ip_str = current_ip.toString(); + +		const ping_result = await probe(current_ip_str, { timeout: 0.1 }); + +		console.log(`${current_ip_str}${ping_result.alive ? " - Alive" : ""}`); + +		current_ip = current_ip.getNext(); +	} +} | 
