mirror of
https://github.com/2dust/v2rayN.git
synced 2025-04-19 13:42:24 +00:00
Optimize and improve code
This commit is contained in:
parent
a02a122dd1
commit
6ef36f521d
2 changed files with 59 additions and 56 deletions
|
@ -871,13 +871,19 @@ namespace ServiceLib.Handler
|
||||||
public static async Task<Tuple<int, int>> DedupServerList(Config config, string subId)
|
public static async Task<Tuple<int, int>> DedupServerList(Config config, string subId)
|
||||||
{
|
{
|
||||||
var lstProfile = await AppHandler.Instance.ProfileItems(subId);
|
var lstProfile = await AppHandler.Instance.ProfileItems(subId);
|
||||||
|
if (lstProfile == null)
|
||||||
|
{
|
||||||
|
return new Tuple<int, int>(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
List<ProfileItem> lstKeep = new();
|
List<ProfileItem> lstKeep = new();
|
||||||
List<ProfileItem> lstRemove = new();
|
List<ProfileItem> lstRemove = new();
|
||||||
if (!config.GuiItem.KeepOlderDedupl)
|
if (!config.GuiItem.KeepOlderDedupl)
|
||||||
|
{
|
||||||
lstProfile.Reverse();
|
lstProfile.Reverse();
|
||||||
|
}
|
||||||
|
|
||||||
foreach (ProfileItem item in lstProfile)
|
foreach (var item in lstProfile)
|
||||||
{
|
{
|
||||||
if (!lstKeep.Exists(i => CompareProfileItem(i, item, false)))
|
if (!lstKeep.Exists(i => CompareProfileItem(i, item, false)))
|
||||||
{
|
{
|
||||||
|
@ -944,35 +950,35 @@ namespace ServiceLib.Handler
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool CompareProfileItem(ProfileItem o, ProfileItem n, bool remarks)
|
private static bool CompareProfileItem(ProfileItem? o, ProfileItem? n, bool remarks)
|
||||||
{
|
{
|
||||||
if (o == null || n == null)
|
if (o == null || n == null)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return o.ConfigType == n.ConfigType
|
||||||
|
&& AreEqual(o.Address, n.Address)
|
||||||
|
&& o.Port == n.Port
|
||||||
|
&& AreEqual(o.Id, n.Id)
|
||||||
|
&& AreEqual(o.Security, n.Security)
|
||||||
|
&& AreEqual(o.Network, n.Network)
|
||||||
|
&& AreEqual(o.HeaderType, n.HeaderType)
|
||||||
|
&& AreEqual(o.RequestHost, n.RequestHost)
|
||||||
|
&& AreEqual(o.Path, n.Path)
|
||||||
|
&& (o.ConfigType == EConfigType.Trojan || o.StreamSecurity == n.StreamSecurity)
|
||||||
|
&& AreEqual(o.Flow, n.Flow)
|
||||||
|
&& AreEqual(o.Sni, n.Sni)
|
||||||
|
&& AreEqual(o.Alpn, n.Alpn)
|
||||||
|
&& AreEqual(o.Fingerprint, n.Fingerprint)
|
||||||
|
&& AreEqual(o.PublicKey, n.PublicKey)
|
||||||
|
&& AreEqual(o.ShortId, n.ShortId)
|
||||||
|
&& (!remarks || o.Remarks == n.Remarks);
|
||||||
|
|
||||||
static bool AreEqual(string? a, string? b)
|
static bool AreEqual(string? a, string? b)
|
||||||
{
|
{
|
||||||
return string.Equals(a, b) || (string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b));
|
return string.Equals(a, b) || (string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
return o.ConfigType == n.ConfigType
|
|
||||||
&& AreEqual(o.Address, n.Address)
|
|
||||||
&& o.Port == n.Port
|
|
||||||
&& AreEqual(o.Id, n.Id)
|
|
||||||
&& AreEqual(o.Security, n.Security)
|
|
||||||
&& AreEqual(o.Network, n.Network)
|
|
||||||
&& AreEqual(o.HeaderType, n.HeaderType)
|
|
||||||
&& AreEqual(o.RequestHost, n.RequestHost)
|
|
||||||
&& AreEqual(o.Path, n.Path)
|
|
||||||
&& (o.ConfigType == EConfigType.Trojan || o.StreamSecurity == n.StreamSecurity)
|
|
||||||
&& AreEqual(o.Flow, n.Flow)
|
|
||||||
&& AreEqual(o.Sni, n.Sni)
|
|
||||||
&& AreEqual(o.Alpn, n.Alpn)
|
|
||||||
&& AreEqual(o.Fingerprint, n.Fingerprint)
|
|
||||||
&& AreEqual(o.PublicKey, n.PublicKey)
|
|
||||||
&& AreEqual(o.ShortId, n.ShortId)
|
|
||||||
&& (!remarks || o.Remarks == n.Remarks);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<int> RemoveProfileItem(Config config, string indexId)
|
private static async Task<int> RemoveProfileItem(Config config, string indexId)
|
||||||
|
@ -1010,8 +1016,7 @@ namespace ServiceLib.Handler
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileName = configPath;
|
if (!File.Exists(configPath))
|
||||||
if (!File.Exists(fileName))
|
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -448,7 +448,7 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
private async Task<List<ProfileItem>?> GetProfileItems(bool latest)
|
private async Task<List<ProfileItem>?> GetProfileItems(bool latest)
|
||||||
{
|
{
|
||||||
var lstSelecteds = new List<ProfileItem>();
|
var lstSelected = new List<ProfileItem>();
|
||||||
if (SelectedProfiles == null || SelectedProfiles.Count <= 0)
|
if (SelectedProfiles == null || SelectedProfiles.Count <= 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
|
@ -462,16 +462,16 @@ namespace ServiceLib.ViewModels
|
||||||
var item = await AppHandler.Instance.GetProfileItem(profile.IndexId);
|
var item = await AppHandler.Instance.GetProfileItem(profile.IndexId);
|
||||||
if (item is not null)
|
if (item is not null)
|
||||||
{
|
{
|
||||||
lstSelecteds.Add(item);
|
lstSelected.Add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lstSelecteds = JsonUtils.Deserialize<List<ProfileItem>>(JsonUtils.Serialize(orderProfiles));
|
lstSelected = JsonUtils.Deserialize<List<ProfileItem>>(JsonUtils.Serialize(orderProfiles));
|
||||||
}
|
}
|
||||||
|
|
||||||
return lstSelecteds;
|
return lstSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task EditServerAsync(EConfigType eConfigType)
|
public async Task EditServerAsync(EConfigType eConfigType)
|
||||||
|
@ -509,8 +509,8 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
public async Task RemoveServerAsync()
|
public async Task RemoveServerAsync()
|
||||||
{
|
{
|
||||||
var lstSelecteds = await GetProfileItems(true);
|
var lstSelected = await GetProfileItems(true);
|
||||||
if (lstSelecteds == null)
|
if (lstSelected == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -518,11 +518,11 @@ namespace ServiceLib.ViewModels
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var exists = lstSelecteds.Exists(t => t.IndexId == _config.IndexId);
|
var exists = lstSelected.Exists(t => t.IndexId == _config.IndexId);
|
||||||
|
|
||||||
await ConfigHandler.RemoveServers(_config, lstSelecteds);
|
await ConfigHandler.RemoveServers(_config, lstSelected);
|
||||||
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
||||||
if (lstSelecteds.Count == _profileItems.Count)
|
if (lstSelected.Count == _profileItems.Count)
|
||||||
{
|
{
|
||||||
_profileItems.Clear();
|
_profileItems.Clear();
|
||||||
}
|
}
|
||||||
|
@ -536,19 +536,22 @@ namespace ServiceLib.ViewModels
|
||||||
private async Task RemoveDuplicateServer()
|
private async Task RemoveDuplicateServer()
|
||||||
{
|
{
|
||||||
var tuple = await ConfigHandler.DedupServerList(_config, _config.SubIndexId);
|
var tuple = await ConfigHandler.DedupServerList(_config, _config.SubIndexId);
|
||||||
RefreshServers();
|
if (tuple.Item1 > 0 || tuple.Item2 > 0)
|
||||||
Reload();
|
{
|
||||||
|
RefreshServers();
|
||||||
|
Reload();
|
||||||
|
}
|
||||||
NoticeHandler.Instance.Enqueue(string.Format(ResUI.RemoveDuplicateServerResult, tuple.Item1, tuple.Item2));
|
NoticeHandler.Instance.Enqueue(string.Format(ResUI.RemoveDuplicateServerResult, tuple.Item1, tuple.Item2));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CopyServer()
|
private async Task CopyServer()
|
||||||
{
|
{
|
||||||
var lstSelecteds = await GetProfileItems(false);
|
var lstSelected = await GetProfileItems(false);
|
||||||
if (lstSelecteds == null)
|
if (lstSelected == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (await ConfigHandler.CopyServer(_config, lstSelecteds) == 0)
|
if (await ConfigHandler.CopyServer(_config, lstSelected) == 0)
|
||||||
{
|
{
|
||||||
RefreshServers();
|
RefreshServers();
|
||||||
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
||||||
|
@ -564,7 +567,7 @@ namespace ServiceLib.ViewModels
|
||||||
await SetDefaultServer(SelectedProfile.IndexId);
|
await SetDefaultServer(SelectedProfile.IndexId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SetDefaultServer(string indexId)
|
public async Task SetDefaultServer(string? indexId)
|
||||||
{
|
{
|
||||||
if (indexId.IsNullOrEmpty())
|
if (indexId.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
|
@ -594,11 +597,7 @@ namespace ServiceLib.ViewModels
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (SelectedServer == null)
|
if (SelectedServer == null || SelectedServer.ID.IsNullOrEmpty())
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (SelectedServer.ID.IsNullOrEmpty())
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -624,13 +623,13 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
private async Task SetDefaultMultipleServer(ECoreType coreType)
|
private async Task SetDefaultMultipleServer(ECoreType coreType)
|
||||||
{
|
{
|
||||||
var lstSelecteds = await GetProfileItems(true);
|
var lstSelected = await GetProfileItems(true);
|
||||||
if (lstSelecteds == null)
|
if (lstSelected == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ret = await ConfigHandler.AddCustomServer4Multiple(_config, lstSelecteds, coreType);
|
var ret = await ConfigHandler.AddCustomServer4Multiple(_config, lstSelected, coreType);
|
||||||
if (ret.Success != true)
|
if (ret.Success != true)
|
||||||
{
|
{
|
||||||
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
|
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
|
||||||
|
@ -679,19 +678,18 @@ namespace ServiceLib.ViewModels
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var lstSelecteds = await GetProfileItems(true);
|
var lstSelected = await GetProfileItems(true);
|
||||||
if (lstSelecteds == null)
|
if (lstSelected == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await ConfigHandler.MoveToGroup(_config, lstSelecteds, SelectedMoveToGroup.Id);
|
await ConfigHandler.MoveToGroup(_config, lstSelected, SelectedMoveToGroup.Id);
|
||||||
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
||||||
|
|
||||||
RefreshServers();
|
RefreshServers();
|
||||||
SelectedMoveToGroup = null;
|
SelectedMoveToGroup = null;
|
||||||
SelectedMoveToGroup = new();
|
SelectedMoveToGroup = new();
|
||||||
//Reload();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task MoveServer(EMove eMove)
|
public async Task MoveServer(EMove eMove)
|
||||||
|
@ -703,7 +701,7 @@ namespace ServiceLib.ViewModels
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = _lstProfile.IndexOf(item);
|
var index = _lstProfile.IndexOf(item);
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
@ -732,14 +730,14 @@ namespace ServiceLib.ViewModels
|
||||||
{
|
{
|
||||||
SelectedProfiles = _profileItems;
|
SelectedProfiles = _profileItems;
|
||||||
}
|
}
|
||||||
var lstSelecteds = await GetProfileItems(false);
|
var lstSelected = await GetProfileItems(false);
|
||||||
if (lstSelecteds == null)
|
if (lstSelected == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_speedtestService ??= new SpeedtestService(_config, (SpeedTestResult result) => _updateView?.Invoke(EViewAction.DispatcherSpeedTest, result));
|
_speedtestService ??= new SpeedtestService(_config, (SpeedTestResult result) => _updateView?.Invoke(EViewAction.DispatcherSpeedTest, result));
|
||||||
_speedtestService?.RunLoop(actionType, lstSelecteds);
|
_speedtestService?.RunLoop(actionType, lstSelected);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ServerSpeedtestStop()
|
public void ServerSpeedtestStop()
|
||||||
|
@ -793,14 +791,14 @@ namespace ServiceLib.ViewModels
|
||||||
|
|
||||||
public async Task Export2ShareUrlAsync(bool blEncode)
|
public async Task Export2ShareUrlAsync(bool blEncode)
|
||||||
{
|
{
|
||||||
var lstSelecteds = await GetProfileItems(true);
|
var lstSelected = await GetProfileItems(true);
|
||||||
if (lstSelecteds == null)
|
if (lstSelected == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new();
|
StringBuilder sb = new();
|
||||||
foreach (var it in lstSelecteds)
|
foreach (var it in lstSelected)
|
||||||
{
|
{
|
||||||
var url = FmtHandler.GetShareUri(it);
|
var url = FmtHandler.GetShareUri(it);
|
||||||
if (url.IsNullOrEmpty())
|
if (url.IsNullOrEmpty())
|
||||||
|
|
Loading…
Reference in a new issue