Optimize Duplicate Server

pull/1912/head
2dust 2021-12-02 16:47:35 +08:00
parent 1138d660b2
commit cb01809093
3 changed files with 60 additions and 37 deletions

View File

@ -587,13 +587,9 @@ namespace v2rayN.Forms
private void menuRemoveDuplicateServer_Click(object sender, EventArgs e)
{
Utils.DedupServerList(config.vmess, out List<VmessItem> servers, config.keepOlderDedupl);
int oldCount = config.vmess.Count;
int newCount = servers.Count;
if (servers != null)
{
config.vmess = servers;
}
ConfigHandler.DedupServerList(ref config);
int newCount = config.vmess.Count;
RefreshServers();
LoadV2ray();
UI.Show(string.Format(UIRes.I18N("RemoveDuplicateServerResult"), oldCount, newCount));

View File

@ -987,6 +987,63 @@ namespace v2rayN.Handler
return 0;
}
public static int DedupServerList(ref Config config)
{
var itemId = config.getItemId();
List<Mode.VmessItem> source = config.vmess;
bool keepOlder = config.keepOlderDedupl;
List<Mode.VmessItem> list = new List<Mode.VmessItem>();
if (!keepOlder) source.Reverse(); // Remove the early items first
bool _isAdded(Mode.VmessItem o, Mode.VmessItem n)
{
return o.configVersion == n.configVersion &&
o.configType == n.configType &&
o.address == n.address &&
o.port == n.port &&
o.id == n.id &&
o.alterId == n.alterId &&
o.security == n.security &&
o.network == n.network &&
o.headerType == n.headerType &&
o.requestHost == n.requestHost &&
o.path == n.path &&
o.streamSecurity == n.streamSecurity;
// skip (will remove) different remarks
}
foreach (Mode.VmessItem item in source)
{
if (!list.Exists(i => _isAdded(i, item)))
{
list.Add(item);
}
}
if (!keepOlder) list.Reverse();
config.vmess = list;
var index_ = config.vmess.FindIndex(it => it.getItemId() == itemId);
if (index_ >= 0)
{
config.index = index_;
}
else
{
if (config.vmess.Count > 0)
{
config.index = 0;
}
else
{
config.index = -1;
}
}
Global.reloadV2ray = true;
return 0;
}
#endregion
#region UI

View File

@ -348,37 +348,7 @@ namespace v2rayN
return $"{string.Format("{0:f1}", result)} {unit}";
}
public static void DedupServerList(List<Mode.VmessItem> source, out List<Mode.VmessItem> result, bool keepOlder)
{
List<Mode.VmessItem> list = new List<Mode.VmessItem>();
if (!keepOlder) source.Reverse(); // Remove the early items first
bool _isAdded(Mode.VmessItem o, Mode.VmessItem n)
{
return o.configVersion == n.configVersion &&
o.configType == n.configType &&
o.address == n.address &&
o.port == n.port &&
o.id == n.id &&
o.alterId == n.alterId &&
o.security == n.security &&
o.network == n.network &&
o.headerType == n.headerType &&
o.requestHost == n.requestHost &&
o.path == n.path &&
o.streamSecurity == n.streamSecurity;
// skip (will remove) different remarks
}
foreach (Mode.VmessItem item in source)
{
if (!list.Exists(i => _isAdded(i, item)))
{
list.Add(item);
}
}
if (!keepOlder) list.Reverse();
result = list;
}
public static string UrlEncode(string url)
{