Skip to content

Commit

Permalink
Create PcapController
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Mar 3, 2021
1 parent e5a968f commit c1644ec
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Netch/Controllers/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority
switch (State)
{
case State.Started:
OnKeywordStarted();
Task.Run(OnKeywordStarted);
return;
case State.Stopped:
Stop();
Expand Down
55 changes: 55 additions & 0 deletions Netch/Controllers/PcapController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Netch.Models;

namespace Netch.Controllers
{
public class PcapController : Guard, IModeController
{
public override string Name { get; } = "pcap2socks";

public override string MainFile { get; protected set; } = "pcap2socks.exe";

protected override IEnumerable<string> StartedKeywords { get; } = new[] {"└"};

private readonly OutboundAdapter _outbound = new();

protected override Encoding? InstanceOutputEncoding { get; } = Encoding.UTF8;

public void Start(in Mode mode)
{
StartInstanceAuto($@"-i \Device\NPF_{_outbound.NetworkInterface.Id} {mode.FullRule.FirstOrDefault() ?? "-P n"}");
}

protected override void OnKeywordStarted()
{
Thread.Sleep(300);
Utils.Utils.Open(LogPath);
}

protected override void OnKeywordStopped()
{
if (File.ReadAllText(LogPath).Length == 0)
{
Task.Run(() =>
{
Thread.Sleep(1000);
Utils.Utils.Open("https://github.com/zhxie/pcap2socks#dependencies");
});

throw new MessageException("Pleases install pcap2socks's dependency");
}

Utils.Utils.Open(LogPath);
}

public override void Stop()
{
StopInstance();
}
}
}
2 changes: 2 additions & 0 deletions Netch/Utils/ModeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public static bool SkipServerController(Server server, Mode mode)
return new HTTPController();
case 4:
return null;
case 6:
return new PcapController();
default:
Logging.Error("未知模式类型");
throw new MessageException("未知模式类型");
Expand Down
2 changes: 1 addition & 1 deletion binaries
2 changes: 1 addition & 1 deletion modes
Submodule modes updated from 13886a to 5f3460

0 comments on commit c1644ec

Please sign in to comment.