diff --git a/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs b/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs index 9a957ae0..b0cb07c6 100644 --- a/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs +++ b/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs @@ -27,16 +27,51 @@ public class ConfigHandlerCustomSubscriptionTests Url = "http://127.0.0.1/sub", PreSocksPort = 10808, }); + var oldProfile = CoreConfigTestFactory.CreateVmessNode(ECoreType.Xray, $"profile-{Guid.NewGuid():N}", "old"); + oldProfile.Subid = subId; + oldProfile.IsSub = true; + await SQLiteHelper.Instance.InsertAsync(oldProfile); var count = await ConfigHandler.AddBatchServers(config, FullXrayConfigs, subId, true); var profiles = await AppManager.Instance.ProfileItems(subId) ?? []; await count.Should().BeEqualTo(2); await profiles.Should().HaveCount(2); + await profiles.Any(profile => profile.IndexId == oldProfile.IndexId).Should().BeFalse(); foreach (var profile in profiles) { await profile.ConfigType.Should().BeEqualTo(EConfigType.Custom); await profile.PreSocksPort.Should().BeEqualTo(10808); } } + + [Test] + public async Task AddBatchServers_UnsuccessfulSubscriptionUpdate_PreservesExistingProfiles() + { + var config = CoreConfigTestFactory.CreateConfig(); + CoreConfigTestFactory.BindAppManagerConfig(config); + SQLiteHelper.Instance.CreateTable(); + SQLiteHelper.Instance.CreateTable(); + + var subId = $"sub-failed-{Guid.NewGuid():N}"; + await SQLiteHelper.Instance.ReplaceAsync(new SubItem + { + Id = subId, + Remarks = "sub", + Url = "http://127.0.0.1/sub", + }); + + var existing = CoreConfigTestFactory.CreateVmessNode(ECoreType.Xray, $"profile-{Guid.NewGuid():N}", "existing"); + existing.Subid = subId; + existing.IsSub = true; + await SQLiteHelper.Instance.InsertAsync(existing); + + var invalidSubscription = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("not a valid profile")); + var count = await ConfigHandler.AddBatchServers(config, invalidSubscription, subId, true); + var profiles = await AppManager.Instance.ProfileItems(subId) ?? []; + + await (count < 1).Should().BeTrue(); + await profiles.Should().HaveCount(1); + await profiles[0].IndexId.Should().BeEqualTo(existing.IndexId); + } } diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 7daad49b..aeccf43d 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -2134,7 +2134,6 @@ public static class ConfigHandler { lstOriSub = await AppManager.Instance.ProfileItems(subid); activeProfile = lstOriSub?.FirstOrDefault(t => t.IndexId == config.IndexId); - await RemoveServersViaSubid(config, subid, true); } var counter = 0; @@ -2194,6 +2193,11 @@ public static class ConfigHandler counter = await AddBatchServers4Custom(config, strData, subid, isSub); } + if (counter > 0 && lstOriSub is { Count: > 0 }) + { + await RemoveServers(config, lstOriSub.Where(item => item.IsSub).ToList()); + } + //Select active node if (activeProfile != null) {