/// <summary> /// The stop tunnel. /// </summary> public static void StopTunnel() { Tun2Socks.StopTun2Socks(); Tun2Socks.CleanAllTun2Socks(); Dns2Socks.StopDns2Socks(); Dns2Socks.CleanAllDns2Socks(); foreach ( IP4RouteTable ip4 in IP4RouteTable.GetCurrentTable() .Where( ip4 => ExceptionIPs.Any( ip => ip4.Destination != null && ip4.NextHop != null && ip4.Mask != null && ip4.Destination == ip.ToString() && ip4.Mask == "255.255.255.255" && ip4.NextHop != "0.0.0.0"))) { ip4.RemoveRouteRule(); } }
/// <summary> /// The start tunnel. /// </summary> /// <returns> /// The <see cref="bool"/>. /// </returns> public static bool StartTunnel() { if (!CommonLibrary.Common.IsValidIpSubnet(IpSubnet)) { return(false); } AdapterAddressRange = CommonLibrary.Common.MergeIpIntoIpSubnet( AdapterAddressRange, IpSubnet, IPAddress.Parse("10.0.0.1")); IPAddress addressGateWay = CommonLibrary.Common.MergeIpIntoIpSubnet( AdapterAddressRange, IpSubnet, IPAddress.Parse("10.0.0.2")); NetworkAdapter net = TapAdapter.InstallAnAdapter(TunnelName); if (net == null) { return(false); } if ( !Tun2Socks.StartTun2Socks( net, AdapterAddressRange, IpSubnet, addressGateWay, SocksProxyEndPoint)) { return(false); } NetworkAdapterConfiguration netconfig = net.GetNetworkConfiguration(); if (netconfig == null) { return(false); } int timeout = 30; timeout = timeout / 3; string dnsString = string.Empty; if (AutoDnsResolving && AutoDnsResolvingAddress != null) { Dns2Socks.StartDns2Socks(AutoDnsResolvingAddress, SocksProxyEndPoint); dnsString = SocksProxyEndPoint.Address.ToString(); } else if (DnsResolvingAddress != null && DnsResolvingAddress2 != null) { dnsString = DnsResolvingAddress + "," + DnsResolvingAddress2; } else if (DnsResolvingAddress != null) { dnsString = DnsResolvingAddress.ToString(); } else if (DnsResolvingAddress2 != null) { dnsString = DnsResolvingAddress2.ToString(); } while (true) { if (netconfig.SetIpAddresses(AdapterAddressRange.ToString(), IpSubnet.ToString()) && netconfig.SetDnsSearchOrder(dnsString)) { break; } if (timeout == 0) { return(false); } timeout--; Thread.Sleep(3000); } IP4RouteTable.AddChangeRouteRule( IPAddress.Parse("0.0.0.0"), addressGateWay, 2, IPAddress.Parse("0.0.0.0"), (int)net.InterfaceIndex); int lowestMetric = 1000; IP4RouteTable me = null; List <IP4RouteTable> internetRules = new List <IP4RouteTable>(); foreach (IP4RouteTable r in IP4RouteTable.GetCurrentTable().ToArray()) { if (r.Destination == "0.0.0.0") { if (r.InterfaceIndex == net.InterfaceIndex) { me = r; } else { internetRules.Add(r); if (lowestMetric > r.Metric1) { lowestMetric = r.Metric1; } } } } if (me == null) { return(false); } foreach (IP4RouteTable ip4 in IP4RouteTable.GetCurrentTable() .Where( ip4 => ExceptionIPs.Any( ip => ip4.Destination != null && ip4.NextHop != null && ip4.Mask != null && ip4.Destination == ip.ToString() && ip4.Mask == "255.255.255.255" && ip4.NextHop != "0.0.0.0"))) { ip4.RemoveRouteRule(); } int des = 0; if (me.Metric1 >= lowestMetric) { des = (me.Metric1 - lowestMetric) + 1; } foreach (IP4RouteTable r in internetRules) { if (des > 0) { r.SetMetric1(r.Metric1 + des); } if (string.IsNullOrEmpty(r.NextHop)) { continue; } foreach (IPAddress ip in ExceptionIPs) { // if (IP4RouteTable.GetBestInterfaceIndexForIP(ip) == net.InterfaceIndex) IP4RouteTable.AddChangeRouteRule(ip, IPAddress.Parse(r.NextHop), 1, null, r.InterfaceIndex); } } FlashDnsCache(); return(true); }