summaryrefslogtreecommitdiff
path: root/master/server/src/networking.ts
blob: 5a58fcd69fbc1ca8634e540596e7911d322b9922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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();
	}
}