Skip to content

Commit 033c16c

Browse files
committed
Refactor to select active nodes when updating subscriptions
1 parent 6ec6143 commit 033c16c

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

v2rayN/ServiceLib/Handler/ConfigHandler.cs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,31 +1034,29 @@ public static async Task<RetResult> AddCustomServer4Multiple(Config config, List
10341034
/// <param name="strData"></param>
10351035
/// <param name="subid"></param>
10361036
/// <returns>成功导入的数量</returns>
1037-
private static async Task<int> AddBatchServers(Config config, string strData, string subid, bool isSub, List<ProfileItem> lstOriSub)
1037+
private static async Task<int> AddBatchServersCommon(Config config, string strData, string subid, bool isSub)
10381038
{
10391039
if (Utils.IsNullOrEmpty(strData))
10401040
{
10411041
return -1;
10421042
}
10431043

1044-
string subFilter = string.Empty;
1044+
var subFilter = string.Empty;
10451045
//remove sub items
10461046
if (isSub && Utils.IsNotEmpty(subid))
10471047
{
10481048
await RemoveServerViaSubid(config, subid, isSub);
10491049
subFilter = (await AppHandler.Instance.GetSubItem(subid))?.Filter ?? "";
10501050
}
10511051

1052-
int countServers = 0;
1053-
//Check for duplicate indexId
1054-
List<string>? lstDbIndexId = null;
1052+
var countServers = 0;
10551053
List<ProfileItem> lstAdd = new();
10561054
var arrData = strData.Split(Environment.NewLine.ToCharArray()).Where(t => !t.IsNullOrEmpty());
10571055
if (isSub)
10581056
{
10591057
arrData = arrData.Distinct();
10601058
}
1061-
foreach (string str in arrData)
1059+
foreach (var str in arrData)
10621060
{
10631061
//maybe sub
10641062
if (!isSub && (str.StartsWith(Global.HttpsProtocol) || str.StartsWith(Global.HttpProtocol)))
@@ -1075,35 +1073,12 @@ private static async Task<int> AddBatchServers(Config config, string strData, st
10751073
continue;
10761074
}
10771075

1078-
//exist sub items
1079-
if (isSub && Utils.IsNotEmpty(subid))
1076+
//exist sub items //filter
1077+
if (isSub && Utils.IsNotEmpty(subid) && Utils.IsNotEmpty(subFilter))
10801078
{
1081-
var existItem = lstOriSub?.FirstOrDefault(t => t.IsSub == isSub
1082-
&& config.UiItem.EnableUpdateSubOnlyRemarksExist ? t.Remarks == profileItem.Remarks : CompareProfileItem(t, profileItem, true));
1083-
if (existItem != null)
1084-
{
1085-
//Check for duplicate indexId
1086-
if (lstDbIndexId is null)
1087-
{
1088-
lstDbIndexId = await AppHandler.Instance.ProfileItemIndexes("");
1089-
}
1090-
if (lstAdd.Any(t => t.IndexId == existItem.IndexId)
1091-
|| lstDbIndexId.Any(t => t == existItem.IndexId))
1092-
{
1093-
profileItem.IndexId = string.Empty;
1094-
}
1095-
else
1096-
{
1097-
profileItem.IndexId = existItem.IndexId;
1098-
}
1099-
}
1100-
//filter
1101-
if (Utils.IsNotEmpty(subFilter))
1079+
if (!Regex.IsMatch(profileItem.Remarks, subFilter))
11021080
{
1103-
if (!Regex.IsMatch(profileItem.Remarks, subFilter))
1104-
{
1105-
continue;
1106-
}
1081+
continue;
11071082
}
11081083
}
11091084
profileItem.Subid = subid;
@@ -1138,7 +1113,7 @@ private static async Task<int> AddBatchServers(Config config, string strData, st
11381113
return countServers;
11391114
}
11401115

1141-
private static async Task<int> AddBatchServers4Custom(Config config, string strData, string subid, bool isSub, List<ProfileItem> lstOriSub)
1116+
private static async Task<int> AddBatchServers4Custom(Config config, string strData, string subid, bool isSub)
11421117
{
11431118
if (Utils.IsNullOrEmpty(strData))
11441119
{
@@ -1222,10 +1197,7 @@ private static async Task<int> AddBatchServers4Custom(Config config, string strD
12221197
{
12231198
await RemoveServerViaSubid(config, subid, isSub);
12241199
}
1225-
if (isSub && lstOriSub?.Count == 1)
1226-
{
1227-
profileItem.IndexId = lstOriSub[0].IndexId;
1228-
}
1200+
12291201
profileItem.Subid = subid;
12301202
profileItem.IsSub = isSub;
12311203
profileItem.PreSocksPort = preSocksPort;
@@ -1239,7 +1211,7 @@ private static async Task<int> AddBatchServers4Custom(Config config, string strD
12391211
}
12401212
}
12411213

1242-
private static async Task<int> AddBatchServers4SsSIP008(Config config, string strData, string subid, bool isSub, List<ProfileItem> lstOriSub)
1214+
private static async Task<int> AddBatchServers4SsSIP008(Config config, string strData, string subid, bool isSub)
12431215
{
12441216
if (Utils.IsNullOrEmpty(strData))
12451217
{
@@ -1278,34 +1250,47 @@ public static async Task<int> AddBatchServers(Config config, string strData, str
12781250
return -1;
12791251
}
12801252
List<ProfileItem>? lstOriSub = null;
1253+
ProfileItem? activeProfile = null;
12811254
if (isSub && Utils.IsNotEmpty(subid))
12821255
{
12831256
lstOriSub = await AppHandler.Instance.ProfileItems(subid);
1257+
activeProfile = lstOriSub?.FirstOrDefault(t => t.IndexId == config.IndexId);
12841258
}
12851259

12861260
var counter = 0;
12871261
if (Utils.IsBase64String(strData))
12881262
{
1289-
counter = await AddBatchServers(config, Utils.Base64Decode(strData), subid, isSub, lstOriSub);
1263+
counter = await AddBatchServersCommon(config, Utils.Base64Decode(strData), subid, isSub);
12901264
}
12911265
if (counter < 1)
12921266
{
1293-
counter = await AddBatchServers(config, strData, subid, isSub, lstOriSub);
1267+
counter = await AddBatchServersCommon(config, strData, subid, isSub);
12941268
}
12951269
if (counter < 1)
12961270
{
1297-
counter = await AddBatchServers(config, Utils.Base64Decode(strData), subid, isSub, lstOriSub);
1271+
counter = await AddBatchServersCommon(config, Utils.Base64Decode(strData), subid, isSub);
12981272
}
12991273

13001274
if (counter < 1)
13011275
{
1302-
counter = await AddBatchServers4SsSIP008(config, strData, subid, isSub, lstOriSub);
1276+
counter = await AddBatchServers4SsSIP008(config, strData, subid, isSub);
13031277
}
13041278

13051279
//maybe other sub
13061280
if (counter < 1)
13071281
{
1308-
counter = await AddBatchServers4Custom(config, strData, subid, isSub, lstOriSub);
1282+
counter = await AddBatchServers4Custom(config, strData, subid, isSub);
1283+
}
1284+
1285+
//Select active node
1286+
if (activeProfile != null)
1287+
{
1288+
var lstSub = await AppHandler.Instance.ProfileItems(subid);
1289+
var existItem = lstSub?.FirstOrDefault(t => config.UiItem.EnableUpdateSubOnlyRemarksExist ? t.Remarks == activeProfile.Remarks : CompareProfileItem(t, activeProfile, true));
1290+
if (existItem != null)
1291+
{
1292+
await ConfigHandler.SetDefaultServerIndex(config, existItem.IndexId);
1293+
}
13091294
}
13101295

13111296
return counter;

0 commit comments

Comments
 (0)