Adjust sing-box dns

pull/5094/head
2dust 2024-05-12 15:42:28 +08:00
parent 568144d6a2
commit c23379b3b6
5 changed files with 49 additions and 33 deletions

View File

@ -481,7 +481,7 @@ namespace v2rayN
/// 验证Domain地址是否合法
/// </summary>
/// <param name="domain"></param>
public static bool IsDomain(string domain)
public static bool IsDomain(string? domain)
{
//如果为空
if (IsNullOrEmpty(domain))

View File

@ -810,24 +810,9 @@ namespace v2rayN.Handler
{
return 0;
}
//Add the dns of the remote server domain
if (dns4Sbox.rules is null)
{
dns4Sbox.rules = new();
}
dns4Sbox.servers.Add(new()
{
tag = "local_local",
address = "223.5.5.5",
detour = Global.DirectTag,
});
dns4Sbox.rules.Add(new()
{
server = "local_local",
outbound = "any"
});
singboxConfig.dns = dns4Sbox;
GenDnsDomains(singboxConfig);
}
catch (Exception ex)
{
@ -836,6 +821,35 @@ namespace v2rayN.Handler
return 0;
}
private int GenDnsDomains(SingboxConfig singboxConfig)
{
var dns4Sbox = singboxConfig.dns ?? new();
dns4Sbox.servers ??= [];
dns4Sbox.rules ??= [];
var lstDomain = singboxConfig.outbounds
.Where(t => !Utils.IsNullOrEmpty(t.server) && Utils.IsDomain(t.server))
.Select(t => t.server)
.ToList();
if (lstDomain != null && lstDomain.Count > 0)
{
var tag = "local_local";
dns4Sbox.servers.Insert(0, new()
{
tag = tag,
address = "223.5.5.5",
detour = Global.DirectTag,
});
dns4Sbox.rules.Insert(0, new()
{
server = tag,
domain = lstDomain
});
}
singboxConfig.dns = dns4Sbox;
return 0;
}
private int GenExperimental(SingboxConfig singboxConfig)
{
if (_config.guiItem.enableStatistics)
@ -1076,18 +1090,18 @@ namespace v2rayN.Handler
singboxConfig.route.rules.Add(rule);
}
GenDns(new(), singboxConfig);
var dnsServer = singboxConfig.dns?.servers.FirstOrDefault();
if (dnsServer != null)
{
dnsServer.detour = singboxConfig.route.rules.LastOrDefault()?.outbound;
}
var dnsRule = singboxConfig.dns?.rules.Where(t => t.outbound != null).FirstOrDefault();
if (dnsRule != null)
{
singboxConfig.dns.rules = [];
singboxConfig.dns.rules.Add(dnsRule);
}
GenDnsDomains(singboxConfig);
//var dnsServer = singboxConfig.dns?.servers.FirstOrDefault();
//if (dnsServer != null)
//{
// dnsServer.detour = singboxConfig.route.rules.LastOrDefault()?.outbound;
//}
//var dnsRule = singboxConfig.dns?.rules.Where(t => t.outbound != null).FirstOrDefault();
//if (dnsRule != null)
//{
// singboxConfig.dns.rules = [];
// singboxConfig.dns.rules.Add(dnsRule);
//}
//msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
return 0;

View File

@ -95,7 +95,7 @@
{
public string type { get; set; }
public string tag { get; set; }
public string server { get; set; }
public string? server { get; set; }
public int? server_port { get; set; }
public string uuid { get; set; }
public string security { get; set; }

View File

@ -30,5 +30,6 @@
],
"server": "block"
}
]
],
"final": "local"
}

View File

@ -30,5 +30,6 @@
],
"server": "block"
}
]
],
"final": "local"
}