Skip to content

Commit

Permalink
✨ 网络测试页面域名检测输入检查
Browse files Browse the repository at this point in the history
  • Loading branch information
luojunyuan committed Sep 29, 2024
1 parent 95c10da commit 14216ab
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ public AcceleratorPageViewModel()
.IsExecuting
.ToPropertyEx(this, x => x.IsNATChecking);

var canDNSCheck = this.WhenAnyValue(x => x.DomainPendingTest)
.Select(domain => domain == string.Empty || DomainRegExp().IsMatch(domain));
DNSCheckCommand = ReactiveCommand.CreateFromTask(async () =>
{
var testDomain = DomainPendingTest == string.Empty ? "store.steampowered.com" : DomainPendingTest;
var testDomain = DomainPendingTest == string.Empty ? DefaultTestDomain : DomainPendingTest;
try
{
long delayMs;
Expand All @@ -124,7 +126,7 @@ public AcceleratorPageViewModel()
DNSTestDelay = string.Empty;
DNSTestResult = "error";
}
});
}, canDNSCheck);
DNSCheckCommand
.IsExecuting
.ToPropertyEx(this, x => x.IsDNSChecking);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ namespace BD.WTTS.UI.ViewModels;

public sealed partial class AcceleratorPageViewModel : TabItemViewModel
{
[GeneratedRegex(DomainValidationAttribute.RegPattern)]
private static partial Regex DomainRegExp();

public class DomainValidationAttribute : RegularExpressionAttribute
{
public const string RegPattern = @"^[^/]+\.[^/]{2,}$";

public override string FormatErrorMessage(string name) => "请填入不带分隔符 \"/\" 的域名";

public DomainValidationAttribute() : base(RegPattern)
{
}
}

public enum NatTypeSimple
{
Unknown,
Expand All @@ -14,6 +28,8 @@ public enum NatTypeSimple

public override string Name => Strings.Welcome;

public string DefaultTestDomain { get; } = "store.steampowered.com";

[Reactive]
public string SelectedSTUNAddress { get; set; }

Expand All @@ -34,6 +50,7 @@ public enum NatTypeSimple
[ObservableAsProperty]
public bool IsIPv6Checking { get; }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,50 @@
<TextBox
Width="220"
Text="{Binding DomainPendingTest, Mode=TwoWay}"
Watermark="store.steampowered.com" />
Watermark="{Binding DefaultTestDomain}">
<TextBox.Styles>
<Style Selector="DataValidationErrors">
<Setter Property="Template">
<ControlTemplate>
<DockPanel LastChildFill="True">
<ContentControl
Content="{Binding (DataValidationErrors.Errors)}"
ContentTemplate="{TemplateBinding ErrorTemplate}"
DataContext="{TemplateBinding Owner}"
DockPanel.Dock="Right"
IsVisible="{Binding (DataValidationErrors.HasErrors)}" />
<ContentPresenter
Name="PART_ContentPresenter"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
CornerRadius="{TemplateBinding CornerRadius}" />
</DockPanel>
</ControlTemplate>
</Setter>
<Setter Property="ErrorTemplate">
<DataTemplate x:DataType="{x:Type x:Object}">
<Canvas
Width="14"
Height="14"
Margin="4,0,1,0"
Background="Transparent">
<ToolTip.Tip>
<ItemsControl ItemsSource="{Binding}" />
</ToolTip.Tip>
<Path
Data="M14,7 A7,7 0 0,0 0,7 M0,7 A7,7 0 1,0 14,7 M7,3l0,5 M7,9l0,2"
Stroke="Red"
StrokeThickness="2" />
</Canvas>
</DataTemplate>
</Setter>
</Style>
</TextBox.Styles>
</TextBox>
</ui:SettingsExpanderItem.Footer>
</ui:SettingsExpanderItem>

Expand Down

0 comments on commit 14216ab

Please sign in to comment.