Bläddra i källkod

fix: dual stack network

fix #188
Bruce Wayne 3 år sedan
förälder
incheckning
c1c214298f

+ 2 - 0
NatTypeTester.ViewModels/NatTypeTesterViewModelModule.cs

@@ -12,5 +12,7 @@ public class NatTypeTesterViewModelModule : AbpModule
 	public override void ConfigureServices(ServiceConfigurationContext context)
 	{
 		context.Services.TryAddTransient<IDnsClient, DefaultDnsClient>();
+		context.Services.TryAddTransient<DefaultAClient>();
+		context.Services.TryAddTransient<DefaultAAAAClient>();
 	}
 }

+ 25 - 12
NatTypeTester.ViewModels/RFC3489ViewModel.cs

@@ -1,4 +1,5 @@
 using Dns.Net.Abstractions;
+using Dns.Net.Clients;
 using JetBrains.Annotations;
 using Microsoft;
 using NatTypeTester.Models;
@@ -24,19 +25,16 @@ public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
 	private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
 	private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
+	private IDnsClient AAAADnsClient => LazyServiceProvider.LazyGetRequiredService<DefaultAAAAClient>();
+	private IDnsClient ADnsClient => LazyServiceProvider.LazyGetRequiredService<DefaultAClient>();
 
 	public ClassicStunResult Result3489 { get; set; }
 
 	public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
 
-	private static readonly IPEndPoint DefaultLocalEndpoint = new(IPAddress.Any, 0);
-
 	public RFC3489ViewModel()
 	{
-		Result3489 = new ClassicStunResult
-		{
-			LocalEndPoint = DefaultLocalEndpoint
-		};
+		Result3489 = new ClassicStunResult();
 		TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeAsync);
 	}
 
@@ -60,22 +58,37 @@ public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
 			}
 		};
 
-		Result3489.LocalEndPoint ??= DefaultLocalEndpoint;
+		IPAddress? serverIp;
+		if (Result3489.LocalEndPoint is null)
+		{
+			serverIp = await DnsClient.QueryAsync(server.Hostname, token);
+			Result3489.LocalEndPoint = serverIp.AddressFamily is AddressFamily.InterNetworkV6 ? new IPEndPoint(IPAddress.IPv6Any, IPEndPoint.MinPort) : new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
+		}
+		else
+		{
+			if (Result3489.LocalEndPoint.AddressFamily is AddressFamily.InterNetworkV6)
+			{
+				serverIp = await AAAADnsClient.QueryAsync(server.Hostname, token);
+			}
+			else
+			{
+				serverIp = await ADnsClient.QueryAsync(server.Hostname, token);
+			}
+		}
+
 		using IUdpProxy proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result3489.LocalEndPoint, socks5Option);
 
-		IPAddress ip = await DnsClient.QueryAsync(server.Hostname, token);
-		using StunClient3489 client = new(new IPEndPoint(ip, server.Port), Result3489.LocalEndPoint, proxy);
+		using StunClient3489 client = new(new IPEndPoint(serverIp, server.Port), Result3489.LocalEndPoint, proxy);
 
 		Result3489 = client.State;
 		using (Observable.Interval(TimeSpan.FromSeconds(0.1))
-				   .ObserveOn(RxApp.MainThreadScheduler)
-				   .Subscribe(_ => this.RaisePropertyChanged(nameof(Result3489))))
+				.ObserveOn(RxApp.MainThreadScheduler)
+				.Subscribe(_ => this.RaisePropertyChanged(nameof(Result3489))))
 		{
 			await client.ConnectProxyAsync(token);
 			try
 			{
 				await client.QueryAsync(token);
-				Result3489.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
 			}
 			finally
 			{

+ 22 - 6
NatTypeTester.ViewModels/RFC5780ViewModel.cs

@@ -1,4 +1,5 @@
 using Dns.Net.Abstractions;
+using Dns.Net.Clients;
 using JetBrains.Annotations;
 using Microsoft;
 using NatTypeTester.Models;
@@ -24,13 +25,13 @@ public class RFC5780ViewModel : ViewModelBase, IRoutableViewModel
 	private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
 	private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
+	private IDnsClient AAAADnsClient => LazyServiceProvider.LazyGetRequiredService<DefaultAAAAClient>();
+	private IDnsClient ADnsClient => LazyServiceProvider.LazyGetRequiredService<DefaultAClient>();
 
 	public StunResult5389 Result5389 { get; set; }
 
 	public ReactiveCommand<Unit, Unit> DiscoveryNatType { get; }
 
-	private static readonly IPEndPoint DefaultLocalEndpoint = new(IPAddress.Any, 0);
-
 	public RFC5780ViewModel()
 	{
 		Result5389 = new StunResult5389();
@@ -57,11 +58,27 @@ public class RFC5780ViewModel : ViewModelBase, IRoutableViewModel
 			}
 		};
 
-		Result5389.LocalEndPoint ??= DefaultLocalEndpoint;
+		IPAddress? serverIp;
+		if (Result5389.LocalEndPoint is null)
+		{
+			serverIp = await DnsClient.QueryAsync(server.Hostname, token);
+			Result5389.LocalEndPoint = serverIp.AddressFamily is AddressFamily.InterNetworkV6 ? new IPEndPoint(IPAddress.IPv6Any, IPEndPoint.MinPort) : new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
+		}
+		else
+		{
+			if (Result5389.LocalEndPoint.AddressFamily is AddressFamily.InterNetworkV6)
+			{
+				serverIp = await AAAADnsClient.QueryAsync(server.Hostname, token);
+			}
+			else
+			{
+				serverIp = await ADnsClient.QueryAsync(server.Hostname, token);
+			}
+		}
+
 		using IUdpProxy proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result5389.LocalEndPoint, socks5Option);
 
-		IPAddress ip = await DnsClient.QueryAsync(server.Hostname, token);
-		using StunClient5389UDP client = new(new IPEndPoint(ip, server.Port), Result5389.LocalEndPoint, proxy);
+		using StunClient5389UDP client = new(new IPEndPoint(serverIp, server.Port), Result5389.LocalEndPoint, proxy);
 
 		Result5389 = client.State;
 		using (Observable.Interval(TimeSpan.FromSeconds(0.1))
@@ -72,7 +89,6 @@ public class RFC5780ViewModel : ViewModelBase, IRoutableViewModel
 			try
 			{
 				await client.QueryAsync(token);
-				Result5389.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
 			}
 			finally
 			{

+ 1 - 1
NatTypeTester.ViewModels/ValueConverters/StringToIPEndpointTypeConverter.cs

@@ -44,7 +44,7 @@ public class StringToIPEndpointTypeConverter : IBindingTypeConverter, ISingleton
 		}
 		else
 		{
-			result = @"AUTO";
+			result = string.Empty;
 		}
 
 		return true;

+ 27 - 27
NatTypeTester/MainWindow.xaml

@@ -1,32 +1,32 @@
 <reactiveUi:ReactiveWindow
-	x:TypeArguments="viewModels:MainWindowViewModel"
-	x:Class="NatTypeTester.MainWindow"
-	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-	xmlns:reactiveUi="http://reactiveui.net"
+    x:TypeArguments="viewModels:MainWindowViewModel"
+    x:Class="NatTypeTester.MainWindow"
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:reactiveUi="http://reactiveui.net"
     xmlns:viewModels="clr-namespace:NatTypeTester.ViewModels;assembly=NatTypeTester.ViewModels"
-	xmlns:ui="http://schemas.modernwpf.com/2019"
-	Title="NatTypeTester"
-	WindowStartupLocation="CenterScreen"
-	Height="480" Width="500"
-	MinHeight="480" MinWidth="500"
-	ui:WindowHelper.UseModernWindowStyle="True">
+    xmlns:ui="http://schemas.modernwpf.com/2019"
+    Title="NatTypeTester"
+    WindowStartupLocation="CenterScreen"
+    Height="480" Width="500"
+    MinHeight="480" MinWidth="500"
+    ui:WindowHelper.UseModernWindowStyle="True">
 
-	<Grid>
-		<DockPanel>
-			<ComboBox DockPanel.Dock="Top"
-				x:Name="ServersComboBox"
-				ui:ControlHelper.Header="STUN Server"
-				IsEditable="True"
-				SelectedIndex="0" VerticalContentAlignment="Center"
-				Margin="10,10"
-				HorizontalAlignment="Stretch">
-				<ComboBox.ItemTemplate>
-					<DataTemplate>
-						<TextBlock Text="{Binding}" />
-					</DataTemplate>
-				</ComboBox.ItemTemplate>
-			</ComboBox>
+    <Grid>
+        <DockPanel>
+            <ComboBox DockPanel.Dock="Top"
+                      x:Name="ServersComboBox"
+                      ui:ControlHelper.Header="STUN Server"
+                      IsEditable="True"
+                      SelectedIndex="0" VerticalContentAlignment="Center"
+                      Margin="10,10"
+                      HorizontalAlignment="Stretch">
+                <ComboBox.ItemTemplate>
+                    <DataTemplate>
+                        <TextBlock Text="{Binding}" />
+                    </DataTemplate>
+                </ComboBox.ItemTemplate>
+            </ComboBox>
             <ui:NavigationView
                 x:Name="NavigationView"
                 IsBackButtonVisible="Collapsed"
@@ -46,5 +46,5 @@
                     Duration="0:0:0.3" />
             </ui:NavigationView>
         </DockPanel>
-	</Grid>
+    </Grid>
 </reactiveUi:ReactiveWindow>

+ 13 - 1
NatTypeTester/NatTypeTester.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <Import Project="..\common.props" />
 
@@ -10,6 +10,10 @@
     <ApplicationIcon>icon.ico</ApplicationIcon>
   </PropertyGroup>
 
+  <ItemGroup>
+    <Page Remove="Properties\DesignTimeResources.xaml" />
+  </ItemGroup>
+
   <ItemGroup>
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
     <PackageReference Include="ModernWpfUI" Version="0.9.4" />
@@ -23,4 +27,12 @@
     <ProjectReference Include="..\NatTypeTester.ViewModels\NatTypeTester.ViewModels.csproj" />
   </ItemGroup>
 
+  <ItemGroup>
+    <None Include="Properties\DesignTimeResources.xaml">
+      <ContainsDesignTimeResources>True</ContainsDesignTimeResources>
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </None>
+  </ItemGroup>
+
 </Project>

+ 7 - 0
NatTypeTester/Properties/DesignTimeResources.xaml

@@ -0,0 +1,7 @@
+<ResourceDictionary
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:ui="http://schemas.modernwpf.com/2019">
+    <ResourceDictionary.MergedDictionaries>
+        <ui:IntellisenseResources Source="/ModernWpf;component/DesignTime/DesignTimeResources.xaml" />
+    </ResourceDictionary.MergedDictionaries>
+</ResourceDictionary>

+ 8 - 4
NatTypeTester/Views/RFC3489View.xaml

@@ -22,10 +22,14 @@
                  Margin="10,5" IsReadOnly="True"
                  VerticalContentAlignment="Center" VerticalAlignment="Center"
                  ui:ControlHelper.Header="NAT type" />
-        <TextBox x:Name="LocalEndTextBox" Grid.Row="1"
-                 Margin="10,5"
-                 VerticalContentAlignment="Center" VerticalAlignment="Center"
-                 ui:ControlHelper.Header="Local end" />
+        <ComboBox x:Name="LocalEndComboBox" Grid.Row="1"
+                  Margin="10,5"
+                  IsEditable="True" HorizontalAlignment="Stretch"
+                  VerticalContentAlignment="Center" VerticalAlignment="Center"
+                  ui:ControlHelper.Header="Local end">
+            <ComboBoxItem>0.0.0.0:0</ComboBoxItem>
+            <ComboBoxItem>[::]:0</ComboBoxItem>
+        </ComboBox>
         <TextBox x:Name="PublicEndTextBox" Grid.Row="2"
                  Margin="10,5" IsReadOnly="True"
                  VerticalContentAlignment="Center" VerticalAlignment="Center"

+ 3 - 1
NatTypeTester/Views/RFC3489View.xaml.cs

@@ -23,7 +23,9 @@ public partial class RFC3489View : ITransientDependency
 		{
 			this.OneWayBind(ViewModel, vm => vm.Result3489.NatType, v => v.NatTypeTextBox.Text).DisposeWith(d);
 
-			this.Bind(ViewModel, vm => vm.Result3489.LocalEndPoint, v => v.LocalEndTextBox.Text).DisposeWith(d);
+			this.Bind(ViewModel, vm => vm.Result3489.LocalEndPoint, v => v.LocalEndComboBox.Text).DisposeWith(d);
+
+			LocalEndComboBox.Events().LostKeyboardFocus.Subscribe(_ => LocalEndComboBox.Text = ViewModel.Result3489.LocalEndPoint?.ToString() ?? string.Empty).DisposeWith(d);
 
 			this.OneWayBind(ViewModel, vm => vm.Result3489.PublicEndPoint, v => v.PublicEndTextBox.Text).DisposeWith(d);
 

+ 8 - 5
NatTypeTester/Views/RFC5780View.xaml

@@ -34,11 +34,14 @@
             Margin="10,5" IsReadOnly="True"
             VerticalContentAlignment="Center" VerticalAlignment="Center"
             ui:ControlHelper.Header="Filtering behavior" />
-        <TextBox
-            x:Name="LocalAddressTextBox" Grid.Row="3"
-            Margin="10,5" IsReadOnly="False"
-            VerticalContentAlignment="Center" VerticalAlignment="Center"
-            ui:ControlHelper.Header="Local end" />
+        <ComboBox x:Name="LocalAddressComboBox" Grid.Row="3"
+                  Margin="10,5"
+                  IsEditable="True" HorizontalAlignment="Stretch"
+                  VerticalContentAlignment="Center" VerticalAlignment="Center"
+                  ui:ControlHelper.Header="Local end">
+            <ComboBoxItem>0.0.0.0:0</ComboBoxItem>
+            <ComboBoxItem>[::]:0</ComboBoxItem>
+        </ComboBox>
         <TextBox
             x:Name="MappingAddressTextBox" Grid.Row="4"
             Margin="10,5" IsReadOnly="True"

+ 3 - 1
NatTypeTester/Views/RFC5780View.xaml.cs

@@ -27,7 +27,9 @@ public partial class RFC5780View : ITransientDependency
 
 			this.OneWayBind(ViewModel, vm => vm.Result5389.FilteringBehavior, v => v.FilteringBehaviorTextBox.Text).DisposeWith(d);
 
-			this.Bind(ViewModel, vm => vm.Result5389.LocalEndPoint, v => v.LocalAddressTextBox.Text).DisposeWith(d);
+			this.Bind(ViewModel, vm => vm.Result5389.LocalEndPoint, v => v.LocalAddressComboBox.Text).DisposeWith(d);
+
+			LocalAddressComboBox.Events().LostKeyboardFocus.Subscribe(_ => LocalAddressComboBox.Text = ViewModel.Result5389.LocalEndPoint?.ToString() ?? string.Empty).DisposeWith(d);
 
 			this.OneWayBind(ViewModel, vm => vm.Result5389.PublicEndPoint, v => v.MappingAddressTextBox.Text).DisposeWith(d);