[Netch] WTF

This commit is contained in:
Connection Refused
2021-10-19 02:41:49 +08:00
parent cf21c08600
commit cc69dff5e1
11 changed files with 117 additions and 73 deletions

View File

@@ -12,6 +12,14 @@ namespace Netch.Controllers.Mode
AIO_FILTERICMP,
AIO_FILTERTCP,
AIO_FILTERUDP,
AIO_FILTERDNS,
AIO_ICMPING,
AIO_DNSONLY,
AIO_DNSPROX,
AIO_DNSHOST,
AIO_DNSPORT,
AIO_TGTHOST,
AIO_TGTPORT,
@@ -46,10 +54,21 @@ namespace Netch.Controllers.Mode
Global.Logger.Info(String.Format("{0:x} Redirector.bin", Utils.FileHelper.Checksum("bin\\Redirector.bin")));
var mode = m as Models.Mode.ProcessMode.ProcessMode;
Methods.aio_dial(NameList.AIO_FILTERLOOPBACK, mode.Loopback ? "true" : "false");
Methods.aio_dial(NameList.AIO_FILTERINTRANET, mode.Intranet ? "true" : "false");
Methods.aio_dial(NameList.AIO_FILTERTCP, mode.TCP ? "true" : "false");
Methods.aio_dial(NameList.AIO_FILTERUDP, mode.UDP ? "true" : "false");
Methods.aio_dial(NameList.AIO_FILTERLOOPBACK, mode.Loopback.ToString().ToLower());
Methods.aio_dial(NameList.AIO_FILTERINTRANET, mode.Intranet.ToString().ToLower());
Methods.aio_dial(NameList.AIO_FILTERTCP, mode.TCP.ToString().ToLower());
Methods.aio_dial(NameList.AIO_FILTERUDP, mode.UDP.ToString().ToLower());
Methods.aio_dial(NameList.AIO_FILTERDNS, mode.DNS.ToString().ToLower());
Methods.aio_dial(NameList.AIO_ICMPING, Global.Config.ProcessMode.Icmping.ToString());
Methods.aio_dial(NameList.AIO_DNSONLY, Global.Config.ProcessMode.DNSOnly.ToString().ToLower());
Methods.aio_dial(NameList.AIO_DNSPROX, Global.Config.ProcessMode.DNSProx.ToString().ToLower());
Methods.aio_dial(NameList.AIO_DNSHOST, Global.Config.ProcessMode.DNSHost);
Methods.aio_dial(NameList.AIO_DNSPORT, Global.Config.ProcessMode.DNSPort.ToString());
Methods.aio_dial(NameList.AIO_TGTUSER, "");
Methods.aio_dial(NameList.AIO_TGTPASS, "");
Methods.aio_dial(NameList.AIO_CLRNAME, "");
Methods.aio_dial(NameList.AIO_BYPNAME, AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "\\\\"));
@@ -65,14 +84,10 @@ namespace Netch.Controllers.Mode
Methods.aio_dial(NameList.AIO_TGTPORT, node.Port.ToString());
if (!String.IsNullOrEmpty(node.Username))
{
Methods.aio_dial(NameList.AIO_TGTUSER, node.Username);
}
if (!String.IsNullOrEmpty(node.Password))
{
Methods.aio_dial(NameList.AIO_TGTPASS, node.Password);
}
}
break;
default:

View File

@@ -60,7 +60,7 @@ namespace Netch.Controllers.Mode
public static extern ulong tun_getDL();
}
private Tools.TunTap.Outbound Outbound = new();
private Tools.Outbound Outbound = new();
private Interface.IController DNSController;
private bool AssignInterface()
@@ -70,9 +70,7 @@ namespace Netch.Controllers.Mode
var address = Global.Config.TunMode.Network.Split('/')[0];
var netmask = byte.Parse(Global.Config.TunMode.Network.Split('/')[1]);
if (!Utils.RouteHelper.CreateUnicastIP(AddressFamily.InterNetwork, address, netmask, index))
{
return false;
}
NetworkInterface adapter = Utils.RouteHelper.GetInterfaceByIndex(index);
if (adapter == null)
@@ -197,43 +195,28 @@ namespace Netch.Controllers.Mode
}
if (!Methods.tun_init())
{
return false;
}
if (Global.Config.Generic.AioDNS)
{
this.DNSController = new Other.DNS.AioDNSController();
}
else
{
this.DNSController = new Other.DNS.DNSProxyController();
}
if (!this.DNSController.Create(s, m))
{
return false;
}
if (!this.AssignInterface())
{
return false;
}
if (!this.CreateServerRoute(s))
{
return false;
}
if (!this.CreateHandleRoute(mode))
{
return false;
}
if (File.Exists("ipcidr.txt"))
{
File.Delete("ipcidr.txt");
}
return true;
}

View File

@@ -12,7 +12,7 @@ namespace Netch.Controllers.Server
{
StartInfo = new ProcessStartInfo()
{
FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\Shadowsocks.exe"),
FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\ss-local.exe"),
WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"),
CreateNoWindow = true,
UseShellExecute = false,

View File

@@ -12,7 +12,7 @@ namespace Netch.Controllers.Server
{
StartInfo = new ProcessStartInfo()
{
FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\ShadowsocksR.exe"),
FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\ssr-local.exe"),
WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"),
CreateNoWindow = true,
UseShellExecute = false,

View File

@@ -3,9 +3,33 @@
public class ProcessMode
{
/// <summary>
/// DNS
/// 伪造 ICMP 延迟
/// </summary>
[Newtonsoft.Json.JsonProperty("dns")]
public string DNS = "1.1.1.1:53";
[Newtonsoft.Json.JsonProperty("icmping")]
public int Icmping = 1;
/// <summary>
/// 仅劫持规则内进程
/// </summary>
[Newtonsoft.Json.JsonProperty("dnsOnly")]
public bool DNSOnly = false;
/// <summary>
/// 远程 DNS 查询
/// </summary>
[Newtonsoft.Json.JsonProperty("dnsProx")]
public bool DNSProx = true;
/// <summary>
/// DNS 地址
/// </summary>
[Newtonsoft.Json.JsonProperty("dnsHost")]
public string DNSHost = "1.1.1.1";
/// <summary>
/// DNS 端口
/// </summary>
[Newtonsoft.Json.JsonProperty("dnsPort")]
public ushort DNSPort = 53;
}
}

View File

@@ -39,6 +39,12 @@ namespace Netch.Models.Mode.ProcessMode
[Newtonsoft.Json.JsonProperty("filterUDP")]
public bool UDP = true;
/// <summary>
/// 过滤 DNS 流量
/// </summary>
[Newtonsoft.Json.JsonProperty("filterDNS")]
public bool DNS = true;
/// <summary>
/// 绕过列表
/// </summary>

View File

@@ -45,7 +45,16 @@ namespace Netch.Models.Server
/// 解析地址
/// </summary>
/// <returns></returns>
public string Resolve() => (Utils.DNS.Fetch(this.Host) != IPAddress.Any) ? Utils.DNS.Fetch(this.Host).ToString() : this.Host;
public string Resolve()
{
var addr = Utils.DNS.Fetch(this.Host);
while (addr == IPAddress.Any)
{
addr = Utils.DNS.Fetch(this.Host);
}
return addr.ToString();
}
/// <summary>
/// 获取备注

View File

@@ -4,7 +4,7 @@ using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace Netch.Tools.TunTap
namespace Netch.Tools
{
public class Outbound
{
@@ -39,23 +39,24 @@ namespace Netch.Tools.TunTap
/// <returns></returns>
public bool Get()
{
if (Vanara.PInvoke.Win32Error.NO_ERROR != Vanara.PInvoke.IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route))
{
if (Vanara.PInvoke.IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != Vanara.PInvoke.Win32Error.NO_ERROR)
return false;
}
this.Index = route.dwForwardIfIndex;
this.Interface = NetworkInterface.GetAllNetworkInterfaces()
.First(nic =>
{
var ipp = nic.GetIPProperties();
if (nic.Supports(NetworkInterfaceComponent.IPv4))
{
return ipp.GetIPv4Properties().Index == this.Index;
}
this.Interface = NetworkInterface.GetAllNetworkInterfaces().First(nic =>
{
var ipp = nic.GetIPProperties();
return false;
});
if (nic.Supports(NetworkInterfaceComponent.IPv4))
{
return ipp.GetIPv4Properties().Index == this.Index;
}
return false;
});
if (this.Interface == null)
return false;
var addr = this.Interface.GetIPProperties().UnicastAddresses.First(ipf =>
{

View File

@@ -6,6 +6,22 @@ namespace Netch.Utils
{
public static class DNS
{
/// <summary>
/// 缓存内容
/// </summary>
private class CacheEntry
{
/// <summary>
/// 缓存时间
/// </summary>
public long Unix;
/// <summary>
/// 地址
/// </summary>
public IPAddress IP;
}
/// <summary>
/// 缓存表
/// </summary>
@@ -22,21 +38,22 @@ namespace Netch.Utils
{
if (Cache.Contains(name))
{
return Cache[name] as IPAddress;
var data = Cache[name] as CacheEntry;
if (DateTimeOffset.Now.ToUnixTimeSeconds() - data.Unix < 120)
return data.IP;
Cache.Remove(name);
}
var task = Dns.GetHostAddressesAsync(name);
if (!task.Wait(1000))
{
return IPAddress.Any;
}
if (task.Result.Length == 0)
{
return IPAddress.Any;
}
Cache.Add(name, task.Result[0]);
Cache.Add(name, new CacheEntry() { Unix = DateTimeOffset.Now.ToUnixTimeSeconds(), IP = task.Result[0] });
return task.Result[0];
}
catch (Exception e)

View File

@@ -8,20 +8,11 @@ namespace Netch.Utils
{
public static class Methods
{
public enum NF_STATUS : int
{
NF_STATUS_SUCCESS = 0,
NF_STATUS_FAIL = -1,
NF_STATUS_INVALID_ENDPOINT_ID = -2,
NF_STATUS_NOT_INITIALIZED = -3,
NF_STATUS_IO_ERROR = -4
}
[DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern bool aio_register([MarshalAs(UnmanagedType.LPWStr)] string name);
[DllImport("nfapinet", CallingConvention = CallingConvention.Cdecl)]
public static extern NF_STATUS nf_registerDriver(string name);
[DllImport("nfapinet", CallingConvention = CallingConvention.Cdecl)]
public static extern NF_STATUS nf_unRegisterDriver(string driverName);
[DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern bool aio_unregister([MarshalAs(UnmanagedType.LPWStr)] string driverName);
}
public static readonly string dName = "netfilter2";
@@ -42,10 +33,9 @@ namespace Netch.Utils
}
File.Copy(nPath, oPath);
var status = Methods.nf_registerDriver(dName);
if (status != Methods.NF_STATUS.NF_STATUS_SUCCESS)
if (!Methods.aio_register(dName))
{
Global.Logger.Error($"注册 Netfilter 驱动失败{status}");
Global.Logger.Error($"注册 Netfilter 驱动失败");
return false;
}
@@ -88,10 +78,9 @@ namespace Netch.Utils
{
if (File.Exists(oPath))
{
var status = Methods.nf_unRegisterDriver(dName);
if (status != Methods.NF_STATUS.NF_STATUS_SUCCESS)
if (!Methods.aio_unregister(dName))
{
Global.Logger.Error($"取消注册 Netfilter 驱动失败{status}");
Global.Logger.Error($"取消注册 Netfilter 驱动失败");
return false;
}

View File

@@ -6,7 +6,7 @@ namespace Netch.Utils
public static class WinTUN
{
public static string oPath = Path.Combine(Environment.SystemDirectory, "wintun.dll");
public static string nPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\wintun.bin");
public static string nPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\wintun.dll");
/// <summary>
/// 注册 WinTUN 驱动