Compare commits

..

No commits in common. "7b31bcdd9fa787e14cd90d67cbbddbcf32f9572f" and "b5800f7dfc50af575e3bd3581fdab5f0d5937470" have entirely different histories.

46 changed files with 2256 additions and 1466 deletions

View file

@ -15,6 +15,7 @@ public class Global
public const string CoreConfigFileName = "config.json"; public const string CoreConfigFileName = "config.json";
public const string CorePreConfigFileName = "configPre.json"; public const string CorePreConfigFileName = "configPre.json";
public const string CoreSpeedtestConfigFileName = "configTest{0}.json"; public const string CoreSpeedtestConfigFileName = "configTest{0}.json";
public const string CoreMultipleLoadConfigFileName = "configMultipleLoad.json";
public const string ClashMixinConfigFileName = "Mixin.yaml"; public const string ClashMixinConfigFileName = "Mixin.yaml";
public const string NamespaceSample = "ServiceLib.Sample."; public const string NamespaceSample = "ServiceLib.Sample.";
@ -87,6 +88,7 @@ public class Global
public const string SingboxLocalDNSTag = "local_local"; public const string SingboxLocalDNSTag = "local_local";
public const string SingboxHostsDNSTag = "hosts_dns"; public const string SingboxHostsDNSTag = "hosts_dns";
public const string SingboxFakeDNSTag = "fake_dns"; public const string SingboxFakeDNSTag = "fake_dns";
public const string SingboxEchDNSTag = "ech_dns";
public const int Hysteria2DefaultHopInt = 10; public const int Hysteria2DefaultHopInt = 10;

View file

@ -254,7 +254,6 @@ public static class ConfigHandler
item.CertSha = profileItem.CertSha; item.CertSha = profileItem.CertSha;
item.EchConfigList = profileItem.EchConfigList; item.EchConfigList = profileItem.EchConfigList;
item.EchForceQuery = profileItem.EchForceQuery; item.EchForceQuery = profileItem.EchForceQuery;
item.Finalmask = profileItem.Finalmask;
item.ProtoExtra = profileItem.ProtoExtra; item.ProtoExtra = profileItem.ProtoExtra;
} }
@ -1123,7 +1122,6 @@ public static class ConfigHandler
&& AreEqual(o.Fingerprint, n.Fingerprint) && AreEqual(o.Fingerprint, n.Fingerprint)
&& AreEqual(o.PublicKey, n.PublicKey) && AreEqual(o.PublicKey, n.PublicKey)
&& AreEqual(o.ShortId, n.ShortId) && AreEqual(o.ShortId, n.ShortId)
&& AreEqual(o.Finalmask, n.Finalmask)
&& (!remarks || o.Remarks == n.Remarks); && (!remarks || o.Remarks == n.Remarks);
static bool AreEqual(string? a, string? b) static bool AreEqual(string? a, string? b)
@ -1233,16 +1231,26 @@ public static class ConfigHandler
/// <param name="node">Server node that might need pre-SOCKS</param> /// <param name="node">Server node that might need pre-SOCKS</param>
/// <param name="coreType">Core type being used</param> /// <param name="coreType">Core type being used</param>
/// <returns>A SOCKS profile item or null if not needed</returns> /// <returns>A SOCKS profile item or null if not needed</returns>
public static ProfileItem? GetPreSocksItem(Config config, ProfileItem node, ECoreType coreType) public static async Task<ProfileItem?> GetPreSocksItem(Config config, ProfileItem node, ECoreType coreType)
{ {
ProfileItem? itemSocks = null; ProfileItem? itemSocks = null;
if (node.ConfigType != EConfigType.Custom && coreType != ECoreType.sing_box && config.TunModeItem.EnableTun) if (node.ConfigType != EConfigType.Custom && coreType != ECoreType.sing_box && config.TunModeItem.EnableTun)
{ {
var tun2SocksAddress = node.Address;
if (node.ConfigType.IsGroupType())
{
var lstAddresses = (await GroupProfileManager.GetAllChildDomainAddresses(node)).ToList();
if (lstAddresses.Count > 0)
{
tun2SocksAddress = Utils.List2String(lstAddresses);
}
}
itemSocks = new ProfileItem() itemSocks = new ProfileItem()
{ {
CoreType = ECoreType.sing_box, CoreType = ECoreType.sing_box,
ConfigType = EConfigType.SOCKS, ConfigType = EConfigType.SOCKS,
Address = Global.Loopback, Address = Global.Loopback,
SpiderX = tun2SocksAddress, // Tun2SocksAddress
Port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks) Port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks)
}; };
} }
@ -1257,6 +1265,7 @@ public static class ConfigHandler
Port = node.PreSocksPort.Value, Port = node.PreSocksPort.Value,
}; };
} }
await Task.CompletedTask;
return itemSocks; return itemSocks;
} }

View file

@ -7,27 +7,27 @@ public static class CoreConfigHandler
{ {
private static readonly string _tag = "CoreConfigHandler"; private static readonly string _tag = "CoreConfigHandler";
public static async Task<RetResult> GenerateClientConfig(CoreConfigContext context, string? fileName) public static async Task<RetResult> GenerateClientConfig(ProfileItem node, string? fileName)
{ {
var config = AppManager.Instance.Config; var config = AppManager.Instance.Config;
var result = new RetResult(); var result = new RetResult();
var node = context.Node;
if (node.ConfigType == EConfigType.Custom) if (node.ConfigType == EConfigType.Custom)
{ {
result = node.CoreType switch result = node.CoreType switch
{ {
ECoreType.mihomo => await new CoreConfigClashService(config).GenerateClientCustomConfig(node, fileName), ECoreType.mihomo => await new CoreConfigClashService(config).GenerateClientCustomConfig(node, fileName),
ECoreType.sing_box => await new CoreConfigSingboxService(config).GenerateClientCustomConfig(node, fileName),
_ => await GenerateClientCustomConfig(node, fileName) _ => await GenerateClientCustomConfig(node, fileName)
}; };
} }
else if (AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box) else if (AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box)
{ {
result = new CoreConfigSingboxService(context).GenerateClientConfigContent(); result = await new CoreConfigSingboxService(config).GenerateClientConfigContent(node);
} }
else else
{ {
result = new CoreConfigV2rayService(context).GenerateClientConfigContent(); result = await new CoreConfigV2rayService(config).GenerateClientConfigContent(node);
} }
if (result.Success != true) if (result.Success != true)
{ {
@ -93,21 +93,13 @@ public static class CoreConfigHandler
public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType) public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType)
{ {
var result = new RetResult(); var result = new RetResult();
var context = await BuildCoreConfigContext(config, new());
var ids = selecteds.Where(serverTestItem => !serverTestItem.IndexId.IsNullOrEmpty())
.Select(serverTestItem => serverTestItem.IndexId);
var nodes = await AppManager.Instance.GetProfileItemsByIndexIds(ids);
foreach (var node in nodes)
{
await FillNodeContext(context, node, false);
}
if (coreType == ECoreType.sing_box) if (coreType == ECoreType.sing_box)
{ {
result = new CoreConfigSingboxService(context).GenerateClientSpeedtestConfig(selecteds); result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(selecteds);
} }
else if (coreType == ECoreType.Xray) else if (coreType == ECoreType.Xray)
{ {
result = new CoreConfigV2rayService(context).GenerateClientSpeedtestConfig(selecteds); result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds);
} }
if (result.Success != true) if (result.Success != true)
{ {
@ -117,21 +109,20 @@ public static class CoreConfigHandler
return result; return result;
} }
public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, CoreConfigContext context, ServerTestItem testItem, string fileName) public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, ProfileItem node, ServerTestItem testItem, string fileName)
{ {
var result = new RetResult(); var result = new RetResult();
var node = context.Node;
var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest); var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest);
var port = Utils.GetFreePort(initPort + testItem.QueueNum); var port = Utils.GetFreePort(initPort + testItem.QueueNum);
testItem.Port = port; testItem.Port = port;
if (AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box) if (AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box)
{ {
result = new CoreConfigSingboxService(context).GenerateClientSpeedtestConfig(port); result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(node, port);
} }
else else
{ {
result = new CoreConfigV2rayService(context).GenerateClientSpeedtestConfig(port); result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(node, port);
} }
if (result.Success != true) if (result.Success != true)
{ {
@ -141,127 +132,4 @@ public static class CoreConfigHandler
await File.WriteAllTextAsync(fileName, result.Data.ToString()); await File.WriteAllTextAsync(fileName, result.Data.ToString());
return result; return result;
} }
public static async Task<CoreConfigContext> BuildCoreConfigContext(Config config, ProfileItem node)
{
var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType) == ECoreType.sing_box ? ECoreType.sing_box : ECoreType.Xray;
var context = new CoreConfigContext()
{
Node = node,
AllProxiesMap = [],
AppConfig = config,
FullConfigTemplate = await AppManager.Instance.GetFullConfigTemplateItem(coreType),
IsTunEnabled = config.TunModeItem.EnableTun,
SimpleDnsItem = config.SimpleDNSItem,
ProtectDomainList = [],
ProtectSocksPort = 0,
RawDnsItem = await AppManager.Instance.GetDNSItem(coreType),
RoutingItem = await ConfigHandler.GetDefaultRouting(config),
};
context = context with
{
Node = await FillNodeContext(context, node)
};
if (!(context.RoutingItem?.RuleSet.IsNullOrEmpty() ?? true))
{
var rules = JsonUtils.Deserialize<List<RulesItem>>(context.RoutingItem?.RuleSet);
foreach (var ruleItem in rules.Where(ruleItem => !Global.OutboundTags.Contains(ruleItem.OutboundTag)))
{
var ruleOutboundNode = await AppManager.Instance.GetProfileItemViaRemarks(ruleItem.OutboundTag);
if (ruleOutboundNode != null)
{
var ruleOutboundNodeAct = await FillNodeContext(context, ruleOutboundNode, false);
context.AllProxiesMap[$"remark:{ruleItem.OutboundTag}"] = ruleOutboundNodeAct;
}
}
}
return context;
}
private static async Task<ProfileItem> FillNodeContext(CoreConfigContext context, ProfileItem node, bool includeSubChain = true)
{
if (node.IndexId.IsNullOrEmpty())
{
return node;
}
var newItems = new List<ProfileItem> { node };
if (node.ConfigType.IsGroupType())
{
var (groupChildList, _) = await GroupProfileManager.GetChildProfileItems(node);
foreach (var childItem in groupChildList.Where(childItem => !context.AllProxiesMap.ContainsKey(childItem.IndexId)))
{
await FillNodeContext(context, childItem, false);
}
node.SetProtocolExtra(node.GetProtocolExtra() with
{
ChildItems = Utils.List2String(groupChildList.Select(n => n.IndexId).ToList()),
});
newItems.AddRange(groupChildList);
}
context.AllProxiesMap[node.IndexId] = node;
foreach (var item in newItems)
{
var address = item.Address;
if (Utils.IsDomain(address))
{
context.ProtectDomainList.Add(address);
}
if (item.EchConfigList.IsNullOrEmpty())
{
continue;
}
var echQuerySni = item.Sni;
if (item.StreamSecurity == Global.StreamSecurity
&& item.EchConfigList?.Contains("://") == true)
{
var idx = item.EchConfigList.IndexOf('+');
echQuerySni = idx > 0 ? item.EchConfigList[..idx] : item.Sni;
}
if (!Utils.IsDomain(echQuerySni))
{
continue;
}
context.ProtectDomainList.Add(echQuerySni);
}
if (!includeSubChain || node.Subid.IsNullOrEmpty())
{
return node;
}
var subItem = await AppManager.Instance.GetSubItem(node.Subid);
if (subItem == null)
{
return node;
}
var prevNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.PrevProfile);
var nextNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.NextProfile);
if (prevNode is null && nextNode is null)
{
return node;
}
var prevNodeAct = prevNode is null ? null : await FillNodeContext(context, prevNode, false);
var nextNodeAct = nextNode is null ? null : await FillNodeContext(context, nextNode, false);
// Build new proxy chain node
var chainNode = new ProfileItem()
{
IndexId = $"inner-{Utils.GetGuid(false)}",
ConfigType = EConfigType.ProxyChain,
CoreType = node.CoreType ?? ECoreType.Xray,
};
List<string?> childItems = [prevNodeAct?.IndexId, node.IndexId, nextNodeAct?.IndexId];
var chainExtraItem = chainNode.GetProtocolExtra() with
{
GroupType = chainNode.ConfigType.ToString(),
ChildItems = string.Join(",", childItems.Where(x => !x.IsNullOrEmpty())),
};
chainNode.SetProtocolExtra(chainExtraItem);
context.AllProxiesMap[chainNode.IndexId] = chainNode;
return chainNode;
}
} }

View file

@ -73,19 +73,6 @@ public class BaseFmt
{ {
dicQuery.Add("pcs", Utils.UrlEncode(item.CertSha)); dicQuery.Add("pcs", Utils.UrlEncode(item.CertSha));
} }
if (item.Finalmask.IsNotEmpty())
{
var node = JsonUtils.ParseJson(item.Finalmask);
var finalmask = node != null
? JsonUtils.Serialize(node, new JsonSerializerOptions
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
})
: item.Finalmask;
dicQuery.Add("fm", Utils.UrlEncode(finalmask));
}
dicQuery.Add("type", item.Network.IsNotEmpty() ? item.Network : nameof(ETransport.tcp)); dicQuery.Add("type", item.Network.IsNotEmpty() ? item.Network : nameof(ETransport.tcp));
@ -227,24 +214,6 @@ public class BaseFmt
item.EchConfigList = GetQueryDecoded(query, "ech"); item.EchConfigList = GetQueryDecoded(query, "ech");
item.CertSha = GetQueryDecoded(query, "pcs"); item.CertSha = GetQueryDecoded(query, "pcs");
var finalmaskDecoded = GetQueryDecoded(query, "fm");
if (finalmaskDecoded.IsNotEmpty())
{
var node = JsonUtils.ParseJson(finalmaskDecoded);
item.Finalmask = node != null
? JsonUtils.Serialize(node, new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
})
: finalmaskDecoded;
}
else
{
item.Finalmask = string.Empty;
}
if (_allowInsecureArray.Any(k => GetQueryDecoded(query, k) == "1")) if (_allowInsecureArray.Any(k => GetQueryDecoded(query, k) == "1"))
{ {
item.AllowInsecure = Global.AllowInsecure.First(); item.AllowInsecure = Global.AllowInsecure.First();

View file

@ -230,18 +230,6 @@ public sealed class AppManager
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.IndexId == indexId); return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.IndexId == indexId);
} }
public async Task<List<ProfileItem>> GetProfileItemsByIndexIds(IEnumerable<string> indexIds)
{
var ids = indexIds.Where(id => !id.IsNullOrEmpty()).Distinct().ToList();
if (ids.Count == 0)
{
return [];
}
return await SQLiteHelper.Instance.TableAsync<ProfileItem>()
.Where(it => ids.Contains(it.IndexId))
.ToListAsync();
}
public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks) public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks)
{ {
if (remarks.IsNullOrEmpty()) if (remarks.IsNullOrEmpty())

View file

@ -215,7 +215,7 @@ public class CertPemManager
using var client = new TcpClient(); using var client = new TcpClient();
await client.ConnectAsync(domain, port > 0 ? port : 443, cts.Token); await client.ConnectAsync(domain, port > 0 ? port : 443, cts.Token);
await using var ssl = new SslStream(client.GetStream(), false, ValidateServerCertificate); using var ssl = new SslStream(client.GetStream(), false, ValidateServerCertificate);
var sslOptions = new SslClientAuthenticationOptions var sslOptions = new SslClientAuthenticationOptions
{ {
@ -262,7 +262,7 @@ public class CertPemManager
using var client = new TcpClient(); using var client = new TcpClient();
await client.ConnectAsync(domain, port > 0 ? port : 443, cts.Token); await client.ConnectAsync(domain, port > 0 ? port : 443, cts.Token);
await using var ssl = new SslStream(client.GetStream(), false, ValidateServerCertificate); using var ssl = new SslStream(client.GetStream(), false, ValidateServerCertificate);
var sslOptions = new SslClientAuthenticationOptions var sslOptions = new SslClientAuthenticationOptions
{ {
@ -280,7 +280,11 @@ public class CertPemManager
var chain = new X509Chain(); var chain = new X509Chain();
chain.Build(certChain); chain.Build(certChain);
pemList.AddRange(chain.ChainElements.Select(element => ExportCertToPem(element.Certificate))); foreach (var element in chain.ChainElements)
{
var pem = ExportCertToPem(element.Certificate);
pemList.Add(pem);
}
return (pemList, null); return (pemList, null);
} }

View file

@ -66,9 +66,7 @@ public class CoreManager
} }
var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName); var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName);
var context = await CoreConfigHandler.BuildCoreConfigContext(_config, node); var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
context = context with { IsTunEnabled = _config.TunModeItem.EnableTun };
var result = await CoreConfigHandler.GenerateClientConfig(context, fileName);
if (result.Success != true) if (result.Success != true)
{ {
await UpdateFunc(true, result.Msg); await UpdateFunc(true, result.Msg);
@ -87,8 +85,8 @@ public class CoreManager
await WindowsUtils.RemoveTunDevice(); await WindowsUtils.RemoveTunDevice();
} }
await CoreStart(context); await CoreStart(node);
await CoreStartPreService(context); await CoreStartPreService(node);
if (_processService != null) if (_processService != null)
{ {
await UpdateFunc(true, $"{node.GetSummary()}"); await UpdateFunc(true, $"{node.GetSummary()}");
@ -124,8 +122,7 @@ public class CoreManager
var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false));
var configPath = Utils.GetBinConfigPath(fileName); var configPath = Utils.GetBinConfigPath(fileName);
var context = await CoreConfigHandler.BuildCoreConfigContext(_config, node); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, node, testItem, configPath);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, context, testItem, configPath);
if (result.Success != true) if (result.Success != true)
{ {
return null; return null;
@ -168,9 +165,8 @@ public class CoreManager
#region Private #region Private
private async Task CoreStart(CoreConfigContext context) private async Task CoreStart(ProfileItem node)
{ {
var node = context.Node;
var coreType = AppManager.Instance.RunningCoreType = AppManager.Instance.GetCoreType(node, node.ConfigType); var coreType = AppManager.Instance.RunningCoreType = AppManager.Instance.GetCoreType(node, node.ConfigType);
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType); var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
@ -183,20 +179,17 @@ public class CoreManager
_processService = proc; _processService = proc;
} }
private async Task CoreStartPreService(CoreConfigContext context) private async Task CoreStartPreService(ProfileItem node)
{ {
var node = context.Node;
if (_processService != null && !_processService.HasExited) if (_processService != null && !_processService.HasExited)
{ {
var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType); var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType);
var itemSocks = ConfigHandler.GetPreSocksItem(_config, node, coreType); var itemSocks = await ConfigHandler.GetPreSocksItem(_config, node, coreType);
if (itemSocks != null) if (itemSocks != null)
{ {
var preCoreType = itemSocks.CoreType ?? ECoreType.sing_box; var preCoreType = itemSocks.CoreType ?? ECoreType.sing_box;
var fileName = Utils.GetBinConfigPath(Global.CorePreConfigFileName); var fileName = Utils.GetBinConfigPath(Global.CorePreConfigFileName);
var itemSocksContext = await CoreConfigHandler.BuildCoreConfigContext(_config, itemSocks); var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName);
itemSocksContext.ProtectDomainList.AddRangeSafe(context.ProtectDomainList);
var result = await CoreConfigHandler.GenerateClientConfig(itemSocksContext, fileName);
if (result.Success) if (result.Success)
{ {
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(preCoreType); var coreInfo = CoreInfoManager.Instance.GetCoreInfo(preCoreType);

View file

@ -79,7 +79,7 @@ public class GroupProfileManager
{ {
if (protocolExtra == null) if (protocolExtra == null)
{ {
return []; return new();
} }
var items = new List<ProfileItem>(); var items = new List<ProfileItem>();
@ -93,44 +93,27 @@ public class GroupProfileManager
{ {
if (extra == null || extra.ChildItems.IsNullOrEmpty()) if (extra == null || extra.ChildItems.IsNullOrEmpty())
{ {
return []; return new();
} }
var childProfileIds = Utils.String2List(extra.ChildItems) var childProfiles = (await Task.WhenAll(
?.Where(p => !string.IsNullOrEmpty(p)) (Utils.String2List(extra.ChildItems) ?? new())
.ToList() ?? []; .Where(p => !p.IsNullOrEmpty())
if (childProfileIds.Count == 0) .Select(AppManager.Instance.GetProfileItem)
{ ))
return []; .Where(p =>
} p != null &&
p.IsValid() &&
var childProfiles = await AppManager.Instance.GetProfileItemsByIndexIds(childProfileIds); p.ConfigType != EConfigType.Custom
if (childProfiles == null || childProfiles.Count == 0) )
{ .ToList();
return []; return childProfiles ?? new();
}
var profileMap = childProfiles
.Where(p => p != null && !p.IndexId.IsNullOrEmpty())
.GroupBy(p => p!.IndexId!)
.ToDictionary(g => g.Key, g => g.First());
var ordered = new List<ProfileItem>(childProfileIds.Count);
foreach (var id in childProfileIds)
{
if (id != null && profileMap.TryGetValue(id, out var item) && item != null)
{
ordered.Add(item);
}
}
return ordered;
} }
private static async Task<List<ProfileItem>> GetSubChildProfileItems(ProtocolExtraItem? extra) private static async Task<List<ProfileItem>> GetSubChildProfileItems(ProtocolExtraItem? extra)
{ {
if (extra == null || extra.SubChildItems.IsNullOrEmpty()) if (extra == null || extra.SubChildItems.IsNullOrEmpty())
{ {
return []; return new();
} }
var childProfiles = await AppManager.Instance.ProfileItems(extra.SubChildItems ?? string.Empty); var childProfiles = await AppManager.Instance.ProfileItems(extra.SubChildItems ?? string.Empty);
@ -140,30 +123,59 @@ public class GroupProfileManager
!p.ConfigType.IsComplexType() && !p.ConfigType.IsComplexType() &&
(extra.Filter.IsNullOrEmpty() || Regex.IsMatch(p.Remarks, extra.Filter)) (extra.Filter.IsNullOrEmpty() || Regex.IsMatch(p.Remarks, extra.Filter))
) )
.ToList() ?? []; .ToList() ?? new();
} }
public static async Task<List<ProfileItem>> GetAllChildProfileItems(ProfileItem profileItem) public static async Task<HashSet<string>> GetAllChildDomainAddresses(ProfileItem profileItem)
{
var allChildItems = new List<ProfileItem>();
var visited = new HashSet<string>();
await CollectChildItems(profileItem, allChildItems, visited);
return allChildItems;
}
private static async Task CollectChildItems(ProfileItem profileItem, List<ProfileItem> allChildItems, HashSet<string> visited)
{ {
var childAddresses = new HashSet<string>();
var (childItems, _) = await GetChildProfileItems(profileItem); var (childItems, _) = await GetChildProfileItems(profileItem);
foreach (var child in childItems.Where(child => visited.Add(child.IndexId))) foreach (var child in childItems)
{ {
allChildItems.Add(child); if (!child.IsComplex())
if (child.ConfigType.IsGroupType())
{ {
await CollectChildItems(child, allChildItems, visited); childAddresses.Add(child.Address);
}
else if (child.ConfigType.IsGroupType())
{
var subAddresses = await GetAllChildDomainAddresses(child);
foreach (var addr in subAddresses)
{
childAddresses.Add(addr);
}
} }
} }
return childAddresses;
}
public static async Task<HashSet<string>> GetAllChildEchQuerySni(ProfileItem profileItem)
{
var childAddresses = new HashSet<string>();
var (childItems, _) = await GetChildProfileItems(profileItem);
foreach (var childNode in childItems)
{
if (!childNode.IsComplex() && !childNode.EchConfigList.IsNullOrEmpty())
{
if (childNode.StreamSecurity == Global.StreamSecurity
&& childNode.EchConfigList?.Contains("://") == true)
{
var idx = childNode.EchConfigList.IndexOf('+');
childAddresses.Add(idx > 0 ? childNode.EchConfigList[..idx] : childNode.Sni);
}
else
{
childAddresses.Add(childNode.Sni);
}
}
else if (childNode.ConfigType.IsGroupType())
{
var subAddresses = await GetAllChildDomainAddresses(childNode);
foreach (var addr in subAddresses)
{
childAddresses.Add(addr);
}
}
}
return childAddresses;
} }
} }

View file

@ -1,17 +0,0 @@
namespace ServiceLib.Models;
public record CoreConfigContext
{
public required ProfileItem Node { get; init; }
public RoutingItem? RoutingItem { get; init; }
public DNSItem? RawDnsItem { get; init; }
public SimpleDNSItem SimpleDnsItem { get; init; } = new();
public Dictionary<string, ProfileItem> AllProxiesMap { get; init; } = new();
public Config AppConfig { get; init; } = new();
public FullConfigTemplateItem? FullConfigTemplate { get; init; } = new();
// TUN Compatibility
public bool IsTunEnabled { get; init; } = false;
public HashSet<string> ProtectDomainList { get; init; } = new();
public int ProtectSocksPort { get; init; } = 0;
}

View file

@ -178,7 +178,6 @@ public class ProfileItem
public string CertSha { get; set; } public string CertSha { get; set; }
public string EchConfigList { get; set; } public string EchConfigList { get; set; }
public string EchForceQuery { get; set; } public string EchForceQuery { get; set; }
public string Finalmask { get; set; }
public string ProtoExtra { get; set; } public string ProtoExtra { get; set; }

View file

@ -255,8 +255,9 @@ public class Server4Sbox : BaseServer4Sbox
// public List<string>? path { get; set; } // hosts // public List<string>? path { get; set; } // hosts
public Dictionary<string, List<string>>? predefined { get; set; } public Dictionary<string, List<string>>? predefined { get; set; }
// Deprecated in sing-box 1.12.0 , kept for backward compatibility // Deprecated
public string? address { get; set; } public string? address { get; set; }
public string? address_resolver { get; set; } public string? address_resolver { get; set; }
public string? address_strategy { get; set; } public string? address_strategy { get; set; }
public string? strategy { get; set; } public string? strategy { get; set; }

View file

@ -341,7 +341,7 @@ public class StreamSettings4Ray
public HysteriaSettings4Ray? hysteriaSettings { get; set; } public HysteriaSettings4Ray? hysteriaSettings { get; set; }
public Finalmask4Ray? finalmask { get; set; } public FinalMask4Ray? finalmask { get; set; }
public Sockopt4Ray? sockopt { get; set; } public Sockopt4Ray? sockopt { get; set; }
} }
@ -476,7 +476,7 @@ public class HysteriaUdpHop4Ray
public string? interval { get; set; } public string? interval { get; set; }
} }
public class Finalmask4Ray public class FinalMask4Ray
{ {
public List<Mask4Ray>? tcp { get; set; } public List<Mask4Ray>? tcp { get; set; }
public List<Mask4Ray>? udp { get; set; } public List<Mask4Ray>? udp { get; set; }
@ -485,7 +485,7 @@ public class Finalmask4Ray
public class Mask4Ray public class Mask4Ray
{ {
public string type { get; set; } public string type { get; set; }
public object? settings { get; set; } public MaskSettings4Ray? settings { get; set; }
} }
public class MaskSettings4Ray public class MaskSettings4Ray

View file

@ -2916,15 +2916,6 @@ namespace ServiceLib.Resx {
} }
} }
/// <summary>
/// 查找类似 Finalmask 的本地化字符串。
/// </summary>
public static string TbFinalmask {
get {
return ResourceManager.GetString("TbFinalmask", resourceCulture);
}
}
/// <summary> /// <summary>
/// 查找类似 Fingerprint 的本地化字符串。 /// 查找类似 Fingerprint 的本地化字符串。
/// </summary> /// </summary>

View file

@ -1671,7 +1671,4 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>Configuration item preview</value> <value>Configuration item preview</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1668,7 +1668,4 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>Configuration item preview</value> <value>Configuration item preview</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1671,7 +1671,4 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>Configuration item preview</value> <value>Configuration item preview</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1671,7 +1671,4 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>Configuration item preview</value> <value>Configuration item preview</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1671,7 +1671,4 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>Configuration item preview</value> <value>Configuration item preview</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1668,7 +1668,4 @@
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>子配置项预览</value> <value>子配置项预览</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1668,7 +1668,4 @@
<data name="menuServerListPreview" xml:space="preserve"> <data name="menuServerListPreview" xml:space="preserve">
<value>Configuration item preview</value> <value>Configuration item preview</value>
</data> </data>
<data name="TbFinalmask" xml:space="preserve">
<value>Finalmask</value>
</data>
</root> </root>

View file

@ -1,4 +1,4 @@
{ {
"log": { "log": {
"access": "Vaccess.log", "access": "Vaccess.log",
"error": "Verror.log", "error": "Verror.log",
@ -6,6 +6,34 @@
}, },
"inbounds": [], "inbounds": [],
"outbounds": [ "outbounds": [
{
"tag": "proxy",
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "",
"port": 0,
"users": [{
"id": "",
"security": "auto"
}]
}],
"servers": [{
"address": "",
"method": "",
"ota": false,
"password": "",
"port": 0,
"level": 1
}]
},
"streamSettings": {
"network": "tcp"
},
"mux": {
"enabled": false
}
},
{ {
"protocol": "freedom", "protocol": "freedom",
"tag": "direct" "tag": "direct"

View file

@ -5,12 +5,19 @@
}, },
"inbounds": [], "inbounds": [],
"outbounds": [ "outbounds": [
{
"type": "vless",
"tag": "proxy",
"server": "",
"server_port": 443
},
{ {
"type": "direct", "type": "direct",
"tag": "direct" "tag": "direct"
} }
], ],
"route": { "route": {
"rules": [] "rules": [
]
} }
} }

View file

@ -1,34 +1,43 @@
namespace ServiceLib.Services.CoreConfig; namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService(CoreConfigContext context) public partial class CoreConfigSingboxService(Config config)
{ {
private readonly Config _config = config;
private static readonly string _tag = "CoreConfigSingboxService"; private static readonly string _tag = "CoreConfigSingboxService";
private readonly Config _config = context.AppConfig;
private readonly ProfileItem _node = context.Node;
private SingboxConfig _coreConfig = new();
#region public gen function #region public gen function
public RetResult GenerateClientConfigContent() public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
{ {
var ret = new RetResult(); var ret = new RetResult();
try try
{ {
if (_node == null if (node == null
|| !_node.IsValid()) || !node.IsValid())
{ {
ret.Msg = ResUI.CheckServerSettings; ret.Msg = ResUI.CheckServerSettings;
return ret; return ret;
} }
if (_node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp)) if (node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp))
{ {
ret.Msg = ResUI.Incorrectconfiguration + $" - {_node.GetNetwork()}"; ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}";
return ret; return ret;
} }
ret.Msg = ResUI.InitialConfiguration; ret.Msg = ResUI.InitialConfiguration;
if (node.ConfigType.IsGroupType())
{
switch (node.ConfigType)
{
case EConfigType.PolicyGroup:
return await GenerateClientMultipleLoadConfig(node);
case EConfigType.ProxyChain:
return await GenerateClientChainConfig(node);
}
}
var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient); var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient);
if (result.IsNullOrEmpty()) if (result.IsNullOrEmpty())
{ {
@ -36,31 +45,44 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
return ret; return ret;
} }
_coreConfig = JsonUtils.Deserialize<SingboxConfig>(result); var singboxConfig = JsonUtils.Deserialize<SingboxConfig>(result);
if (_coreConfig == null) if (singboxConfig == null)
{ {
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
} }
GenLog(); await GenLog(singboxConfig);
GenInbounds(); await GenInbounds(singboxConfig);
GenOutbounds(); if (node.ConfigType == EConfigType.WireGuard)
{
singboxConfig.outbounds.RemoveAt(0);
var endpoints = new Endpoints4Sbox();
await GenEndpoint(node, endpoints);
endpoints.tag = Global.ProxyTag;
singboxConfig.endpoints = new() { endpoints };
}
else
{
await GenOutbound(node, singboxConfig.outbounds.First());
}
GenRouting(); await GenMoreOutbounds(node, singboxConfig);
GenDns(); await GenRouting(singboxConfig);
GenExperimental(); await GenDns(node, singboxConfig);
ConvertGeo2Ruleset(); await GenExperimental(singboxConfig);
await ConvertGeo2Ruleset(singboxConfig);
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Success = true; ret.Success = true;
ret.Data = ApplyFullConfigTemplate(); ret.Data = await ApplyFullConfigTemplate(singboxConfig);
return ret; return ret;
} }
catch (Exception ex) catch (Exception ex)
@ -71,11 +93,17 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
} }
} }
public RetResult GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds) public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
{ {
var ret = new RetResult(); var ret = new RetResult();
try try
{ {
if (_config == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration; ret.Msg = ResUI.InitialConfiguration;
var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient); var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient);
@ -86,8 +114,8 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
return ret; return ret;
} }
_coreConfig = JsonUtils.Deserialize<SingboxConfig>(result); var singboxConfig = JsonUtils.Deserialize<SingboxConfig>(result);
if (_coreConfig == null) if (singboxConfig == null)
{ {
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
@ -105,10 +133,10 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
GenLog(); await GenLog(singboxConfig);
GenMinimizedDns(); //GenDns(new(), singboxConfig);
_coreConfig.inbounds.Clear(); singboxConfig.inbounds.Clear();
_coreConfig.outbounds.RemoveAt(0); singboxConfig.outbounds.RemoveAt(0);
var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest); var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest);
@ -122,7 +150,7 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
{ {
continue; continue;
} }
var item = context.AllProxiesMap.GetValueOrDefault(it.IndexId); var item = await AppManager.Instance.GetProfileItem(it.IndexId);
if (item is null || item.IsComplex() || !item.IsValid()) if (item is null || item.IsComplex() || !item.IsValid())
{ {
continue; continue;
@ -162,11 +190,26 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
type = EInboundProtocol.mixed.ToString(), type = EInboundProtocol.mixed.ToString(),
}; };
inbound.tag = inbound.type + inbound.listen_port.ToString(); inbound.tag = inbound.type + inbound.listen_port.ToString();
_coreConfig.inbounds.Add(inbound); singboxConfig.inbounds.Add(inbound);
//outbound
var server = await GenServer(item);
if (server is null)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
var tag = Global.ProxyTag + inbound.listen_port.ToString(); var tag = Global.ProxyTag + inbound.listen_port.ToString();
var serverList = new CoreConfigSingboxService(context with { Node = item }).BuildAllProxyOutbounds(tag); server.tag = tag;
FillRangeProxy(serverList, _coreConfig, false); if (server is Endpoints4Sbox endpoint)
{
singboxConfig.endpoints ??= new();
singboxConfig.endpoints.Add(endpoint);
}
else if (server is Outbound4Sbox outbound)
{
singboxConfig.outbounds.Add(outbound);
}
//rule //rule
Rule4Sbox rule = new() Rule4Sbox rule = new()
@ -174,11 +217,25 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
inbound = new List<string> { inbound.tag }, inbound = new List<string> { inbound.tag },
outbound = tag outbound = tag
}; };
_coreConfig.route.rules.Add(rule); singboxConfig.route.rules.Add(rule);
} }
var rawDNSItem = await AppManager.Instance.GetDNSItem(ECoreType.sing_box);
if (rawDNSItem != null && rawDNSItem.Enabled == true)
{
await GenDnsDomainsCompatible(singboxConfig, rawDNSItem);
}
else
{
await GenDnsDomains(singboxConfig, _config.SimpleDNSItem);
}
singboxConfig.route.default_domain_resolver = new()
{
server = Global.SingboxLocalDNSTag,
};
ret.Success = true; ret.Success = true;
ret.Data = JsonUtils.Serialize(_coreConfig); ret.Data = JsonUtils.Serialize(singboxConfig);
return ret; return ret;
} }
catch (Exception ex) catch (Exception ex)
@ -189,20 +246,20 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
} }
} }
public RetResult GenerateClientSpeedtestConfig(int port) public async Task<RetResult> GenerateClientSpeedtestConfig(ProfileItem node, int port)
{ {
var ret = new RetResult(); var ret = new RetResult();
try try
{ {
if (_node == null if (node == null
|| !_node.IsValid()) || !node.IsValid())
{ {
ret.Msg = ResUI.CheckServerSettings; ret.Msg = ResUI.CheckServerSettings;
return ret; return ret;
} }
if (_node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp)) if (node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp))
{ {
ret.Msg = ResUI.Incorrectconfiguration + $" - {_node.GetNetwork()}"; ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}";
return ret; return ret;
} }
@ -215,20 +272,44 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
return ret; return ret;
} }
_coreConfig = JsonUtils.Deserialize<SingboxConfig>(result); var singboxConfig = JsonUtils.Deserialize<SingboxConfig>(result);
if (_coreConfig == null) if (singboxConfig == null)
{ {
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
} }
GenLog(); await GenLog(singboxConfig);
GenOutbounds(); if (node.ConfigType == EConfigType.WireGuard)
GenMinimizedDns(); {
singboxConfig.outbounds.RemoveAt(0);
var endpoints = new Endpoints4Sbox();
await GenEndpoint(node, endpoints);
endpoints.tag = Global.ProxyTag;
singboxConfig.endpoints = new() { endpoints };
}
else
{
await GenOutbound(node, singboxConfig.outbounds.First());
}
await GenMoreOutbounds(node, singboxConfig);
var item = await AppManager.Instance.GetDNSItem(ECoreType.sing_box);
if (item != null && item.Enabled == true)
{
await GenDnsDomainsCompatible(singboxConfig, item);
}
else
{
await GenDnsDomains(singboxConfig, _config.SimpleDNSItem);
}
singboxConfig.route.default_domain_resolver = new()
{
server = Global.SingboxLocalDNSTag,
};
_coreConfig.route.rules.Clear(); singboxConfig.route.rules.Clear();
_coreConfig.inbounds.Clear(); singboxConfig.inbounds.Clear();
_coreConfig.inbounds.Add(new() singboxConfig.inbounds.Add(new()
{ {
tag = $"{EInboundProtocol.mixed}{port}", tag = $"{EInboundProtocol.mixed}{port}",
listen = Global.Loopback, listen = Global.Loopback,
@ -238,7 +319,202 @@ public partial class CoreConfigSingboxService(CoreConfigContext context)
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Success = true; ret.Success = true;
ret.Data = JsonUtils.Serialize(_coreConfig); ret.Data = JsonUtils.Serialize(singboxConfig);
return ret;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
}
public async Task<RetResult> GenerateClientMultipleLoadConfig(ProfileItem parentNode)
{
var ret = new RetResult();
try
{
if (_config == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration;
var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient);
var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound);
if (result.IsNullOrEmpty() || txtOutbound.IsNullOrEmpty())
{
ret.Msg = ResUI.FailedGetDefaultConfiguration;
return ret;
}
var singboxConfig = JsonUtils.Deserialize<SingboxConfig>(result);
if (singboxConfig == null)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
singboxConfig.outbounds.RemoveAt(0);
await GenLog(singboxConfig);
await GenInbounds(singboxConfig);
var groupRet = await GenGroupOutbound(parentNode, singboxConfig);
if (groupRet != 0)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
await GenRouting(singboxConfig);
await GenExperimental(singboxConfig);
await GenDns(parentNode, singboxConfig);
await ConvertGeo2Ruleset(singboxConfig);
ret.Success = true;
ret.Data = await ApplyFullConfigTemplate(singboxConfig);
return ret;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
}
public async Task<RetResult> GenerateClientChainConfig(ProfileItem parentNode)
{
var ret = new RetResult();
try
{
if (_config == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration;
var result = EmbedUtils.GetEmbedText(Global.SingboxSampleClient);
var txtOutbound = EmbedUtils.GetEmbedText(Global.SingboxSampleOutbound);
if (result.IsNullOrEmpty() || txtOutbound.IsNullOrEmpty())
{
ret.Msg = ResUI.FailedGetDefaultConfiguration;
return ret;
}
var singboxConfig = JsonUtils.Deserialize<SingboxConfig>(result);
if (singboxConfig == null)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
singboxConfig.outbounds.RemoveAt(0);
await GenLog(singboxConfig);
await GenInbounds(singboxConfig);
var groupRet = await GenGroupOutbound(parentNode, singboxConfig);
if (groupRet != 0)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
await GenRouting(singboxConfig);
await GenExperimental(singboxConfig);
await GenDns(parentNode, singboxConfig);
await ConvertGeo2Ruleset(singboxConfig);
ret.Success = true;
ret.Data = await ApplyFullConfigTemplate(singboxConfig);
return ret;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
}
public async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
{
var ret = new RetResult();
if (node == null || fileName is null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration;
try
{
if (node == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
if (File.Exists(fileName))
{
File.Delete(fileName);
}
var addressFileName = node.Address;
if (addressFileName.IsNullOrEmpty())
{
ret.Msg = ResUI.FailedGetDefaultConfiguration;
return ret;
}
if (!File.Exists(addressFileName))
{
addressFileName = Path.Combine(Utils.GetConfigPath(), addressFileName);
}
if (!File.Exists(addressFileName))
{
ret.Msg = ResUI.FailedReadConfiguration + "1";
return ret;
}
if (node.Address == Global.CoreMultipleLoadConfigFileName)
{
var txtFile = File.ReadAllText(addressFileName);
var singboxConfig = JsonUtils.Deserialize<SingboxConfig>(txtFile);
if (singboxConfig == null)
{
File.Copy(addressFileName, fileName);
}
else
{
await GenInbounds(singboxConfig);
await GenExperimental(singboxConfig);
var content = JsonUtils.Serialize(singboxConfig, true);
await File.WriteAllTextAsync(fileName, content);
}
}
else
{
File.Copy(addressFileName, fileName);
}
//check again
if (!File.Exists(fileName))
{
ret.Msg = ResUI.FailedReadConfiguration + "2";
return ret;
}
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Success = true;
return ret; return ret;
} }
catch (Exception ex) catch (Exception ex)

View file

@ -2,29 +2,29 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private string ApplyFullConfigTemplate() private async Task<string> ApplyFullConfigTemplate(SingboxConfig singboxConfig)
{ {
var fullConfigTemplate = context.FullConfigTemplate; var fullConfigTemplate = await AppManager.Instance.GetFullConfigTemplateItem(ECoreType.sing_box);
if (fullConfigTemplate is not { Enabled: true }) if (fullConfigTemplate == null || !fullConfigTemplate.Enabled)
{ {
return JsonUtils.Serialize(_coreConfig); return JsonUtils.Serialize(singboxConfig);
} }
var fullConfigTemplateItem = context.IsTunEnabled ? fullConfigTemplate.TunConfig : fullConfigTemplate.Config; var fullConfigTemplateItem = _config.TunModeItem.EnableTun ? fullConfigTemplate.TunConfig : fullConfigTemplate.Config;
if (fullConfigTemplateItem.IsNullOrEmpty()) if (fullConfigTemplateItem.IsNullOrEmpty())
{ {
return JsonUtils.Serialize(_coreConfig); return JsonUtils.Serialize(singboxConfig);
} }
var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplateItem); var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplateItem);
if (fullConfigTemplateNode == null) if (fullConfigTemplateNode == null)
{ {
return JsonUtils.Serialize(_coreConfig); return JsonUtils.Serialize(singboxConfig);
} }
// Process outbounds // Process outbounds
var customOutboundsNode = fullConfigTemplateNode["outbounds"] is JsonArray outbounds ? outbounds : []; var customOutboundsNode = fullConfigTemplateNode["outbounds"] is JsonArray outbounds ? outbounds : new JsonArray();
foreach (var outbound in _coreConfig.outbounds) foreach (var outbound in singboxConfig.outbounds)
{ {
if (outbound.type.ToLower() is "direct" or "block") if (outbound.type.ToLower() is "direct" or "block")
{ {
@ -42,10 +42,10 @@ public partial class CoreConfigSingboxService
fullConfigTemplateNode["outbounds"] = customOutboundsNode; fullConfigTemplateNode["outbounds"] = customOutboundsNode;
// Process endpoints // Process endpoints
if (_coreConfig.endpoints != null && _coreConfig.endpoints.Count > 0) if (singboxConfig.endpoints != null && singboxConfig.endpoints.Count > 0)
{ {
var customEndpointsNode = fullConfigTemplateNode["endpoints"] is JsonArray endpoints ? endpoints : []; var customEndpointsNode = fullConfigTemplateNode["endpoints"] is JsonArray endpoints ? endpoints : new JsonArray();
foreach (var endpoint in _coreConfig.endpoints) foreach (var endpoint in singboxConfig.endpoints)
{ {
if (endpoint.detour.IsNullOrEmpty() && !fullConfigTemplate.ProxyDetour.IsNullOrEmpty()) if (endpoint.detour.IsNullOrEmpty() && !fullConfigTemplate.ProxyDetour.IsNullOrEmpty())
{ {
@ -56,6 +56,6 @@ public partial class CoreConfigSingboxService
fullConfigTemplateNode["endpoints"] = customEndpointsNode; fullConfigTemplateNode["endpoints"] = customEndpointsNode;
} }
return JsonUtils.Serialize(fullConfigTemplateNode); return await Task.FromResult(JsonUtils.Serialize(fullConfigTemplateNode));
} }
} }

View file

@ -2,25 +2,25 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private void GenDns() private async Task<int> GenDns(ProfileItem? node, SingboxConfig singboxConfig)
{ {
try try
{ {
var item = context.RawDnsItem; var item = await AppManager.Instance.GetDNSItem(ECoreType.sing_box);
if (item is { Enabled: true }) if (item != null && item.Enabled == true)
{ {
GenDnsCustom(); return await GenDnsCompatible(node, singboxConfig);
return;
} }
GenDnsServers(); var simpleDNSItem = _config.SimpleDNSItem;
GenDnsRules(); await GenDnsServers(node, singboxConfig, simpleDNSItem);
await GenDnsRules(node, singboxConfig, simpleDNSItem);
_coreConfig.dns ??= new Dns4Sbox(); singboxConfig.dns ??= new Dns4Sbox();
_coreConfig.dns.independent_cache = true; singboxConfig.dns.independent_cache = true;
// final dns // final dns
var routing = context.RoutingItem; var routing = await ConfigHandler.GetDefaultRouting(_config);
var useDirectDns = false; var useDirectDns = false;
if (routing != null) if (routing != null)
{ {
@ -32,34 +32,35 @@ public partial class CoreConfigSingboxService
lastRule.Network == "tcp,udp" || lastRule.Network == "tcp,udp" ||
lastRule.Ip?.Contains("0.0.0.0/0") == true); lastRule.Ip?.Contains("0.0.0.0/0") == true);
} }
_coreConfig.dns.final = useDirectDns ? Global.SingboxDirectDNSTag : Global.SingboxRemoteDNSTag; singboxConfig.dns.final = useDirectDns ? Global.SingboxDirectDNSTag : Global.SingboxRemoteDNSTag;
var simpleDnsItem = context.SimpleDnsItem; if ((!useDirectDns) && simpleDNSItem.FakeIP == true && simpleDNSItem.GlobalFakeIp == false)
if ((!useDirectDns) && simpleDnsItem.FakeIP == true && simpleDnsItem.GlobalFakeIp == false)
{ {
_coreConfig.dns.rules.Add(new() singboxConfig.dns.rules.Add(new()
{ {
server = Global.SingboxFakeDNSTag, server = Global.SingboxFakeDNSTag,
query_type = new List<int> { 1, 28 }, // A and AAAA query_type = new List<int> { 1, 28 }, // A and AAAA
rewrite_ttl = 1, rewrite_ttl = 1,
}); });
} }
await GenOutboundDnsRule(node, singboxConfig);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private void GenDnsServers() private async Task<int> GenDnsServers(ProfileItem? node, SingboxConfig singboxConfig, SimpleDNSItem simpleDNSItem)
{ {
var simpleDnsItem = context.SimpleDnsItem; var finalDns = await GenDnsDomains(singboxConfig, simpleDNSItem);
var finalDns = GenBootstrapDns();
var directDns = ParseDnsAddress(simpleDnsItem.DirectDNS ?? Global.DomainDirectDNSAddress.First()); var directDns = ParseDnsAddress(simpleDNSItem.DirectDNS);
directDns.tag = Global.SingboxDirectDNSTag; directDns.tag = Global.SingboxDirectDNSTag;
directDns.domain_resolver = Global.SingboxLocalDNSTag; directDns.domain_resolver = Global.SingboxLocalDNSTag;
var remoteDns = ParseDnsAddress(simpleDnsItem.RemoteDNS ?? Global.DomainRemoteDNSAddress.First()); var remoteDns = ParseDnsAddress(simpleDNSItem.RemoteDNS);
remoteDns.tag = Global.SingboxRemoteDNSTag; remoteDns.tag = Global.SingboxRemoteDNSTag;
remoteDns.detour = Global.ProxyTag; remoteDns.detour = Global.ProxyTag;
remoteDns.domain_resolver = Global.SingboxLocalDNSTag; remoteDns.domain_resolver = Global.SingboxLocalDNSTag;
@ -70,12 +71,12 @@ public partial class CoreConfigSingboxService
type = "hosts", type = "hosts",
predefined = new(), predefined = new(),
}; };
if (simpleDnsItem.AddCommonHosts == true) if (simpleDNSItem.AddCommonHosts == true)
{ {
hostsDns.predefined = Global.PredefinedHosts; hostsDns.predefined = Global.PredefinedHosts;
} }
if (simpleDnsItem.UseSystemHosts == true) if (simpleDNSItem.UseSystemHosts == true)
{ {
var systemHosts = Utils.GetSystemHosts(); var systemHosts = Utils.GetSystemHosts();
if (systemHosts != null && systemHosts.Count > 0) if (systemHosts != null && systemHosts.Count > 0)
@ -87,9 +88,9 @@ public partial class CoreConfigSingboxService
} }
} }
foreach (var kvp in Utils.ParseHostsToDictionary(simpleDnsItem.Hosts)) foreach (var kvp in Utils.ParseHostsToDictionary(simpleDNSItem.Hosts))
{ {
hostsDns.predefined[kvp.Key] = kvp.Value.Where(Utils.IsIpAddress).ToList(); hostsDns.predefined[kvp.Key] = kvp.Value.Where(s => Utils.IsIpAddress(s)).ToList();
} }
foreach (var host in hostsDns.predefined) foreach (var host in hostsDns.predefined)
@ -108,14 +109,14 @@ public partial class CoreConfigSingboxService
} }
} }
_coreConfig.dns ??= new Dns4Sbox(); singboxConfig.dns ??= new Dns4Sbox();
_coreConfig.dns.servers ??= []; singboxConfig.dns.servers ??= [];
_coreConfig.dns.servers.Add(remoteDns); singboxConfig.dns.servers.Add(remoteDns);
_coreConfig.dns.servers.Add(directDns); singboxConfig.dns.servers.Add(directDns);
_coreConfig.dns.servers.Add(hostsDns); singboxConfig.dns.servers.Add(hostsDns);
// fake ip // fake ip
if (simpleDnsItem.FakeIP == true) if (simpleDNSItem.FakeIP == true)
{ {
var fakeip = new Server4Sbox var fakeip = new Server4Sbox
{ {
@ -124,50 +125,68 @@ public partial class CoreConfigSingboxService
inet4_range = "198.18.0.0/15", inet4_range = "198.18.0.0/15",
inet6_range = "fc00::/18", inet6_range = "fc00::/18",
}; };
_coreConfig.dns.servers.Add(fakeip); singboxConfig.dns.servers.Add(fakeip);
} }
// ech
var (_, dnsServer) = ParseEchParam(node?.EchConfigList);
if (dnsServer is not null)
{
dnsServer.tag = Global.SingboxEchDNSTag;
if (dnsServer.server is not null
&& hostsDns.predefined.ContainsKey(dnsServer.server))
{
dnsServer.domain_resolver = Global.SingboxHostsDNSTag;
}
else
{
dnsServer.domain_resolver = Global.SingboxLocalDNSTag;
}
singboxConfig.dns.servers.Add(dnsServer);
}
else if (node?.ConfigType.IsGroupType() == true)
{
var echDnsObject = JsonUtils.DeepCopy(directDns);
echDnsObject.tag = Global.SingboxEchDNSTag;
singboxConfig.dns.servers.Add(echDnsObject);
}
return await Task.FromResult(0);
} }
private Server4Sbox GenBootstrapDns() private async Task<Server4Sbox> GenDnsDomains(SingboxConfig singboxConfig, SimpleDNSItem? simpleDNSItem)
{ {
var finalDns = ParseDnsAddress(context.SimpleDnsItem?.BootstrapDNS ?? Global.DomainPureIPDNSAddress.First()); var finalDns = ParseDnsAddress(simpleDNSItem.BootstrapDNS);
finalDns.tag = Global.SingboxLocalDNSTag; finalDns.tag = Global.SingboxLocalDNSTag;
_coreConfig.dns ??= new Dns4Sbox(); singboxConfig.dns ??= new Dns4Sbox();
_coreConfig.dns.servers ??= []; singboxConfig.dns.servers ??= new List<Server4Sbox>();
_coreConfig.dns.servers.Add(finalDns); singboxConfig.dns.servers.Add(finalDns);
return finalDns; return await Task.FromResult(finalDns);
} }
private void GenDnsRules() private async Task<int> GenDnsRules(ProfileItem? node, SingboxConfig singboxConfig, SimpleDNSItem simpleDNSItem)
{ {
var simpleDnsItem = context.SimpleDnsItem; singboxConfig.dns ??= new Dns4Sbox();
_coreConfig.dns ??= new Dns4Sbox(); singboxConfig.dns.rules ??= new List<Rule4Sbox>();
_coreConfig.dns.rules ??= [];
_coreConfig.dns.rules.AddRange(new[] singboxConfig.dns.rules.AddRange(new[]
{ {
new Rule4Sbox { ip_accept_any = true, server = Global.SingboxHostsDNSTag }, new Rule4Sbox { ip_accept_any = true, server = Global.SingboxHostsDNSTag },
new Rule4Sbox new Rule4Sbox
{
server = Global.SingboxDirectDNSTag,
strategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Freedom),
domain = context.ProtectDomainList.ToList(),
},
new Rule4Sbox
{ {
server = Global.SingboxRemoteDNSTag, server = Global.SingboxRemoteDNSTag,
strategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Proxy), strategy = Utils.DomainStrategy4Sbox(simpleDNSItem.Strategy4Proxy),
clash_mode = ERuleMode.Global.ToString() clash_mode = ERuleMode.Global.ToString()
}, },
new Rule4Sbox new Rule4Sbox
{ {
server = Global.SingboxDirectDNSTag, server = Global.SingboxDirectDNSTag,
strategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Freedom), strategy = Utils.DomainStrategy4Sbox(simpleDNSItem.Strategy4Freedom),
clash_mode = ERuleMode.Direct.ToString() clash_mode = ERuleMode.Direct.ToString()
} }
}); });
foreach (var kvp in Utils.ParseHostsToDictionary(simpleDnsItem.Hosts)) foreach (var kvp in Utils.ParseHostsToDictionary(simpleDNSItem.Hosts))
{ {
var predefined = kvp.Value.First(); var predefined = kvp.Value.First();
if (predefined.IsNullOrEmpty() || Utils.IsIpAddress(predefined)) if (predefined.IsNullOrEmpty() || Utils.IsIpAddress(predefined))
@ -178,7 +197,7 @@ public partial class CoreConfigSingboxService
{ {
// xray syntactic sugar for predefined // xray syntactic sugar for predefined
// etc. #0 -> NOERROR // etc. #0 -> NOERROR
_coreConfig.dns.rules.Add(new() singboxConfig.dns.rules.Add(new()
{ {
query_type = [1, 28], query_type = [1, 28],
domain = [kvp.Key], domain = [kvp.Key],
@ -206,13 +225,38 @@ public partial class CoreConfigSingboxService
}; };
if (ParseV2Domain(kvp.Key, rule)) if (ParseV2Domain(kvp.Key, rule))
{ {
_coreConfig.dns.rules.Add(rule); singboxConfig.dns.rules.Add(rule);
} }
} }
if (simpleDnsItem.BlockBindingQuery == true) var (ech, _) = ParseEchParam(node?.EchConfigList);
if (ech is not null)
{ {
_coreConfig.dns.rules.Add(new() var echDomain = ech.query_server_name ?? node?.Sni;
singboxConfig.dns.rules.Add(new()
{
query_type = [64, 65],
server = Global.SingboxEchDNSTag,
domain = echDomain is not null ? new List<string> { echDomain } : null,
});
}
else if (node?.ConfigType.IsGroupType() == true)
{
var queryServerNames = (await GroupProfileManager.GetAllChildEchQuerySni(node)).ToList();
if (queryServerNames.Count > 0)
{
singboxConfig.dns.rules.Add(new()
{
query_type = [64, 65],
server = Global.SingboxEchDNSTag,
domain = queryServerNames,
});
}
}
if (simpleDNSItem.BlockBindingQuery == true)
{
singboxConfig.dns.rules.Add(new()
{ {
query_type = [64, 65], query_type = [64, 65],
action = "predefined", action = "predefined",
@ -220,7 +264,7 @@ public partial class CoreConfigSingboxService
}); });
} }
if (simpleDnsItem.FakeIP == true && simpleDnsItem.GlobalFakeIp == true) if (simpleDNSItem.FakeIP == true && simpleDNSItem.GlobalFakeIp == true)
{ {
var fakeipFilterRule = JsonUtils.Deserialize<Rule4Sbox>(EmbedUtils.GetEmbedText(Global.SingboxFakeIPFilterFileName)); var fakeipFilterRule = JsonUtils.Deserialize<Rule4Sbox>(EmbedUtils.GetEmbedText(Global.SingboxFakeIPFilterFileName));
fakeipFilterRule.invert = true; fakeipFilterRule.invert = true;
@ -240,13 +284,13 @@ public partial class CoreConfigSingboxService
] ]
}; };
_coreConfig.dns.rules.Add(rule4Fake); singboxConfig.dns.rules.Add(rule4Fake);
} }
var routing = context.RoutingItem; var routing = await ConfigHandler.GetDefaultRouting(_config);
if (routing == null) if (routing == null)
{ {
return; return 0;
} }
var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.RuleSet) ?? []; var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.RuleSet) ?? [];
@ -254,9 +298,9 @@ public partial class CoreConfigSingboxService
var expectedIPsRegions = new List<string>(); var expectedIPsRegions = new List<string>();
var regionNames = new HashSet<string>(); var regionNames = new HashSet<string>();
if (!string.IsNullOrEmpty(simpleDnsItem?.DirectExpectedIPs)) if (!string.IsNullOrEmpty(simpleDNSItem?.DirectExpectedIPs))
{ {
var ipItems = simpleDnsItem.DirectExpectedIPs var ipItems = simpleDNSItem.DirectExpectedIPs
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim()) .Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s)) .Where(s => !string.IsNullOrEmpty(s))
@ -304,7 +348,7 @@ public partial class CoreConfigSingboxService
if (item.OutboundTag == Global.DirectTag) if (item.OutboundTag == Global.DirectTag)
{ {
rule.server = Global.SingboxDirectDNSTag; rule.server = Global.SingboxDirectDNSTag;
rule.strategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Freedom); rule.strategy = Utils.DomainStrategy4Sbox(simpleDNSItem.Strategy4Freedom);
if (expectedIPsRegions.Count > 0 && rule.geosite?.Count > 0) if (expectedIPsRegions.Count > 0 && rule.geosite?.Count > 0)
{ {
@ -329,46 +373,31 @@ public partial class CoreConfigSingboxService
} }
else else
{ {
if (simpleDnsItem.FakeIP == true && simpleDnsItem.GlobalFakeIp == false) if (simpleDNSItem.FakeIP == true && simpleDNSItem.GlobalFakeIp == false)
{ {
var rule4Fake = JsonUtils.DeepCopy(rule); var rule4Fake = JsonUtils.DeepCopy(rule);
rule4Fake.server = Global.SingboxFakeDNSTag; rule4Fake.server = Global.SingboxFakeDNSTag;
rule4Fake.query_type = new List<int> { 1, 28 }; // A and AAAA rule4Fake.query_type = new List<int> { 1, 28 }; // A and AAAA
rule4Fake.rewrite_ttl = 1; rule4Fake.rewrite_ttl = 1;
_coreConfig.dns.rules.Add(rule4Fake); singboxConfig.dns.rules.Add(rule4Fake);
} }
rule.server = Global.SingboxRemoteDNSTag; rule.server = Global.SingboxRemoteDNSTag;
rule.strategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Proxy); rule.strategy = Utils.DomainStrategy4Sbox(simpleDNSItem.Strategy4Proxy);
} }
_coreConfig.dns.rules.Add(rule); singboxConfig.dns.rules.Add(rule);
} }
return 0;
} }
private void GenMinimizedDns() private async Task<int> GenDnsCompatible(ProfileItem? node, SingboxConfig singboxConfig)
{
GenDnsServers();
foreach (var server in _coreConfig.dns!.servers.Where(s => !string.IsNullOrEmpty(s.detour)).ToList())
{
_coreConfig.dns.servers.Remove(server);
}
_coreConfig.dns ??= new();
_coreConfig.dns.rules ??= [];
_coreConfig.dns.rules.Clear();
_coreConfig.dns.final = Global.SingboxDirectDNSTag;
_coreConfig.route.default_domain_resolver = new()
{
server = Global.SingboxDirectDNSTag,
};
}
private void GenDnsCustom()
{ {
try try
{ {
var item = context.RawDnsItem; var item = await AppManager.Instance.GetDNSItem(ECoreType.sing_box);
var strDNS = string.Empty; var strDNS = string.Empty;
if (context.IsTunEnabled) if (_config.TunModeItem.EnableTun)
{ {
strDNS = string.IsNullOrEmpty(item?.TunDNS) ? EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.TunDNS; strDNS = string.IsNullOrEmpty(item?.TunDNS) ? EmbedUtils.GetEmbedText(Global.TunSingboxDNSFileName) : item?.TunDNS;
} }
@ -380,33 +409,61 @@ public partial class CoreConfigSingboxService
var dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(strDNS); var dns4Sbox = JsonUtils.Deserialize<Dns4Sbox>(strDNS);
if (dns4Sbox is null) if (dns4Sbox is null)
{ {
return; return 0;
} }
_coreConfig.dns = dns4Sbox; singboxConfig.dns = dns4Sbox;
if (dns4Sbox.servers?.Count > 0 &&
dns4Sbox.servers.First().address.IsNullOrEmpty()) if (dns4Sbox.servers != null && dns4Sbox.servers.Count > 0 && dns4Sbox.servers.First().address.IsNullOrEmpty())
{ {
GenDnsProtectCustom(); await GenDnsDomainsCompatible(singboxConfig, item);
} }
else else
{ {
GenDnsProtectCustomLegacy(); await GenDnsDomainsLegacyCompatible(singboxConfig, item);
} }
await GenOutboundDnsRule(node, singboxConfig);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private void GenDnsProtectCustom() private async Task<int> GenDnsDomainsCompatible(SingboxConfig singboxConfig, DNSItem? dnsItem)
{ {
var dnsItem = context.RawDnsItem; var dns4Sbox = singboxConfig.dns ?? new();
var dns4Sbox = _coreConfig.dns ?? new();
dns4Sbox.servers ??= []; dns4Sbox.servers ??= [];
dns4Sbox.rules ??= []; dns4Sbox.rules ??= [];
var tag = Global.SingboxLocalDNSTag; var tag = Global.SingboxLocalDNSTag;
var finalDnsAddress = string.IsNullOrEmpty(dnsItem?.DomainDNSAddress) ? Global.DomainPureIPDNSAddress.FirstOrDefault() : dnsItem?.DomainDNSAddress;
var localDnsServer = ParseDnsAddress(finalDnsAddress);
localDnsServer.tag = tag;
dns4Sbox.servers.Add(localDnsServer);
singboxConfig.dns = dns4Sbox;
return await Task.FromResult(0);
}
private async Task<int> GenDnsDomainsLegacyCompatible(SingboxConfig singboxConfig, DNSItem? dnsItem)
{
var dns4Sbox = singboxConfig.dns ?? new();
dns4Sbox.servers ??= [];
dns4Sbox.rules ??= [];
var tag = Global.SingboxLocalDNSTag;
dns4Sbox.servers.Add(new()
{
tag = tag,
address = string.IsNullOrEmpty(dnsItem?.DomainDNSAddress) ? Global.DomainPureIPDNSAddress.FirstOrDefault() : dnsItem?.DomainDNSAddress,
detour = Global.DirectTag,
strategy = string.IsNullOrEmpty(dnsItem?.DomainStrategy4Freedom) ? null : dnsItem?.DomainStrategy4Freedom,
});
dns4Sbox.rules.Insert(0, new() dns4Sbox.rules.Insert(0, new()
{ {
server = tag, server = tag,
@ -418,41 +475,56 @@ public partial class CoreConfigSingboxService
clash_mode = ERuleMode.Global.ToString() clash_mode = ERuleMode.Global.ToString()
}); });
var finalDnsAddress = string.IsNullOrEmpty(dnsItem?.DomainDNSAddress) ? Global.DomainPureIPDNSAddress.FirstOrDefault() : dnsItem?.DomainDNSAddress; var lstDomain = singboxConfig.outbounds
.Where(t => t.server.IsNotEmpty() && Utils.IsDomain(t.server))
var localDnsServer = ParseDnsAddress(finalDnsAddress); .Select(t => t.server)
localDnsServer.tag = tag; .Distinct()
.ToList();
dns4Sbox.servers.Add(localDnsServer); if (lstDomain != null && lstDomain.Count > 0)
dns4Sbox.rules.Insert(0, BuildProtectDomainRule());
_coreConfig.dns = dns4Sbox;
}
private void GenDnsProtectCustomLegacy()
{
GenDnsProtectCustom();
_coreConfig.dns?.servers?.RemoveAll(s => s.tag == Global.SingboxLocalDNSTag);
var dnsItem = context.RawDnsItem;
var localDnsServer = new Server4Sbox()
{ {
address = string.IsNullOrEmpty(dnsItem?.DomainDNSAddress) dns4Sbox.rules.Insert(0, new()
? Global.DomainPureIPDNSAddress.FirstOrDefault() {
: dnsItem?.DomainDNSAddress, server = tag,
tag = Global.SingboxLocalDNSTag, domain = lstDomain
detour = Global.DirectTag, });
}; }
_coreConfig.dns?.servers?.Add(localDnsServer);
singboxConfig.dns = dns4Sbox;
return await Task.FromResult(0);
} }
private Rule4Sbox BuildProtectDomainRule() private async Task<int> GenOutboundDnsRule(ProfileItem? node, SingboxConfig singboxConfig)
{ {
return new() if (node == null)
{
return 0;
}
List<string> domain = new();
if (Utils.IsDomain(node.Address)) // normal outbound
{
domain.Add(node.Address);
}
if (node.Address == Global.Loopback && node.SpiderX.IsNotEmpty()) // Tun2SocksAddress
{
domain.AddRange(Utils.String2List(node.SpiderX)
.Where(Utils.IsDomain)
.Distinct()
.ToList());
}
if (domain.Count == 0)
{
return 0;
}
singboxConfig.dns.rules ??= new List<Rule4Sbox>();
singboxConfig.dns.rules.Insert(0, new Rule4Sbox
{ {
server = Global.SingboxLocalDNSTag, server = Global.SingboxLocalDNSTag,
domain = context.ProtectDomainList.ToList(), domain = domain,
}; });
return await Task.FromResult(0);
} }
private static Server4Sbox? ParseDnsAddress(string address) private static Server4Sbox? ParseDnsAddress(string address)

View file

@ -2,12 +2,12 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private void GenInbounds() private async Task<int> GenInbounds(SingboxConfig singboxConfig)
{ {
try try
{ {
var listen = "0.0.0.0"; var listen = "0.0.0.0";
_coreConfig.inbounds = []; singboxConfig.inbounds = [];
if (!_config.TunModeItem.EnableTun if (!_config.TunModeItem.EnableTun
|| (_config.TunModeItem.EnableTun && _config.TunModeItem.EnableExInbound && AppManager.Instance.RunningCoreType == ECoreType.sing_box)) || (_config.TunModeItem.EnableTun && _config.TunModeItem.EnableExInbound && AppManager.Instance.RunningCoreType == ECoreType.sing_box))
@ -18,23 +18,23 @@ public partial class CoreConfigSingboxService
tag = EInboundProtocol.socks.ToString(), tag = EInboundProtocol.socks.ToString(),
listen = Global.Loopback, listen = Global.Loopback,
}; };
_coreConfig.inbounds.Add(inbound); singboxConfig.inbounds.Add(inbound);
inbound.listen_port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks); inbound.listen_port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks);
if (_config.Inbound.First().SecondLocalPortEnabled) if (_config.Inbound.First().SecondLocalPortEnabled)
{ {
var inbound2 = BuildInbound(inbound, EInboundProtocol.socks2, true); var inbound2 = GetInbound(inbound, EInboundProtocol.socks2, true);
_coreConfig.inbounds.Add(inbound2); singboxConfig.inbounds.Add(inbound2);
} }
if (_config.Inbound.First().AllowLANConn) if (_config.Inbound.First().AllowLANConn)
{ {
if (_config.Inbound.First().NewPort4LAN) if (_config.Inbound.First().NewPort4LAN)
{ {
var inbound3 = BuildInbound(inbound, EInboundProtocol.socks3, true); var inbound3 = GetInbound(inbound, EInboundProtocol.socks3, true);
inbound3.listen = listen; inbound3.listen = listen;
_coreConfig.inbounds.Add(inbound3); singboxConfig.inbounds.Add(inbound3);
//auth //auth
if (_config.Inbound.First().User.IsNotEmpty() && _config.Inbound.First().Pass.IsNotEmpty()) if (_config.Inbound.First().User.IsNotEmpty() && _config.Inbound.First().Pass.IsNotEmpty())
@ -71,16 +71,17 @@ public partial class CoreConfigSingboxService
tunInbound.address = ["172.18.0.1/30"]; tunInbound.address = ["172.18.0.1/30"];
} }
_coreConfig.inbounds.Add(tunInbound); singboxConfig.inbounds.Add(tunInbound);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
private Inbound4Sbox BuildInbound(Inbound4Sbox inItem, EInboundProtocol protocol, bool bSocks) private Inbound4Sbox GetInbound(Inbound4Sbox inItem, EInboundProtocol protocol, bool bSocks)
{ {
var inbound = JsonUtils.DeepCopy(inItem); var inbound = JsonUtils.DeepCopy(inItem);
inbound.tag = protocol.ToString(); inbound.tag = protocol.ToString();

View file

@ -2,7 +2,7 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private void GenLog() private async Task<int> GenLog(SingboxConfig singboxConfig)
{ {
try try
{ {
@ -11,11 +11,11 @@ public partial class CoreConfigSingboxService
case "debug": case "debug":
case "info": case "info":
case "error": case "error":
_coreConfig.log.level = _config.CoreBasicItem.Loglevel; singboxConfig.log.level = _config.CoreBasicItem.Loglevel;
break; break;
case "warning": case "warning":
_coreConfig.log.level = "warn"; singboxConfig.log.level = "warn";
break; break;
default: default:
@ -23,17 +23,18 @@ public partial class CoreConfigSingboxService
} }
if (_config.CoreBasicItem.Loglevel == Global.None) if (_config.CoreBasicItem.Loglevel == Global.None)
{ {
_coreConfig.log.disabled = true; singboxConfig.log.disabled = true;
} }
if (_config.CoreBasicItem.LogEnabled) if (_config.CoreBasicItem.LogEnabled)
{ {
var dtNow = DateTime.Now; var dtNow = DateTime.Now;
_coreConfig.log.output = Utils.GetLogPath($"sbox_{dtNow:yyyy-MM-dd}.txt"); singboxConfig.log.output = Utils.GetLogPath($"sbox_{dtNow:yyyy-MM-dd}.txt");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
} }

View file

@ -2,23 +2,23 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private void GenRouting() private async Task<int> GenRouting(SingboxConfig singboxConfig)
{ {
try try
{ {
_coreConfig.route.final = Global.ProxyTag; singboxConfig.route.final = Global.ProxyTag;
var simpleDnsItem = context.SimpleDnsItem; var simpleDnsItem = _config.SimpleDNSItem;
var defaultDomainResolverTag = Global.SingboxDirectDNSTag; var defaultDomainResolverTag = Global.SingboxDirectDNSTag;
var directDnsStrategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Freedom); var directDnsStrategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Freedom);
var rawDNSItem = context.RawDnsItem; var rawDNSItem = await AppManager.Instance.GetDNSItem(ECoreType.sing_box);
if (rawDNSItem is { Enabled: true }) if (rawDNSItem is { Enabled: true })
{ {
defaultDomainResolverTag = Global.SingboxLocalDNSTag; defaultDomainResolverTag = Global.SingboxLocalDNSTag;
directDnsStrategy = rawDNSItem.DomainStrategy4Freedom.IsNullOrEmpty() ? null : rawDNSItem.DomainStrategy4Freedom; directDnsStrategy = rawDNSItem.DomainStrategy4Freedom.IsNullOrEmpty() ? null : rawDNSItem.DomainStrategy4Freedom;
} }
_coreConfig.route.default_domain_resolver = new() singboxConfig.route.default_domain_resolver = new()
{ {
server = defaultDomainResolverTag, server = defaultDomainResolverTag,
strategy = directDnsStrategy strategy = directDnsStrategy
@ -26,23 +26,23 @@ public partial class CoreConfigSingboxService
if (_config.TunModeItem.EnableTun) if (_config.TunModeItem.EnableTun)
{ {
_coreConfig.route.auto_detect_interface = true; singboxConfig.route.auto_detect_interface = true;
var tunRules = JsonUtils.Deserialize<List<Rule4Sbox>>(EmbedUtils.GetEmbedText(Global.TunSingboxRulesFileName)); var tunRules = JsonUtils.Deserialize<List<Rule4Sbox>>(EmbedUtils.GetEmbedText(Global.TunSingboxRulesFileName));
if (tunRules != null) if (tunRules != null)
{ {
_coreConfig.route.rules.AddRange(tunRules); singboxConfig.route.rules.AddRange(tunRules);
} }
var (lstDnsExe, lstDirectExe) = BuildRoutingDirectExe(); GenRoutingDirectExe(out var lstDnsExe, out var lstDirectExe);
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
port = [53], port = new() { 53 },
action = "hijack-dns", action = "hijack-dns",
process_name = lstDnsExe process_name = lstDnsExe
}); });
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
outbound = Global.DirectTag, outbound = Global.DirectTag,
process_name = lstDirectExe process_name = lstDirectExe
@ -51,62 +51,66 @@ public partial class CoreConfigSingboxService
if (_config.Inbound.First().SniffingEnabled) if (_config.Inbound.First().SniffingEnabled)
{ {
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
action = "sniff" action = "sniff"
}); });
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
protocol = ["dns"], protocol = new() { "dns" },
action = "hijack-dns" action = "hijack-dns"
}); });
} }
else else
{ {
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
port = [53], port = new() { 53 },
network = ["udp"], network = new() { "udp" },
action = "hijack-dns" action = "hijack-dns"
}); });
} }
var hostsDomains = new List<string>(); var hostsDomains = new List<string>();
if (rawDNSItem is not { Enabled: true }) var dnsItem = await AppManager.Instance.GetDNSItem(ECoreType.sing_box);
if (dnsItem == null || !dnsItem.Enabled)
{ {
var userHostsMap = Utils.ParseHostsToDictionary(simpleDnsItem.Hosts); var userHostsMap = Utils.ParseHostsToDictionary(simpleDnsItem.Hosts);
hostsDomains.AddRange(userHostsMap.Select(kvp => kvp.Key)); hostsDomains.AddRange(userHostsMap.Select(kvp => kvp.Key));
if (simpleDnsItem.UseSystemHosts == true) if (simpleDnsItem.UseSystemHosts == true)
{ {
var systemHostsMap = Utils.GetSystemHosts(); var systemHostsMap = Utils.GetSystemHosts();
hostsDomains.AddRange(systemHostsMap.Select(kvp => kvp.Key)); foreach (var kvp in systemHostsMap)
{
hostsDomains.Add(kvp.Key);
}
} }
} }
if (hostsDomains.Count > 0) if (hostsDomains.Count > 0)
{ {
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
action = "resolve", action = "resolve",
domain = hostsDomains, domain = hostsDomains,
}); });
} }
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
outbound = Global.DirectTag, outbound = Global.DirectTag,
clash_mode = ERuleMode.Direct.ToString() clash_mode = ERuleMode.Direct.ToString()
}); });
_coreConfig.route.rules.Add(new() singboxConfig.route.rules.Add(new()
{ {
outbound = Global.ProxyTag, outbound = Global.ProxyTag,
clash_mode = ERuleMode.Global.ToString() clash_mode = ERuleMode.Global.ToString()
}); });
var domainStrategy = _config.RoutingBasicItem.DomainStrategy4Singbox.NullIfEmpty(); var domainStrategy = _config.RoutingBasicItem.DomainStrategy4Singbox.NullIfEmpty();
var routing = context.RoutingItem; var defaultRouting = await ConfigHandler.GetDefaultRouting(_config);
if (routing.DomainStrategy4Singbox.IsNotEmpty()) if (defaultRouting.DomainStrategy4Singbox.IsNotEmpty())
{ {
domainStrategy = routing.DomainStrategy4Singbox; domainStrategy = defaultRouting.DomainStrategy4Singbox;
} }
var resolveRule = new Rule4Sbox var resolveRule = new Rule4Sbox
{ {
@ -115,9 +119,10 @@ public partial class CoreConfigSingboxService
}; };
if (_config.RoutingBasicItem.DomainStrategy == Global.IPOnDemand) if (_config.RoutingBasicItem.DomainStrategy == Global.IPOnDemand)
{ {
_coreConfig.route.rules.Add(resolveRule); singboxConfig.route.rules.Add(resolveRule);
} }
var routing = await ConfigHandler.GetDefaultRouting(_config);
var ipRules = new List<RulesItem>(); var ipRules = new List<RulesItem>();
if (routing != null) if (routing != null)
{ {
@ -134,7 +139,7 @@ public partial class CoreConfigSingboxService
continue; continue;
} }
GenRoutingUserRule(item1); await GenRoutingUserRule(item1, singboxConfig);
if (item1.Ip?.Count > 0) if (item1.Ip?.Count > 0)
{ {
@ -144,10 +149,10 @@ public partial class CoreConfigSingboxService
} }
if (_config.RoutingBasicItem.DomainStrategy == Global.IPIfNonMatch) if (_config.RoutingBasicItem.DomainStrategy == Global.IPIfNonMatch)
{ {
_coreConfig.route.rules.Add(resolveRule); singboxConfig.route.rules.Add(resolveRule);
foreach (var item2 in ipRules) foreach (var item2 in ipRules)
{ {
GenRoutingUserRule(item2); await GenRoutingUserRule(item2, singboxConfig);
} }
} }
} }
@ -155,9 +160,10 @@ public partial class CoreConfigSingboxService
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private static (List<string> lstDnsExe, List<string> lstDirectExe) BuildRoutingDirectExe() private void GenRoutingDirectExe(out List<string> lstDnsExe, out List<string> lstDirectExe)
{ {
var dnsExeSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase); var dnsExeSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var directExeSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase); var directExeSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
@ -181,22 +187,20 @@ public partial class CoreConfigSingboxService
} }
} }
var lstDnsExe = new List<string>(dnsExeSet); lstDnsExe = new List<string>(dnsExeSet);
var lstDirectExe = new List<string>(directExeSet); lstDirectExe = new List<string>(directExeSet);
return (lstDnsExe, lstDirectExe);
} }
private void GenRoutingUserRule(RulesItem? item) private async Task<int> GenRoutingUserRule(RulesItem item, SingboxConfig singboxConfig)
{ {
try try
{ {
if (item == null) if (item == null)
{ {
return; return 0;
} }
item.OutboundTag = GenRoutingUserRuleOutbound(item.OutboundTag ?? Global.ProxyTag); item.OutboundTag = await GenRoutingUserRuleOutbound(item.OutboundTag, singboxConfig);
var rules = _coreConfig.route.rules; var rules = singboxConfig.route.rules;
var rule = new Rule4Sbox(); var rule = new Rule4Sbox();
if (item.OutboundTag == "block") if (item.OutboundTag == "block")
@ -322,9 +326,10 @@ public partial class CoreConfigSingboxService
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
private static bool ParseV2Domain(string domain, Rule4Sbox rule) private bool ParseV2Domain(string domain, Rule4Sbox rule)
{ {
if (domain.StartsWith('#') || domain.StartsWith("ext:") || domain.StartsWith("ext-domain:")) if (domain.StartsWith('#') || domain.StartsWith("ext:") || domain.StartsWith("ext-domain:"))
{ {
@ -363,7 +368,7 @@ public partial class CoreConfigSingboxService
return true; return true;
} }
private static bool ParseV2Address(string address, Rule4Sbox rule) private bool ParseV2Address(string address, Rule4Sbox rule)
{ {
if (address.StartsWith("ext:") || address.StartsWith("ext-ip:")) if (address.StartsWith("ext:") || address.StartsWith("ext-ip:"))
{ {
@ -396,14 +401,14 @@ public partial class CoreConfigSingboxService
return true; return true;
} }
private string GenRoutingUserRuleOutbound(string outboundTag) private async Task<string?> GenRoutingUserRuleOutbound(string outboundTag, SingboxConfig singboxConfig)
{ {
if (Global.OutboundTags.Contains(outboundTag)) if (Global.OutboundTags.Contains(outboundTag))
{ {
return outboundTag; return outboundTag;
} }
var node = context.AllProxiesMap.GetValueOrDefault($"remark:{outboundTag}"); var node = await AppManager.Instance.GetProfileItemViaRemarks(outboundTag);
if (node == null if (node == null
|| (!Global.SingboxSupportConfigType.Contains(node.ConfigType) || (!Global.SingboxSupportConfigType.Contains(node.ConfigType)
@ -413,15 +418,39 @@ public partial class CoreConfigSingboxService
} }
var tag = $"{node.IndexId}-{Global.ProxyTag}"; var tag = $"{node.IndexId}-{Global.ProxyTag}";
if (_coreConfig.outbounds.Any(o => o.tag.StartsWith(tag)) if (singboxConfig.outbounds.Any(o => o.tag == tag)
|| (_coreConfig.endpoints != null && _coreConfig.endpoints.Any(e => e.tag.StartsWith(tag)))) || (singboxConfig.endpoints != null && singboxConfig.endpoints.Any(e => e.tag == tag)))
{ {
return tag; return tag;
} }
var proxyOutbounds = new CoreConfigSingboxService(context with { Node = node, }).BuildAllProxyOutbounds(tag); if (node.ConfigType.IsGroupType())
FillRangeProxy(proxyOutbounds, _coreConfig, false); {
var ret = await GenGroupOutbound(node, singboxConfig, tag);
if (ret == 0)
{
return tag;
}
return Global.ProxyTag;
}
return tag; var server = await GenServer(node);
if (server is null)
{
return Global.ProxyTag;
}
server.tag = tag;
if (server is Endpoints4Sbox endpoint)
{
singboxConfig.endpoints ??= new();
singboxConfig.endpoints.Add(endpoint);
}
else if (server is Outbound4Sbox outbound)
{
singboxConfig.outbounds.Add(outbound);
}
return server.tag;
} }
} }

View file

@ -2,7 +2,7 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private void ConvertGeo2Ruleset() private async Task<int> ConvertGeo2Ruleset(SingboxConfig singboxConfig)
{ {
static void AddRuleSets(List<string> ruleSets, List<string>? rule_set) static void AddRuleSets(List<string> ruleSets, List<string>? rule_set)
{ {
@ -16,14 +16,14 @@ public partial class CoreConfigSingboxService
var ruleSets = new List<string>(); var ruleSets = new List<string>();
//convert route geosite & geoip to ruleset //convert route geosite & geoip to ruleset
foreach (var rule in _coreConfig.route.rules.Where(t => t.geosite?.Count > 0).ToList() ?? []) foreach (var rule in singboxConfig.route.rules.Where(t => t.geosite?.Count > 0).ToList() ?? [])
{ {
rule.rule_set ??= new List<string>(); rule.rule_set ??= new List<string>();
rule.rule_set.AddRange(rule?.geosite?.Select(t => $"{geosite}-{t}").ToList()); rule.rule_set.AddRange(rule?.geosite?.Select(t => $"{geosite}-{t}").ToList());
rule.geosite = null; rule.geosite = null;
AddRuleSets(ruleSets, rule.rule_set); AddRuleSets(ruleSets, rule.rule_set);
} }
foreach (var rule in _coreConfig.route.rules.Where(t => t.geoip?.Count > 0).ToList() ?? []) foreach (var rule in singboxConfig.route.rules.Where(t => t.geoip?.Count > 0).ToList() ?? [])
{ {
rule.rule_set ??= new List<string>(); rule.rule_set ??= new List<string>();
rule.rule_set.AddRange(rule?.geoip?.Select(t => $"{geoip}-{t}").ToList()); rule.rule_set.AddRange(rule?.geoip?.Select(t => $"{geoip}-{t}").ToList());
@ -32,24 +32,24 @@ public partial class CoreConfigSingboxService
} }
//convert dns geosite & geoip to ruleset //convert dns geosite & geoip to ruleset
foreach (var rule in _coreConfig.dns?.rules.Where(t => t.geosite?.Count > 0).ToList() ?? []) foreach (var rule in singboxConfig.dns?.rules.Where(t => t.geosite?.Count > 0).ToList() ?? [])
{ {
rule.rule_set ??= new List<string>(); rule.rule_set ??= new List<string>();
rule.rule_set.AddRange(rule?.geosite?.Select(t => $"{geosite}-{t}").ToList()); rule.rule_set.AddRange(rule?.geosite?.Select(t => $"{geosite}-{t}").ToList());
rule.geosite = null; rule.geosite = null;
} }
foreach (var rule in _coreConfig.dns?.rules.Where(t => t.geoip?.Count > 0).ToList() ?? []) foreach (var rule in singboxConfig.dns?.rules.Where(t => t.geoip?.Count > 0).ToList() ?? [])
{ {
rule.rule_set ??= new List<string>(); rule.rule_set ??= new List<string>();
rule.rule_set.AddRange(rule?.geoip?.Select(t => $"{geoip}-{t}").ToList()); rule.rule_set.AddRange(rule?.geoip?.Select(t => $"{geoip}-{t}").ToList());
rule.geoip = null; rule.geoip = null;
} }
foreach (var dnsRule in _coreConfig.dns?.rules.Where(t => t.rule_set?.Count > 0).ToList() ?? []) foreach (var dnsRule in singboxConfig.dns?.rules.Where(t => t.rule_set?.Count > 0).ToList() ?? [])
{ {
AddRuleSets(ruleSets, dnsRule.rule_set); AddRuleSets(ruleSets, dnsRule.rule_set);
} }
//rules in rules //rules in rules
foreach (var item in _coreConfig.dns?.rules.Where(t => t.rules?.Count > 0).Select(t => t.rules).ToList() ?? []) foreach (var item in singboxConfig.dns?.rules.Where(t => t.rules?.Count > 0).Select(t => t.rules).ToList() ?? [])
{ {
foreach (var item2 in item ?? []) foreach (var item2 in item ?? [])
{ {
@ -60,7 +60,7 @@ public partial class CoreConfigSingboxService
//load custom ruleset file //load custom ruleset file
List<Ruleset4Sbox> customRulesets = []; List<Ruleset4Sbox> customRulesets = [];
var routing = context.RoutingItem; var routing = await ConfigHandler.GetDefaultRouting(_config);
if (routing.CustomRulesetPath4Singbox.IsNotEmpty()) if (routing.CustomRulesetPath4Singbox.IsNotEmpty())
{ {
var result = EmbedUtils.LoadResource(routing.CustomRulesetPath4Singbox); var result = EmbedUtils.LoadResource(routing.CustomRulesetPath4Singbox);
@ -78,7 +78,7 @@ public partial class CoreConfigSingboxService
var localSrss = Utils.GetBinPath("srss"); var localSrss = Utils.GetBinPath("srss");
//Add ruleset srs //Add ruleset srs
_coreConfig.route.rule_set = []; singboxConfig.route.rule_set = [];
foreach (var item in new HashSet<string>(ruleSets)) foreach (var item in new HashSet<string>(ruleSets))
{ {
if (item.IsNullOrEmpty()) if (item.IsNullOrEmpty())
@ -113,7 +113,9 @@ public partial class CoreConfigSingboxService
}; };
} }
} }
_coreConfig.route.rule_set.Add(customRuleset); singboxConfig.route.rule_set.Add(customRuleset);
} }
return 0;
} }
} }

View file

@ -2,12 +2,12 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigSingboxService public partial class CoreConfigSingboxService
{ {
private void GenExperimental() private async Task<int> GenExperimental(SingboxConfig singboxConfig)
{ {
//if (_config.guiItem.enableStatistics) //if (_config.guiItem.enableStatistics)
{ {
_coreConfig.experimental ??= new Experimental4Sbox(); singboxConfig.experimental ??= new Experimental4Sbox();
_coreConfig.experimental.clash_api = new Clash_Api4Sbox() singboxConfig.experimental.clash_api = new Clash_Api4Sbox()
{ {
external_controller = $"{Global.Loopback}:{AppManager.Instance.StatePort2}", external_controller = $"{Global.Loopback}:{AppManager.Instance.StatePort2}",
}; };
@ -15,13 +15,15 @@ public partial class CoreConfigSingboxService
if (_config.CoreBasicItem.EnableCacheFile4Sbox) if (_config.CoreBasicItem.EnableCacheFile4Sbox)
{ {
_coreConfig.experimental ??= new Experimental4Sbox(); singboxConfig.experimental ??= new Experimental4Sbox();
_coreConfig.experimental.cache_file = new CacheFile4Sbox() singboxConfig.experimental.cache_file = new CacheFile4Sbox()
{ {
enabled = true, enabled = true,
path = Utils.GetBinPath("cache.db"), path = Utils.GetBinPath("cache.db"),
store_fakeip = context.SimpleDnsItem.FakeIP == true store_fakeip = _config.SimpleDNSItem.FakeIP == true
}; };
} }
return await Task.FromResult(0);
} }
} }

View file

@ -1,35 +1,44 @@
namespace ServiceLib.Services.CoreConfig; namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService(CoreConfigContext context) public partial class CoreConfigV2rayService(Config config)
{ {
private readonly Config _config = config;
private static readonly string _tag = "CoreConfigV2rayService"; private static readonly string _tag = "CoreConfigV2rayService";
private readonly Config _config = context.AppConfig;
private readonly ProfileItem _node = context.Node;
private V2rayConfig _coreConfig = new();
#region public gen function #region public gen function
public RetResult GenerateClientConfigContent() public async Task<RetResult> GenerateClientConfigContent(ProfileItem node)
{ {
var ret = new RetResult(); var ret = new RetResult();
try try
{ {
if (_node == null if (node == null
|| !_node.IsValid()) || !node.IsValid())
{ {
ret.Msg = ResUI.CheckServerSettings; ret.Msg = ResUI.CheckServerSettings;
return ret; return ret;
} }
if (_node.GetNetwork() is nameof(ETransport.quic)) if (node.GetNetwork() is nameof(ETransport.quic))
{ {
ret.Msg = ResUI.Incorrectconfiguration + $" - {_node.GetNetwork()}"; ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}";
return ret; return ret;
} }
ret.Msg = ResUI.InitialConfiguration; ret.Msg = ResUI.InitialConfiguration;
if (node.ConfigType.IsGroupType())
{
switch (node.ConfigType)
{
case EConfigType.PolicyGroup:
return await GenerateClientMultipleLoadConfig(node);
case EConfigType.ProxyChain:
return await GenerateClientChainConfig(node);
}
}
var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient); var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient);
if (result.IsNullOrEmpty()) if (result.IsNullOrEmpty())
{ {
@ -37,28 +46,30 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
return ret; return ret;
} }
_coreConfig = JsonUtils.Deserialize<V2rayConfig>(result); var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (_coreConfig == null) if (v2rayConfig == null)
{ {
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
} }
GenLog(); await GenLog(v2rayConfig);
GenInbounds(); await GenInbounds(v2rayConfig);
GenOutbounds(); await GenOutbound(node, v2rayConfig.outbounds.First());
GenRouting(); await GenMoreOutbounds(node, v2rayConfig);
GenDns(); await GenRouting(v2rayConfig);
GenStatistic(); await GenDns(node, v2rayConfig);
await GenStatistic(v2rayConfig);
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Success = true; ret.Success = true;
ret.Data = ApplyFullConfigTemplate(); ret.Data = await ApplyFullConfigTemplate(v2rayConfig);
return ret; return ret;
} }
catch (Exception ex) catch (Exception ex)
@ -69,11 +80,18 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
} }
} }
public RetResult GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds) public async Task<RetResult> GenerateClientMultipleLoadConfig(ProfileItem parentNode)
{ {
var ret = new RetResult(); var ret = new RetResult();
try try
{ {
if (_config == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration; ret.Msg = ResUI.InitialConfiguration;
var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient); var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient);
@ -84,8 +102,172 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
return ret; return ret;
} }
_coreConfig = JsonUtils.Deserialize<V2rayConfig>(result); var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (_coreConfig == null) if (v2rayConfig == null)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
v2rayConfig.outbounds.RemoveAt(0);
await GenLog(v2rayConfig);
await GenInbounds(v2rayConfig);
var groupRet = await GenGroupOutbound(parentNode, v2rayConfig);
if (groupRet != 0)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
await GenRouting(v2rayConfig);
await GenDns(null, v2rayConfig);
await GenStatistic(v2rayConfig);
var defaultBalancerTag = $"{Global.ProxyTag}{Global.BalancerTagSuffix}";
//add rule
var rules = v2rayConfig.routing.rules;
if (rules?.Count > 0 && ((v2rayConfig.routing.balancers?.Count ?? 0) > 0))
{
var balancerTagSet = v2rayConfig.routing.balancers
.Select(b => b.tag)
.ToHashSet();
foreach (var rule in rules)
{
if (rule.outboundTag == null)
{
continue;
}
if (balancerTagSet.Contains(rule.outboundTag))
{
rule.balancerTag = rule.outboundTag;
rule.outboundTag = null;
continue;
}
var outboundWithSuffix = rule.outboundTag + Global.BalancerTagSuffix;
if (balancerTagSet.Contains(outboundWithSuffix))
{
rule.balancerTag = outboundWithSuffix;
rule.outboundTag = null;
}
}
}
if (v2rayConfig.routing.domainStrategy == Global.IPIfNonMatch)
{
v2rayConfig.routing.rules.Add(new()
{
ip = ["0.0.0.0/0", "::/0"],
balancerTag = defaultBalancerTag,
type = "field"
});
}
else
{
v2rayConfig.routing.rules.Add(new()
{
network = "tcp,udp",
balancerTag = defaultBalancerTag,
type = "field"
});
}
ret.Success = true;
ret.Data = await ApplyFullConfigTemplate(v2rayConfig);
return ret;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
}
public async Task<RetResult> GenerateClientChainConfig(ProfileItem parentNode)
{
var ret = new RetResult();
try
{
if (_config == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration;
var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient);
var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
if (result.IsNullOrEmpty() || txtOutbound.IsNullOrEmpty())
{
ret.Msg = ResUI.FailedGetDefaultConfiguration;
return ret;
}
var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (v2rayConfig == null)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
v2rayConfig.outbounds.RemoveAt(0);
await GenLog(v2rayConfig);
await GenInbounds(v2rayConfig);
var groupRet = await GenGroupOutbound(parentNode, v2rayConfig);
if (groupRet != 0)
{
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
await GenRouting(v2rayConfig);
await GenDns(null, v2rayConfig);
await GenStatistic(v2rayConfig);
ret.Success = true;
ret.Data = await ApplyFullConfigTemplate(v2rayConfig);
return ret;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
}
public async Task<RetResult> GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds)
{
var ret = new RetResult();
try
{
if (_config == null)
{
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
ret.Msg = ResUI.InitialConfiguration;
var result = EmbedUtils.GetEmbedText(Global.V2raySampleClient);
var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
if (result.IsNullOrEmpty() || txtOutbound.IsNullOrEmpty())
{
ret.Msg = ResUI.FailedGetDefaultConfiguration;
return ret;
}
var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (v2rayConfig == null)
{ {
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
@ -103,10 +285,10 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
GenLog(); await GenLog(v2rayConfig);
_coreConfig.inbounds.Clear(); v2rayConfig.inbounds.Clear();
_coreConfig.outbounds.Clear(); v2rayConfig.outbounds.Clear();
_coreConfig.routing.rules.Clear(); v2rayConfig.routing.rules.Clear();
var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest); var initPort = AppManager.Instance.GetLocalPort(EInboundProtocol.speedtest);
@ -120,7 +302,7 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
{ {
continue; continue;
} }
var item = context.AllProxiesMap.GetValueOrDefault(it.IndexId); var item = await AppManager.Instance.GetProfileItem(it.IndexId);
if (item is null || item.IsComplex() || !item.IsValid()) if (item is null || item.IsComplex() || !item.IsValid())
{ {
continue; continue;
@ -160,40 +342,27 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
protocol = EInboundProtocol.mixed.ToString(), protocol = EInboundProtocol.mixed.ToString(),
}; };
inbound.tag = inbound.protocol + inbound.port.ToString(); inbound.tag = inbound.protocol + inbound.port.ToString();
_coreConfig.inbounds.Add(inbound); v2rayConfig.inbounds.Add(inbound);
var tag = Global.ProxyTag + inbound.port.ToString();
var isBalancer = false;
//outbound //outbound
var proxyOutbounds = var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
new CoreConfigV2rayService(context with { Node = item }).BuildAllProxyOutbounds(tag); await GenOutbound(item, outbound);
_coreConfig.outbounds.AddRange(proxyOutbounds); outbound.tag = Global.ProxyTag + inbound.port.ToString();
if (proxyOutbounds.Count(n => n.tag.StartsWith(tag)) > 1) v2rayConfig.outbounds.Add(outbound);
{
isBalancer = true;
var multipleLoad = _node.GetProtocolExtra().MultipleLoad ?? EMultipleLoad.LeastPing;
GenObservatory(multipleLoad, tag);
GenBalancer(multipleLoad, tag);
}
//rule //rule
RulesItem4Ray rule = new() RulesItem4Ray rule = new()
{ {
inboundTag = new List<string> { inbound.tag }, inboundTag = new List<string> { inbound.tag },
outboundTag = tag, outboundTag = outbound.tag,
type = "field" type = "field"
}; };
if (isBalancer) v2rayConfig.routing.rules.Add(rule);
{
rule.balancerTag = tag;
rule.outboundTag = null;
}
_coreConfig.routing.rules.Add(rule);
} }
//ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary()); //ret.Msg =string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
ret.Success = true; ret.Success = true;
ret.Data = JsonUtils.Serialize(_coreConfig); ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret; return ret;
} }
catch (Exception ex) catch (Exception ex)
@ -204,21 +373,21 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
} }
} }
public RetResult GenerateClientSpeedtestConfig(int port) public async Task<RetResult> GenerateClientSpeedtestConfig(ProfileItem node, int port)
{ {
var ret = new RetResult(); var ret = new RetResult();
try try
{ {
if (_node == null if (node == null
|| !_node.IsValid()) || !node.IsValid())
{ {
ret.Msg = ResUI.CheckServerSettings; ret.Msg = ResUI.CheckServerSettings;
return ret; return ret;
} }
if (_node.GetNetwork() is nameof(ETransport.quic)) if (node.GetNetwork() is nameof(ETransport.quic))
{ {
ret.Msg = ResUI.Incorrectconfiguration + $" - {_node.GetNetwork()}"; ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}";
return ret; return ret;
} }
@ -229,19 +398,20 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
return ret; return ret;
} }
_coreConfig = JsonUtils.Deserialize<V2rayConfig>(result); var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (_coreConfig == null) if (v2rayConfig == null)
{ {
ret.Msg = ResUI.FailedGenDefaultConfiguration; ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret; return ret;
} }
GenLog(); await GenLog(v2rayConfig);
GenOutbounds(); await GenOutbound(node, v2rayConfig.outbounds.First());
await GenMoreOutbounds(node, v2rayConfig);
_coreConfig.routing.rules.Clear(); v2rayConfig.routing.rules.Clear();
_coreConfig.inbounds.Clear(); v2rayConfig.inbounds.Clear();
_coreConfig.inbounds.Add(new() v2rayConfig.inbounds.Add(new()
{ {
tag = $"{EInboundProtocol.socks}{port}", tag = $"{EInboundProtocol.socks}{port}",
listen = Global.Loopback, listen = Global.Loopback,
@ -251,7 +421,7 @@ public partial class CoreConfigV2rayService(CoreConfigContext context)
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Success = true; ret.Success = true;
ret.Data = JsonUtils.Serialize(_coreConfig); ret.Data = JsonUtils.Serialize(v2rayConfig);
return ret; return ret;
} }
catch (Exception ex) catch (Exception ex)

View file

@ -2,17 +2,17 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenObservatory(EMultipleLoad multipleLoad, string baseTagName = Global.ProxyTag) private async Task<int> GenObservatory(V2rayConfig v2rayConfig, EMultipleLoad multipleLoad, string baseTagName = Global.ProxyTag)
{ {
// Collect all existing subject selectors from both observatories // Collect all existing subject selectors from both observatories
var subjectSelectors = new List<string>(); var subjectSelectors = new List<string>();
subjectSelectors.AddRange(_coreConfig.burstObservatory?.subjectSelector ?? []); subjectSelectors.AddRange(v2rayConfig.burstObservatory?.subjectSelector ?? []);
subjectSelectors.AddRange(_coreConfig.observatory?.subjectSelector ?? []); subjectSelectors.AddRange(v2rayConfig.observatory?.subjectSelector ?? []);
// Case 1: exact match already exists -> nothing to do // Case 1: exact match already exists -> nothing to do
if (subjectSelectors.Any(baseTagName.StartsWith)) if (subjectSelectors.Any(baseTagName.StartsWith))
{ {
return; return await Task.FromResult(0);
} }
// Case 2: prefix match exists -> reuse it and move to the first position // Case 2: prefix match exists -> reuse it and move to the first position
@ -21,28 +21,28 @@ public partial class CoreConfigV2rayService
{ {
baseTagName = matched; baseTagName = matched;
if (_coreConfig.burstObservatory?.subjectSelector?.Contains(baseTagName) == true) if (v2rayConfig.burstObservatory?.subjectSelector?.Contains(baseTagName) == true)
{ {
_coreConfig.burstObservatory.subjectSelector.Remove(baseTagName); v2rayConfig.burstObservatory.subjectSelector.Remove(baseTagName);
_coreConfig.burstObservatory.subjectSelector.Insert(0, baseTagName); v2rayConfig.burstObservatory.subjectSelector.Insert(0, baseTagName);
} }
if (_coreConfig.observatory?.subjectSelector?.Contains(baseTagName) == true) if (v2rayConfig.observatory?.subjectSelector?.Contains(baseTagName) == true)
{ {
_coreConfig.observatory.subjectSelector.Remove(baseTagName); v2rayConfig.observatory.subjectSelector.Remove(baseTagName);
_coreConfig.observatory.subjectSelector.Insert(0, baseTagName); v2rayConfig.observatory.subjectSelector.Insert(0, baseTagName);
} }
return; return await Task.FromResult(0);
} }
// Case 3: need to create or insert based on multipleLoad type // Case 3: need to create or insert based on multipleLoad type
if (multipleLoad is EMultipleLoad.LeastLoad or EMultipleLoad.Fallback) if (multipleLoad is EMultipleLoad.LeastLoad or EMultipleLoad.Fallback)
{ {
if (_coreConfig.burstObservatory is null) if (v2rayConfig.burstObservatory is null)
{ {
// Create new burst observatory with default ping config // Create new burst observatory with default ping config
_coreConfig.burstObservatory = new BurstObservatory4Ray v2rayConfig.burstObservatory = new BurstObservatory4Ray
{ {
subjectSelector = [baseTagName], subjectSelector = [baseTagName],
pingConfig = new() pingConfig = new()
@ -56,16 +56,16 @@ public partial class CoreConfigV2rayService
} }
else else
{ {
_coreConfig.burstObservatory.subjectSelector ??= new(); v2rayConfig.burstObservatory.subjectSelector ??= new();
_coreConfig.burstObservatory.subjectSelector.Add(baseTagName); v2rayConfig.burstObservatory.subjectSelector.Add(baseTagName);
} }
} }
else if (multipleLoad is EMultipleLoad.LeastPing) else if (multipleLoad is EMultipleLoad.LeastPing)
{ {
if (_coreConfig.observatory is null) if (v2rayConfig.observatory is null)
{ {
// Create new observatory with default probe config // Create new observatory with default probe config
_coreConfig.observatory = new Observatory4Ray v2rayConfig.observatory = new Observatory4Ray
{ {
subjectSelector = [baseTagName], subjectSelector = [baseTagName],
probeUrl = AppManager.Instance.Config.SpeedTestItem.SpeedPingTestUrl, probeUrl = AppManager.Instance.Config.SpeedTestItem.SpeedPingTestUrl,
@ -75,13 +75,15 @@ public partial class CoreConfigV2rayService
} }
else else
{ {
_coreConfig.observatory.subjectSelector ??= new(); v2rayConfig.observatory.subjectSelector ??= new();
_coreConfig.observatory.subjectSelector.Add(baseTagName); v2rayConfig.observatory.subjectSelector.Add(baseTagName);
} }
} }
return await Task.FromResult(0);
} }
private void GenBalancer(EMultipleLoad multipleLoad, string selector = Global.ProxyTag) private async Task<string> GenBalancer(V2rayConfig v2rayConfig, EMultipleLoad multipleLoad, string selector = Global.ProxyTag)
{ {
var strategyType = multipleLoad switch var strategyType = multipleLoad switch
{ {
@ -105,7 +107,8 @@ public partial class CoreConfigV2rayService
}, },
tag = balancerTag, tag = balancerTag,
}; };
_coreConfig.routing.balancers ??= new(); v2rayConfig.routing.balancers ??= new();
_coreConfig.routing.balancers.Add(balancer); v2rayConfig.routing.balancers.Add(balancer);
return await Task.FromResult(balancerTag);
} }
} }

View file

@ -2,39 +2,35 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private string ApplyFullConfigTemplate() private async Task<string> ApplyFullConfigTemplate(V2rayConfig v2rayConfig)
{ {
var fullConfigTemplate = context.FullConfigTemplate; var fullConfigTemplate = await AppManager.Instance.GetFullConfigTemplateItem(ECoreType.Xray);
if (fullConfigTemplate == null || !fullConfigTemplate.Enabled || fullConfigTemplate.Config.IsNullOrEmpty()) if (fullConfigTemplate == null || !fullConfigTemplate.Enabled || fullConfigTemplate.Config.IsNullOrEmpty())
{ {
return JsonUtils.Serialize(_coreConfig); return JsonUtils.Serialize(v2rayConfig);
} }
var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplate.Config); var fullConfigTemplateNode = JsonNode.Parse(fullConfigTemplate.Config);
if (fullConfigTemplateNode == null) if (fullConfigTemplateNode == null)
{ {
return JsonUtils.Serialize(_coreConfig); return JsonUtils.Serialize(v2rayConfig);
} }
// Handle balancer and rules modifications (for multiple load scenarios) // Handle balancer and rules modifications (for multiple load scenarios)
if (_coreConfig.routing?.balancers?.Count > 0) if (v2rayConfig.routing?.balancers?.Count > 0)
{ {
var balancer = var balancer = v2rayConfig.routing.balancers.First();
_coreConfig.routing.balancers.FirstOrDefault(b => b.tag == Global.ProxyTag + Global.BalancerTagSuffix, null);
// Modify existing rules in custom config // Modify existing rules in custom config
if (balancer != null) var rulesNode = fullConfigTemplateNode["routing"]?["rules"];
if (rulesNode != null)
{ {
var rulesNode = fullConfigTemplateNode["routing"]?["rules"]; foreach (var rule in rulesNode.AsArray())
if (rulesNode != null)
{ {
foreach (var rule in rulesNode.AsArray()) if (rule["outboundTag"]?.GetValue<string>() == Global.ProxyTag)
{ {
if (rule["outboundTag"]?.GetValue<string>() == Global.ProxyTag) rule.AsObject().Remove("outboundTag");
{ rule["balancerTag"] = balancer.tag;
rule.AsObject().Remove("outboundTag");
rule["balancerTag"] = balancer.tag;
}
} }
} }
} }
@ -48,7 +44,7 @@ public partial class CoreConfigV2rayService
// Handle balancers - append instead of override // Handle balancers - append instead of override
if (fullConfigTemplateNode["routing"]["balancers"] is JsonArray customBalancersNode) if (fullConfigTemplateNode["routing"]["balancers"] is JsonArray customBalancersNode)
{ {
if (JsonNode.Parse(JsonUtils.Serialize(_coreConfig.routing.balancers)) is JsonArray newBalancers) if (JsonNode.Parse(JsonUtils.Serialize(v2rayConfig.routing.balancers)) is JsonArray newBalancers)
{ {
foreach (var balancerNode in newBalancers) foreach (var balancerNode in newBalancers)
{ {
@ -58,33 +54,33 @@ public partial class CoreConfigV2rayService
} }
else else
{ {
fullConfigTemplateNode["routing"]["balancers"] = JsonNode.Parse(JsonUtils.Serialize(_coreConfig.routing.balancers)); fullConfigTemplateNode["routing"]["balancers"] = JsonNode.Parse(JsonUtils.Serialize(v2rayConfig.routing.balancers));
} }
} }
if (_coreConfig.observatory != null) if (v2rayConfig.observatory != null)
{ {
if (fullConfigTemplateNode["observatory"] == null) if (fullConfigTemplateNode["observatory"] == null)
{ {
fullConfigTemplateNode["observatory"] = JsonNode.Parse(JsonUtils.Serialize(_coreConfig.observatory)); fullConfigTemplateNode["observatory"] = JsonNode.Parse(JsonUtils.Serialize(v2rayConfig.observatory));
} }
else else
{ {
var subjectSelector = _coreConfig.observatory.subjectSelector; var subjectSelector = v2rayConfig.observatory.subjectSelector;
subjectSelector.AddRange(fullConfigTemplateNode["observatory"]?["subjectSelector"]?.AsArray()?.Select(x => x?.GetValue<string>()) ?? []); subjectSelector.AddRange(fullConfigTemplateNode["observatory"]?["subjectSelector"]?.AsArray()?.Select(x => x?.GetValue<string>()) ?? []);
fullConfigTemplateNode["observatory"]["subjectSelector"] = JsonNode.Parse(JsonUtils.Serialize(subjectSelector.Distinct().ToList())); fullConfigTemplateNode["observatory"]["subjectSelector"] = JsonNode.Parse(JsonUtils.Serialize(subjectSelector.Distinct().ToList()));
} }
} }
if (_coreConfig.burstObservatory != null) if (v2rayConfig.burstObservatory != null)
{ {
if (fullConfigTemplateNode["burstObservatory"] == null) if (fullConfigTemplateNode["burstObservatory"] == null)
{ {
fullConfigTemplateNode["burstObservatory"] = JsonNode.Parse(JsonUtils.Serialize(_coreConfig.burstObservatory)); fullConfigTemplateNode["burstObservatory"] = JsonNode.Parse(JsonUtils.Serialize(v2rayConfig.burstObservatory));
} }
else else
{ {
var subjectSelector = _coreConfig.burstObservatory.subjectSelector; var subjectSelector = v2rayConfig.burstObservatory.subjectSelector;
subjectSelector.AddRange(fullConfigTemplateNode["burstObservatory"]?["subjectSelector"]?.AsArray()?.Select(x => x?.GetValue<string>()) ?? []); subjectSelector.AddRange(fullConfigTemplateNode["burstObservatory"]?["subjectSelector"]?.AsArray()?.Select(x => x?.GetValue<string>()) ?? []);
fullConfigTemplateNode["burstObservatory"]["subjectSelector"] = JsonNode.Parse(JsonUtils.Serialize(subjectSelector.Distinct().ToList())); fullConfigTemplateNode["burstObservatory"]["subjectSelector"] = JsonNode.Parse(JsonUtils.Serialize(subjectSelector.Distinct().ToList()));
} }
@ -92,7 +88,7 @@ public partial class CoreConfigV2rayService
var customOutboundsNode = new JsonArray(); var customOutboundsNode = new JsonArray();
foreach (var outbound in _coreConfig.outbounds) foreach (var outbound in v2rayConfig.outbounds)
{ {
if (outbound.protocol.ToLower() is "blackhole" or "dns" or "freedom") if (outbound.protocol.ToLower() is "blackhole" or "dns" or "freedom")
{ {
@ -101,8 +97,8 @@ public partial class CoreConfigV2rayService
continue; continue;
} }
} }
else if (!fullConfigTemplate.ProxyDetour.IsNullOrEmpty() else if ((!fullConfigTemplate.ProxyDetour.IsNullOrEmpty())
&& (outbound.streamSettings?.sockopt?.dialerProxy.IsNullOrEmpty() ?? true)) && ((outbound.streamSettings?.sockopt?.dialerProxy.IsNullOrEmpty() ?? true) == true))
{ {
var outboundAddress = outbound.settings?.servers?.FirstOrDefault()?.address var outboundAddress = outbound.settings?.servers?.FirstOrDefault()?.address
?? outbound.settings?.vnext?.FirstOrDefault()?.address ?? outbound.settings?.vnext?.FirstOrDefault()?.address
@ -127,6 +123,6 @@ public partial class CoreConfigV2rayService
fullConfigTemplateNode["outbounds"] = customOutboundsNode; fullConfigTemplateNode["outbounds"] = customOutboundsNode;
return JsonUtils.Serialize(fullConfigTemplateNode); return await Task.FromResult(JsonUtils.Serialize(fullConfigTemplateNode));
} }
} }

View file

@ -2,45 +2,46 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenDns() private async Task<int> GenDns(ProfileItem? node, V2rayConfig v2rayConfig)
{ {
try try
{ {
var item = context.RawDnsItem; var item = await AppManager.Instance.GetDNSItem(ECoreType.Xray);
if (item is { Enabled: true }) if (item is { Enabled: true })
{ {
GenDnsCustom(); var result = await GenDnsCompatible(node, v2rayConfig);
if (_coreConfig.routing.domainStrategy != Global.IPIfNonMatch) if (v2rayConfig.routing.domainStrategy != Global.IPIfNonMatch)
{ {
return; return result;
} }
// DNS routing // DNS routing
var dnsObj = JsonUtils.SerializeToNode(_coreConfig.dns); var dnsObj = JsonUtils.SerializeToNode(v2rayConfig.dns);
if (dnsObj == null) if (dnsObj == null)
{ {
return; return result;
} }
dnsObj["tag"] = Global.DnsTag; dnsObj["tag"] = Global.DnsTag;
_coreConfig.dns = JsonUtils.Deserialize<Dns4Ray>(JsonUtils.Serialize(dnsObj)); v2rayConfig.dns = JsonUtils.Deserialize<Dns4Ray>(JsonUtils.Serialize(dnsObj));
_coreConfig.routing.rules.Add(new RulesItem4Ray v2rayConfig.routing.rules.Add(new RulesItem4Ray
{ {
type = "field", type = "field",
inboundTag = new List<string> { Global.DnsTag }, inboundTag = new List<string> { Global.DnsTag },
outboundTag = Global.ProxyTag, outboundTag = Global.ProxyTag,
}); });
return;
return result;
} }
var simpleDnsItem = context.SimpleDnsItem; var simpleDnsItem = _config.SimpleDNSItem;
var dnsItem = _coreConfig.dns is Dns4Ray dns4Ray ? dns4Ray : new Dns4Ray(); var dnsItem = v2rayConfig.dns is Dns4Ray dns4Ray ? dns4Ray : new Dns4Ray();
var strategy4Freedom = simpleDnsItem?.Strategy4Freedom ?? Global.AsIs; var strategy4Freedom = simpleDnsItem?.Strategy4Freedom ?? Global.AsIs;
//Outbound Freedom domainStrategy //Outbound Freedom domainStrategy
if (strategy4Freedom.IsNotEmpty() && strategy4Freedom != Global.AsIs) if (strategy4Freedom.IsNotEmpty() && strategy4Freedom != Global.AsIs)
{ {
var outbound = _coreConfig.outbounds.FirstOrDefault(t => t is { protocol: "freedom", tag: Global.DirectTag }); var outbound = v2rayConfig.outbounds.FirstOrDefault(t => t is { protocol: "freedom", tag: Global.DirectTag });
if (outbound != null) if (outbound != null)
{ {
outbound.settings = new() outbound.settings = new()
@ -58,23 +59,23 @@ public partial class CoreConfigV2rayService
var xraySupportConfigTypeNames = Global.XraySupportConfigType var xraySupportConfigTypeNames = Global.XraySupportConfigType
.Select(x => x == EConfigType.Hysteria2 ? "hysteria" : Global.ProtocolTypes[x]) .Select(x => x == EConfigType.Hysteria2 ? "hysteria" : Global.ProtocolTypes[x])
.ToHashSet(); .ToHashSet();
_coreConfig.outbounds v2rayConfig.outbounds
.Where(t => xraySupportConfigTypeNames.Contains(t.protocol)) .Where(t => xraySupportConfigTypeNames.Contains(t.protocol))
.ToList() .ToList()
.ForEach(outbound => outbound.targetStrategy = strategy4Proxy); .ForEach(outbound => outbound.targetStrategy = strategy4Proxy);
} }
FillDnsServers(dnsItem); await GenDnsServers(node, dnsItem, simpleDnsItem);
FillDnsHosts(dnsItem); await GenDnsHosts(dnsItem, simpleDnsItem);
dnsItem.serveStale = simpleDnsItem?.ServeStale is true ? true : null; dnsItem.serveStale = simpleDnsItem?.ServeStale is true ? true : null;
dnsItem.enableParallelQuery = simpleDnsItem?.ParallelQuery is true ? true : null; dnsItem.enableParallelQuery = simpleDnsItem?.ParallelQuery is true ? true : null;
if (_coreConfig.routing.domainStrategy == Global.IPIfNonMatch) if (v2rayConfig.routing.domainStrategy == Global.IPIfNonMatch)
{ {
// DNS routing // DNS routing
dnsItem.tag = Global.DnsTag; dnsItem.tag = Global.DnsTag;
_coreConfig.routing.rules.Add(new RulesItem4Ray v2rayConfig.routing.rules.Add(new RulesItem4Ray
{ {
type = "field", type = "field",
inboundTag = new List<string> { Global.DnsTag }, inboundTag = new List<string> { Global.DnsTag },
@ -82,17 +83,17 @@ public partial class CoreConfigV2rayService
}); });
} }
_coreConfig.dns = dnsItem; v2rayConfig.dns = dnsItem;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private void FillDnsServers(Dns4Ray dnsItem) private async Task<int> GenDnsServers(ProfileItem? node, Dns4Ray dnsItem, SimpleDNSItem simpleDNSItem)
{ {
var simpleDNSItem = context.SimpleDnsItem;
static List<string> ParseDnsAddresses(string? dnsInput, string defaultAddress) static List<string> ParseDnsAddresses(string? dnsInput, string defaultAddress)
{ {
var addresses = dnsInput?.Split(dnsInput.Contains(',') ? ',' : ';') var addresses = dnsInput?.Split(dnsInput.Contains(',') ? ',' : ';')
@ -196,9 +197,9 @@ public partial class CoreConfigV2rayService
} }
} }
var routing = context.RoutingItem; var routing = await ConfigHandler.GetDefaultRouting(_config);
List<RulesItem>? rules = null; List<RulesItem>? rules = null;
rules = JsonUtils.Deserialize<List<RulesItem>>(routing?.RuleSet) ?? []; rules = JsonUtils.Deserialize<List<RulesItem>>(routing.RuleSet) ?? [];
foreach (var item in rules) foreach (var item in rules)
{ {
if (!item.Enabled || item.Domain is null || item.Domain.Count == 0) if (!item.Enabled || item.Domain is null || item.Domain.Count == 0)
@ -245,9 +246,27 @@ public partial class CoreConfigV2rayService
} }
} }
if (context.ProtectDomainList.Count > 0) if (Utils.IsDomain(node?.Address))
{ {
directDomainList.AddRange(context.ProtectDomainList); directDomainList.Add(node.Address);
}
if (node?.Subid is not null)
{
var subItem = await AppManager.Instance.GetSubItem(node.Subid);
if (subItem is not null)
{
foreach (var profile in new[] { subItem.PrevProfile, subItem.NextProfile })
{
var profileNode = await AppManager.Instance.GetProfileItemViaRemarks(profile);
if (profileNode is not null
&& Global.XraySupportConfigType.Contains(profileNode.ConfigType)
&& Utils.IsDomain(profileNode.Address))
{
directDomainList.Add(profileNode.Address);
}
}
}
} }
dnsItem.servers ??= []; dnsItem.servers ??= [];
@ -281,14 +300,15 @@ public partial class CoreConfigV2rayService
var defaultDnsServers = useDirectDns ? directDNSAddress : remoteDNSAddress; var defaultDnsServers = useDirectDns ? directDNSAddress : remoteDNSAddress;
dnsItem.servers.AddRange(defaultDnsServers); dnsItem.servers.AddRange(defaultDnsServers);
return 0;
} }
private void FillDnsHosts(Dns4Ray dnsItem) private async Task<int> GenDnsHosts(Dns4Ray dnsItem, SimpleDNSItem simpleDNSItem)
{ {
var simpleDNSItem = context.SimpleDnsItem;
if (simpleDNSItem.AddCommonHosts == false && simpleDNSItem.UseSystemHosts == false && simpleDNSItem.Hosts.IsNullOrEmpty()) if (simpleDNSItem.AddCommonHosts == false && simpleDNSItem.UseSystemHosts == false && simpleDNSItem.Hosts.IsNullOrEmpty())
{ {
return; return await Task.FromResult(0);
} }
dnsItem.hosts ??= new Dictionary<string, object>(); dnsItem.hosts ??= new Dictionary<string, object>();
if (simpleDNSItem.AddCommonHosts == true) if (simpleDNSItem.AddCommonHosts == true)
@ -317,13 +337,14 @@ public partial class CoreConfigV2rayService
{ {
dnsItem.hosts[kvp.Key] = kvp.Value; dnsItem.hosts[kvp.Key] = kvp.Value;
} }
return await Task.FromResult(0);
} }
private void GenDnsCustom() private async Task<int> GenDnsCompatible(ProfileItem? node, V2rayConfig v2rayConfig)
{ {
try try
{ {
var item = context.RawDnsItem; var item = await AppManager.Instance.GetDNSItem(ECoreType.Xray);
var normalDNS = item?.NormalDNS; var normalDNS = item?.NormalDNS;
var domainStrategy4Freedom = item?.DomainStrategy4Freedom; var domainStrategy4Freedom = item?.DomainStrategy4Freedom;
if (normalDNS.IsNullOrEmpty()) if (normalDNS.IsNullOrEmpty())
@ -334,7 +355,7 @@ public partial class CoreConfigV2rayService
//Outbound Freedom domainStrategy //Outbound Freedom domainStrategy
if (domainStrategy4Freedom.IsNotEmpty()) if (domainStrategy4Freedom.IsNotEmpty())
{ {
var outbound = _coreConfig.outbounds.FirstOrDefault(t => t is { protocol: "freedom", tag: Global.DirectTag }); var outbound = v2rayConfig.outbounds.FirstOrDefault(t => t is { protocol: "freedom", tag: Global.DirectTag });
if (outbound != null) if (outbound != null)
{ {
outbound.settings = new(); outbound.settings = new();
@ -389,37 +410,63 @@ public partial class CoreConfigV2rayService
} }
} }
FillDnsDomainsCustom(obj); await GenDnsDomainsCompatible(node, obj, item);
_coreConfig.dns = JsonUtils.Deserialize<Dns4Ray>(JsonUtils.Serialize(obj)); v2rayConfig.dns = JsonUtils.Deserialize<Dns4Ray>(JsonUtils.Serialize(obj));
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private void FillDnsDomainsCustom(JsonNode dns) private async Task<int> GenDnsDomainsCompatible(ProfileItem? node, JsonNode dns, DNSItem? dnsItem)
{ {
if (node == null)
{
return 0;
}
var servers = dns["servers"]; var servers = dns["servers"];
if (servers == null) if (servers != null)
{ {
return; var domainList = new List<string>();
} if (Utils.IsDomain(node.Address))
{
domainList.Add(node.Address);
}
var subItem = await AppManager.Instance.GetSubItem(node.Subid);
if (subItem is not null)
{
// Previous proxy
var prevNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.PrevProfile);
if (prevNode is not null
&& Global.SingboxSupportConfigType.Contains(prevNode.ConfigType)
&& Utils.IsDomain(prevNode.Address))
{
domainList.Add(prevNode.Address);
}
var domainList = context.ProtectDomainList; // Next proxy
if (domainList.Count <= 0) var nextNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.NextProfile);
{ if (nextNode is not null
return; && Global.SingboxSupportConfigType.Contains(nextNode.ConfigType)
&& Utils.IsDomain(nextNode.Address))
{
domainList.Add(nextNode.Address);
}
}
if (domainList.Count > 0)
{
var dnsServer = new DnsServer4Ray()
{
address = string.IsNullOrEmpty(dnsItem?.DomainDNSAddress) ? Global.DomainPureIPDNSAddress.FirstOrDefault() : dnsItem?.DomainDNSAddress,
skipFallback = true,
domains = domainList
};
servers.AsArray().Add(JsonUtils.SerializeToNode(dnsServer));
}
} }
return await Task.FromResult(0);
var dnsItem = context.RawDnsItem;
var dnsServer = new DnsServer4Ray()
{
address = string.IsNullOrEmpty(dnsItem?.DomainDNSAddress) ? Global.DomainPureIPDNSAddress.FirstOrDefault() : dnsItem?.DomainDNSAddress,
skipFallback = true,
domains = domainList.ToList(),
};
servers.AsArray().Add(JsonUtils.SerializeToNode(dnsServer));
} }
} }

View file

@ -2,35 +2,35 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenInbounds() private async Task<int> GenInbounds(V2rayConfig v2rayConfig)
{ {
try try
{ {
var listen = "0.0.0.0"; var listen = "0.0.0.0";
_coreConfig.inbounds = []; v2rayConfig.inbounds = [];
var inbound = BuildInbound(_config.Inbound.First(), EInboundProtocol.socks, true); var inbound = GetInbound(_config.Inbound.First(), EInboundProtocol.socks, true);
_coreConfig.inbounds.Add(inbound); v2rayConfig.inbounds.Add(inbound);
if (_config.Inbound.First().SecondLocalPortEnabled) if (_config.Inbound.First().SecondLocalPortEnabled)
{ {
var inbound2 = BuildInbound(_config.Inbound.First(), EInboundProtocol.socks2, true); var inbound2 = GetInbound(_config.Inbound.First(), EInboundProtocol.socks2, true);
_coreConfig.inbounds.Add(inbound2); v2rayConfig.inbounds.Add(inbound2);
} }
if (_config.Inbound.First().AllowLANConn) if (_config.Inbound.First().AllowLANConn)
{ {
if (_config.Inbound.First().NewPort4LAN) if (_config.Inbound.First().NewPort4LAN)
{ {
var inbound3 = BuildInbound(_config.Inbound.First(), EInboundProtocol.socks3, true); var inbound3 = GetInbound(_config.Inbound.First(), EInboundProtocol.socks3, true);
inbound3.listen = listen; inbound3.listen = listen;
_coreConfig.inbounds.Add(inbound3); v2rayConfig.inbounds.Add(inbound3);
//auth //auth
if (_config.Inbound.First().User.IsNotEmpty() && _config.Inbound.First().Pass.IsNotEmpty()) if (_config.Inbound.First().User.IsNotEmpty() && _config.Inbound.First().Pass.IsNotEmpty())
{ {
inbound3.settings.auth = "password"; inbound3.settings.auth = "password";
inbound3.settings.accounts = new List<AccountsItem4Ray> { new() { user = _config.Inbound.First().User, pass = _config.Inbound.First().Pass } }; inbound3.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.Inbound.First().User, pass = _config.Inbound.First().Pass } };
} }
} }
else else
@ -43,9 +43,10 @@ public partial class CoreConfigV2rayService
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
private Inbounds4Ray BuildInbound(InItem inItem, EInboundProtocol protocol, bool bSocks) private Inbounds4Ray GetInbound(InItem inItem, EInboundProtocol protocol, bool bSocks)
{ {
var result = EmbedUtils.GetEmbedText(Global.V2raySampleInbound); var result = EmbedUtils.GetEmbedText(Global.V2raySampleInbound);
if (result.IsNullOrEmpty()) if (result.IsNullOrEmpty())

View file

@ -2,27 +2,28 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenLog() private async Task<int> GenLog(V2rayConfig v2rayConfig)
{ {
try try
{ {
if (_config.CoreBasicItem.LogEnabled) if (_config.CoreBasicItem.LogEnabled)
{ {
var dtNow = DateTime.Now; var dtNow = DateTime.Now;
_coreConfig.log.loglevel = _config.CoreBasicItem.Loglevel; v2rayConfig.log.loglevel = _config.CoreBasicItem.Loglevel;
_coreConfig.log.access = Utils.GetLogPath($"Vaccess_{dtNow:yyyy-MM-dd}.txt"); v2rayConfig.log.access = Utils.GetLogPath($"Vaccess_{dtNow:yyyy-MM-dd}.txt");
_coreConfig.log.error = Utils.GetLogPath($"Verror_{dtNow:yyyy-MM-dd}.txt"); v2rayConfig.log.error = Utils.GetLogPath($"Verror_{dtNow:yyyy-MM-dd}.txt");
} }
else else
{ {
_coreConfig.log.loglevel = _config.CoreBasicItem.Loglevel; v2rayConfig.log.loglevel = _config.CoreBasicItem.Loglevel;
_coreConfig.log.access = null; v2rayConfig.log.access = null;
_coreConfig.log.error = null; v2rayConfig.log.error = null;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
} }

View file

@ -2,94 +2,13 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenOutbounds() private async Task<int> GenOutbound(ProfileItem node, Outbounds4Ray outbound)
{
var proxyOutboundList = BuildAllProxyOutbounds();
_coreConfig.outbounds.InsertRange(0, proxyOutboundList);
if (proxyOutboundList.Count(n => n.tag.StartsWith(Global.ProxyTag)) > 1)
{
var multipleLoad = _node.GetProtocolExtra().MultipleLoad ?? EMultipleLoad.LeastPing;
GenObservatory(multipleLoad);
GenBalancer(multipleLoad);
}
}
private List<Outbounds4Ray> BuildAllProxyOutbounds(string baseTagName = Global.ProxyTag)
{
var proxyOutboundList = new List<Outbounds4Ray>();
if (_node.ConfigType.IsGroupType())
{
proxyOutboundList.AddRange(BuildGroupProxyOutbounds(baseTagName));
}
else
{
proxyOutboundList.Add(BuildProxyOutbound(baseTagName));
}
if (_config.CoreBasicItem.EnableFragment)
{
var fragmentOutbound = new Outbounds4Ray
{
protocol = "freedom",
tag = $"frag-{Global.ProxyTag}",
settings = new()
{
fragment = new()
{
packets = _config.Fragment4RayItem?.Packets,
length = _config.Fragment4RayItem?.Length,
interval = _config.Fragment4RayItem?.Interval
}
}
};
var actOutboundWithTlsList =
proxyOutboundList.Where(n => n.streamSettings?.security.IsNullOrEmpty() == false
&& (n.streamSettings?.sockopt?.dialerProxy?.IsNullOrEmpty() ?? true));
foreach (var outbound in actOutboundWithTlsList)
{
var fragmentOutboundClone = JsonUtils.DeepCopy(fragmentOutbound);
fragmentOutboundClone.tag = $"frag-{outbound.tag}";
outbound.streamSettings.sockopt = new()
{
dialerProxy = fragmentOutboundClone.tag
};
proxyOutboundList.Add(fragmentOutboundClone);
}
}
return proxyOutboundList;
}
private List<Outbounds4Ray> BuildGroupProxyOutbounds(string baseTagName = Global.ProxyTag)
{
var proxyOutboundList = new List<Outbounds4Ray>();
switch (_node.ConfigType)
{
case EConfigType.PolicyGroup:
proxyOutboundList.AddRange(BuildOutboundsList(baseTagName));
break;
case EConfigType.ProxyChain:
proxyOutboundList.AddRange(BuildChainOutboundsList(baseTagName));
break;
}
return proxyOutboundList;
}
private Outbounds4Ray BuildProxyOutbound(string baseTagName = Global.ProxyTag)
{
var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
FillOutbound(outbound);
outbound.tag = baseTagName;
return outbound;
}
private void FillOutbound(Outbounds4Ray outbound)
{ {
try try
{ {
var protocolExtra = _node.GetProtocolExtra(); var protocolExtra = node.GetProtocolExtra();
var muxEnabled = _node.MuxEnabled ?? _config.CoreBasicItem.MuxEnabled; var muxEnabled = node.MuxEnabled ?? _config.CoreBasicItem.MuxEnabled;
switch (_node.ConfigType) switch (node.ConfigType)
{ {
case EConfigType.VMess: case EConfigType.VMess:
{ {
@ -103,8 +22,8 @@ public partial class CoreConfigV2rayService
{ {
vnextItem = outbound.settings.vnext.First(); vnextItem = outbound.settings.vnext.First();
} }
vnextItem.address = _node.Address; vnextItem.address = node.Address;
vnextItem.port = _node.Port; vnextItem.port = node.Port;
UsersItem4Ray usersItem; UsersItem4Ray usersItem;
if (vnextItem.users.Count <= 0) if (vnextItem.users.Count <= 0)
@ -117,7 +36,7 @@ public partial class CoreConfigV2rayService
usersItem = vnextItem.users.First(); usersItem = vnextItem.users.First();
} }
usersItem.id = _node.Password; usersItem.id = node.Password;
usersItem.alterId = int.TryParse(protocolExtra?.AlterId, out var result) ? result : 0; usersItem.alterId = int.TryParse(protocolExtra?.AlterId, out var result) ? result : 0;
usersItem.email = Global.UserEMail; usersItem.email = Global.UserEMail;
if (Global.VmessSecurities.Contains(protocolExtra.VmessSecurity)) if (Global.VmessSecurities.Contains(protocolExtra.VmessSecurity))
@ -129,7 +48,7 @@ public partial class CoreConfigV2rayService
usersItem.security = Global.DefaultSecurity; usersItem.security = Global.DefaultSecurity;
} }
FillOutboundMux(outbound, muxEnabled, muxEnabled); await GenOutboundMux(node, outbound, muxEnabled, muxEnabled);
outbound.settings.servers = null; outbound.settings.servers = null;
break; break;
@ -146,16 +65,16 @@ public partial class CoreConfigV2rayService
{ {
serversItem = outbound.settings.servers.First(); serversItem = outbound.settings.servers.First();
} }
serversItem.address = _node.Address; serversItem.address = node.Address;
serversItem.port = _node.Port; serversItem.port = node.Port;
serversItem.password = _node.Password; serversItem.password = node.Password;
serversItem.method = AppManager.Instance.GetShadowsocksSecurities(_node).Contains(protocolExtra.SsMethod) serversItem.method = AppManager.Instance.GetShadowsocksSecurities(node).Contains(protocolExtra.SsMethod)
? protocolExtra.SsMethod : "none"; ? protocolExtra.SsMethod : "none";
serversItem.ota = false; serversItem.ota = false;
serversItem.level = 1; serversItem.level = 1;
FillOutboundMux(outbound); await GenOutboundMux(node, outbound);
outbound.settings.vnext = null; outbound.settings.vnext = null;
break; break;
@ -173,25 +92,25 @@ public partial class CoreConfigV2rayService
{ {
serversItem = outbound.settings.servers.First(); serversItem = outbound.settings.servers.First();
} }
serversItem.address = _node.Address; serversItem.address = node.Address;
serversItem.port = _node.Port; serversItem.port = node.Port;
serversItem.method = null; serversItem.method = null;
serversItem.password = null; serversItem.password = null;
if (_node.Username.IsNotEmpty() if (node.Username.IsNotEmpty()
&& _node.Password.IsNotEmpty()) && node.Password.IsNotEmpty())
{ {
SocksUsersItem4Ray socksUsersItem = new() SocksUsersItem4Ray socksUsersItem = new()
{ {
user = _node.Username ?? "", user = node.Username ?? "",
pass = _node.Password, pass = node.Password,
level = 1 level = 1
}; };
serversItem.users = new List<SocksUsersItem4Ray>() { socksUsersItem }; serversItem.users = new List<SocksUsersItem4Ray>() { socksUsersItem };
} }
FillOutboundMux(outbound); await GenOutboundMux(node, outbound);
outbound.settings.vnext = null; outbound.settings.vnext = null;
break; break;
@ -208,8 +127,8 @@ public partial class CoreConfigV2rayService
{ {
vnextItem = outbound.settings.vnext.First(); vnextItem = outbound.settings.vnext.First();
} }
vnextItem.address = _node.Address; vnextItem.address = node.Address;
vnextItem.port = _node.Port; vnextItem.port = node.Port;
UsersItem4Ray usersItem; UsersItem4Ray usersItem;
if (vnextItem.users.Count <= 0) if (vnextItem.users.Count <= 0)
@ -221,7 +140,7 @@ public partial class CoreConfigV2rayService
{ {
usersItem = vnextItem.users.First(); usersItem = vnextItem.users.First();
} }
usersItem.id = _node.Password; usersItem.id = node.Password;
usersItem.email = Global.UserEMail; usersItem.email = Global.UserEMail;
usersItem.encryption = protocolExtra.VlessEncryption; usersItem.encryption = protocolExtra.VlessEncryption;
@ -231,7 +150,7 @@ public partial class CoreConfigV2rayService
} }
else else
{ {
FillOutboundMux(outbound, false, muxEnabled); await GenOutboundMux(node, outbound, false, muxEnabled);
} }
outbound.settings.servers = null; outbound.settings.servers = null;
break; break;
@ -248,14 +167,14 @@ public partial class CoreConfigV2rayService
{ {
serversItem = outbound.settings.servers.First(); serversItem = outbound.settings.servers.First();
} }
serversItem.address = _node.Address; serversItem.address = node.Address;
serversItem.port = _node.Port; serversItem.port = node.Port;
serversItem.password = _node.Password; serversItem.password = node.Password;
serversItem.ota = false; serversItem.ota = false;
serversItem.level = 1; serversItem.level = 1;
FillOutboundMux(outbound); await GenOutboundMux(node, outbound);
outbound.settings.vnext = null; outbound.settings.vnext = null;
break; break;
@ -265,8 +184,8 @@ public partial class CoreConfigV2rayService
outbound.settings = new() outbound.settings = new()
{ {
version = 2, version = 2,
address = _node.Address, address = node.Address,
port = _node.Port, port = node.Port,
}; };
outbound.settings.vnext = null; outbound.settings.vnext = null;
outbound.settings.servers = null; outbound.settings.servers = null;
@ -274,7 +193,7 @@ public partial class CoreConfigV2rayService
} }
case EConfigType.WireGuard: case EConfigType.WireGuard:
{ {
var address = _node.Address; var address = node.Address;
if (Utils.IsIpv6(address)) if (Utils.IsIpv6(address))
{ {
address = $"[{address}]"; address = $"[{address}]";
@ -282,12 +201,12 @@ public partial class CoreConfigV2rayService
var peer = new WireguardPeer4Ray var peer = new WireguardPeer4Ray
{ {
publicKey = protocolExtra.WgPublicKey ?? "", publicKey = protocolExtra.WgPublicKey ?? "",
endpoint = address + ":" + _node.Port.ToString() endpoint = address + ":" + node.Port.ToString()
}; };
var setting = new Outboundsettings4Ray var setting = new Outboundsettings4Ray
{ {
address = Utils.String2List(protocolExtra.WgInterfaceAddress), address = Utils.String2List(protocolExtra.WgInterfaceAddress),
secretKey = _node.Password, secretKey = node.Password,
reserved = Utils.String2List(protocolExtra.WgReserved)?.Select(int.Parse).ToList(), reserved = Utils.String2List(protocolExtra.WgReserved)?.Select(int.Parse).ToList(),
mtu = protocolExtra.WgMtu > 0 ? protocolExtra.WgMtu : Global.TunMtus.First(), mtu = protocolExtra.WgMtu > 0 ? protocolExtra.WgMtu : Global.TunMtus.First(),
peers = [peer] peers = [peer]
@ -299,20 +218,21 @@ public partial class CoreConfigV2rayService
} }
} }
outbound.protocol = Global.ProtocolTypes[_node.ConfigType]; outbound.protocol = Global.ProtocolTypes[node.ConfigType];
if (_node.ConfigType == EConfigType.Hysteria2) if (node.ConfigType == EConfigType.Hysteria2)
{ {
outbound.protocol = "hysteria"; outbound.protocol = "hysteria";
} }
FillBoundStreamSettings(outbound); await GenBoundStreamSettings(node, outbound);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private void FillOutboundMux(Outbounds4Ray outbound, bool enabledTCP = false, bool enabledUDP = false) private async Task<int> GenOutboundMux(ProfileItem node, Outbounds4Ray outbound, bool enabledTCP = false, bool enabledUDP = false)
{ {
try try
{ {
@ -335,40 +255,48 @@ public partial class CoreConfigV2rayService
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
private void FillBoundStreamSettings(Outbounds4Ray outbound) private async Task<int> GenBoundStreamSettings(ProfileItem node, Outbounds4Ray outbound)
{ {
try try
{ {
var streamSettings = outbound.streamSettings; var streamSettings = outbound.streamSettings;
var network = _node.GetNetwork(); var network = node.GetNetwork();
if (_node.ConfigType == EConfigType.Hysteria2) if (node.ConfigType == EConfigType.Hysteria2)
{ {
network = "hysteria"; network = "hysteria";
} }
streamSettings.network = network; streamSettings.network = network;
var host = _node.RequestHost.TrimEx(); var host = node.RequestHost.TrimEx();
var path = _node.Path.TrimEx(); var path = node.Path.TrimEx();
var sni = _node.Sni.TrimEx(); var sni = node.Sni.TrimEx();
var useragent = ""; var useragent = "";
if (!_config.CoreBasicItem.DefUserAgent.IsNullOrEmpty()) if (!_config.CoreBasicItem.DefUserAgent.IsNullOrEmpty())
{ {
useragent = Global.UserAgentTexts.GetValueOrDefault(_config.CoreBasicItem.DefUserAgent, _config.CoreBasicItem.DefUserAgent); try
{
useragent = Global.UserAgentTexts[_config.CoreBasicItem.DefUserAgent];
}
catch (KeyNotFoundException)
{
useragent = _config.CoreBasicItem.DefUserAgent;
}
} }
//if tls //if tls
if (_node.StreamSecurity == Global.StreamSecurity) if (node.StreamSecurity == Global.StreamSecurity)
{ {
streamSettings.security = _node.StreamSecurity; streamSettings.security = node.StreamSecurity;
TlsSettings4Ray tlsSettings = new() TlsSettings4Ray tlsSettings = new()
{ {
allowInsecure = Utils.ToBool(_node.AllowInsecure.IsNullOrEmpty() ? _config.CoreBasicItem.DefAllowInsecure.ToString().ToLower() : _node.AllowInsecure), allowInsecure = Utils.ToBool(node.AllowInsecure.IsNullOrEmpty() ? _config.CoreBasicItem.DefAllowInsecure.ToString().ToLower() : node.AllowInsecure),
alpn = _node.GetAlpn(), alpn = node.GetAlpn(),
fingerprint = _node.Fingerprint.IsNullOrEmpty() ? _config.CoreBasicItem.DefFingerprint : _node.Fingerprint, fingerprint = node.Fingerprint.IsNullOrEmpty() ? _config.CoreBasicItem.DefFingerprint : node.Fingerprint,
echConfigList = _node.EchConfigList.NullIfEmpty(), echConfigList = node.EchConfigList.NullIfEmpty(),
echForceQuery = _node.EchForceQuery.NullIfEmpty() echForceQuery = node.EchForceQuery.NullIfEmpty()
}; };
if (sni.IsNotEmpty()) if (sni.IsNotEmpty())
{ {
@ -378,7 +306,7 @@ public partial class CoreConfigV2rayService
{ {
tlsSettings.serverName = Utils.String2List(host)?.First(); tlsSettings.serverName = Utils.String2List(host)?.First();
} }
var certs = CertPemManager.ParsePemChain(_node.Cert); var certs = CertPemManager.ParsePemChain(node.Cert);
if (certs.Count > 0) if (certs.Count > 0)
{ {
var certsettings = new List<CertificateSettings4Ray>(); var certsettings = new List<CertificateSettings4Ray>();
@ -395,27 +323,27 @@ public partial class CoreConfigV2rayService
tlsSettings.disableSystemRoot = true; tlsSettings.disableSystemRoot = true;
tlsSettings.allowInsecure = false; tlsSettings.allowInsecure = false;
} }
else if (!_node.CertSha.IsNullOrEmpty()) else if (!node.CertSha.IsNullOrEmpty())
{ {
tlsSettings.pinnedPeerCertSha256 = _node.CertSha; tlsSettings.pinnedPeerCertSha256 = node.CertSha;
tlsSettings.allowInsecure = false; tlsSettings.allowInsecure = false;
} }
streamSettings.tlsSettings = tlsSettings; streamSettings.tlsSettings = tlsSettings;
} }
//if Reality //if Reality
if (_node.StreamSecurity == Global.StreamSecurityReality) if (node.StreamSecurity == Global.StreamSecurityReality)
{ {
streamSettings.security = _node.StreamSecurity; streamSettings.security = node.StreamSecurity;
TlsSettings4Ray realitySettings = new() TlsSettings4Ray realitySettings = new()
{ {
fingerprint = _node.Fingerprint.IsNullOrEmpty() ? _config.CoreBasicItem.DefFingerprint : _node.Fingerprint, fingerprint = node.Fingerprint.IsNullOrEmpty() ? _config.CoreBasicItem.DefFingerprint : node.Fingerprint,
serverName = sni, serverName = sni,
publicKey = _node.PublicKey, publicKey = node.PublicKey,
shortId = _node.ShortId, shortId = node.ShortId,
spiderX = _node.SpiderX, spiderX = node.SpiderX,
mldsa65Verify = _node.Mldsa65Verify, mldsa65Verify = node.Mldsa65Verify,
show = false, show = false,
}; };
@ -439,14 +367,14 @@ public partial class CoreConfigV2rayService
kcpSettings.readBufferSize = _config.KcpItem.ReadBufferSize; kcpSettings.readBufferSize = _config.KcpItem.ReadBufferSize;
kcpSettings.writeBufferSize = _config.KcpItem.WriteBufferSize; kcpSettings.writeBufferSize = _config.KcpItem.WriteBufferSize;
streamSettings.finalmask ??= new(); streamSettings.finalmask ??= new();
if (Global.KcpHeaderMaskMap.TryGetValue(_node.HeaderType, out var header)) if (Global.KcpHeaderMaskMap.TryGetValue(node.HeaderType, out var header))
{ {
streamSettings.finalmask.udp = streamSettings.finalmask.udp =
[ [
new Mask4Ray new Mask4Ray
{ {
type = header, type = header,
settings = _node.HeaderType == "dns" && !host.IsNullOrEmpty() ? new MaskSettings4Ray { domain = host } : null settings = node.HeaderType == "dns" && !host.IsNullOrEmpty() ? new MaskSettings4Ray { domain = host } : null
} }
]; ];
} }
@ -516,17 +444,17 @@ public partial class CoreConfigV2rayService
{ {
xhttpSettings.host = host; xhttpSettings.host = host;
} }
if (_node.HeaderType.IsNotEmpty() && Global.XhttpMode.Contains(_node.HeaderType)) if (node.HeaderType.IsNotEmpty() && Global.XhttpMode.Contains(node.HeaderType))
{ {
xhttpSettings.mode = _node.HeaderType; xhttpSettings.mode = node.HeaderType;
} }
if (_node.Extra.IsNotEmpty()) if (node.Extra.IsNotEmpty())
{ {
xhttpSettings.extra = JsonUtils.ParseJson(_node.Extra); xhttpSettings.extra = JsonUtils.ParseJson(node.Extra);
} }
streamSettings.xhttpSettings = xhttpSettings; streamSettings.xhttpSettings = xhttpSettings;
FillOutboundMux(outbound); await GenOutboundMux(node, outbound);
break; break;
//h2 //h2
@ -550,11 +478,11 @@ public partial class CoreConfigV2rayService
key = path, key = path,
header = new Header4Ray header = new Header4Ray
{ {
type = _node.HeaderType type = node.HeaderType
} }
}; };
streamSettings.quicSettings = quicsettings; streamSettings.quicSettings = quicsettings;
if (_node.StreamSecurity == Global.StreamSecurity) if (node.StreamSecurity == Global.StreamSecurity)
{ {
if (sni.IsNotEmpty()) if (sni.IsNotEmpty())
{ {
@ -562,7 +490,7 @@ public partial class CoreConfigV2rayService
} }
else else
{ {
streamSettings.tlsSettings.serverName = _node.Address; streamSettings.tlsSettings.serverName = node.Address;
} }
} }
break; break;
@ -572,7 +500,7 @@ public partial class CoreConfigV2rayService
{ {
authority = host.NullIfEmpty(), authority = host.NullIfEmpty(),
serviceName = path, serviceName = path,
multiMode = _node.HeaderType == Global.GrpcMultiMode, multiMode = node.HeaderType == Global.GrpcMultiMode,
idle_timeout = _config.GrpcItem.IdleTimeout, idle_timeout = _config.GrpcItem.IdleTimeout,
health_check_timeout = _config.GrpcItem.HealthCheckTimeout, health_check_timeout = _config.GrpcItem.HealthCheckTimeout,
permit_without_stream = _config.GrpcItem.PermitWithoutStream, permit_without_stream = _config.GrpcItem.PermitWithoutStream,
@ -582,7 +510,7 @@ public partial class CoreConfigV2rayService
break; break;
case "hysteria": case "hysteria":
var protocolExtra = _node.GetProtocolExtra(); var protocolExtra = node.GetProtocolExtra();
var ports = protocolExtra?.Ports; var ports = protocolExtra?.Ports;
int? upMbps = protocolExtra?.UpMbps is { } su and >= 0 int? upMbps = protocolExtra?.UpMbps is { } su and >= 0
? su ? su
@ -608,7 +536,7 @@ public partial class CoreConfigV2rayService
streamSettings.hysteriaSettings = new() streamSettings.hysteriaSettings = new()
{ {
version = 2, version = 2,
auth = _node.Password, auth = node.Password,
up = upMbps > 0 ? $"{upMbps}mbps" : null, up = upMbps > 0 ? $"{upMbps}mbps" : null,
down = downMbps > 0 ? $"{downMbps}mbps" : null, down = downMbps > 0 ? $"{downMbps}mbps" : null,
udphop = udpHop, udphop = udpHop,
@ -629,13 +557,13 @@ public partial class CoreConfigV2rayService
default: default:
//tcp //tcp
if (_node.HeaderType == Global.TcpHeaderHttp) if (node.HeaderType == Global.TcpHeaderHttp)
{ {
TcpSettings4Ray tcpSettings = new() TcpSettings4Ray tcpSettings = new()
{ {
header = new Header4Ray header = new Header4Ray
{ {
type = _node.HeaderType type = node.HeaderType
} }
}; };
@ -659,142 +587,415 @@ public partial class CoreConfigV2rayService
} }
break; break;
} }
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return 0;
}
if (!_node.Finalmask.IsNullOrEmpty()) private async Task<int> GenGroupOutbound(ProfileItem node, V2rayConfig v2rayConfig, string baseTagName = Global.ProxyTag, bool ignoreOriginChain = false)
{
try
{
if (!node.ConfigType.IsGroupType())
{ {
streamSettings.finalmask = JsonUtils.Deserialize<Finalmask4Ray>(_node.Finalmask); return -1;
}
var hasCycle = await GroupProfileManager.HasCycle(node);
if (hasCycle)
{
return -1;
}
var (childProfiles, profileExtraItem) = await GroupProfileManager.GetChildProfileItems(node);
if (childProfiles.Count <= 0)
{
return -1;
}
switch (node.ConfigType)
{
case EConfigType.PolicyGroup:
if (ignoreOriginChain)
{
await GenOutboundsList(childProfiles, v2rayConfig, baseTagName);
}
else
{
await GenOutboundsListWithChain(childProfiles, v2rayConfig, baseTagName);
}
break;
case EConfigType.ProxyChain:
await GenChainOutboundsList(childProfiles, v2rayConfig, baseTagName);
break;
default:
break;
}
//add balancers
if (node.ConfigType == EConfigType.PolicyGroup)
{
var multipleLoad = profileExtraItem?.MultipleLoad ?? EMultipleLoad.LeastPing;
await GenObservatory(v2rayConfig, multipleLoad, baseTagName);
await GenBalancer(v2rayConfig, multipleLoad, baseTagName);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
private List<Outbounds4Ray> BuildOutboundsList(string baseTagName = Global.ProxyTag) private async Task<int> GenMoreOutbounds(ProfileItem node, V2rayConfig v2rayConfig)
{ {
var nodes = new List<ProfileItem>(); //fragment proxy
foreach (var nodeId in Utils.String2List(_node.GetProtocolExtra().ChildItems) ?? []) if (_config.CoreBasicItem.EnableFragment
&& v2rayConfig.outbounds.First().streamSettings?.security.IsNullOrEmpty() == false)
{ {
if (context.AllProxiesMap.TryGetValue(nodeId, out var node)) var fragmentOutbound = new Outbounds4Ray
{ {
nodes.Add(node); protocol = "freedom",
tag = $"frag-{Global.ProxyTag}",
settings = new()
{
fragment = new()
{
packets = _config.Fragment4RayItem?.Packets,
length = _config.Fragment4RayItem?.Length,
interval = _config.Fragment4RayItem?.Interval
}
}
};
v2rayConfig.outbounds.Add(fragmentOutbound);
v2rayConfig.outbounds.First().streamSettings.sockopt = new()
{
dialerProxy = fragmentOutbound.tag
};
return 0;
}
if (node.Subid.IsNullOrEmpty())
{
return 0;
}
try
{
var subItem = await AppManager.Instance.GetSubItem(node.Subid);
if (subItem is null)
{
return 0;
}
//current proxy
var outbound = v2rayConfig.outbounds.First();
var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
//Previous proxy
var prevNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.PrevProfile);
string? prevOutboundTag = null;
if (prevNode is not null
&& Global.XraySupportConfigType.Contains(prevNode.ConfigType))
{
var prevOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
await GenOutbound(prevNode, prevOutbound);
prevOutboundTag = $"prev-{Global.ProxyTag}";
prevOutbound.tag = prevOutboundTag;
v2rayConfig.outbounds.Add(prevOutbound);
}
var nextOutbound = await GenChainOutbounds(subItem, outbound, prevOutboundTag);
if (nextOutbound is not null)
{
v2rayConfig.outbounds.Insert(0, nextOutbound);
} }
} }
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return 0;
}
private async Task<int> GenOutboundsListWithChain(List<ProfileItem> nodes, V2rayConfig v2rayConfig, string baseTagName = Global.ProxyTag)
{
try
{
// Get template and initialize list
var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
if (txtOutbound.IsNullOrEmpty())
{
return 0;
}
var resultOutbounds = new List<Outbounds4Ray>();
var prevOutbounds = new List<Outbounds4Ray>(); // Separate list for prev outbounds and fragment
// Cache for chain proxies to avoid duplicate generation
var nextProxyCache = new Dictionary<string, Outbounds4Ray?>();
var prevProxyTags = new Dictionary<string, string?>(); // Map from profile name to tag
var prevIndex = 0; // Index for prev outbounds
// Process nodes
var index = 0;
foreach (var node in nodes)
{
index++;
if (node.ConfigType.IsGroupType())
{
var (childProfiles, _) = await GroupProfileManager.GetChildProfileItems(node);
if (childProfiles.Count <= 0)
{
continue;
}
var childBaseTagName = $"{baseTagName}-{index}";
var ret = node.ConfigType switch
{
EConfigType.PolicyGroup =>
await GenOutboundsListWithChain(childProfiles, v2rayConfig, childBaseTagName),
EConfigType.ProxyChain =>
await GenChainOutboundsList(childProfiles, v2rayConfig, childBaseTagName),
_ => throw new NotImplementedException()
};
continue;
}
// Handle proxy chain
string? prevTag = null;
var currentOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
var nextOutbound = nextProxyCache.GetValueOrDefault(node.Subid, null);
if (nextOutbound != null)
{
nextOutbound = JsonUtils.DeepCopy(nextOutbound);
}
var subItem = await AppManager.Instance.GetSubItem(node.Subid);
// current proxy
await GenOutbound(node, currentOutbound);
currentOutbound.tag = $"{baseTagName}-{index}";
if (!node.Subid.IsNullOrEmpty())
{
if (prevProxyTags.TryGetValue(node.Subid, out var value))
{
prevTag = value; // maybe null
}
else
{
var prevNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.PrevProfile);
if (prevNode is not null
&& Global.XraySupportConfigType.Contains(prevNode.ConfigType))
{
var prevOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
await GenOutbound(prevNode, prevOutbound);
prevTag = $"prev-{baseTagName}-{++prevIndex}";
prevOutbound.tag = prevTag;
prevOutbounds.Add(prevOutbound);
}
prevProxyTags[node.Subid] = prevTag;
}
nextOutbound = await GenChainOutbounds(subItem, currentOutbound, prevTag, nextOutbound);
if (!nextProxyCache.ContainsKey(node.Subid))
{
nextProxyCache[node.Subid] = nextOutbound;
}
}
if (nextOutbound is not null)
{
resultOutbounds.Add(nextOutbound);
}
resultOutbounds.Add(currentOutbound);
}
// Merge results: first the main chain outbounds, then other outbounds, and finally utility outbounds
if (baseTagName == Global.ProxyTag)
{
resultOutbounds.AddRange(prevOutbounds);
resultOutbounds.AddRange(v2rayConfig.outbounds);
v2rayConfig.outbounds = resultOutbounds;
}
else
{
v2rayConfig.outbounds.AddRange(prevOutbounds);
v2rayConfig.outbounds.AddRange(resultOutbounds);
}
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return 0;
}
/// <summary>
/// Generates a chained outbound configuration for the given subItem and outbound.
/// The outbound's tag must be set before calling this method.
/// Returns the next proxy's outbound configuration, which may be null if no next proxy exists.
/// </summary>
/// <param name="subItem">The subscription item containing proxy chain information.</param>
/// <param name="outbound">The current outbound configuration. Its tag must be set before calling this method.</param>
/// <param name="prevOutboundTag">The tag of the previous outbound in the chain, if any.</param>
/// <param name="nextOutbound">The outbound for the next proxy in the chain, if already created. If null, will be created inside.</param>
/// <returns>
/// The outbound configuration for the next proxy in the chain, or null if no next proxy exists.
/// </returns>
private async Task<Outbounds4Ray?> GenChainOutbounds(SubItem subItem, Outbounds4Ray outbound, string? prevOutboundTag, Outbounds4Ray? nextOutbound = null)
{
try
{
var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
if (!prevOutboundTag.IsNullOrEmpty())
{
outbound.streamSettings.sockopt = new()
{
dialerProxy = prevOutboundTag
};
}
// Next proxy
var nextNode = await AppManager.Instance.GetProfileItemViaRemarks(subItem.NextProfile);
if (nextNode is not null
&& Global.XraySupportConfigType.Contains(nextNode.ConfigType))
{
if (nextOutbound == null)
{
nextOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
await GenOutbound(nextNode, nextOutbound);
}
nextOutbound.tag = outbound.tag;
outbound.tag = $"mid-{outbound.tag}";
nextOutbound.streamSettings.sockopt = new()
{
dialerProxy = outbound.tag
};
}
return nextOutbound;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return null;
}
private async Task<int> GenOutboundsList(List<ProfileItem> nodes, V2rayConfig v2rayConfig, string baseTagName = Global.ProxyTag)
{
var resultOutbounds = new List<Outbounds4Ray>(); var resultOutbounds = new List<Outbounds4Ray>();
for (var i = 0; i < nodes.Count; i++) for (var i = 0; i < nodes.Count; i++)
{ {
var node = nodes[i]; var node = nodes[i];
var currentTag = $"{baseTagName}-{i + 1}"; if (node == null)
{
continue;
}
if (node.ConfigType.IsGroupType()) if (node.ConfigType.IsGroupType())
{ {
var childProfiles = new CoreConfigV2rayService(context with { Node = node, }).BuildGroupProxyOutbounds(currentTag); var (childProfiles, _) = await GroupProfileManager.GetChildProfileItems(node);
resultOutbounds.AddRange(childProfiles); if (childProfiles.Count <= 0)
{
continue;
}
var childBaseTagName = $"{baseTagName}-{i + 1}";
var ret = node.ConfigType switch
{
EConfigType.PolicyGroup =>
await GenOutboundsListWithChain(childProfiles, v2rayConfig, childBaseTagName),
EConfigType.ProxyChain =>
await GenChainOutboundsList(childProfiles, v2rayConfig, childBaseTagName),
_ => throw new NotImplementedException()
};
continue; continue;
} }
var outbound = new CoreConfigV2rayService(context with { Node = node, }).BuildProxyOutbound(); var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
outbound.tag = currentTag; if (txtOutbound.IsNullOrEmpty())
{
break;
}
var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
var result = await GenOutbound(node, outbound);
if (result != 0)
{
break;
}
outbound.tag = baseTagName + (i + 1).ToString();
resultOutbounds.Add(outbound); resultOutbounds.Add(outbound);
} }
return resultOutbounds; if (baseTagName == Global.ProxyTag)
{
resultOutbounds.AddRange(v2rayConfig.outbounds);
v2rayConfig.outbounds = resultOutbounds;
}
else
{
v2rayConfig.outbounds.AddRange(resultOutbounds);
}
return await Task.FromResult(0);
} }
private List<Outbounds4Ray> BuildChainOutboundsList(string baseTagName = Global.ProxyTag) private async Task<int> GenChainOutboundsList(List<ProfileItem> nodes, V2rayConfig v2rayConfig, string baseTagName = Global.ProxyTag)
{ {
var nodes = new List<ProfileItem>();
foreach (var nodeId in Utils.String2List(_node.GetProtocolExtra().ChildItems) ?? [])
{
if (context.AllProxiesMap.TryGetValue(nodeId, out var node))
{
nodes.Add(node);
}
}
// Based on actual network flow instead of data packets // Based on actual network flow instead of data packets
var nodesReverse = nodes.AsEnumerable().Reverse().ToList(); var nodesReverse = nodes.AsEnumerable().Reverse().ToList();
var resultOutbounds = new List<Outbounds4Ray>(); var resultOutbounds = new List<Outbounds4Ray>();
for (var i = 0; i < nodesReverse.Count; i++) for (var i = 0; i < nodesReverse.Count; i++)
{ {
var node = nodesReverse[i]; var node = nodesReverse[i];
var currentTag = i == 0 ? baseTagName : $"chain-{baseTagName}-{i}"; var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
var dialerProxyTag = i != nodesReverse.Count - 1 ? $"chain-{baseTagName}-{i + 1}" : null; if (txtOutbound.IsNullOrEmpty())
if (node.ConfigType.IsGroupType())
{ {
var childProfiles = new CoreConfigV2rayService(context with { Node = node, }).BuildGroupProxyOutbounds(currentTag); break;
if (!dialerProxyTag.IsNullOrEmpty())
{
var chainEndNodes =
childProfiles.Where(n => n?.streamSettings?.sockopt?.dialerProxy?.IsNullOrEmpty() ?? true);
foreach (var chainEndNode in chainEndNodes)
{
chainEndNode.streamSettings.sockopt = new()
{
dialerProxy = dialerProxyTag
};
}
}
if (i != 0)
{
var chainStartNodes = childProfiles.Where(n => n.tag.StartsWith(currentTag)).ToList();
if (chainStartNodes.Count == 1)
{
foreach (var existedChainEndNode in resultOutbounds.Where(n => n.streamSettings?.sockopt?.dialerProxy == currentTag))
{
existedChainEndNode.streamSettings.sockopt = new()
{
dialerProxy = chainStartNodes.First().tag
};
}
}
else if (chainStartNodes.Count > 1)
{
var existedChainNodes = JsonUtils.DeepCopy(resultOutbounds);
resultOutbounds.Clear();
var j = 0;
foreach (var chainStartNode in chainStartNodes)
{
var existedChainNodesClone = JsonUtils.DeepCopy(existedChainNodes);
foreach (var existedChainNode in existedChainNodesClone)
{
var cloneTag = $"{existedChainNode.tag}-clone-{j + 1}";
existedChainNode.tag = cloneTag;
}
for (var k = 0; k < existedChainNodesClone.Count; k++)
{
var existedChainNode = existedChainNodesClone[k];
var previousDialerProxyTag = existedChainNode.streamSettings?.sockopt?.dialerProxy;
var nextTag = k + 1 < existedChainNodesClone.Count
? existedChainNodesClone[k + 1].tag
: chainStartNode.tag;
existedChainNode.streamSettings.sockopt = new()
{
dialerProxy = (previousDialerProxyTag == currentTag)
? chainStartNode.tag
: nextTag
};
resultOutbounds.Add(existedChainNode);
}
j++;
}
}
}
resultOutbounds.AddRange(childProfiles);
continue;
} }
var outbound = new CoreConfigV2rayService(context with { Node = node, }).BuildProxyOutbound(); var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
var result = await GenOutbound(node, outbound);
outbound.tag = currentTag; if (result != 0)
{
break;
}
if (!dialerProxyTag.IsNullOrEmpty()) if (i == 0)
{
outbound.tag = baseTagName;
}
else
{
// avoid v2ray observe
outbound.tag = "chain-" + baseTagName + i.ToString();
}
if (i != nodesReverse.Count - 1)
{ {
outbound.streamSettings.sockopt = new() outbound.streamSettings.sockopt = new()
{ {
dialerProxy = dialerProxyTag dialerProxy = "chain-" + baseTagName + (i + 1).ToString()
}; };
} }
resultOutbounds.Add(outbound); resultOutbounds.Add(outbound);
} }
return resultOutbounds; if (baseTagName == Global.ProxyTag)
{
resultOutbounds.AddRange(v2rayConfig.outbounds);
v2rayConfig.outbounds = resultOutbounds;
}
else
{
v2rayConfig.outbounds.AddRange(resultOutbounds);
}
return await Task.FromResult(0);
} }
} }

View file

@ -2,20 +2,20 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenRouting() private async Task<int> GenRouting(V2rayConfig v2rayConfig)
{ {
try try
{ {
if (_coreConfig.routing?.rules != null) if (v2rayConfig.routing?.rules != null)
{ {
_coreConfig.routing.domainStrategy = _config.RoutingBasicItem.DomainStrategy; v2rayConfig.routing.domainStrategy = _config.RoutingBasicItem.DomainStrategy;
var routing = context.RoutingItem; var routing = await ConfigHandler.GetDefaultRouting(_config);
if (routing != null) if (routing != null)
{ {
if (routing.DomainStrategy.IsNotEmpty()) if (routing.DomainStrategy.IsNotEmpty())
{ {
_coreConfig.routing.domainStrategy = routing.DomainStrategy; v2rayConfig.routing.domainStrategy = routing.DomainStrategy;
} }
var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.RuleSet); var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.RuleSet);
foreach (var item in rules) foreach (var item in rules)
@ -31,18 +31,7 @@ public partial class CoreConfigV2rayService
} }
var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item)); var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item));
GenRoutingUserRule(item2); await GenRoutingUserRule(item2, v2rayConfig);
}
}
var balancerTagList = _coreConfig.routing.balancers
?.Select(p => p.tag)
.ToList() ?? [];
if (balancerTagList.Count > 0)
{
foreach (var rulesItem in _coreConfig.routing.rules.Where(r => balancerTagList.Contains(r.outboundTag + Global.BalancerTagSuffix)))
{
rulesItem.balancerTag = rulesItem.outboundTag + Global.BalancerTagSuffix;
rulesItem.outboundTag = null;
} }
} }
} }
@ -51,94 +40,95 @@ public partial class CoreConfigV2rayService
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return 0;
} }
private void GenRoutingUserRule(RulesItem4Ray? userRule) private async Task<int> GenRoutingUserRule(RulesItem4Ray? rule, V2rayConfig v2rayConfig)
{ {
try try
{ {
if (userRule == null) if (rule == null)
{ {
return; return 0;
} }
userRule.outboundTag = GenRoutingUserRuleOutbound(userRule.outboundTag ?? Global.ProxyTag); rule.outboundTag = await GenRoutingUserRuleOutbound(rule.outboundTag, v2rayConfig);
if (userRule.port.IsNullOrEmpty()) if (rule.port.IsNullOrEmpty())
{ {
userRule.port = null; rule.port = null;
} }
if (userRule.network.IsNullOrEmpty()) if (rule.network.IsNullOrEmpty())
{ {
userRule.network = null; rule.network = null;
} }
if (userRule.domain?.Count == 0) if (rule.domain?.Count == 0)
{ {
userRule.domain = null; rule.domain = null;
} }
if (userRule.ip?.Count == 0) if (rule.ip?.Count == 0)
{ {
userRule.ip = null; rule.ip = null;
} }
if (userRule.protocol?.Count == 0) if (rule.protocol?.Count == 0)
{ {
userRule.protocol = null; rule.protocol = null;
} }
if (userRule.inboundTag?.Count == 0) if (rule.inboundTag?.Count == 0)
{ {
userRule.inboundTag = null; rule.inboundTag = null;
} }
if (userRule.process?.Count == 0) if (rule.process?.Count == 0)
{ {
userRule.process = null; rule.process = null;
} }
var hasDomainIp = false; var hasDomainIp = false;
if (userRule.domain?.Count > 0) if (rule.domain?.Count > 0)
{ {
var it = JsonUtils.DeepCopy(userRule); var it = JsonUtils.DeepCopy(rule);
it.ip = null; it.ip = null;
it.process = null; it.process = null;
it.type = "field"; it.type = "field";
for (var k = it.domain.Count - 1; k >= 0; k--) for (var k = it.domain.Count - 1; k >= 0; k--)
{ {
if (it.domain[k].StartsWith('#')) if (it.domain[k].StartsWith("#"))
{ {
it.domain.RemoveAt(k); it.domain.RemoveAt(k);
} }
it.domain[k] = it.domain[k].Replace(Global.RoutingRuleComma, ","); it.domain[k] = it.domain[k].Replace(Global.RoutingRuleComma, ",");
} }
_coreConfig.routing.rules.Add(it); v2rayConfig.routing.rules.Add(it);
hasDomainIp = true; hasDomainIp = true;
} }
if (userRule.ip?.Count > 0) if (rule.ip?.Count > 0)
{ {
var it = JsonUtils.DeepCopy(userRule); var it = JsonUtils.DeepCopy(rule);
it.domain = null; it.domain = null;
it.process = null; it.process = null;
it.type = "field"; it.type = "field";
_coreConfig.routing.rules.Add(it); v2rayConfig.routing.rules.Add(it);
hasDomainIp = true; hasDomainIp = true;
} }
if (userRule.process?.Count > 0) if (rule.process?.Count > 0)
{ {
var it = JsonUtils.DeepCopy(userRule); var it = JsonUtils.DeepCopy(rule);
it.domain = null; it.domain = null;
it.ip = null; it.ip = null;
it.type = "field"; it.type = "field";
_coreConfig.routing.rules.Add(it); v2rayConfig.routing.rules.Add(it);
hasDomainIp = true; hasDomainIp = true;
} }
if (!hasDomainIp) if (!hasDomainIp)
{ {
if (userRule.port.IsNotEmpty() if (rule.port.IsNotEmpty()
|| userRule.protocol?.Count > 0 || rule.protocol?.Count > 0
|| userRule.inboundTag?.Count > 0 || rule.inboundTag?.Count > 0
|| userRule.network != null || rule.network != null
) )
{ {
var it = JsonUtils.DeepCopy(userRule); var it = JsonUtils.DeepCopy(rule);
it.type = "field"; it.type = "field";
_coreConfig.routing.rules.Add(it); v2rayConfig.routing.rules.Add(it);
} }
} }
} }
@ -146,16 +136,17 @@ public partial class CoreConfigV2rayService
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
} }
return await Task.FromResult(0);
} }
private string GenRoutingUserRuleOutbound(string outboundTag) private async Task<string?> GenRoutingUserRuleOutbound(string outboundTag, V2rayConfig v2rayConfig)
{ {
if (Global.OutboundTags.Contains(outboundTag)) if (Global.OutboundTags.Contains(outboundTag))
{ {
return outboundTag; return outboundTag;
} }
var node = context.AllProxiesMap.GetValueOrDefault($"remark:{outboundTag}"); var node = await AppManager.Instance.GetProfileItemViaRemarks(outboundTag);
if (node == null if (node == null
|| (!Global.XraySupportConfigType.Contains(node.ConfigType) || (!Global.XraySupportConfigType.Contains(node.ConfigType)
@ -165,20 +156,27 @@ public partial class CoreConfigV2rayService
} }
var tag = $"{node.IndexId}-{Global.ProxyTag}"; var tag = $"{node.IndexId}-{Global.ProxyTag}";
if (_coreConfig.outbounds.Any(p => p.tag.StartsWith(tag))) if (v2rayConfig.outbounds.Any(p => p.tag == tag))
{ {
return tag; return tag;
} }
var proxyOutbounds = new CoreConfigV2rayService(context with { Node = node, }).BuildAllProxyOutbounds(tag); if (node.ConfigType.IsGroupType())
_coreConfig.outbounds.AddRange(proxyOutbounds);
if (proxyOutbounds.Count(n => n.tag.StartsWith(tag)) > 1)
{ {
var multipleLoad = node.GetProtocolExtra().MultipleLoad ?? EMultipleLoad.LeastPing; var ret = await GenGroupOutbound(node, v2rayConfig, tag);
GenObservatory(multipleLoad, tag); if (ret == 0)
GenBalancer(multipleLoad, tag); {
return tag;
}
return Global.ProxyTag;
} }
return tag; var txtOutbound = EmbedUtils.GetEmbedText(Global.V2raySampleOutbound);
var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
await GenOutbound(node, outbound);
outbound.tag = tag;
v2rayConfig.outbounds.Add(outbound);
return outbound.tag;
} }
} }

View file

@ -2,7 +2,7 @@ namespace ServiceLib.Services.CoreConfig;
public partial class CoreConfigV2rayService public partial class CoreConfigV2rayService
{ {
private void GenStatistic() private async Task<int> GenStatistic(V2rayConfig v2rayConfig)
{ {
if (_config.GuiItem.EnableStatistics || _config.GuiItem.DisplayRealTimeSpeed) if (_config.GuiItem.EnableStatistics || _config.GuiItem.DisplayRealTimeSpeed)
{ {
@ -11,17 +11,17 @@ public partial class CoreConfigV2rayService
Policy4Ray policyObj = new(); Policy4Ray policyObj = new();
SystemPolicy4Ray policySystemSetting = new(); SystemPolicy4Ray policySystemSetting = new();
_coreConfig.stats = new Stats4Ray(); v2rayConfig.stats = new Stats4Ray();
apiObj.tag = tag; apiObj.tag = tag;
_coreConfig.metrics = apiObj; v2rayConfig.metrics = apiObj;
policySystemSetting.statsOutboundDownlink = true; policySystemSetting.statsOutboundDownlink = true;
policySystemSetting.statsOutboundUplink = true; policySystemSetting.statsOutboundUplink = true;
policyObj.system = policySystemSetting; policyObj.system = policySystemSetting;
_coreConfig.policy = policyObj; v2rayConfig.policy = policyObj;
if (!_coreConfig.inbounds.Exists(item => item.tag == tag)) if (!v2rayConfig.inbounds.Exists(item => item.tag == tag))
{ {
Inbounds4Ray apiInbound = new(); Inbounds4Ray apiInbound = new();
Inboundsettings4Ray apiInboundSettings = new(); Inboundsettings4Ray apiInboundSettings = new();
@ -31,10 +31,10 @@ public partial class CoreConfigV2rayService
apiInbound.protocol = Global.InboundAPIProtocol; apiInbound.protocol = Global.InboundAPIProtocol;
apiInboundSettings.address = Global.Loopback; apiInboundSettings.address = Global.Loopback;
apiInbound.settings = apiInboundSettings; apiInbound.settings = apiInboundSettings;
_coreConfig.inbounds.Add(apiInbound); v2rayConfig.inbounds.Add(apiInbound);
} }
if (!_coreConfig.routing.rules.Exists(item => item.outboundTag == tag)) if (!v2rayConfig.routing.rules.Exists(item => item.outboundTag == tag))
{ {
RulesItem4Ray apiRoutingRule = new() RulesItem4Ray apiRoutingRule = new()
{ {
@ -43,8 +43,9 @@ public partial class CoreConfigV2rayService
type = "field" type = "field"
}; };
_coreConfig.routing.rules.Add(apiRoutingRule); v2rayConfig.routing.rules.Add(apiRoutingRule);
} }
} }
return await Task.FromResult(0);
} }
} }

View file

@ -247,6 +247,11 @@ public class AddServerViewModel : MyReactiveObject
{ {
serverName = SelectedSource.Address; serverName = SelectedSource.Address;
} }
if (!Utils.IsDomain(serverName))
{
UpdateCertTip(ResUI.ServerNameMustBeValidDomain);
return;
}
if (SelectedSource.Port > 0) if (SelectedSource.Port > 0)
{ {
domain += $":{SelectedSource.Port}"; domain += $":{SelectedSource.Port}";
@ -272,6 +277,11 @@ public class AddServerViewModel : MyReactiveObject
{ {
serverName = SelectedSource.Address; serverName = SelectedSource.Address;
} }
if (!Utils.IsDomain(serverName))
{
UpdateCertTip(ResUI.ServerNameMustBeValidDomain);
return;
}
if (SelectedSource.Port > 0) if (SelectedSource.Port > 0)
{ {
domain += $":{SelectedSource.Port}"; domain += $":{SelectedSource.Port}";

View file

@ -801,8 +801,7 @@ public class ProfilesViewModel : MyReactiveObject
if (blClipboard) if (blClipboard)
{ {
var context = await CoreConfigHandler.BuildCoreConfigContext(_config, item); var result = await CoreConfigHandler.GenerateClientConfig(item, null);
var result = await CoreConfigHandler.GenerateClientConfig(context, null);
if (result.Success != true) if (result.Success != true)
{ {
NoticeManager.Instance.Enqueue(result.Msg); NoticeManager.Instance.Enqueue(result.Msg);
@ -825,8 +824,7 @@ public class ProfilesViewModel : MyReactiveObject
{ {
return; return;
} }
var context = await CoreConfigHandler.BuildCoreConfigContext(_config, item); var result = await CoreConfigHandler.GenerateClientConfig(item, fileName);
var result = await CoreConfigHandler.GenerateClientConfig(context, fileName);
if (result.Success != true) if (result.Success != true)
{ {
NoticeManager.Instance.Enqueue(result.Msg); NoticeManager.Instance.Enqueue(result.Msg);

View file

@ -32,7 +32,7 @@
IsCancel="True" /> IsCancel="True" />
</StackPanel> </StackPanel>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto"> <Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto">
<Grid <Grid
Grid.Row="0" Grid.Row="0"
@ -722,51 +722,11 @@
Text="{x:Static resx:ResUI.TbPath}" /> Text="{x:Static resx:ResUI.TbPath}" />
</Grid> </Grid>
<Grid <Separator Grid.Row="5" Margin="{StaticResource MarginTb8}" />
x:Name="gridFinalmask"
Grid.Row="5"
ColumnDefinitions="300,Auto">
<TextBlock
Grid.Row="0"
Grid.Column="0"
Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbFinalmask}" />
<Button
Grid.Row="0"
Grid.Column="1"
Margin="{StaticResource MarginLr4}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Classes="IconButton">
<Button.Content>
<PathIcon Data="{StaticResource SemiIconMore}">
<PathIcon.RenderTransform>
<RotateTransform Angle="90" />
</PathIcon.RenderTransform>
</PathIcon>
</Button.Content>
<Button.Flyout>
<Flyout>
<StackPanel>
<TextBox
x:Name="txtFinalmask"
Width="400"
Margin="{StaticResource Margin4}"
AcceptsReturn="True"
Classes="TextArea"
TextWrapping="NoWrap" />
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
</Grid>
<Separator Grid.Row="6" Margin="{StaticResource MarginTb8}" />
<Grid <Grid
x:Name="gridTls" x:Name="gridTls"
Grid.Row="7" Grid.Row="6"
ColumnDefinitions="300,Auto" ColumnDefinitions="300,Auto"
RowDefinitions="Auto,Auto,Auto,Auto,Auto"> RowDefinitions="Auto,Auto,Auto,Auto,Auto">
@ -785,7 +745,7 @@
</Grid> </Grid>
<Grid <Grid
x:Name="gridTlsMore" x:Name="gridTlsMore"
Grid.Row="8" Grid.Row="7"
ColumnDefinitions="300,Auto" ColumnDefinitions="300,Auto"
IsVisible="False" IsVisible="False"
RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto"> RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto">
@ -948,7 +908,7 @@
</Grid> </Grid>
<Grid <Grid
x:Name="gridRealityMore" x:Name="gridRealityMore"
Grid.Row="8" Grid.Row="7"
ColumnDefinitions="300,Auto" ColumnDefinitions="300,Auto"
IsVisible="False" IsVisible="False"
RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto"> RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto">
@ -1036,7 +996,7 @@
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
HorizontalAlignment="Left" /> HorizontalAlignment="Left" />
</Grid> </Grid>
<Separator Grid.Row="9" Margin="{StaticResource MarginTb8}" /> <Separator Grid.Row="8" Margin="{StaticResource MarginTb8}" />
</Grid> </Grid>
</ScrollViewer> </ScrollViewer>
</DockPanel> </DockPanel>

View file

@ -78,7 +78,6 @@ public partial class AddServerWindow : WindowBase<AddServerViewModel>
cmbCoreType.IsEnabled = false; cmbCoreType.IsEnabled = false;
cmbFingerprint.IsEnabled = false; cmbFingerprint.IsEnabled = false;
cmbFingerprint.SelectedValue = string.Empty; cmbFingerprint.SelectedValue = string.Empty;
gridFinalmask.IsVisible = false;
cmbHeaderType8.ItemsSource = Global.TuicCongestionControls; cmbHeaderType8.ItemsSource = Global.TuicCongestionControls;
break; break;
@ -96,7 +95,6 @@ public partial class AddServerWindow : WindowBase<AddServerViewModel>
gridAnytls.IsVisible = true; gridAnytls.IsVisible = true;
lstStreamSecurity.Add(Global.StreamSecurityReality); lstStreamSecurity.Add(Global.StreamSecurityReality);
cmbCoreType.IsEnabled = false; cmbCoreType.IsEnabled = false;
gridFinalmask.IsVisible = false;
break; break;
} }
cmbStreamSecurity.ItemsSource = lstStreamSecurity; cmbStreamSecurity.ItemsSource = lstStreamSecurity;
@ -196,8 +194,6 @@ public partial class AddServerWindow : WindowBase<AddServerViewModel>
this.Bind(ViewModel, vm => vm.SelectedSource.SpiderX, v => v.txtSpiderX.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.SpiderX, v => v.txtSpiderX.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.Mldsa65Verify, v => v.txtMldsa65Verify.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.Mldsa65Verify, v => v.txtMldsa65Verify.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.Finalmask, v => v.txtFinalmask.Text).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.FetchCertCmd, v => v.btnFetchCert).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.FetchCertCmd, v => v.btnFetchCert).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.FetchCertChainCmd, v => v.btnFetchCertChain).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.FetchCertChainCmd, v => v.btnFetchCertChain).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);

View file

@ -60,8 +60,6 @@
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid Grid.Row="0"> <Grid Grid.Row="0">
@ -930,47 +928,12 @@
Text="{x:Static resx:ResUI.TbPath}" /> Text="{x:Static resx:ResUI.TbPath}" />
</Grid> </Grid>
<Grid x:Name="gridFinalmask" Grid.Row="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="0"
Grid.Column="0"
Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Style="{StaticResource ToolbarTextBlock}"
Text="{x:Static resx:ResUI.TbFinalmask}" />
<materialDesign:PopupBox
Grid.Row="0"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Center"
StaysOpen="True"
Style="{StaticResource MaterialDesignToolForegroundPopupBox}">
<StackPanel>
<TextBox
x:Name="txtFinalmask"
Width="400"
Margin="{StaticResource Margin4}"
AcceptsReturn="True"
HorizontalScrollBarVisibility="Auto"
MaxLines="8"
MinLines="4"
Style="{StaticResource MyOutlinedTextBox}"
TextWrapping="NoWrap"
VerticalScrollBarVisibility="Auto" />
</StackPanel>
</materialDesign:PopupBox>
</Grid>
<Separator <Separator
Grid.Row="6" Grid.Row="5"
Margin="0,2" Margin="0,2"
Style="{DynamicResource MaterialDesignSeparator}" /> Style="{DynamicResource MaterialDesignSeparator}" />
<Grid x:Name="gridTls" Grid.Row="7"> <Grid x:Name="gridTls" Grid.Row="6">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
@ -1000,7 +963,7 @@
</Grid> </Grid>
<Grid <Grid
x:Name="gridTlsMore" x:Name="gridTlsMore"
Grid.Row="8" Grid.Row="7"
Visibility="Hidden"> Visibility="Hidden">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
@ -1189,7 +1152,7 @@
</Grid> </Grid>
<Grid <Grid
x:Name="gridRealityMore" x:Name="gridRealityMore"
Grid.Row="8" Grid.Row="7"
Visibility="Hidden"> Visibility="Hidden">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
@ -1301,7 +1264,7 @@
Style="{StaticResource DefTextBox}" /> Style="{StaticResource DefTextBox}" />
</Grid> </Grid>
<Separator <Separator
Grid.Row="9" Grid.Row="8"
Margin="0,2" Margin="0,2"
Style="{DynamicResource MaterialDesignSeparator}" /> Style="{DynamicResource MaterialDesignSeparator}" />
</Grid> </Grid>

View file

@ -73,7 +73,6 @@ public partial class AddServerWindow
cmbCoreType.IsEnabled = false; cmbCoreType.IsEnabled = false;
cmbFingerprint.IsEnabled = false; cmbFingerprint.IsEnabled = false;
cmbFingerprint.Text = string.Empty; cmbFingerprint.Text = string.Empty;
gridFinalmask.Visibility = Visibility.Collapsed;
cmbHeaderType8.ItemsSource = Global.TuicCongestionControls; cmbHeaderType8.ItemsSource = Global.TuicCongestionControls;
break; break;
@ -91,7 +90,6 @@ public partial class AddServerWindow
gridAnytls.Visibility = Visibility.Visible; gridAnytls.Visibility = Visibility.Visible;
cmbCoreType.IsEnabled = false; cmbCoreType.IsEnabled = false;
lstStreamSecurity.Add(Global.StreamSecurityReality); lstStreamSecurity.Add(Global.StreamSecurityReality);
gridFinalmask.Visibility = Visibility.Collapsed;
break; break;
} }
cmbStreamSecurity.ItemsSource = lstStreamSecurity; cmbStreamSecurity.ItemsSource = lstStreamSecurity;
@ -192,8 +190,6 @@ public partial class AddServerWindow
this.Bind(ViewModel, vm => vm.SelectedSource.SpiderX, v => v.txtSpiderX.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.SpiderX, v => v.txtSpiderX.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.Mldsa65Verify, v => v.txtMldsa65Verify.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.Mldsa65Verify, v => v.txtMldsa65Verify.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource.Finalmask, v => v.txtFinalmask.Text).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.FetchCertCmd, v => v.btnFetchCert).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.FetchCertCmd, v => v.btnFetchCert).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.FetchCertChainCmd, v => v.btnFetchCertChain).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.FetchCertChainCmd, v => v.btnFetchCertChain).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);