Skip to content

Commit

Permalink
fix: dual stack network
Browse files Browse the repository at this point in the history
fix #188
  • Loading branch information
HMBSbige committed May 16, 2022
1 parent 6499acc commit c1c2142
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 58 deletions.
2 changes: 2 additions & 0 deletions NatTypeTester.ViewModels/NatTypeTesterViewModelModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}
}
37 changes: 25 additions & 12 deletions NatTypeTester.ViewModels/RFC3489ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dns.Net.Abstractions;
using Dns.Net.Clients;
using JetBrains.Annotations;
using Microsoft;
using NatTypeTester.Models;
Expand All @@ -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);
}

Expand All @@ -60,22 +58,37 @@ private async Task TestClassicNatTypeAsync(CancellationToken token)
}
};

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
{
Expand Down
28 changes: 22 additions & 6 deletions NatTypeTester.ViewModels/RFC5780ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dns.Net.Abstractions;
using Dns.Net.Clients;
using JetBrains.Annotations;
using Microsoft;
using NatTypeTester.Models;
Expand All @@ -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();
Expand All @@ -57,11 +58,27 @@ private async Task DiscoveryNatTypeAsync(CancellationToken token)
}
};

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))
Expand All @@ -72,7 +89,6 @@ private async Task DiscoveryNatTypeAsync(CancellationToken token)
try
{
await client.QueryAsync(token);
Result5389.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob
}
else
{
result = @"AUTO";
result = string.Empty;
}

return true;
Expand Down
54 changes: 27 additions & 27 deletions NatTypeTester/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -46,5 +46,5 @@
Duration="0:0:0.3" />
</ui:NavigationView>
</DockPanel>
</Grid>
</Grid>
</reactiveUi:ReactiveWindow>
14 changes: 13 additions & 1 deletion NatTypeTester/NatTypeTester.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\common.props" />

Expand All @@ -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" />
Expand All @@ -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 changes: 7 additions & 0 deletions NatTypeTester/Properties/DesignTimeResources.xaml
Original file line number Diff line number Diff line change
@@ -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>
12 changes: 8 additions & 4 deletions NatTypeTester/Views/RFC3489View.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion NatTypeTester/Views/RFC3489View.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public RFC3489View(RFC3489ViewModel viewModel)
{
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);

Expand Down
13 changes: 8 additions & 5 deletions NatTypeTester/Views/RFC5780View.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion NatTypeTester/Views/RFC5780View.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public RFC5780View(RFC5780ViewModel viewModel)

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);

Expand Down

0 comments on commit c1c2142

Please sign in to comment.