mirror of
https://github.com/2dust/v2rayN.git
synced 2026-10-27 23:55:00 +03:00
csharp_style_namespace_declarations = file_scoped
This commit is contained in:
@@ -5,151 +5,150 @@ using DynamicData.Binding;
|
||||
using ReactiveUI;
|
||||
using ReactiveUI.Fody.Helpers;
|
||||
|
||||
namespace ServiceLib.ViewModels
|
||||
namespace ServiceLib.ViewModels;
|
||||
|
||||
public class ClashConnectionsViewModel : MyReactiveObject
|
||||
{
|
||||
public class ClashConnectionsViewModel : MyReactiveObject
|
||||
private IObservableCollection<ClashConnectionModel> _connectionItems = new ObservableCollectionExtended<ClashConnectionModel>();
|
||||
public IObservableCollection<ClashConnectionModel> ConnectionItems => _connectionItems;
|
||||
|
||||
[Reactive]
|
||||
public ClashConnectionModel SelectedSource { get; set; }
|
||||
|
||||
public ReactiveCommand<Unit, Unit> ConnectionCloseCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> ConnectionCloseAllCmd { get; }
|
||||
|
||||
[Reactive]
|
||||
public string HostFilter { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public bool AutoRefresh { get; set; }
|
||||
|
||||
public ClashConnectionsViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
||||
{
|
||||
private IObservableCollection<ClashConnectionModel> _connectionItems = new ObservableCollectionExtended<ClashConnectionModel>();
|
||||
public IObservableCollection<ClashConnectionModel> ConnectionItems => _connectionItems;
|
||||
_config = AppHandler.Instance.Config;
|
||||
_updateView = updateView;
|
||||
AutoRefresh = _config.ClashUIItem.ConnectionsAutoRefresh;
|
||||
|
||||
[Reactive]
|
||||
public ClashConnectionModel SelectedSource { get; set; }
|
||||
var canEditRemove = this.WhenAnyValue(
|
||||
x => x.SelectedSource,
|
||||
selectedSource => selectedSource != null && selectedSource.Id.IsNotEmpty());
|
||||
|
||||
public ReactiveCommand<Unit, Unit> ConnectionCloseCmd { get; }
|
||||
public ReactiveCommand<Unit, Unit> ConnectionCloseAllCmd { get; }
|
||||
|
||||
[Reactive]
|
||||
public string HostFilter { get; set; }
|
||||
|
||||
[Reactive]
|
||||
public bool AutoRefresh { get; set; }
|
||||
|
||||
public ClashConnectionsViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
||||
this.WhenAnyValue(
|
||||
x => x.AutoRefresh,
|
||||
y => y == true)
|
||||
.Subscribe(c => { _config.ClashUIItem.ConnectionsAutoRefresh = AutoRefresh; });
|
||||
ConnectionCloseCmd = ReactiveCommand.CreateFromTask(async () =>
|
||||
{
|
||||
_config = AppHandler.Instance.Config;
|
||||
_updateView = updateView;
|
||||
AutoRefresh = _config.ClashUIItem.ConnectionsAutoRefresh;
|
||||
await ClashConnectionClose(false);
|
||||
}, canEditRemove);
|
||||
|
||||
var canEditRemove = this.WhenAnyValue(
|
||||
x => x.SelectedSource,
|
||||
selectedSource => selectedSource != null && selectedSource.Id.IsNotEmpty());
|
||||
ConnectionCloseAllCmd = ReactiveCommand.CreateFromTask(async () =>
|
||||
{
|
||||
await ClashConnectionClose(true);
|
||||
});
|
||||
|
||||
this.WhenAnyValue(
|
||||
x => x.AutoRefresh,
|
||||
y => y == true)
|
||||
.Subscribe(c => { _config.ClashUIItem.ConnectionsAutoRefresh = AutoRefresh; });
|
||||
ConnectionCloseCmd = ReactiveCommand.CreateFromTask(async () =>
|
||||
{
|
||||
await ClashConnectionClose(false);
|
||||
}, canEditRemove);
|
||||
_ = Init();
|
||||
}
|
||||
|
||||
ConnectionCloseAllCmd = ReactiveCommand.CreateFromTask(async () =>
|
||||
{
|
||||
await ClashConnectionClose(true);
|
||||
});
|
||||
private async Task Init()
|
||||
{
|
||||
await DelayTestTask();
|
||||
}
|
||||
|
||||
_ = Init();
|
||||
private async Task GetClashConnections()
|
||||
{
|
||||
var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync();
|
||||
if (ret == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
private async Task Init()
|
||||
_ = _updateView?.Invoke(EViewAction.DispatcherRefreshConnections, ret?.connections);
|
||||
}
|
||||
|
||||
public void RefreshConnections(List<ConnectionItem>? connections)
|
||||
{
|
||||
_connectionItems.Clear();
|
||||
|
||||
var dtNow = DateTime.Now;
|
||||
var lstModel = new List<ClashConnectionModel>();
|
||||
foreach (var item in connections ?? new())
|
||||
{
|
||||
await DelayTestTask();
|
||||
var host = $"{(item.metadata.host.IsNullOrEmpty() ? item.metadata.destinationIP : item.metadata.host)}:{item.metadata.destinationPort}";
|
||||
if (HostFilter.IsNotEmpty() && !host.Contains(HostFilter))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var model = new ClashConnectionModel
|
||||
{
|
||||
Id = item.id,
|
||||
Network = item.metadata.network,
|
||||
Type = item.metadata.type,
|
||||
Host = host,
|
||||
Time = (dtNow - item.start).TotalSeconds < 0 ? 1 : (dtNow - item.start).TotalSeconds,
|
||||
Elapsed = (dtNow - item.start).ToString(@"hh\:mm\:ss"),
|
||||
Chain = $"{item.rule} , {string.Join("->", item.chains ?? new())}"
|
||||
};
|
||||
|
||||
lstModel.Add(model);
|
||||
}
|
||||
if (lstModel.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
private async Task GetClashConnections()
|
||||
_connectionItems.AddRange(lstModel);
|
||||
}
|
||||
|
||||
public async Task ClashConnectionClose(bool all)
|
||||
{
|
||||
var id = string.Empty;
|
||||
if (!all)
|
||||
{
|
||||
var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync();
|
||||
if (ret == null)
|
||||
var item = SelectedSource;
|
||||
if (item is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_ = _updateView?.Invoke(EViewAction.DispatcherRefreshConnections, ret?.connections);
|
||||
id = item.Id;
|
||||
}
|
||||
|
||||
public void RefreshConnections(List<ConnectionItem>? connections)
|
||||
else
|
||||
{
|
||||
_connectionItems.Clear();
|
||||
}
|
||||
await ClashApiHandler.Instance.ClashConnectionClose(id);
|
||||
await GetClashConnections();
|
||||
}
|
||||
|
||||
var dtNow = DateTime.Now;
|
||||
var lstModel = new List<ClashConnectionModel>();
|
||||
foreach (var item in connections ?? new())
|
||||
public async Task DelayTestTask()
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
var numOfExecuted = 1;
|
||||
while (true)
|
||||
{
|
||||
var host = $"{(item.metadata.host.IsNullOrEmpty() ? item.metadata.destinationIP : item.metadata.host)}:{item.metadata.destinationPort}";
|
||||
if (HostFilter.IsNotEmpty() && !host.Contains(HostFilter))
|
||||
await Task.Delay(1000 * 5);
|
||||
numOfExecuted++;
|
||||
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var model = new ClashConnectionModel
|
||||
if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0)
|
||||
{
|
||||
Id = item.id,
|
||||
Network = item.metadata.network,
|
||||
Type = item.metadata.type,
|
||||
Host = host,
|
||||
Time = (dtNow - item.start).TotalSeconds < 0 ? 1 : (dtNow - item.start).TotalSeconds,
|
||||
Elapsed = (dtNow - item.start).ToString(@"hh\:mm\:ss"),
|
||||
Chain = $"{item.rule} , {string.Join("->", item.chains ?? new())}"
|
||||
};
|
||||
|
||||
lstModel.Add(model);
|
||||
}
|
||||
if (lstModel.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_connectionItems.AddRange(lstModel);
|
||||
}
|
||||
|
||||
public async Task ClashConnectionClose(bool all)
|
||||
{
|
||||
var id = string.Empty;
|
||||
if (!all)
|
||||
{
|
||||
var item = SelectedSource;
|
||||
if (item is null)
|
||||
{
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
id = item.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
_connectionItems.Clear();
|
||||
}
|
||||
await ClashApiHandler.Instance.ClashConnectionClose(id);
|
||||
await GetClashConnections();
|
||||
}
|
||||
|
||||
public async Task DelayTestTask()
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
var numOfExecuted = 1;
|
||||
while (true)
|
||||
if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0)
|
||||
{
|
||||
await Task.Delay(1000 * 5);
|
||||
numOfExecuted++;
|
||||
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_config.ClashUIItem.ConnectionsRefreshInterval <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (numOfExecuted % _config.ClashUIItem.ConnectionsRefreshInterval != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
await GetClashConnections();
|
||||
continue;
|
||||
}
|
||||
});
|
||||
await GetClashConnections();
|
||||
}
|
||||
});
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user