From 97f6d601fbcf5d9ebf6e24154ccebb8f79c6fe8b Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 5 Mar 2021 01:02:59 +0800 Subject: [PATCH] Fix pcap2socks no --destination argument, Update Guard --- Netch/Controllers/Guard.cs | 12 ++++++---- Netch/Controllers/PcapController.cs | 34 +++++++++++++++++------------ Netch/Forms/LogForm.cs | 4 ++-- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Netch/Controllers/Guard.cs b/Netch/Controllers/Guard.cs index 43209500..2a9da808 100644 --- a/Netch/Controllers/Guard.cs +++ b/Netch/Controllers/Guard.cs @@ -166,6 +166,7 @@ namespace Netch.Controllers return; case State.Stopped: Stop(); + CloseLogFile(); OnKeywordStopped(); throw new MessageException($"{Name} 控制器启动失败"); } @@ -180,6 +181,9 @@ namespace Netch.Controllers private void OpenLogFile() { + if (!RedirectToFile) + return; + _logFileStream = File.Open(LogPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); _logStreamWriter = new StreamWriter(_logFileStream); @@ -201,8 +205,8 @@ namespace Netch.Controllers return; _flushFileStreamTimer.Enabled = false; - _logStreamWriter!.Close(); - _logFileStream!.Close(); + _logStreamWriter?.Close(); + _logFileStream?.Close(); _logStreamWriter = _logStreamWriter = null; } @@ -234,8 +238,8 @@ namespace Netch.Controllers string? line; while ((line = reader.ReadLine()) != null) { - OnReadNewLine(line); WriteLog(line); + OnReadNewLine(line); // State == State.Started if !StartedKeywords.Any() if (State == State.Starting) @@ -247,8 +251,8 @@ namespace Netch.Controllers } } - State = State.Stopped; CloseLogFile(); + State = State.Stopped; } /// diff --git a/Netch/Controllers/PcapController.cs b/Netch/Controllers/PcapController.cs index 556d12fb..1bd8c467 100644 --- a/Netch/Controllers/PcapController.cs +++ b/Netch/Controllers/PcapController.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Netch.Forms; using Netch.Models; +using Netch.Servers.Socks5; namespace Netch.Controllers { @@ -22,31 +23,37 @@ namespace Netch.Controllers protected override Encoding? InstanceOutputEncoding { get; } = Encoding.UTF8; - public PcapController() - { - RedirectToFile = false; - } - private LogForm? _form; public void Start(in Mode mode) { - Global.MainForm.BeginInvoke(new Action(() => - { - _form = new LogForm(Global.MainForm); - _form.Show(); - })); + var server = MainController.Server!; - StartInstanceAuto($@"-i \Device\NPF_{_outbound.NetworkInterface.Id} {mode.FullRule.FirstOrDefault() ?? "-P n"}"); + _form = new LogForm(Global.MainForm); + _form.CreateControl(); + + var argument = new StringBuilder($@"-i \Device\NPF_{_outbound.NetworkInterface.Id}"); + if (server is Socks5 socks5 && !socks5.Auth()) + argument.Append($" --destination {server.AutoResolveHostname()}:{server.Port}"); + else + argument.Append($" --destination 127.0.0.1:{Global.Settings.Socks5LocalPort}"); + + argument.Append($" {mode.FullRule.FirstOrDefault() ?? "-P n"}"); + StartInstanceAuto(argument.ToString()); } protected override void OnReadNewLine(string line) { - Global.MainForm.BeginInvoke(new Action(() => { _form!.richTextBox1.AppendText(line + "\n"); })); + Global.MainForm.BeginInvoke(new Action(() => + { + if (!_form!.IsDisposed) + _form!.richTextBox1.AppendText(line + "\n"); + })); } protected override void OnKeywordStarted() { + Global.MainForm.BeginInvoke(new Action(() => { _form!.Show(); })); } protected override void OnKeywordStopped() @@ -67,8 +74,7 @@ namespace Netch.Controllers public override void Stop() { - Global.MainForm.Invoke(new Action(() => { _form!.Close(); })); - + _form!.Close(); StopInstance(); } } diff --git a/Netch/Forms/LogForm.cs b/Netch/Forms/LogForm.cs index 24d01a93..8afb72d5 100644 --- a/Netch/Forms/LogForm.cs +++ b/Netch/Forms/LogForm.cs @@ -16,9 +16,9 @@ namespace Netch.Forms _parent = parent; } - protected override void OnShown(EventArgs e) + protected override void OnLoad(EventArgs e) { - base.OnShown(e); + base.OnLoad(e); Parent_Move(null!, null!); }