Compare commits

...

8 Commits

Author SHA1 Message Date
Amazing_DM
5985f6cf57 Merge pull request #248 from henning724/master
修复程序启动崩溃,增加tun/tap相关的log
2020-02-28 11:43:58 +08:00
henning
2ae4f454ff 修复:当logging文件夹存在时,程序启动崩溃 2020-02-27 15:45:39 +08:00
henning
48ab9a8995 增加寻找适配器的logging 2020-02-27 15:33:09 +08:00
henning
6f817ee245 增加更多tun/tap错误反馈信息到application.log 2020-02-27 15:24:10 +08:00
Connection Refused
b091424d19 Merge pull request #247 from henning724/master
An attempt to fix forever "Starting" problem while using TUN/TAP
2020-02-27 00:26:13 +08:00
henning
d612ab76de An attempt to fix forever "Starting" problem while using TUN/TAP 2020-02-26 15:30:15 +08:00
Connection Refused
61838ac8bc Remove built-in UDP DNS to TCP query 2020-02-19 18:29:44 +08:00
Connection Refused
6d66733644 Update submodule 2020-02-19 18:23:39 +08:00
7 changed files with 39 additions and 116 deletions

View File

@@ -1,37 +0,0 @@
using System;
using System.Net;
namespace Netch.Controllers
{
public class DNSController
{
public static DNS.Server.DnsServer Server = new DNS.Server.DnsServer(new Resolver());
public bool Start()
{
try
{
_ = Server.Listen(new IPEndPoint(IPAddress.IPv6Any, 53));
}
catch (Exception e)
{
Utils.Logging.Info(e.ToString());
return false;
}
return true;
}
public void Stop()
{
try
{
Server.Dispose();
}
catch (Exception e)
{
Utils.Logging.Info(e.ToString());
}
}
}
}

View File

@@ -5,6 +5,8 @@ using System.Net;
using System.Text;
using System.Threading;
using Netch.Utils;
namespace Netch.Controllers
{
public class TUNTAPController
@@ -30,11 +32,6 @@ namespace Netch.Controllers
public Models.Server SavedServer = new Models.Server();
public Models.Mode SavedMode = new Models.Mode();
/// <summary>
/// 本地 DNS 服务控制器
/// </summary>
public DNSController pDNSController = new DNSController();
/// <summary>
/// 配置 TUNTAP 适配器
/// </summary>
@@ -269,12 +266,16 @@ namespace Netch.Controllers
{
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)
@@ -291,18 +292,17 @@ namespace Netch.Controllers
}
else
{
pDNSController.Start();
dns = "127.0.0.1";
dns = "1.1.1.1,1.0.0.1";
}
if (server.Type == "Socks5")
{
Instance.StartInfo.Arguments = string.Format("-proxyServer {0}:{1} -tunAddr {2} -tunMask {3} -tunGw {4} -tunDns {5} -tunName \"{6}\"", server.Hostname, server.Port, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name);
Instance.StartInfo.Arguments = string.Format("-proxyServer {0}:{1} -tunAddr {2} -tunMask {3} -tunGw {4} -tunDns {5} -tunName \"{6}\"", server.Hostname, server.Port, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, adapterName);
}
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, Global.TUNTAP.Adapter.Name);
}
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;

View File

@@ -24,7 +24,17 @@ namespace Netch
// 清理上一次的日志文件,防止淤积占用磁盘空间
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);
}
}
// 预创建目录

View File

@@ -65,8 +65,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DNS" Version="5.0.0" />
<PackageReference Include="DnsClient" Version="1.2.0" />
<PackageReference Include="ini-parser" Version="2.5.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Buffers" Version="4.5.0" />

View File

@@ -1,59 +0,0 @@
using DNS.Protocol;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Netch
{
public class Resolver : DNS.Client.RequestResolver.IRequestResolver
{
public Task<IResponse> Resolve(IRequest request)
{
IResponse response = Response.FromRequest(request);
foreach (var question in response.Questions)
{
if (question.Type == RecordType.A)
{
var client = new DnsClient.LookupClient(DnsClient.NameServer.GooglePublicDns);
client.UseTcpOnly = true;
client.UseCache = true;
try
{
var result = client.Query(question.Name.ToString(), DnsClient.QueryType.A);
foreach (var item in result.Answers.ARecords())
{
response.AnswerRecords.Add(new DNS.Protocol.ResourceRecords.IPAddressResourceRecord(question.Name, item.Address));
}
}
catch (Exception)
{
// 跳过
}
}
else if (question.Type == RecordType.AAAA)
{
var client = new DnsClient.LookupClient(DnsClient.NameServer.GooglePublicDns);
client.UseTcpOnly = true;
client.UseCache = true;
try
{
var result = client.Query(question.Name.ToString(), DnsClient.QueryType.AAAA);
foreach (var item in result.Answers.AaaaRecords())
{
response.AnswerRecords.Add(new DNS.Protocol.ResourceRecords.IPAddressResourceRecord(question.Name, item.Address));
}
}
catch (Exception)
{
// 跳过
}
}
}
return Task.FromResult(response);
}
}
}

View File

@@ -105,15 +105,17 @@ namespace Netch.Utils
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)
@@ -142,6 +144,12 @@ namespace Netch.Utils
}
}
if (!AddressGot)
{
Logging.Info("无法找到当前使用适配器");
return false;
}
// 搜索 TUN/TAP 适配器的索引
Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
@@ -165,18 +173,21 @@ namespace Netch.Utils
// return false;
}
var name = TUNTAP.GetName(Global.TUNTAP.ComponentID);
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
{
if (adapter.Name == name)
if (adapter.Id == Global.TUNTAP.ComponentID)
{
Global.TUNTAP.Adapter = adapter;
Global.TUNTAP.Index = adapter.GetIPProperties().GetIPv4Properties().Index;
break;
Logging.Info($"找到适配器:{adapter.Id}");
return true;
}
}
return true;
Logging.Info("无法找到出口");
return false;
}
}
}