From 9638b4c368cf0a036227bf86c9dbbdb486a44a7b Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 6 Aug 2026 09:29:33 +0800 Subject: [PATCH] Fix custom config import https://github.com/2dust/v2rayN/issues/9902 --- v2rayN/ServiceLib/Common/Utils.cs | 4 +-- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 36 +++++++++++++++------ v2rayN/ServiceLib/Handler/Fmt/SingboxFmt.cs | 1 + v2rayN/ServiceLib/Handler/Fmt/V2rayFmt.cs | 1 + 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 57a42c80..c6433b6a 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -753,11 +753,11 @@ public class Utils return false; } - public static int GetFreePort(int defaultPort = 0) + public static int GetFreePort(int defaultPort) { try { - if (!(defaultPort == 0 || Utils.PortInUse(defaultPort))) + if (!PortInUse(defaultPort)) { return defaultPort; } diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 4a774a2e..308c86f9 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -586,7 +586,6 @@ public static class ConfigHandler return 0; } - public static async Task AddCustomOutboundServer(Config config, ProfileItem profileItem, bool blDelete, bool toFile = true) { var fileName = profileItem.Address; @@ -1739,8 +1738,16 @@ public static class ConfigHandler SubItem? subItem) { var subRemarks = subItem?.Remarks; - // Safe Mode: Only allow full configuration if it's not from a subscription - var lstProfiles = V2rayFmt.ResolveToCustomOutbound(strData, subRemarks); + // Prioritize using complete custom parsing, followed by custom outbound parsing. + var lstProfiles = V2rayFmt.ResolveToCustom(strData, subRemarks); + if (lstProfiles.Count == 0) + { + lstProfiles = SingboxFmt.ResolveToCustom(strData, subRemarks); + } + if (lstProfiles.Count == 0) + { + lstProfiles = V2rayFmt.ResolveToCustomOutbound(strData, subRemarks); + } if (lstProfiles.Count == 0) { lstProfiles = SingboxFmt.ResolveToCustomOutbound(strData, subRemarks); @@ -1750,7 +1757,7 @@ public static class ConfigHandler return -1; } - var count = await AddCustomOutboundServers(config, lstProfiles, subid, isSub); + var count = await AddBatchCustomServers(config, lstProfiles, subid, isSub); if (count > 0) { return count; @@ -1786,7 +1793,7 @@ public static class ConfigHandler var subRemarks = subItem.Remarks; var customCoreType = subItem.CustomCoreType!.Value; - List? lstProfiles = customCoreType switch + var lstProfiles = customCoreType switch { ECoreType.Xray => V2rayFmt.ResolveToCustom(strData, subRemarks), ECoreType.sing_box => SingboxFmt.ResolveToCustom(strData, subRemarks), @@ -1800,7 +1807,7 @@ public static class ConfigHandler return -1; } - var count = await AddCustomOutboundServers(config, lstProfiles, subid, isSub); + var count = await AddBatchCustomServers(config, lstProfiles, subid, isSub); if (count > 0) { return count; @@ -1810,7 +1817,7 @@ public static class ConfigHandler return await SaveCustomRawFileServer(config, strData, subid, isSub, subItem, customCoreType); } - private static async Task AddCustomOutboundServers( + private static async Task AddBatchCustomServers( Config config, List lstProfiles, string subid, @@ -1821,9 +1828,20 @@ public static class ConfigHandler { it.Subid = subid; it.IsSub = isSub; - if (await AddCustomOutboundServer(config, it, true) == 0) + + if (it.ConfigType == EConfigType.Custom) { - count++; + if (await AddCustomServer(config, it, true) == 0) + { + count++; + } + } + else + { + if (await AddCustomOutboundServer(config, it, true) == 0) + { + count++; + } } } return count; diff --git a/v2rayN/ServiceLib/Handler/Fmt/SingboxFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/SingboxFmt.cs index cc40166d..f8b36773 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/SingboxFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/SingboxFmt.cs @@ -76,6 +76,7 @@ public class SingboxFmt : BaseFmt var fileName = WriteAllText(JsonUtils.Serialize(jsonObject)); var profileItem = new ProfileItem { + ConfigType = EConfigType.Custom, CoreType = ECoreType.sing_box, Address = fileName, Remarks = subRemarks ?? "singbox_custom", diff --git a/v2rayN/ServiceLib/Handler/Fmt/V2rayFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/V2rayFmt.cs index a077062a..bd014165 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/V2rayFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/V2rayFmt.cs @@ -77,6 +77,7 @@ public class V2rayFmt : BaseFmt var profileItem = new ProfileItem { + ConfigType = EConfigType.Custom, CoreType = ECoreType.Xray, Address = fileName, Remarks = jsonObject["remarks"]?.ToString() ?? subRemarks ?? "v2ray_custom",