Skip to content

Commit

Permalink
Merge pull request #3471 from BeyondDimension/feature/ipv6check
Browse files Browse the repository at this point in the history
Feature/ipv6check
  • Loading branch information
rmbadmin authored Sep 24, 2024
2 parents de69402 + 1039180 commit aa44b13
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 367 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentAvalonia.Core;
using System.Reactive;

namespace BD.WTTS.Models;

public class ProxyDomainGroupViewModel : ReactiveObject
{
private readonly INetworkTestService networkTestService = INetworkTestService.Instance;

public string Name { get; set; } = string.Empty;

public string IconUrl { get; set; } = string.Empty;

public ReadOnlyCollection<ProxyDomainViewModel>? EnableProxyDomainVMs { get; set; }

public ReactiveCommand<Unit, Unit> ConnectTestCommand { get; set; }

public ProxyDomainGroupViewModel()
{
ConnectTestCommand = ReactiveCommand.CreateFromTask(async () =>
{
if (EnableProxyDomainVMs == null)
{
Toast.Show(ToastIcon.Warning, "没有可测试的项目");
return;
}
var tasks = EnableProxyDomainVMs.Select(async enableDomain =>
{
enableDomain.DelayMillseconds = "- ms";
var (success, delayMs) = await networkTestService.TestOpenUrlAsync(enableDomain.Url);
enableDomain.DelayMillseconds = success switch
{
true when delayMs > 20000 => "Timeout",
true => delayMs.ToString() + " ms",
false => "error",
};
});
await Task.WhenAll(tasks);
if (EnableProxyDomainVMs.Count(x => x.DelayMillseconds == "Timeout") == EnableProxyDomainVMs.Count)
{
Toast.Show(ToastIcon.Error, "当前测试项全部未通过,请检查网络链接状况,以及代理设置里的设置项是否正常。");
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Avalonia.Media;

namespace BD.WTTS.Models;

public class ProxyDomainViewModel : ReactiveObject
{
private readonly INetworkTestService networkTestService = INetworkTestService.Instance;

public string Name { get; }

public ProxyType ProxyType { get; }

public string Url { get; }

[Reactive]
public string DelayMillseconds { get; set; } = string.Empty;

[ObservableAsProperty]
public bool RetestButtonVisible { get; }

[ObservableAsProperty]
public IBrush DelayColor { get; } = null!;

public ReadOnlyCollection<ProxyDomainViewModel>? Children { get; }

public ProxyDomainViewModel(string name, ProxyType proxyType, string url, List<ProxyDomainViewModel>? children = null)
{
Name = name;
ProxyType = proxyType;
Url = url;
Children = children?.AsReadOnly();

this.WhenAnyValue(x => x.DelayMillseconds)
.Subscribe(delay => Children?.ForEach(child => child.DelayMillseconds = delay));

this.WhenAnyValue(x => x.DelayMillseconds)
.Select(d => d != string.Empty && d != "-")
.ToPropertyEx(this, x => x.RetestButtonVisible);

const int DelayMiddle = 1000;
this.WhenAnyValue(x => x.DelayMillseconds)
.Select(d => d switch
{
"Timeout" or "error" => Brushes.Red,
var s when s.Split(' ') is [var num, "ms"] && int.TryParse(num, out int ms)
=> ms switch
{
<= DelayMiddle => Brushes.Green,
> DelayMiddle => Brushes.Orange,
},
_ => Brushes.Gray,
})
.ToPropertyEx(this, x => x.DelayColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ public AcceleratorPageViewModel()
.IsExecuting
.ToPropertyEx(this, x => x.IsDNSChecking);

IPv6CheckCommand = ReactiveCommand.CreateFromTask(async () =>
{
var result = await IMicroServiceClient.Instance.Accelerate.GetMyIP(ipV6: true);
if (result.IsSuccess)
{
IsSupportIPv6 = true;
IPv6Address = result.Content ?? string.Empty;
}
else
{
IsSupportIPv6 = false;
IPv6Address = string.Empty;
}
});
IPv6CheckCommand
.IsExecuting
.ToPropertyEx(this, x => x.IsIPv6Checking);

ProxySettingsCommand = ReactiveCommand.Create(() =>
{
var vm = new ProxySettingsWindowViewModel();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,95 +1,7 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using System.Reactive;

namespace BD.WTTS.UI.ViewModels;

public class ProxyDomainGroupViewModel : ReactiveObject
{
private readonly INetworkTestService networkTestService = INetworkTestService.Instance;

public string Name { get; set; } = string.Empty;

public string IconUrl { get; set; } = string.Empty;

public ReadOnlyCollection<ProxyDomainViewModel>? EnableProxyDomainVMs { get; set; }

public ReactiveCommand<Unit, Unit> ConnectTestCommand { get; set; }

public ProxyDomainGroupViewModel()
{
ConnectTestCommand = ReactiveCommand.CreateFromTask(async () =>
{
var tasks = EnableProxyDomainVMs!.Select(async enableDomain =>
{
enableDomain.DelayMillseconds = "- ms";
var (success, delayMs) = await networkTestService.TestOpenUrlAsync(enableDomain.Url);
enableDomain.DelayMillseconds = success switch
{
true when delayMs > 20000 => "Timeout",
true => delayMs.ToString() + " ms",
false => "error",
};
});
await Task.WhenAll(tasks);
});
}
}

public class ProxyDomainViewModel : ReactiveObject
{
private readonly INetworkTestService networkTestService = INetworkTestService.Instance;

public string Name { get; }

public ProxyType ProxyType { get; }

public string Url { get; }

[Reactive]
public string DelayMillseconds { get; set; } = string.Empty;

[ObservableAsProperty]
public bool RetestButtonVisible { get; }

[ObservableAsProperty]
public IBrush DelayColor { get; } = null!;

public ReadOnlyCollection<ProxyDomainViewModel>? Children { get; }

public ProxyDomainViewModel(string name, ProxyType proxyType, string url, List<ProxyDomainViewModel>? children = null)
{
Name = name;
ProxyType = proxyType;
Url = url;
Children = children?.AsReadOnly();

this.WhenAnyValue(x => x.DelayMillseconds)
.Subscribe(delay => Children?.ForEach(child => child.DelayMillseconds = delay));

this.WhenAnyValue(x => x.DelayMillseconds)
.Select(d => d != string.Empty && d != "-")
.ToPropertyEx(this, x => x.RetestButtonVisible);

const int DelayMiddle = 1000;
this.WhenAnyValue(x => x.DelayMillseconds)
.Select(d => d switch
{
"Timeout" or "error" => Brushes.Red,
var s when s.Split(' ') is [var num, "ms"] && int.TryParse(num, out int ms)
=> ms switch
{
<= DelayMiddle => Brushes.Green,
> DelayMiddle => Brushes.Orange,
},
_ => Brushes.Gray,
})
.ToPropertyEx(this, x => x.DelayColor);
}
}

public sealed partial class AcceleratorPageViewModel : TabItemViewModel
{
public enum NatTypeSimple
Expand Down Expand Up @@ -119,6 +31,9 @@ public enum NatTypeSimple
[ObservableAsProperty]
public bool IsDNSChecking { get; }

[ObservableAsProperty]
public bool IsIPv6Checking { get; }

[Reactive]
public string DomainPendingTest { get; set; } = string.Empty;

Expand All @@ -137,10 +52,18 @@ public enum NatTypeSimple
[Reactive]
public string DNSTestResult { get; set; } = string.Empty;

[Reactive]
public string IPv6Address { get; set; } = string.Empty;

[Reactive]
public bool IsSupportIPv6 { get; set; }

public ReactiveCommand<Unit, (NatTypeSimple Nat, bool PingSuccess)> NATCheckCommand { get; }

public ReactiveCommand<Unit, Unit> DNSCheckCommand { get; }

public ReactiveCommand<Unit, Unit> IPv6CheckCommand { get; }

public ICommand StartProxyCommand { get; }

public ICommand RefreshCommand { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ public ProxySettingsWindowViewModel()
DohAddres_360,
TUNA_DohAddres,
};

public void ResetSettings()
{
ProxySettings.SystemProxyIp.Reset();
ProxySettings.ProxyMasterDns.Reset();
ProxySettings.SystemProxyPortId.Reset();
ProxySettings.ProgramStartupRunProxy.Reset();
ProxySettings.EnableHttpProxyToHttps.Reset();
ProxySettings.UseDoh.Reset();
ProxySettings.CustomDohAddres2.Reset();
ProxySettings.OnlyEnableProxyScript.Reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</ItemsControl.ItemsPanel>

<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate x:DataType="spp:ProxyDomainGroupViewModel">
<Border>
<ui:SettingsExpander IsClickEnabled="False" IsExpanded="True">
<ui:SettingsExpander.Header>
Expand Down Expand Up @@ -77,7 +77,8 @@
DockPanel.Dock="Right"
Foreground="{Binding DelayColor}"
Text="{Binding DelayMillseconds}"
ToolTip.Tip="此数值代表测试网页打开访问所需时间,仅供参考" />
ToolTip.ShowDelay="0"
ToolTip.Tip="此数值代表测试网页打开访问所需时间,请以实际浏览器访问网页状况为准" />
</DockPanel>
</TreeDataTemplate>
</TreeView.ItemTemplate>
Expand Down
Loading

0 comments on commit aa44b13

Please sign in to comment.