Browse Source

Version 2.2

Close #3
Bruce Wayne 5 years ago
parent
commit
ed3a798a75

+ 1 - 1
NatTypeTester-Console/Program.cs

@@ -7,7 +7,7 @@ namespace NatTypeTester_Console
 	{
 		private static void Main(string[] args)
 		{
-			var server = @"stun.miwifi.com";
+			var server = @"stun.qq.com";
 			ushort port = 3478;
 			if (args.Length > 0)
 			{

+ 0 - 23
NatTypeTester/Controls/NumberUpDown.xaml

@@ -1,23 +0,0 @@
-<UserControl x:Class="NatTypeTester.Controls.NumberUpDown"
-             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             mc:Ignorable="d"
-             x:Name="Uc"
-             Loaded="UserControl_Loaded">
-    <Grid LostFocus="Grid_LostFocus">
-        <Grid.RowDefinitions>
-            <RowDefinition Height="1*" />
-            <RowDefinition Height="1*" />
-        </Grid.RowDefinitions>
-        <Grid.ColumnDefinitions>
-            <ColumnDefinition Width="*" />
-            <ColumnDefinition Width="12" />
-        </Grid.ColumnDefinitions>
-        <TextBox x:FieldModifier="private" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" x:Name="TxtNum"
-        Text="{Binding Value , Mode=TwoWay, ElementName=Uc,UpdateSourceTrigger=PropertyChanged}" TextChanged="TxtNum_TextChanged" VerticalContentAlignment="Center"/>
-        <RepeatButton Grid.Row="0" Grid.Column="1" Content="▲" Click="Up_Click" FontSize="6" />
-        <RepeatButton Grid.Row="1" Grid.Column="1" Content="▼" Click="Down_Click" FontSize="6" />
-    </Grid>
-</UserControl>

+ 0 - 114
NatTypeTester/Controls/NumberUpDown.xaml.cs

@@ -1,114 +0,0 @@
-using System;
-using System.Windows;
-using System.Windows.Controls;
-
-namespace NatTypeTester.Controls
-{
-	public partial class NumberUpDown
-	{
-		private int _numValue;
-
-		private int _maxNum = 65535;
-
-		private int _minNum;
-
-		public int NumValue
-		{
-			get
-			{
-				if (_numValue > _maxNum)
-				{
-					return _maxNum;
-				}
-				if (_numValue < _minNum)
-				{
-					return _minNum;
-				}
-				return _numValue;
-			}
-			set
-			{
-				if (_numValue != value)
-				{
-					_numValue = value;
-					TxtNum.Text = value.ToString();
-					ValueChanged?.Invoke(this, new EventArgs());
-				}
-			}
-		}
-
-		public string Value
-		{
-			get => GetValue(ValueProperty) as string;
-			set => SetValue(ValueProperty, value);
-		}
-
-		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(@"Value", typeof(string), typeof(NumberUpDown));
-
-		public int MinNum
-		{
-			get => _minNum;
-			set => _minNum = value > _maxNum ? _maxNum : value;
-		}
-
-		public int MaxNum
-		{
-			get => _maxNum;
-			set => _maxNum = value < _minNum ? _minNum : value;
-		}
-
-		public event EventHandler ValueChanged;
-
-		public NumberUpDown()
-		{
-			InitializeComponent();
-		}
-
-		private void Up_Click(object sender, RoutedEventArgs e)
-		{
-			if (NumValue < _maxNum)
-			{
-				++NumValue;
-			}
-			else
-			{
-				NumValue = _maxNum;
-			}
-		}
-
-		private void Down_Click(object sender, RoutedEventArgs e)
-		{
-			if (NumValue > _minNum)
-			{
-				--NumValue;
-			}
-			else
-			{
-				NumValue = _minNum;
-			}
-		}
-
-		private void TxtNum_TextChanged(object sender, TextChangedEventArgs e)
-		{
-			if (TxtNum == null)
-			{
-				return;
-			}
-
-			if (int.TryParse(TxtNum.Text, out var num))
-			{
-				NumValue = num;
-			}
-		}
-
-		private void UserControl_Loaded(object sender, RoutedEventArgs e)
-		{
-			TxtNum.Text = NumValue.ToString();
-		}
-
-		private void Grid_LostFocus(object sender, RoutedEventArgs e)
-		{
-			TxtNum.Text = NumValue.ToString();
-		}
-	}
-}

+ 3 - 3
NatTypeTester/MainWindow.xaml

@@ -1,7 +1,6 @@
 <Window x:Class="NatTypeTester.MainWindow"
 		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-		xmlns:controls="clr-namespace:NatTypeTester.Controls"
 		Title="NatTypeTester" Height="210" Width="385" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize"
 		DataContext="{Binding RelativeSource={RelativeSource Self}}"
 		KeyDown="MainWindow_OnKeyDown">
@@ -36,8 +35,9 @@
 		<TextBlock Grid.Row="2" Grid.Column="0" Margin="5,0" VerticalAlignment="Center" Text="Local end" />
 		<TextBlock Grid.Row="3" Grid.Column="0" Margin="5,0" VerticalAlignment="Center" Text="Public end" />
 
-		<ComboBox x:Name="ServersComboBox" ItemsSource="{Binding StunServers}" Grid.Row="0" Grid.Column="1" Height="23.24" IsEditable="True" SelectedIndex="0" VerticalContentAlignment="Center" />
-		<controls:NumberUpDown x:Name="PortNumber" Grid.Row="0" Grid.Column="2" Height="23.24" Margin="5,0" MinNum="1" MaxNum="65535" NumValue="3478"/>
+		<ComboBox x:Name="ServersComboBox" ItemsSource="{Binding StunServers}" 
+					Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Height="23.24"
+					Margin="0,5,5,0" IsEditable="True" SelectedIndex="0" VerticalContentAlignment="Center" />
 		<TextBox x:Name="NatTypeTextBox" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Height="23.24" Margin="0,5,5,0"
 		IsReadOnly="True" VerticalContentAlignment="Center" />
 		<TextBox x:Name="LocalEndTextBox" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Height="23.24" Margin="0,5,5,0" 

+ 49 - 26
NatTypeTester/MainWindow.xaml.cs

@@ -1,5 +1,7 @@
-using NatTypeTester.Net;
-using System;
+using NatTypeTester.Model;
+using NatTypeTester.Net;
+using System.Collections.Generic;
+using System.IO;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Input;
@@ -11,41 +13,44 @@ namespace NatTypeTester
 		public MainWindow()
 		{
 			InitializeComponent();
+			LoadStunServer();
 		}
 
-		public static string[] StunServers { get; set; } =
+		public static HashSet<string> StunServers { get; set; } = new HashSet<string>
 		{
+				@"stun.qq.com",
 				@"stun.miwifi.com",
 				@"stun.bige0.com",
 				@"stun.syncthing.net",
-				@"stun.stunprotocol.org",
-				@"iphone-stun.strato-iphone.de",
-				@"stun.voipstunt.com",
-				@"stun.xten.com",
-				@"stun.schlund.de",
-				@"numb.viagenie.ca",
-				@"stun.ekiga.net",
-				@"stun.sipgate.net",
+				@"stun.stunprotocol.org"
 		};
 
-		private void TestButton_OnClick(object sender, RoutedEventArgs e)
+		private async void TestButton_OnClick(object sender, RoutedEventArgs e)
 		{
-			TestButton.IsEnabled = false;
-			var server = ServersComboBox.Text;
-			var port = PortNumber.NumValue;
-			var local = LocalEndTextBox.Text;
-			Task.Run(() =>
+			var stun = new StunServer();
+			if (stun.Parse(ServersComboBox.Text))
 			{
-				var (natType, localEnd, publicEnd) = NetUtils.NatTypeTestCore(local, server, port);
-
-				Dispatcher?.BeginInvoke(new Action(() =>
+				var server = stun.Hostname;
+				var port = stun.Port;
+				var local = LocalEndTextBox.Text;
+				TestButton.IsEnabled = false;
+				await Task.Run(() =>
 				{
-					NatTypeTextBox.Text = natType;
-					LocalEndTextBox.Text = localEnd;
-					PublicEndTextBox.Text = publicEnd;
-					TestButton.IsEnabled = true;
-				}));
-			});
+					var (natType, localEnd, publicEnd) = NetUtils.NatTypeTestCore(local, server, port);
+
+					Dispatcher?.InvokeAsync(() =>
+					{
+						NatTypeTextBox.Text = natType;
+						LocalEndTextBox.Text = localEnd;
+						PublicEndTextBox.Text = publicEnd;
+						TestButton.IsEnabled = true;
+					});
+				});
+			}
+			else
+			{
+				MessageBox.Show(@"Wrong Stun server!", @"NatTypeTester", MessageBoxButton.OK, MessageBoxImage.Error);
+			}
 		}
 
 		private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
@@ -55,5 +60,23 @@ namespace NatTypeTester
 				TestButton_OnClick(this, new RoutedEventArgs());
 			}
 		}
+
+		private async void LoadStunServer()
+		{
+			const string path = @"stun.txt";
+			if (File.Exists(path))
+			{
+				using var sw = new StreamReader(path);
+				string line;
+				var stun = new StunServer();
+				while ((line = await sw.ReadLineAsync()) != null)
+				{
+					if (!string.IsNullOrWhiteSpace(line) && stun.Parse(line))
+					{
+						StunServers.Add(stun.ToString());
+					}
+				}
+			}
+		}
 	}
 }

+ 44 - 0
NatTypeTester/Model/StunServer.cs

@@ -0,0 +1,44 @@
+namespace NatTypeTester.Model
+{
+	public class StunServer
+	{
+		public string Hostname;
+		public ushort Port;
+
+		public StunServer()
+		{
+			Hostname = @"stun.qq.com";
+			Port = 3478;
+		}
+
+		public bool Parse(string str)
+		{
+			var ipPort = str.Trim().Split(':', ':');
+			if (ipPort.Length == 2)
+			{
+				if (!string.IsNullOrWhiteSpace(ipPort[0]) && ushort.TryParse(ipPort[1], out var port))
+				{
+					Hostname = ipPort[0];
+					Port = port;
+					return true;
+				}
+			}
+			if (ipPort.Length == 1)
+			{
+				Hostname = ipPort[0];
+				Port = 3478;
+				return true;
+			}
+			return false;
+		}
+
+		public override string ToString()
+		{
+			if (Port == 3478)
+			{
+				return Hostname;
+			}
+			return $@"{Hostname}:{Port}";
+		}
+	}
+}

+ 2 - 2
NatTypeTester/NatTypeTester.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
+<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
 
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
@@ -6,7 +6,7 @@
     <UseWPF>true</UseWPF>
     <ApplicationIcon>icon.ico</ApplicationIcon>
     <LangVersion>8</LangVersion>
-    <Version>2.1</Version>
+    <Version>2.2</Version>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">