public static void SearchOutboundAdapter(bool logging = true) { // 寻找出口适配器 if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) != 0) { Logging.Error("GetBestRoute 搜索失败"); throw new Exception("GetBestRoute 搜索失败"); } Global.Outbound.Index = (int)pRoute.dwForwardIfIndex; // 根据 IP Index 寻找 出口适配器 var adapter = NetworkInterface.GetAllNetworkInterfaces() .First(_ => { try { return(_.GetIPProperties().GetIPv4Properties().Index == Global.Outbound.Index); } catch { return(false); } }); Global.Outbound.Adapter = adapter; Global.Outbound.Gateway = new IPAddress(pRoute.dwForwardNextHop.S_un_b); if (logging) { Logging.Info($"出口 IPv4 地址:{Global.Outbound.Address}"); Logging.Info($"出口 网关 地址:{Global.Outbound.Gateway}"); Logging.Info($"出口适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.Outbound.Index}"); } }
public static IEnumerable <IpInterface> GetIpInterfaces() { var adapters = IpHlpApi.GetIPAdapters(IpHlpApi.FAMILY.AF_UNSPEC); var devices = CaptureDeviceList.Instance.OfType <NpcapDevice>(); return(devices.Where(x => !x.Loopback).Select(x => new IpInterface(x.Name, GetFriendlyName(x, adapters)))); }
public static NetRoute GetBestRouteTemplate(out IPAddress address) { if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != 0) { throw new MessageException("GetBestRoute 搜索失败"); } address = new IPAddress(route.dwForwardNextHop.S_addr); var gateway = new IPAddress(route.dwForwardNextHop.S_un_b); return(TemplateBuilder(gateway.ToString(), (int)route.dwForwardIfIndex)); }
private static bool AddressIsFoundInTcpTable(IPAddress[] addresses) { var tcpTable = IpHlpApi.GetTcpTableEx(); bool addressFound = false; for (int i = 0; i < tcpTable.Length; i++) { if (addresses.Any(x => x.ToString() == tcpTable[i].RemoteAddress.ToString())) { addressFound = true; break; } } return(addressFound); }
public static NetworkInterface GetBest(AddressFamily addressFamily = AddressFamily.InterNetwork) { var ipAddress = addressFamily switch { AddressFamily.InterNetwork => "114.114.114.114", AddressFamily.InterNetworkV6 => throw new NotImplementedException(), _ => throw new ArgumentOutOfRangeException(nameof(addressFamily), addressFamily, null) }; if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0), 0, out var route) != 0) { throw new MessageException("GetBestRoute 搜索失败"); } return(Get((int)route.dwForwardIfIndex)); }
public OutboundAdapter() { // 寻找出口适配器 if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) != 0) { throw new MessageException("GetBestRoute 搜索失败"); } NetworkInterface = NetworkInterfaceUtils.Get((int)pRoute.dwForwardIfIndex); Address = new IPAddress(pRoute.dwForwardNextHop.S_addr); InterfaceIndex = pRoute.dwForwardIfIndex; Gateway = new IPAddress(pRoute.dwForwardNextHop.S_un_b); Global.Logger.Info($"出口 网关 地址:{Gateway}"); Global.Logger.Info($"出口适配器:{NetworkInterface.Name} {NetworkInterface.Id} {NetworkInterface.Description}, index: {InterfaceIndex}"); }
public void IpHlpApi_CloseRemoteTcpConnectionTest() { if (!IsElevated()) { Assert.Inconclusive("Must be run as administrator (or elevated) in order to perform this test"); return; } MakeConnection("http://hotmail.com"); var remoteIps = GetIps("hotmail.com"); Assert.IsTrue(AddressIsFoundInTcpTable(remoteIps)); foreach (var item in remoteIps) { IpHlpApi.CloseRemoteTcpConnection(item); } Assert.IsTrue(!AddressIsFoundInTcpTable(remoteIps)); }
public object System() { SystemInfo sys = new SystemInfo(); var memoryLoad = 100 - ((double)sys.MemoryAvailable / (double)sys.PhysicalMemory) * 100; var cpuLoad = sys.CpuLoad; var iftable1 = IpHlpApi.GetIfTable(); long inSpeed1 = iftable1.Sum(m => m.dwInOctets); long outSpeed1 = iftable1.Sum(m => m.dwOutOctets); Thread.Sleep(1000); var iftable2 = IpHlpApi.GetIfTable(); var inSpeed2 = iftable2.Sum(m => m.dwInOctets); var outSpeed2 = iftable2.Sum(m => m.dwOutOctets); var inSpeed = inSpeed2 - inSpeed1; var outSpeed = outSpeed2 - outSpeed1; var ada = IpHlpApi.GetInterfaceInfo(); ulong total = 0; foreach (var a in ada.Adapter) { MIB_IF_ROW2 row = new MIB_IF_ROW2(a.Index); IpHlpApi.GetIfEntry2(ref row); if (row.InOctets > 0) { total += row.ReceiveLinkSpeed; } } total = total / 8; return(new { memoryLoad = memoryLoad, cpuLoad = cpuLoad, inSpeed = (double)inSpeed * 100 / Convert.ToDouble(total), outSpeed = (double)outSpeed * 100 / Convert.ToDouble(total) }); }
public OutboundAdapter() { // 寻找出口适配器 if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) != 0) { throw new MessageException("GetBestRoute 搜索失败"); } NetworkInterface = NetworkInterface.GetAllNetworkInterfaces() .First(ni => ni.Supports(NetworkInterfaceComponent.IPv4) && ni.GetIPProperties().GetIPv4Properties().Index == pRoute.dwForwardIfIndex); Address = new IPAddress(pRoute.dwForwardNextHop.S_addr); InterfaceIndex = (int)pRoute.dwForwardIfIndex; Gateway = new IPAddress(pRoute.dwForwardNextHop.S_un_b); Logging.Info($"出口 网关 地址:{Gateway}"); Logging.Info($"出口适配器:{NetworkInterface.Name} {NetworkInterface.Id} {NetworkInterface.Description}, index: {InterfaceIndex}"); }
private void TerminateAllConnectionsToSelectedItemRemoteAddress() { if (SelectedItem != null && !string.IsNullOrWhiteSpace(SelectedItem.RemoteAddress)) { try { IpHlpApi.CloseRemoteTcpConnection(IPAddress.Parse(SelectedItem.RemoteAddress)); } catch (Exception ex) { if (!ex.IsCritical()) { MessageBox.Show(string.Format("Exception encountered while attempting to terminate all connections to remote address '{0}':\r\n\r\n{1}", SelectedItem.RemoteAddress, ex.Message), "", MessageBoxButton.OK, MessageBoxImage.Warning); } else { throw; } } } }
public OutboundAdapter(bool logging = true) { // 寻找出口适配器 if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) != 0) { throw new MessageException("GetBestRoute 搜索失败"); } NetworkInterface = NetworkInterface.GetAllNetworkInterfaces() .First(ni => ni.Supports(NetworkInterfaceComponent.IPv4) && ni.GetIPProperties().GetIPv4Properties().Index == pRoute.dwForwardIfIndex); InterfaceIndex = (int)pRoute.dwForwardIfIndex; Gateway = new IPAddress(pRoute.dwForwardNextHop.S_un_b); _parametersRegistry = Registry.LocalMachine.OpenSubKey($@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{NetworkInterface.Id}", true) !; if (logging) { Logging.Info($"出口 网关 地址:{Gateway}"); Logging.Info($"出口适配器:{NetworkInterface.Name} {NetworkInterface.Id} {NetworkInterface.Description}, index: {InterfaceIndex}"); } }