Exemplo n.º 1
0
        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}");
            }
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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}");
        }
Exemplo n.º 5
0
        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}");
        }
Exemplo n.º 6
0
        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}");
            }
        }