using System.Text.Json.Nodes; using CliWrap; using CliWrap.Buffered; namespace InnovEnergy.EmuMeter; public readonly struct Nic { private static Command IpCommand { get; } = Cli .Wrap("/sbin/ip") .WithValidation(CommandResultValidation.None); private readonly JsonNode _Node; public Nic(JsonNode node) { _Node = node; } public Boolean IsEthernet { get { try { return _Node["link_type"]!.GetValue() == "ether"; } catch { return false; } } } public Boolean IsUp { get { // ReSharper disable once StringLiteralTypo try { return _Node["operstate"]!.GetValue() == "UP"; } catch { return false; } } } public IReadOnlyList Ip4Addresses { get { // ReSharper disable once StringLiteralTypo try { return _Node["addr_info"]! .AsArray() .TryWhere(n => n!["family"]!.GetValue() == "inet") .TrySelect(n => n!["local"]!.GetValue()) .ToList(); } catch { return Array.Empty(); } } } public String Name { get { // ReSharper disable once StringLiteralTypo try { return _Node["ifname"]!.GetValue(); } catch { return ""; } } } public async Task AddPointToPoint(String sourceAddress, String destinationAddress) { var result = await IpCommand .WithArguments($"address add local {sourceAddress} peer {destinationAddress} dev {Name}") .ExecuteAsync(); return result.ExitCode == 0; } public async Task RemoveAddress(String address) { var result = await IpCommand .WithArguments($"address del {address} dev {Name}") .ExecuteBufferedAsync(); return result.ExitCode == 0; } public async Task AddRoute(String route) { var result = await IpCommand .WithArguments($"route add {route} dev {Name}") .ExecuteAsync(); return result.ExitCode == 0; } public async Task RemoveRoute(String route) { var result = await IpCommand .WithArguments($"route del {route} dev {Name}") .ExecuteAsync(); return result.ExitCode == 0; } public static async Task> GetNetworkInterfaces() { try { var result = await IpCommand .WithArguments("-details -pretty -json address") .ExecuteBufferedAsync(); return JsonNode .Parse(result.StandardOutput)! .AsArray() .Where(n=>n != null) .Select(n=> new Nic(n!)) .ToList(); } catch { return Array.Empty(); } } }