summaryrefslogtreecommitdiff
path: root/master/server/src/networking.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-05-31 00:02:58 +0200
committerHampusM <hampus@hampusmat.com>2022-05-31 00:02:58 +0200
commitea33f87e590dd6f010590ecfc5d5b07dcabe0c64 (patch)
tree44f8575239552cdfaadfbe87fb27d51e2c2c8dc9 /master/server/src/networking.ts
parent7afb8aedd6f3553aa0ec138373aea18068abd2f8 (diff)
feat(master): add server package
Diffstat (limited to 'master/server/src/networking.ts')
-rw-r--r--master/server/src/networking.ts38
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();
+ }
+}