Skip to content

Commit

Permalink
Create LogForm and use on PcapController
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Mar 4, 2021
1 parent 74e1635 commit 64260b1
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 3 deletions.
26 changes: 23 additions & 3 deletions Netch/Controllers/PcapController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;

namespace Netch.Controllers
Expand All @@ -20,15 +22,31 @@ 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();
}));

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

protected override void OnReadNewLine(string line)
{
Global.MainForm.BeginInvoke(new Action(() => { _form!.richTextBox1.AppendText(line + "\n"); }));
}

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

protected override void OnKeywordStopped()
Expand All @@ -49,6 +67,8 @@ protected override void OnKeywordStopped()

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

StopInstance();
}
}
Expand Down
88 changes: 88 additions & 0 deletions Netch/Forms/LogForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions Netch/Forms/LogForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Vanara.PInvoke;
using static Vanara.PInvoke.User32;

namespace Netch.Forms
{
public partial class LogForm : Form
{
private readonly Form _parent;

public LogForm(Form parent)
{
InitializeComponent();
_parent = parent;
}

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

private void Parent_Move(object sender, EventArgs e)
{
var cl = Location;
var fl = _parent.Location;

cl.X = fl.X + _parent.Width;
cl.Y = fl.Y;
Location = cl;
}

private void Parent_Activated(object sender, EventArgs e)
{
SetWindowPos(Handle,
HWND.HWND_TOPMOST,
0,
0,
0,
0,
SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_SHOWWINDOW);

SetWindowPos(Handle,
HWND.HWND_NOTOPMOST,
0,
0,
0,
0,
SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_SHOWWINDOW);
}

private void richTextBox1_TextChanged(object sender, System.EventArgs e)
{
if (!checkBox1.Checked)
return;

richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
}

private void Notifycation_Load(object sender, EventArgs e)
{
_parent.LocationChanged += Parent_Move;
_parent.SizeChanged += Parent_Move;
_parent.Activated += Parent_Activated;
}

protected override void OnClosing(CancelEventArgs e)
{
_parent.Activated -= Parent_Activated;
_parent.LocationChanged -= Parent_Move;
_parent.SizeChanged -= Parent_Move;
base.OnClosing(e);
}
}
}
2 changes: 2 additions & 0 deletions Netch/Netch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageReference Include="TaskScheduler" Version="2.9.1" />
<PackageReference Include="Vanara.PInvoke.IpHlpApi" Version="3.3.5" />
<PackageReference Include="Microsoft-WindowsAPICodePack-Shell" Version="1.1.4" />
<PackageReference Include="Vanara.PInvoke.User32" Version="3.3.5" />
<PackageReference Include="WindowsFirewallHelper" Version="2.0.4.70-beta2" />
<PackageReference Include="WindowsJobAPI" Version="5.0.1" />
<PackageReference Include="WindowsProxy" Version="5.0.0" />
Expand Down Expand Up @@ -101,6 +102,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Remove="Forms\LogForm.resx" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 64260b1

Please sign in to comment.