Skip to content

Commit

Permalink
Fix pcap2socks no --destination argument, Update Guard
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Mar 4, 2021
1 parent 1ea0bb4 commit 97f6d60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
12 changes: 8 additions & 4 deletions Netch/Controllers/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority
return;
case State.Stopped:
Stop();
CloseLogFile();
OnKeywordStopped();
throw new MessageException($"{Name} 控制器启动失败");
}
Expand All @@ -180,6 +181,9 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority

private void OpenLogFile()
{
if (!RedirectToFile)
return;

_logFileStream = File.Open(LogPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
_logStreamWriter = new StreamWriter(_logFileStream);

Expand All @@ -201,8 +205,8 @@ private void CloseLogFile()
return;

_flushFileStreamTimer.Enabled = false;
_logStreamWriter!.Close();
_logFileStream!.Close();
_logStreamWriter?.Close();
_logFileStream?.Close();
_logStreamWriter = _logStreamWriter = null;
}

Expand Down Expand Up @@ -234,8 +238,8 @@ protected void ReadOutput(TextReader reader)
string? line;
while ((line = reader.ReadLine()) != null)
{
OnReadNewLine(line);
WriteLog(line);
OnReadNewLine(line);

// State == State.Started if !StartedKeywords.Any()
if (State == State.Starting)
Expand All @@ -247,8 +251,8 @@ protected void ReadOutput(TextReader reader)
}
}

State = State.Stopped;
CloseLogFile();
State = State.Stopped;
}

/// <summary>
Expand Down
34 changes: 20 additions & 14 deletions Netch/Controllers/PcapController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;
using Netch.Servers.Socks5;

namespace Netch.Controllers
{
Expand All @@ -22,31 +23,37 @@ public class PcapController : Guard, IModeController

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()
Expand All @@ -67,8 +74,7 @@ protected override void OnKeywordStopped()

public override void Stop()
{
Global.MainForm.Invoke(new Action(() => { _form!.Close(); }));

_form!.Close();
StopInstance();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Netch/Forms/LogForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public LogForm(Form parent)
_parent = parent;
}

protected override void OnShown(EventArgs e)
protected override void OnLoad(EventArgs e)
{
base.OnShown(e);
base.OnLoad(e);
Parent_Move(null!, null!);
}

Expand Down

0 comments on commit 97f6d60

Please sign in to comment.