Skip to content

Commit

Permalink
Update TUNController.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed May 11, 2021
1 parent 4cc5998 commit 8b81df0
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions Netch/Controllers/TUNController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Utils;
using Netch.Interops;
using static Netch.Interops.tun2socks;

namespace Netch.Controllers
Expand Down Expand Up @@ -92,10 +93,10 @@ public void Start(in Mode mode)
if (!Init())
throw new MessageException("tun2socks start failed, reboot your system and start again.");

var tunIndex = (int)Interops.RouteHelper.ConvertLuidToIndex(tun_luid());
var tunIndex = (int)RouteHelper.ConvertLuidToIndex(tun_luid());
_tun = NetRoute.TemplateBuilder(Global.Settings.TUNTAP.Gateway, tunIndex);

Interops.RouteHelper.CreateUnicastIP(AddressFamily.InterNetwork,
RouteHelper.CreateUnicastIP(AddressFamily.InterNetwork,
Global.Settings.TUNTAP.Address,
(byte)Utils.Utils.SubnetToCidr(Global.Settings.TUNTAP.Netmask),
(ulong)tunIndex);
Expand All @@ -110,13 +111,12 @@ private void SetupRouteTable(Mode mode)
Global.MainForm.StatusText(i18N.Translate("Setup Route Table Rule"));
Global.Logger.Info("设置路由规则");

// Server Address
if (!IPAddress.IsLoopback(_serverAddresses))
// Server Address
RouteUtils.CreateRoute(_outbound.FillTemplate(_serverAddresses.ToString(), 32));

// Global Bypass IPs
foreach (var ip in Global.Settings.TUNTAP.BypassIPs)
RouteUtils.CreateRouteFill(_outbound, ip);
RouteUtils.CreateRouteFill(_outbound, Global.Settings.TUNTAP.BypassIPs);

var tunNetworkInterface = NetworkInterfaceUtils.Get(_tun.InterfaceIndex);
switch (mode.Type)
Expand Down Expand Up @@ -152,13 +152,12 @@ private void SetupRouteTable(Mode mode)
}
}

private bool ClearRouteTable()
private void ClearRouteTable()
{
if (!IPAddress.IsLoopback(_serverAddresses))
RouteUtils.DeleteRoute(_outbound.FillTemplate(_serverAddresses.ToString(), 32));

foreach (var ip in Global.Settings.TUNTAP.BypassIPs)
RouteUtils.DeleteRouteFill(_outbound, ip);
RouteUtils.DeleteRouteFill(_outbound, Global.Settings.TUNTAP.BypassIPs);

switch (_mode.Type)
{
Expand All @@ -167,8 +166,6 @@ private bool ClearRouteTable()
NetworkInterfaceUtils.SetInterfaceMetric(_outbound.InterfaceIndex);
break;
}

return true;
}

#endregion
Expand Down

0 comments on commit 8b81df0

Please sign in to comment.