From ea33f87e590dd6f010590ecfc5d5b07dcabe0c64 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 31 May 2022 00:02:58 +0200 Subject: feat(master): add server package --- master/server/src/networking.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 master/server/src/networking.ts (limited to 'master/server/src/networking.ts') 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; + + return filter_object( + nw_interfaces, + (name, opt_info) => { + return !invalid_network_interfaces.includes(name) && opt_info !== undefined; + } + ) as Record; +} + +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(); + } +} -- cgit v1.2.3-18-g5258