Skip to content

Commit

Permalink
Adjust sing-box dns
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed May 12, 2024
1 parent 568144d commit c23379b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public static bool IsIP(string ip)
/// 验证Domain地址是否合法
/// </summary>
/// <param name="domain"></param>
public static bool IsDomain(string domain)
public static bool IsDomain(string? domain)
{
//如果为空
if (IsNullOrEmpty(domain))
Expand Down
70 changes: 42 additions & 28 deletions v2rayN/v2rayN/Handler/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -810,29 +810,43 @@ private int GenDns(ProfileItem node, SingboxConfig singboxConfig)
{
return 0;
}
//Add the dns of the remote server domain
if (dns4Sbox.rules is null)
{
dns4Sbox.rules = new();
}
dns4Sbox.servers.Add(new()
singboxConfig.dns = dns4Sbox;

GenDnsDomains(singboxConfig);
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}
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 = "local_local",
tag = tag,
address = "223.5.5.5",
detour = Global.DirectTag,
});
dns4Sbox.rules.Add(new()
dns4Sbox.rules.Insert(0, new()
{
server = "local_local",
outbound = "any"
server = tag,
domain = lstDomain
});

singboxConfig.dns = dns4Sbox;
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}
singboxConfig.dns = dns4Sbox;
return 0;
}

Expand Down Expand Up @@ -1076,18 +1090,18 @@ public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out Sin
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;
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Models/SingboxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class Outbound4Sbox
{
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; }
Expand Down
3 changes: 2 additions & 1 deletion v2rayN/v2rayN/Sample/dns_singbox_normal
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
],
"server": "block"
}
]
],
"final": "local"
}
3 changes: 2 additions & 1 deletion v2rayN/v2rayN/Sample/tun_singbox_dns
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
],
"server": "block"
}
]
],
"final": "local"
}

0 comments on commit c23379b

Please sign in to comment.