mirror of
https://github.com/netchx/netch.git
synced 2026-05-11 23:45:06 +08:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5985f6cf57 | ||
|
|
2ae4f454ff | ||
|
|
48ab9a8995 | ||
|
|
6f817ee245 | ||
|
|
b091424d19 | ||
|
|
d612ab76de | ||
|
|
61838ac8bc | ||
|
|
6d66733644 |
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,8 @@ using System.Net;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
|
using Netch.Utils;
|
||||||
|
|
||||||
namespace Netch.Controllers
|
namespace Netch.Controllers
|
||||||
{
|
{
|
||||||
public class TUNTAPController
|
public class TUNTAPController
|
||||||
@@ -30,11 +32,6 @@ namespace Netch.Controllers
|
|||||||
public Models.Server SavedServer = new Models.Server();
|
public Models.Server SavedServer = new Models.Server();
|
||||||
public Models.Mode SavedMode = new Models.Mode();
|
public Models.Mode SavedMode = new Models.Mode();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 本地 DNS 服务控制器
|
|
||||||
/// </summary>
|
|
||||||
public DNSController pDNSController = new DNSController();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 配置 TUNTAP 适配器
|
/// 配置 TUNTAP 适配器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -269,12 +266,16 @@ namespace Netch.Controllers
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logging.Info("设置绕行规则");
|
||||||
SetupBypass();
|
SetupBypass();
|
||||||
|
Logging.Info("设置绕行规则完毕");
|
||||||
|
|
||||||
Instance = new Process();
|
Instance = new Process();
|
||||||
Instance.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory());
|
Instance.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory());
|
||||||
Instance.StartInfo.FileName = string.Format("{0}\\bin\\tun2socks.exe", 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;
|
string dns;
|
||||||
if (Global.Settings.TUNTAP.UseCustomDNS)
|
if (Global.Settings.TUNTAP.UseCustomDNS)
|
||||||
@@ -291,18 +292,17 @@ namespace Netch.Controllers
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pDNSController.Start();
|
dns = "1.1.1.1,1.0.0.1";
|
||||||
dns = "127.0.0.1";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (server.Type == "Socks5")
|
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
|
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.CreateNoWindow = true;
|
||||||
Instance.StartInfo.RedirectStandardError = true;
|
Instance.StartInfo.RedirectStandardError = true;
|
||||||
|
|||||||
@@ -24,7 +24,17 @@ namespace Netch
|
|||||||
// 清理上一次的日志文件,防止淤积占用磁盘空间
|
// 清理上一次的日志文件,防止淤积占用磁盘空间
|
||||||
if (Directory.Exists("logging"))
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 预创建目录
|
// 预创建目录
|
||||||
|
|||||||
@@ -65,8 +65,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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="ini-parser" Version="2.5.2" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
<PackageReference Include="System.Buffers" Version="4.5.0" />
|
<PackageReference Include="System.Buffers" Version="4.5.0" />
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -105,15 +105,17 @@ namespace Netch.Utils
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logging.Info($"搜索适配器index:{Global.Adapter.Index}");
|
||||||
|
var AddressGot = false;
|
||||||
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
||||||
{
|
{
|
||||||
var adapterProperties = adapter.GetIPProperties();
|
var adapterProperties = adapter.GetIPProperties();
|
||||||
var p = adapterProperties.GetIPv4Properties();
|
var p = adapterProperties.GetIPv4Properties();
|
||||||
|
Logging.Info($"检测适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {p.Index}");
|
||||||
|
|
||||||
// 通过索引查找对应适配器的 IPv4 地址
|
// 通过索引查找对应适配器的 IPv4 地址
|
||||||
if (p.Index == Global.Adapter.Index)
|
if (p.Index == Global.Adapter.Index)
|
||||||
{
|
{
|
||||||
var AddressGot = false;
|
|
||||||
var AdapterIPs = "";
|
var AdapterIPs = "";
|
||||||
|
|
||||||
foreach (var ip in adapterProperties.UnicastAddresses)
|
foreach (var ip in adapterProperties.UnicastAddresses)
|
||||||
@@ -142,6 +144,12 @@ namespace Netch.Utils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!AddressGot)
|
||||||
|
{
|
||||||
|
Logging.Info("无法找到当前使用适配器");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// 搜索 TUN/TAP 适配器的索引
|
// 搜索 TUN/TAP 适配器的索引
|
||||||
Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
|
Global.TUNTAP.ComponentID = TUNTAP.GetComponentID();
|
||||||
if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
|
if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID))
|
||||||
@@ -165,18 +173,21 @@ namespace Netch.Utils
|
|||||||
// return false;
|
// return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var name = TUNTAP.GetName(Global.TUNTAP.ComponentID);
|
|
||||||
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
||||||
{
|
{
|
||||||
if (adapter.Name == name)
|
if (adapter.Id == Global.TUNTAP.ComponentID)
|
||||||
{
|
{
|
||||||
Global.TUNTAP.Adapter = adapter;
|
Global.TUNTAP.Adapter = adapter;
|
||||||
Global.TUNTAP.Index = adapter.GetIPProperties().GetIPv4Properties().Index;
|
Global.TUNTAP.Index = adapter.GetIPProperties().GetIPv4Properties().Index;
|
||||||
|
|
||||||
break;
|
Logging.Info($"找到适配器:{adapter.Id}");
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
Logging.Info("无法找到出口");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Submodule translations updated: b96659f737...db0c5ae291
Reference in New Issue
Block a user