AI-optimized code
Some checks failed
release Linux / build (Release) (push) Has been cancelled
release macOS / build (Release) (push) Has been cancelled
release Windows desktop (Avalonia UI) / build (Release) (push) Has been cancelled
release Windows / build (Release) (push) Has been cancelled

This commit is contained in:
2dust 2025-03-07 12:11:19 +08:00
parent bcf43e2928
commit e6011cfede
7 changed files with 54 additions and 63 deletions

View file

@ -11,9 +11,9 @@ namespace ServiceLib.Handler
private Dictionary<string, ProxiesItem>? _proxies; private Dictionary<string, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; } public Dictionary<string, object> ProfileContent { get; set; }
public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync(Config config) public async Task<Tuple<ClashProxies, ClashProviders>?> GetClashProxiesAsync()
{ {
for (var i = 0; i < 5; i++) for (var i = 0; i < 3; i++)
{ {
var url = $"{GetApiUrl()}/proxies"; var url = $"{GetApiUrl()}/proxies";
var result = await HttpClientHelper.Instance.TryGetAsync(url); var result = await HttpClientHelper.Instance.TryGetAsync(url);
@ -41,34 +41,26 @@ namespace ServiceLib.Handler
{ {
if (blAll) if (blAll)
{ {
for (var i = 0; i < 5; i++)
{
if (_proxies != null)
{
break;
}
await Task.Delay(5000);
}
if (_proxies == null) if (_proxies == null)
{ {
return; await GetClashProxiesAsync();
} }
lstProxy = new List<ClashProxyModel>(); lstProxy = new List<ClashProxyModel>();
foreach (var kv in _proxies) foreach (var kv in _proxies ?? [])
{ {
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower())) if (Global.notAllowTestType.Contains(kv.Value.type?.ToLower()))
{ {
continue; continue;
} }
lstProxy.Add(new ClashProxyModel() lstProxy.Add(new ClashProxyModel()
{ {
Name = kv.Value.name, Name = kv.Value.name,
Type = kv.Value.type.ToLower(), Type = kv.Value.type?.ToLower(),
}); });
} }
} }
if (lstProxy == null) if (lstProxy is not { Count: > 0 })
{ {
return; return;
} }
@ -157,7 +149,7 @@ namespace ServiceLib.Handler
} }
} }
public async Task<ClashConnections?> GetClashConnectionsAsync(Config config) public async Task<ClashConnections?> GetClashConnectionsAsync()
{ {
try try
{ {

View file

@ -582,7 +582,7 @@ namespace ServiceLib.Services.CoreConfig
var it = JsonUtils.DeepCopy(rule); var it = JsonUtils.DeepCopy(rule);
it.ip = null; it.ip = null;
it.type = "field"; it.type = "field";
for (int k = it.domain.Count - 1; k >= 0; k--) for (var k = it.domain.Count - 1; k >= 0; k--)
{ {
if (it.domain[k].StartsWith("#")) if (it.domain[k].StartsWith("#"))
{ {

View file

@ -74,6 +74,14 @@ namespace ServiceLib.ViewModels
} }
private async Task CheckUpdate() private async Task CheckUpdate()
{
await Task.Run(async () =>
{
await CheckUpdateTask();
});
}
private async Task CheckUpdateTask()
{ {
_lstUpdated.Clear(); _lstUpdated.Clear();
_lstUpdated = _checkUpdateModel.Where(x => x.IsSelected == true) _lstUpdated = _checkUpdateModel.Where(x => x.IsSelected == true)

View file

@ -58,7 +58,7 @@ namespace ServiceLib.ViewModels
private async Task GetClashConnections() private async Task GetClashConnections()
{ {
var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync(_config); var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync();
if (ret == null) if (ret == null)
{ {
return; return;

View file

@ -13,7 +13,7 @@ namespace ServiceLib.ViewModels
{ {
private Dictionary<string, ProxiesItem>? _proxies; private Dictionary<string, ProxiesItem>? _proxies;
private Dictionary<string, ProvidersItem>? _providers; private Dictionary<string, ProvidersItem>? _providers;
private int _delayTimeout = 99999999; private readonly int _delayTimeout = 99999999;
private IObservableCollection<ClashProxyModel> _proxyGroups = new ObservableCollectionExtended<ClashProxyModel>(); private IObservableCollection<ClashProxyModel> _proxyGroups = new ObservableCollectionExtended<ClashProxyModel>();
private IObservableCollection<ClashProxyModel> _proxyDetails = new ObservableCollectionExtended<ClashProxyModel>(); private IObservableCollection<ClashProxyModel> _proxyDetails = new ObservableCollectionExtended<ClashProxyModel>();
@ -28,8 +28,8 @@ namespace ServiceLib.ViewModels
public ClashProxyModel SelectedDetail { get; set; } public ClashProxyModel SelectedDetail { get; set; }
public ReactiveCommand<Unit, Unit> ProxiesReloadCmd { get; } public ReactiveCommand<Unit, Unit> ProxiesReloadCmd { get; }
public ReactiveCommand<Unit, Unit> ProxiesDelaytestCmd { get; } public ReactiveCommand<Unit, Unit> ProxiesDelayTestCmd { get; }
public ReactiveCommand<Unit, Unit> ProxiesDelaytestPartCmd { get; } public ReactiveCommand<Unit, Unit> ProxiesDelayTestPartCmd { get; }
public ReactiveCommand<Unit, Unit> ProxiesSelectActivityCmd { get; } public ReactiveCommand<Unit, Unit> ProxiesSelectActivityCmd { get; }
[Reactive] [Reactive]
@ -50,12 +50,12 @@ namespace ServiceLib.ViewModels
{ {
await ProxiesReload(); await ProxiesReload();
}); });
ProxiesDelaytestCmd = ReactiveCommand.CreateFromTask(async () => ProxiesDelayTestCmd = ReactiveCommand.CreateFromTask(async () =>
{ {
await ProxiesDelayTest(true); await ProxiesDelayTest(true);
}); });
ProxiesDelaytestPartCmd = ReactiveCommand.CreateFromTask(async () => ProxiesDelayTestPartCmd = ReactiveCommand.CreateFromTask(async () =>
{ {
await ProxiesDelayTest(false); await ProxiesDelayTest(false);
}); });
@ -78,7 +78,7 @@ namespace ServiceLib.ViewModels
this.WhenAnyValue( this.WhenAnyValue(
x => x.RuleModeSelected, x => x.RuleModeSelected,
y => y >= 0) y => y >= 0)
.Subscribe(async c => await DoRulemodeSelected(c)); .Subscribe(async c => await DoRuleModeSelected(c));
this.WhenAnyValue( this.WhenAnyValue(
x => x.SortingSelected, x => x.SortingSelected,
@ -98,7 +98,7 @@ namespace ServiceLib.ViewModels
_ = DelayTestTask(); _ = DelayTestTask();
} }
private async Task DoRulemodeSelected(bool c) private async Task DoRuleModeSelected(bool c)
{ {
if (!c) if (!c)
{ {
@ -158,7 +158,7 @@ namespace ServiceLib.ViewModels
private async Task GetClashProxies(bool refreshUI) private async Task GetClashProxies(bool refreshUI)
{ {
var ret = await ClashApiHandler.Instance.GetClashProxiesAsync(_config); var ret = await ClashApiHandler.Instance.GetClashProxiesAsync();
if (ret?.Item1 == null || ret.Item2 == null) if (ret?.Item1 == null || ret.Item2 == null)
{ {
return; return;
@ -255,29 +255,23 @@ namespace ServiceLib.ViewModels
} }
_proxies.TryGetValue(name, out var proxy); _proxies.TryGetValue(name, out var proxy);
if (proxy == null || proxy.all == null) if (proxy?.all == null)
{ {
return; return;
} }
var lstDetails = new List<ClashProxyModel>(); var lstDetails = new List<ClashProxyModel>();
foreach (var item in proxy.all) foreach (var item in proxy.all)
{ {
var IsActive = item == proxy.now;
var proxy2 = TryGetProxy(item); var proxy2 = TryGetProxy(item);
if (proxy2 == null) if (proxy2 == null)
{ {
continue; continue;
} }
int delay = -1; var delay = proxy2.history?.Count > 0 ? proxy2.history.Last().delay : -1;
if (proxy2.history.Count > 0)
{
delay = proxy2.history[proxy2.history.Count - 1].delay;
}
lstDetails.Add(new ClashProxyModel() lstDetails.Add(new ClashProxyModel()
{ {
IsActive = IsActive, IsActive = item == proxy.now,
Name = item, Name = item,
Type = proxy2.type, Type = proxy2.type,
Delay = delay <= 0 ? _delayTimeout : delay, Delay = delay <= 0 ? _delayTimeout : delay,
@ -374,12 +368,7 @@ namespace ServiceLib.ViewModels
{ {
ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) => ClashApiHandler.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) =>
{ {
if (item == null) if (item == null || result.IsNullOrEmpty())
{
await GetClashProxies(true);
return;
}
if (result.IsNullOrEmpty())
{ {
return; return;
} }
@ -393,26 +382,28 @@ namespace ServiceLib.ViewModels
{ {
//UpdateHandler(false, $"{item.name}={result}"); //UpdateHandler(false, $"{item.name}={result}");
var detail = _proxyDetails.FirstOrDefault(it => it.Name == result.IndexId); var detail = _proxyDetails.FirstOrDefault(it => it.Name == result.IndexId);
if (detail != null) if (detail == null)
{ {
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result.Delay); return;
if (dicResult != null && dicResult.TryGetValue("delay", out var value))
{
detail.Delay = Convert.ToInt32(value.ToString());
detail.DelayName = $"{detail.Delay}ms";
}
else if (dicResult != null && dicResult.TryGetValue("message", out var value1))
{
detail.Delay = _delayTimeout;
detail.DelayName = $"{value1}";
}
else
{
detail.Delay = _delayTimeout;
detail.DelayName = string.Empty;
}
_proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail));
} }
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result.Delay);
if (dicResult != null && dicResult.TryGetValue("delay", out var value))
{
detail.Delay = Convert.ToInt32(value.ToString());
detail.DelayName = $"{detail.Delay}ms";
}
else if (dicResult != null && dicResult.TryGetValue("message", out var value1))
{
detail.Delay = _delayTimeout;
detail.DelayName = $"{value1}";
}
else
{
detail.Delay = _delayTimeout;
detail.DelayName = string.Empty;
}
_proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail));
} }
#endregion proxy function #endregion proxy function

View file

@ -27,9 +27,9 @@ namespace v2rayN.Desktop.Views
this.Bind(ViewModel, vm => vm.SelectedDetail, v => v.lstProxyDetails.SelectedItem).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedDetail, v => v.lstProxyDetails.SelectedItem).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesReloadCmd, v => v.menuProxiesReload).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesReloadCmd, v => v.menuProxiesReload).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesDelaytestCmd, v => v.menuProxiesDelaytest).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesDelayTestCmd, v => v.menuProxiesDelaytest).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesDelaytestPartCmd, v => v.menuProxiesDelaytestPart).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesDelayTestPartCmd, v => v.menuProxiesDelaytestPart).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesSelectActivityCmd, v => v.menuProxiesSelectActivity).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesSelectActivityCmd, v => v.menuProxiesSelectActivity).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.RuleModeSelected, v => v.cmbRulemode.SelectedIndex).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.RuleModeSelected, v => v.cmbRulemode.SelectedIndex).DisposeWith(disposables);

View file

@ -28,9 +28,9 @@ namespace v2rayN.Views
this.Bind(ViewModel, vm => vm.SelectedDetail, v => v.lstProxyDetails.SelectedItem).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedDetail, v => v.lstProxyDetails.SelectedItem).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesReloadCmd, v => v.menuProxiesReload).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesReloadCmd, v => v.menuProxiesReload).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesDelaytestCmd, v => v.menuProxiesDelaytest).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesDelayTestCmd, v => v.menuProxiesDelaytest).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesDelaytestPartCmd, v => v.menuProxiesDelaytestPart).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesDelayTestPartCmd, v => v.menuProxiesDelaytestPart).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ProxiesSelectActivityCmd, v => v.menuProxiesSelectActivity).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ProxiesSelectActivityCmd, v => v.menuProxiesSelectActivity).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.RuleModeSelected, v => v.cmbRulemode.SelectedIndex).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.RuleModeSelected, v => v.cmbRulemode.SelectedIndex).DisposeWith(disposables);