mirror of
https://github.com/netchx/netch.git
synced 2026-04-07 19:49:23 +08:00
Fix NTT parse stderr
Refactor Split string Refactor GetReservedPortRange
This commit is contained in:
@@ -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
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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<string, object> _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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ namespace Netch.Utils
|
||||
|
||||
public static IEnumerable<string> 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<string> result, ushort maxCount = 0)
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ namespace Netch.Utils
|
||||
|
||||
private static void GetReservedPortRange(PortType portType, ref List<Range> targetList)
|
||||
{
|
||||
var lines = new List<string>();
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user