feat(dns): support optional IPv6 FakeIP pools (#10464)

This commit is contained in:
yc99-gif
2026-11-04 09:51:43 +08:00
committed by GitHub
parent 0d9e84556e
commit 2239982a63
19 changed files with 420 additions and 28 deletions
+25
View File
@@ -0,0 +1,25 @@
namespace ServiceLib.Common;
public static class FakeIPUtils
{
public static bool TryParseIPv6Range(string? value, out string range, out long poolSize)
{
range = string.IsNullOrWhiteSpace(value) ? Global.FakeIPv6Ranges.First() : value.Trim();
poolSize = 65535;
if (!System.Net.IPNetwork.TryParse(range, out var network)
|| network.BaseAddress.AddressFamily != AddressFamily.InterNetworkV6
|| network.PrefixLength == 128)
{
return false;
}
range = network.ToString();
var hostBits = 128 - network.PrefixLength;
if (hostBits < 16)
{
// Xray requires the mapping capacity to be smaller than the address pool.
poolSize = (1L << hostBits) - 1;
}
return true;
}
}
+1
View File
@@ -240,6 +240,7 @@ public class JsonUtils
[JsonSerializable(typeof(List<Rule4Sbox>))]
[JsonSerializable(typeof(List<Ruleset4Sbox>))]
[JsonSerializable(typeof(V2rayConfig))]
[JsonSerializable(typeof(FakeDns4Ray))]
[JsonSerializable(typeof(Dns4Ray))]
[JsonSerializable(typeof(DnsServer4Ray))]
[JsonSerializable(typeof(Inbounds4Ray))]
+6
View File
@@ -773,6 +773,12 @@ public class Global
"11.0.0.0/8",
];
public static readonly List<string> FakeIPv6Ranges =
[
"fc00::/18",
"2001:2::/48",
];
public static readonly List<string> RootCertProviders =
[
"system",
@@ -115,6 +115,8 @@ public static class ConfigHandler
config.SimpleDNSItem ??= InitBuiltinSimpleDNS();
config.SimpleDNSItem.BlockAAAAQuery ??= false;
config.SimpleDNSItem.FakeIPRange ??= Global.FakeIPRanges.FirstOrDefault();
config.SimpleDNSItem.FakeIPv6 ??= false;
config.SimpleDNSItem.FakeIPv6Range ??= Global.FakeIPv6Ranges.First();
config.SimpleDNSItem.GlobalFakeIp ??= true;
config.SimpleDNSItem.BootstrapDNS ??= Global.DomainPureIPDNSAddress.FirstOrDefault();
config.SimpleDNSItem.ServeStale ??= false;
@@ -254,6 +254,8 @@ public class SimpleDNSItem
public bool? FakeIP { get; set; }
public bool? GlobalFakeIp { get; set; }
public string? FakeIPRange { get; set; }
public bool? FakeIPv6 { get; set; }
public string? FakeIPv6Range { get; set; }
public bool? BlockBindingQuery { get; set; }
public bool? BlockAAAAQuery { get; set; }
public string? DirectDNS { get; set; }
@@ -4,7 +4,7 @@ public class V2rayConfig
{
public Log4Ray log { get; set; }
public JsonObject? dns { get; set; }
public FakeDns4Ray? fakedns { get; set; }
public JsonNode? fakedns { get; set; }
public List<Inbounds4Ray> inbounds { get; set; }
public List<Outbounds4Ray> outbounds { get; set; }
public Routing4Ray routing { get; set; }
+28 -1
View File
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
@@ -5444,5 +5444,32 @@ namespace ServiceLib.Resx {
return ResourceManager.GetString("UpgradeAppNotExistTip", resourceCulture);
}
}
/// <summary>
/// IPv6 FakeIP
/// </summary>
public static string TbFakeIPv6 {
get {
return ResourceManager.GetString("TbFakeIPv6", resourceCulture);
}
}
/// <summary>
/// Return fake IPv6 addresses as well. Requires FakeIP to be enabled and AAAA queries to be allowed.
/// </summary>
public static string TbFakeIPv6Tips {
get {
return ResourceManager.GetString("TbFakeIPv6Tips", resourceCulture);
}
}
/// <summary>
/// Enter a valid IPv6 CIDR range with at least two addresses, for example fc00::/18.
/// </summary>
public static string FillCorrectFakeIPv6Range {
get {
return ResourceManager.GetString("FillCorrectFakeIPv6Range", resourceCulture);
}
}
}
}
+10 -1
View File
@@ -1905,6 +1905,15 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="menuAddMasqueServer" xml:space="preserve">
<value>Add [MASQUE]</value>
</data>
<data name="TbFakeIPv6" xml:space="preserve">
<value>IPv6 FakeIP</value>
</data>
<data name="TbFakeIPv6Tips" xml:space="preserve">
<value>Return fake IPv6 addresses as well. Requires FakeIP to be enabled and AAAA queries to be allowed.</value>
</data>
<data name="FillCorrectFakeIPv6Range" xml:space="preserve">
<value>Enter a valid IPv6 CIDR range with at least two addresses, for example fc00::/18.</value>
</data>
<data name="menuColumns" xml:space="preserve">
<value>Columns</value>
</data>
@@ -1914,4 +1923,4 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="LvPreResolve" xml:space="preserve">
<value>Pre-resolve server domains</value>
</data>
</root>
</root>
+10 -1
View File
@@ -1897,6 +1897,15 @@
<data name="menuAddMasqueServer" xml:space="preserve">
<value>添加 [MASQUE]</value>
</data>
<data name="TbFakeIPv6" xml:space="preserve">
<value>IPv6 FakeIP</value>
</data>
<data name="TbFakeIPv6Tips" xml:space="preserve">
<value>同时返回 IPv6 假地址,需启用 FakeIP 且未屏蔽 AAAA 查询。</value>
</data>
<data name="FillCorrectFakeIPv6Range" xml:space="preserve">
<value>请输入至少包含两个地址的有效 IPv6 网段,例如 fc00::/18。</value>
</data>
<data name="menuColumns" xml:space="preserve">
<value>列</value>
</data>
@@ -1906,4 +1915,4 @@
<data name="LvPreResolve" xml:space="preserve">
<value>预解析节点域名</value>
</data>
</root>
</root>
@@ -1903,4 +1903,13 @@
<data name="TbSortingProcess" xml:space="preserve">
<value>行程</value>
</data>
<data name="TbFakeIPv6" xml:space="preserve">
<value>IPv6 FakeIP</value>
</data>
<data name="TbFakeIPv6Tips" xml:space="preserve">
<value>同時傳回 IPv6 假位址,需啟用 FakeIP 且未封鎖 AAAA 查詢。</value>
</data>
<data name="FillCorrectFakeIPv6Range" xml:space="preserve">
<value>請輸入至少包含兩個位址的有效 IPv6 網段,例如 fc00::/18。</value>
</data>
</root>
@@ -131,6 +131,14 @@ public partial class CoreConfigSingboxService
type = "fakeip",
inet4_range = fakeipRange,
};
if (simpleDnsItem.FakeIPv6 == true && simpleDnsItem.BlockAAAAQuery != true)
{
if (!FakeIPUtils.TryParseIPv6Range(simpleDnsItem.FakeIPv6Range, out var range6, out _))
{
throw new ArgumentException("Invalid IPv6 FakeIP range.");
}
fakeip.inet6_range = range6;
}
_coreConfig.dns.servers.Add(fakeip);
}
}
@@ -145,11 +145,22 @@ public partial class CoreConfigV2rayService
{
// Ignore
}
_coreConfig.fakedns = new()
var fakeDns = new FakeDns4Ray
{
ipPool = fakeipRange,
poolSize = poolSize,
};
_coreConfig.fakedns = JsonUtils.SerializeToNode(fakeDns);
if (_config.SimpleDNSItem.FakeIPv6 == true && _config.SimpleDNSItem.BlockAAAAQuery != true)
{
if (!FakeIPUtils.TryParseIPv6Range(_config.SimpleDNSItem.FakeIPv6Range, out var range6, out var poolSize6))
{
throw new ArgumentException("Invalid IPv6 FakeIP range.");
}
_coreConfig.fakedns = new JsonArray(
_coreConfig.fakedns,
JsonUtils.SerializeToNode(new FakeDns4Ray { ipPool = range6, poolSize = poolSize6 }));
}
}
private void FillDnsServers(Dns4Ray dnsItem)
@@ -8,6 +8,8 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable
[Reactive] public partial bool AddCommonHosts { get; set; }
[Reactive] public partial bool FakeIP { get; set; }
[Reactive] public partial string FakeIPRange { get; set; }
[Reactive] public partial bool FakeIPv6 { get; set; }
[Reactive] public partial string FakeIPv6Range { get; set; }
[Reactive] public partial bool BlockBindingQuery { get; set; }
[Reactive] public partial bool BlockAAAAQuery { get; set; }
[Reactive] public partial string DirectDNS { get; set; }
@@ -36,6 +38,7 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable
[Reactive] public partial bool SBCustomDNSEnableCompatible { get; set; }
[ObservableAsProperty] public partial bool IsSimpleDNSEnabled { get; }
[ObservableAsProperty] public partial bool IsFakeIPv6Available { get; }
public ReactiveCommand<RxVoid, RxVoid> SaveCmd { get; }
public ReactiveCommand<RxVoid, RxVoid> ImportDefConfig4V2rayCompatibleCmd { get; }
@@ -66,6 +69,12 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable
(a, b) => !(a && b))
.ToProperty(this, nameof(IsSimpleDNSEnabled));
_isFakeIPv6AvailableHelper = this.WhenAnyValue(
x => x.FakeIP,
x => x.BlockAAAAQuery,
(fakeIP, blockAAAA) => fakeIP && !blockAAAA)
.ToProperty(this, nameof(IsFakeIPv6Available));
_ = Init();
}
@@ -77,6 +86,8 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable
AddCommonHosts = item.AddCommonHosts ?? false;
FakeIP = item.FakeIP ?? false;
FakeIPRange = item.FakeIPRange ?? string.Empty;
FakeIPv6 = item.FakeIPv6 ?? false;
FakeIPv6Range = item.FakeIPv6Range ?? Global.FakeIPv6Ranges.First();
BlockBindingQuery = item.BlockBindingQuery ?? false;
BlockAAAAQuery = item.BlockAAAAQuery ?? false;
DirectDNS = item.DirectDNS ?? string.Empty;
@@ -108,10 +119,19 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable
private async Task SaveSettingAsync()
{
if (IsSimpleDNSEnabled && IsFakeIPv6Available && FakeIPv6
&& !FakeIPUtils.TryParseIPv6Range(FakeIPv6Range, out _, out _))
{
NoticeManager.Instance.Enqueue(ResUI.FillCorrectFakeIPv6Range);
return;
}
_config.SimpleDNSItem.UseSystemHosts = UseSystemHosts;
_config.SimpleDNSItem.AddCommonHosts = AddCommonHosts;
_config.SimpleDNSItem.FakeIP = FakeIP;
_config.SimpleDNSItem.FakeIPRange = FakeIPRange;
_config.SimpleDNSItem.FakeIPv6 = FakeIPv6;
_config.SimpleDNSItem.FakeIPv6Range = FakeIPv6Range;
_config.SimpleDNSItem.BlockBindingQuery = BlockBindingQuery;
_config.SimpleDNSItem.BlockAAAAQuery = BlockAAAAQuery;
_config.SimpleDNSItem.DirectDNS = DirectDNS;