diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs
index b32f550b..c6c799d8 100644
--- a/Netch/Controllers/NTTController.cs
+++ b/Netch/Controllers/NTTController.cs
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
+using System.Threading.Tasks;
using Netch.Utils;
namespace Netch.Controllers
@@ -20,7 +21,7 @@ namespace Netch.Controllers
/// 启动 NatTypeTester
///
///
- public (string, string, string) Start()
+ public async Task<(string, string, string)> Start()
{
string localEnd = null;
string publicEnd = null;
@@ -33,19 +34,30 @@ namespace Netch.Controllers
Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;
Instance.Start();
- var output = Instance.StandardOutput.ReadToEnd();
+
+ var output = await Instance.StandardOutput.ReadToEndAsync();
+ var error = await Instance.StandardError.ReadToEndAsync();
+
try
{
- File.WriteAllText(Path.Combine(Global.NetchDir, $"logging\\{Name}.log"), output);
+ File.WriteAllText(Path.Combine(Global.NetchDir, $"logging\\{Name}.log"), $"{output}\r\n{error}");
}
catch (Exception e)
{
Logging.Warning($"写入 {Name} 日志错误:\n" + e.Message);
}
+ if (output.IsNullOrWhiteSpace())
+ if (!error.IsNullOrWhiteSpace())
+ {
+ error = error.Trim();
+ var errorFirst = error.Substring(0, error.IndexOf('\n')).Trim();
+ return (errorFirst.SplitTrimEntries(':').Last(), null, null);
+ }
+
foreach (var line in output.Split('\n'))
{
- var str = line.Split(':').Select(s => s.Trim()).ToArray();
+ var str = line.SplitTrimEntries(':');
if (str.Length < 2)
continue;
@@ -69,14 +81,11 @@ namespace Netch.Controllers
case "result":
result = value;
break;
- default:
- result = str.Last();
- break;
}
}
if (bindingTest == "Fail")
- result = "UdpBlocked";
+ result = "Fail";
return (result, localEnd, publicEnd);
}
diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs
index 858314eb..2e92692b 100644
--- a/Netch/Forms/MainForm.cs
+++ b/Netch/Forms/MainForm.cs
@@ -19,6 +19,13 @@ namespace Netch.Forms
{
public partial class MainForm : Form
{
+ private void createRouteTableModeToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ Hide();
+ new Route().ShowDialog();
+ Show();
+ }
+
#region Start
private readonly Dictionary _mainFormText = new();
@@ -1263,8 +1270,8 @@ namespace Netch.Forms
Task.Run(() =>
{
NatTypeStatusText(i18N.Translate("Starting NatTester"));
- // Thread.Sleep(1000);
- var (result, localEnd, publicEnd) = MainController.NTTController.Start();
+
+ var (result, localEnd, publicEnd) = MainController.NTTController.Start().Result;
if (!string.IsNullOrEmpty(publicEnd))
{
@@ -1583,12 +1590,5 @@ namespace Netch.Forms
#endregion
#endregion
-
- private void createRouteTableModeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Hide();
- new Route().ShowDialog();
- Show();
- }
}
}
\ No newline at end of file
diff --git a/Netch/Utils/DNS.cs b/Netch/Utils/DNS.cs
index 3f6d23cd..6ce62a38 100644
--- a/Netch/Utils/DNS.cs
+++ b/Netch/Utils/DNS.cs
@@ -75,7 +75,7 @@ namespace Netch.Utils
public static IEnumerable Split(string dns)
{
- return dns.Split(',').Where(ip => !string.IsNullOrWhiteSpace(ip)).Select(ip => ip.Trim());
+ return dns.SplitRemoveEmptyEntriesAndTrimEntries(',');
}
public static bool TrySplit(string value, out IEnumerable result, ushort maxCount = 0)
diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs
index a25ed97b..0a191e5f 100644
--- a/Netch/Utils/ModeHelper.cs
+++ b/Netch/Utils/ModeHelper.cs
@@ -81,7 +81,7 @@ namespace Netch.Utils
try
{
- var splited = text.Substring(1).Split(',').Select(s => s.Trim()).ToArray();
+ var splited = text.Substring(1).SplitTrimEntries(',');
mode.Remark = splited[0];
diff --git a/Netch/Utils/PortHelper.cs b/Netch/Utils/PortHelper.cs
index 5e03d302..790ad8fe 100644
--- a/Netch/Utils/PortHelper.cs
+++ b/Netch/Utils/PortHelper.cs
@@ -28,7 +28,6 @@ namespace Netch.Utils
private static void GetReservedPortRange(PortType portType, ref List targetList)
{
- var lines = new List();
var process = new Process
{
StartInfo = new ProcessStartInfo
@@ -41,32 +40,20 @@ namespace Netch.Utils
}
};
- process.OutputDataReceived += (s, e) =>
- {
- if (e.Data != null)
- lines.Add(e.Data);
- };
-
process.Start();
- process.BeginOutputReadLine();
- process.WaitForExit();
+ var output = process.StandardOutput.ReadToEnd();
- var splitLine = false;
- foreach (var line in lines)
- if (!splitLine)
- {
- if (line.StartsWith("-"))
- splitLine = true;
- }
- else
- {
- if (line == string.Empty)
- break;
+ foreach (var line in output.SplitRemoveEmptyEntriesAndTrimEntries('\n'))
+ {
+ var value = line.Trim().SplitRemoveEmptyEntries(' ');
+ if (value.Length != 2)
+ continue;
- var value = line.Trim().Split(' ').Where(s => s != string.Empty).ToArray();
+ if (!ushort.TryParse(value[0], out var start) || !ushort.TryParse(value[1], out var end))
+ continue;
- targetList.Add(new Range(ushort.Parse(value[0]), ushort.Parse(value[1])));
- }
+ targetList.Add(new Range(start, end));
+ }
}
///
diff --git a/Netch/Utils/StringExtension.cs b/Netch/Utils/StringExtension.cs
index e044b6c4..14c4bb45 100644
--- a/Netch/Utils/StringExtension.cs
+++ b/Netch/Utils/StringExtension.cs
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
@@ -63,5 +64,20 @@ namespace Netch.Utils
return sb.ToString();
}
+
+ public static string[] SplitRemoveEmptyEntriesAndTrimEntries(this string value, params char[] separator)
+ {
+ return value.Split(separator).Select(s => s.Trim()).Where(s => s != string.Empty).ToArray();
+ }
+
+ public static string[] SplitTrimEntries(this string value, params char[] separator)
+ {
+ return value.Split(separator).Select(s => s.Trim()).ToArray();
+ }
+
+ public static string[] SplitRemoveEmptyEntries(this string value, params char[] separator)
+ {
+ return value.Split(separator, StringSplitOptions.RemoveEmptyEntries);
+ }
}
}
\ No newline at end of file