2024-06-27 08:36:36 +00:00
|
|
|
using DynamicData;
|
|
|
|
using DynamicData.Binding;
|
|
|
|
using ReactiveUI;
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
|
|
|
using System.Reactive;
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
2024-08-20 06:15:29 +00:00
|
|
|
namespace ServiceLib.ViewModels
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-08-10 02:02:00 +00:00
|
|
|
public class ClashConnectionsViewModel : MyReactiveObject
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
|
|
|
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; }
|
|
|
|
|
2024-09-17 06:50:31 +00:00
|
|
|
[Reactive]
|
|
|
|
public string HostFilter { get; set; }
|
|
|
|
|
2024-06-27 08:36:36 +00:00
|
|
|
[Reactive]
|
|
|
|
public bool AutoRefresh { get; set; }
|
|
|
|
|
2024-08-22 11:51:10 +00:00
|
|
|
public ClashConnectionsViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-10-07 01:51:41 +00:00
|
|
|
_config = AppHandler.Instance.Config;
|
2024-08-11 07:45:55 +00:00
|
|
|
_updateView = updateView;
|
2024-10-23 02:42:35 +00:00
|
|
|
AutoRefresh = _config.ClashUIItem.ConnectionsAutoRefresh;
|
2024-06-27 08:36:36 +00:00
|
|
|
|
|
|
|
var canEditRemove = this.WhenAnyValue(
|
|
|
|
x => x.SelectedSource,
|
2024-10-23 09:19:57 +00:00
|
|
|
selectedSource => selectedSource != null && Utils.IsNotEmpty(selectedSource.Id));
|
2024-06-27 08:36:36 +00:00
|
|
|
|
|
|
|
this.WhenAnyValue(
|
|
|
|
x => x.AutoRefresh,
|
|
|
|
y => y == true)
|
2024-10-23 02:42:35 +00:00
|
|
|
.Subscribe(c => { _config.ClashUIItem.ConnectionsAutoRefresh = AutoRefresh; });
|
2024-10-10 10:02:09 +00:00
|
|
|
ConnectionCloseCmd = ReactiveCommand.CreateFromTask(async () =>
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-10-10 10:02:09 +00:00
|
|
|
await ClashConnectionClose(false);
|
2024-06-27 08:36:36 +00:00
|
|
|
}, canEditRemove);
|
|
|
|
|
2024-10-10 10:02:09 +00:00
|
|
|
ConnectionCloseAllCmd = ReactiveCommand.CreateFromTask(async () =>
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-10-10 10:02:09 +00:00
|
|
|
await ClashConnectionClose(true);
|
2024-06-27 08:36:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2024-10-21 05:46:13 +00:00
|
|
|
private async Task Init()
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-06-28 08:14:19 +00:00
|
|
|
var lastTime = DateTime.Now;
|
|
|
|
|
2024-09-17 06:50:31 +00:00
|
|
|
Observable.Interval(TimeSpan.FromSeconds(5))
|
2024-10-21 05:46:13 +00:00
|
|
|
.Subscribe(async x =>
|
2024-06-28 08:14:19 +00:00
|
|
|
{
|
2024-10-23 02:42:35 +00:00
|
|
|
if (!(AutoRefresh && _config.UiItem.ShowInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
|
2024-06-28 08:14:19 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var dtNow = DateTime.Now;
|
2024-10-23 02:42:35 +00:00
|
|
|
if (_config.ClashUIItem.ConnectionsRefreshInterval > 0)
|
2024-06-28 08:14:19 +00:00
|
|
|
{
|
2024-10-23 02:42:35 +00:00
|
|
|
if ((dtNow - lastTime).Minutes % _config.ClashUIItem.ConnectionsRefreshInterval == 0)
|
2024-06-28 08:14:19 +00:00
|
|
|
{
|
2024-10-21 05:46:13 +00:00
|
|
|
await GetClashConnections();
|
2024-06-28 08:14:19 +00:00
|
|
|
lastTime = dtNow;
|
|
|
|
}
|
2024-08-18 07:04:56 +00:00
|
|
|
Task.Delay(1000).Wait();
|
2024-06-28 08:14:19 +00:00
|
|
|
}
|
|
|
|
});
|
2024-06-27 08:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-20 03:51:05 +00:00
|
|
|
private async Task GetClashConnections()
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-10-20 03:51:05 +00:00
|
|
|
var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync(_config);
|
|
|
|
if (ret == null)
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-10-20 03:51:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-06-27 08:36:36 +00:00
|
|
|
|
2024-10-20 03:51:05 +00:00
|
|
|
_updateView?.Invoke(EViewAction.DispatcherRefreshConnections, ret?.connections);
|
2024-06-27 08:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 07:45:55 +00:00
|
|
|
public void RefreshConnections(List<ConnectionItem>? connections)
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
|
|
|
_connectionItems.Clear();
|
|
|
|
|
|
|
|
var dtNow = DateTime.Now;
|
|
|
|
var lstModel = new List<ClashConnectionModel>();
|
2024-12-01 02:18:36 +00:00
|
|
|
foreach (var item in connections ?? new())
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
2024-09-17 08:52:41 +00:00
|
|
|
var host = $"{(Utils.IsNullOrEmpty(item.metadata.host) ? item.metadata.destinationIP : item.metadata.host)}:{item.metadata.destinationPort}";
|
2024-09-17 06:50:31 +00:00
|
|
|
if (HostFilter.IsNotEmpty() && !host.Contains(HostFilter))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-06-27 08:36:36 +00:00
|
|
|
ClashConnectionModel model = new();
|
|
|
|
|
2024-10-23 09:19:57 +00:00
|
|
|
model.Id = item.id;
|
|
|
|
model.Network = item.metadata.network;
|
|
|
|
model.Type = item.metadata.type;
|
|
|
|
model.Host = host;
|
2024-06-27 08:36:36 +00:00
|
|
|
var sp = (dtNow - item.start);
|
2024-10-23 09:19:57 +00:00
|
|
|
model.Time = sp.TotalSeconds < 0 ? 1 : sp.TotalSeconds;
|
|
|
|
model.Elapsed = sp.ToString(@"hh\:mm\:ss");
|
2024-12-01 02:18:36 +00:00
|
|
|
item.chains?.Reverse();
|
|
|
|
model.Chain = $"{item.rule} , {string.Join("->", item.chains ?? new())}";
|
2024-06-27 08:36:36 +00:00
|
|
|
|
|
|
|
lstModel.Add(model);
|
|
|
|
}
|
|
|
|
if (lstModel.Count <= 0) { return; }
|
|
|
|
|
|
|
|
_connectionItems.AddRange(lstModel);
|
|
|
|
}
|
|
|
|
|
2024-10-10 10:02:09 +00:00
|
|
|
public async Task ClashConnectionClose(bool all)
|
2024-06-27 08:36:36 +00:00
|
|
|
{
|
|
|
|
var id = string.Empty;
|
|
|
|
if (!all)
|
|
|
|
{
|
|
|
|
var item = SelectedSource;
|
|
|
|
if (item is null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-10-23 09:19:57 +00:00
|
|
|
id = item.Id;
|
2024-06-27 08:36:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_connectionItems.Clear();
|
|
|
|
}
|
2024-10-20 03:51:05 +00:00
|
|
|
await ClashApiHandler.Instance.ClashConnectionClose(id);
|
|
|
|
await GetClashConnections();
|
2024-06-27 08:36:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|