Esempio n. 1
0
        /// <summary>
        ///		搜索出口
        /// </summary>
        public static bool SearchOutbounds()
        {
            Logging.Info("正在搜索出口中");

            if (Win32Native.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) == 0)
            {
                Global.Adapter.Index   = pRoute.dwForwardIfIndex;
                Global.Adapter.Gateway = new IPAddress(pRoute.dwForwardNextHop);
                Logging.Info($"当前 网关 地址:{Global.Adapter.Gateway}");
            }
            else
            {
                Logging.Info("GetBestRoute 搜索失败");
                return(false);
            }

            Logging.Info($"搜索适配器index:{Global.Adapter.Index}");
            var AddressGot = false;

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                var adapterProperties = adapter.GetIPProperties();
                var p = adapterProperties.GetIPv4Properties();
                Logging.Info($"检测适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {p.Index}");

                // 通过索引查找对应适配器的 IPv4 地址
                if (p.Index == Global.Adapter.Index)
                {
                    var AdapterIPs = "";

                    foreach (var ip in adapterProperties.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            AddressGot             = true;
                            Global.Adapter.Address = ip.Address;
                            Logging.Info($"当前出口 IPv4 地址:{Global.Adapter.Address}");
                            break;
                        }
                        AdapterIPs = $"{ip.Address} | ";
                    }

                    if (!AddressGot)
                    {
                        if (AdapterIPs.Length > 3)
                        {
                            AdapterIPs = AdapterIPs.Substring(0, AdapterIPs.Length - 3);
                            Logging.Info($"所有出口地址:{AdapterIPs}");
                        }
                        Logging.Info("出口无 IPv4 地址,当前只支持 IPv4 地址");
                        return(false);
                    }
                    break;
                }
            }

            if (!AddressGot)
            {
                Logging.Info("无法找到当前使用适配器");
                return(false);
            }

            // 搜索 TUN/TAP 适配器的索引
            Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
            if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
            {
                if (MessageBox.Show(i18N.Translate("TUN/TAP driver is not detected. Is it installed now?"), i18N.Translate("Information"), MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    //安装Tap Driver
                    Process installProcess = new Process();
                    installProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    installProcess.StartInfo.FileName    = Path.Combine("bin/tap-driver", "install.bat");
                    installProcess.Start();
                    installProcess.WaitForExit();
                    installProcess.Close();

                    Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
                }
                else
                {
                    return(false);
                }
                //MessageBox.Show(i18N.Translate("Please install TAP-Windows and create an TUN/TAP adapter manually"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                // return false;
            }

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (adapter.Id == Global.TUNTAP.ComponentID)
                {
                    Global.TUNTAP.Adapter = adapter;
                    Global.TUNTAP.Index   = adapter.GetIPProperties().GetIPv4Properties().Index;

                    Logging.Info($"找到适配器:{adapter.Id}");

                    return(true);
                }
            }

            Logging.Info("无法找到出口");
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        ///		搜索出口
        /// </summary>
        public static bool SearchOutbounds()
        {
            Logging.Info("正在搜索出口中");

            if (Win32Native.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) == 0)
            {
                Global.Adapter.Index   = pRoute.dwForwardIfIndex;
                Global.Adapter.Gateway = new IPAddress(pRoute.dwForwardNextHop);
                Logging.Info($"当前 网关 地址:{Global.Adapter.Gateway}");
            }
            else
            {
                Logging.Info("GetBestRoute 搜索失败");
                return(false);
            }

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                var adapterProperties = adapter.GetIPProperties();
                var p = adapterProperties.GetIPv4Properties();

                // 通过索引查找对应适配器的 IPv4 地址
                if (p.Index == Global.Adapter.Index)
                {
                    var AddressGot = false;
                    var AdapterIPs = "";

                    foreach (var ip in adapterProperties.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            AddressGot             = true;
                            Global.Adapter.Address = ip.Address;
                            Logging.Info($"当前出口 IPv4 地址:{Global.Adapter.Address}");
                            break;
                        }
                        AdapterIPs = $"{ip.Address} | ";
                    }

                    if (!AddressGot)
                    {
                        if (AdapterIPs.Length > 3)
                        {
                            AdapterIPs = AdapterIPs.Substring(0, AdapterIPs.Length - 3);
                            Logging.Info($"所有出口地址:{AdapterIPs}");
                        }
                        Logging.Info("出口无 IPv4 地址,当前只支持 IPv4 地址");
                        return(false);
                    }
                    break;
                }
            }

            // 搜索 TUN/TAP 适配器的索引
            Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
            if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
            {
                MessageBox.Show(i18N.Translate("Please install TAP-Windows and create an TUN/TAP adapter manually"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);
            }

            var name = TUNTAP.GetName(Global.TUNTAP.ComponentID);

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (adapter.Name == name)
                {
                    Global.TUNTAP.Adapter = adapter;
                    Global.TUNTAP.Index   = adapter.GetIPProperties().GetIPv4Properties().Index;

                    break;
                }
            }
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        ///		搜索出口
        /// </summary>
        public static void SearchOutbounds()
        {
            Logging.Info("正在搜索出口中");

            using (var client = new UdpClient("114.114.114.114", 53))
            {
                var address = ((IPEndPoint)client.Client.LocalEndPoint).Address;
                Global.Adapter.Address = address;

                Logging.Info($"当前 IP 地址:{Global.Adapter.Address}");

                var addressGeted = false;

                var adapters = NetworkInterface.GetAllNetworkInterfaces();
                foreach (var adapter in adapters)
                {
                    var properties = adapter.GetIPProperties();

                    foreach (var information in properties.UnicastAddresses)
                    {
                        if (information.Address.AddressFamily == AddressFamily.InterNetwork && Equals(information.Address, address))
                        {
                            addressGeted = true;
                        }
                    }

                    foreach (var information in properties.GatewayAddresses)
                    {
                        if (information.Address.AddressFamily == AddressFamily.InterNetwork && addressGeted)
                        {
                            Global.Adapter.Index   = properties.GetIPv4Properties().Index;
                            Global.Adapter.Gateway = information.Address;

                            Logging.Info($"当前 网关 地址:{Global.Adapter.Gateway}");
                            break;
                        }
                    }

                    if (addressGeted)
                    {
                        break;
                    }
                }
            }

            // 搜索 TUN/TAP 适配器的索引
            Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
            if (String.IsNullOrEmpty(Global.TUNTAP.ComponentID))
            {
                MessageBox.Show(Utils.i18N.Translate("Please install TAP-Windows and create an TUN/TAP adapter manually"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                Environment.Exit(1);
            }

            var name = TUNTAP.GetName(Global.TUNTAP.ComponentID);

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (adapter.Name == name)
                {
                    Global.TUNTAP.Adapter = adapter;
                    Global.TUNTAP.Index   = adapter.GetIPProperties().GetIPv4Properties().Index;

                    break;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        ///		搜索出口
        /// </summary>
        public static bool SearchOutbounds()
        {
            Logging.Info("正在搜索出口中");

            if (Win32Native.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) == 0)
            {
                Global.Adapter.Index   = pRoute.dwForwardIfIndex;
                Global.Adapter.Gateway = new IPAddress(pRoute.dwForwardNextHop);
                Logging.Info($"当前 网关 地址:{Global.Adapter.Gateway}");
            }
            else
            {
                Logging.Info("GetBestRoute 搜索失败");
                return(false);
            }

            Logging.Info($"搜索适配器index:{Global.Adapter.Index}");
            var AddressGot = false;

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                try
                {
                    var adapterProperties = adapter.GetIPProperties();
                    var p = adapterProperties.GetIPv4Properties();
                    Logging.Info($"检测适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {p.Index}");

                    // 通过索引查找对应适配器的 IPv4 地址
                    if (p.Index == Global.Adapter.Index)
                    {
                        var AdapterIPs = "";

                        foreach (var ip in adapterProperties.UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                AddressGot             = true;
                                Global.Adapter.Address = ip.Address;
                                Logging.Info($"当前出口 IPv4 地址:{Global.Adapter.Address}");
                                break;
                            }
                            AdapterIPs = $"{ip.Address} | ";
                        }

                        if (!AddressGot)
                        {
                            if (AdapterIPs.Length > 3)
                            {
                                AdapterIPs = AdapterIPs.Substring(0, AdapterIPs.Length - 3);
                                Logging.Info($"所有出口地址:{AdapterIPs}");
                            }
                            Logging.Info("出口无 IPv4 地址,当前只支持 IPv4 地址");
                            return(false);
                        }
                        break;
                    }
                }
                catch (Exception)
                { }
            }

            if (!AddressGot)
            {
                Logging.Info("无法找到当前使用适配器");
                return(false);
            }

            // 搜索 TUN/TAP 适配器的索引
            Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
            if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
            {
                Logging.Info("未找到可用 TUN/TAP 适配器");
                if (MessageBoxX.Show(i18N.Translate("TUN/TAP driver is not detected. Is it installed now?"), confirm: true) == DialogResult.OK)
                {
                    addtap();
                    //给点时间,不然立马安装完毕就查找适配器可能会导致找不到适配器ID
                    Thread.Sleep(1000);
                    Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
                }
                else
                {
                    return(false);
                }
                //MessageBoxX.Show(i18N.Translate("Please install TAP-Windows and create an TUN/TAP adapter manually"));
                // return false;
            }

            foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (adapter.Id == Global.TUNTAP.ComponentID)
                {
                    Global.TUNTAP.Adapter = adapter;
                    Global.TUNTAP.Index   = adapter.GetIPProperties().GetIPv4Properties().Index;

                    Logging.Info($"找到适配器:{adapter.Id}");

                    return(true);
                }
            }

            Logging.Info("无法找到出口");
            return(false);
        }