Skip to content

Commit

Permalink
Merge pull request #248 from henning724/master
Browse files Browse the repository at this point in the history
修复程序启动崩溃,增加tun/tap相关的log
  • Loading branch information
AmazingDM authored Feb 28, 2020
2 parents b091424 + 2ae4f45 commit 5985f6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Netch/Controllers/TUNTAPController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,16 @@ public bool Start(Models.Server server, Models.Mode mode)
{
return false;
}


Logging.Info("设置绕行规则");
SetupBypass();
Logging.Info("设置绕行规则完毕");

Instance = new Process();
Instance.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory());
Instance.StartInfo.FileName = string.Format("{0}\\bin\\tun2socks.exe", Directory.GetCurrentDirectory());
var adapterName = TUNTAP.GetName(Global.TUNTAP.ComponentID);
Logging.Info($"tun2sock使用适配器:{adapterName}");

string dns;
if (Global.Settings.TUNTAP.UseCustomDNS)
Expand All @@ -299,7 +302,7 @@ public bool Start(Models.Server server, Models.Mode mode)
else
{
Instance.StartInfo.Arguments = string.Format("-proxyServer 127.0.0.1:{0} -tunAddr {1} -tunMask {2} -tunGw {3} -tunDns {4} -tunName \"{5}\"", Global.Settings.Socks5LocalPort, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, adapterName);
}
}

Instance.StartInfo.CreateNoWindow = true;
Instance.StartInfo.RedirectStandardError = true;
Expand Down
12 changes: 11 additions & 1 deletion Netch/Netch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ public static void Main(string[] args)
// 清理上一次的日志文件,防止淤积占用磁盘空间
if (Directory.Exists("logging"))
{
Directory.Delete("logging", true);
DirectoryInfo directory = new DirectoryInfo("logging");

foreach (FileInfo file in directory.GetFiles())
{
file.Delete();
}

foreach (DirectoryInfo dir in directory.GetDirectories())
{
dir.Delete(true);
}
}

// 预创建目录
Expand Down
12 changes: 11 additions & 1 deletion Netch/Utils/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ public static bool SearchOutbounds()
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 AddressGot = false;
var AdapterIPs = "";

foreach (var ip in adapterProperties.UnicastAddresses)
Expand Down Expand Up @@ -142,6 +144,12 @@ public static bool SearchOutbounds()
}
}

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

// 搜索 TUN/TAP 适配器的索引
Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
Expand Down Expand Up @@ -172,6 +180,8 @@ public static bool SearchOutbounds()
Global.TUNTAP.Adapter = adapter;
Global.TUNTAP.Index = adapter.GetIPProperties().GetIPv4Properties().Index;

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

return true;
}
}
Expand Down

0 comments on commit 5985f6c

Please sign in to comment.