Skip to content

Commit c23379b

Browse files
committed
Adjust sing-box dns
1 parent 568144d commit c23379b

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

v2rayN/v2rayN/Common/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public static bool IsIP(string ip)
481481
/// 验证Domain地址是否合法
482482
/// </summary>
483483
/// <param name="domain"></param>
484-
public static bool IsDomain(string domain)
484+
public static bool IsDomain(string? domain)
485485
{
486486
//如果为空
487487
if (IsNullOrEmpty(domain))

v2rayN/v2rayN/Handler/CoreConfigSingbox.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -810,29 +810,43 @@ private int GenDns(ProfileItem node, SingboxConfig singboxConfig)
810810
{
811811
return 0;
812812
}
813-
//Add the dns of the remote server domain
814-
if (dns4Sbox.rules is null)
815-
{
816-
dns4Sbox.rules = new();
817-
}
818-
dns4Sbox.servers.Add(new()
813+
singboxConfig.dns = dns4Sbox;
814+
815+
GenDnsDomains(singboxConfig);
816+
}
817+
catch (Exception ex)
818+
{
819+
Logging.SaveLog(ex.Message, ex);
820+
}
821+
return 0;
822+
}
823+
824+
private int GenDnsDomains(SingboxConfig singboxConfig)
825+
{
826+
var dns4Sbox = singboxConfig.dns ?? new();
827+
dns4Sbox.servers ??= [];
828+
dns4Sbox.rules ??= [];
829+
830+
var lstDomain = singboxConfig.outbounds
831+
.Where(t => !Utils.IsNullOrEmpty(t.server) && Utils.IsDomain(t.server))
832+
.Select(t => t.server)
833+
.ToList();
834+
if (lstDomain != null && lstDomain.Count > 0)
835+
{
836+
var tag = "local_local";
837+
dns4Sbox.servers.Insert(0, new()
819838
{
820-
tag = "local_local",
839+
tag = tag,
821840
address = "223.5.5.5",
822841
detour = Global.DirectTag,
823842
});
824-
dns4Sbox.rules.Add(new()
843+
dns4Sbox.rules.Insert(0, new()
825844
{
826-
server = "local_local",
827-
outbound = "any"
845+
server = tag,
846+
domain = lstDomain
828847
});
829-
830-
singboxConfig.dns = dns4Sbox;
831-
}
832-
catch (Exception ex)
833-
{
834-
Logging.SaveLog(ex.Message, ex);
835848
}
849+
singboxConfig.dns = dns4Sbox;
836850
return 0;
837851
}
838852

@@ -1076,18 +1090,18 @@ public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out Sin
10761090
singboxConfig.route.rules.Add(rule);
10771091
}
10781092

1079-
GenDns(new(), singboxConfig);
1080-
var dnsServer = singboxConfig.dns?.servers.FirstOrDefault();
1081-
if (dnsServer != null)
1082-
{
1083-
dnsServer.detour = singboxConfig.route.rules.LastOrDefault()?.outbound;
1084-
}
1085-
var dnsRule = singboxConfig.dns?.rules.Where(t => t.outbound != null).FirstOrDefault();
1086-
if (dnsRule != null)
1087-
{
1088-
singboxConfig.dns.rules = [];
1089-
singboxConfig.dns.rules.Add(dnsRule);
1090-
}
1093+
GenDnsDomains(singboxConfig);
1094+
//var dnsServer = singboxConfig.dns?.servers.FirstOrDefault();
1095+
//if (dnsServer != null)
1096+
//{
1097+
// dnsServer.detour = singboxConfig.route.rules.LastOrDefault()?.outbound;
1098+
//}
1099+
//var dnsRule = singboxConfig.dns?.rules.Where(t => t.outbound != null).FirstOrDefault();
1100+
//if (dnsRule != null)
1101+
//{
1102+
// singboxConfig.dns.rules = [];
1103+
// singboxConfig.dns.rules.Add(dnsRule);
1104+
//}
10911105

10921106
//msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
10931107
return 0;

v2rayN/v2rayN/Models/SingboxConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class Outbound4Sbox
9595
{
9696
public string type { get; set; }
9797
public string tag { get; set; }
98-
public string server { get; set; }
98+
public string? server { get; set; }
9999
public int? server_port { get; set; }
100100
public string uuid { get; set; }
101101
public string security { get; set; }

v2rayN/v2rayN/Sample/dns_singbox_normal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
],
3131
"server": "block"
3232
}
33-
]
33+
],
34+
"final": "local"
3435
}

v2rayN/v2rayN/Sample/tun_singbox_dns

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
],
3131
"server": "block"
3232
}
33-
]
33+
],
34+
"final": "local"
3435
}

0 commit comments

Comments
 (0)