From 64260b18cc9cee92ed46b01b5dd3bec14943a974 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Thu, 4 Mar 2021 21:25:34 +0800 Subject: [PATCH] Create LogForm and use on PcapController --- Netch/Controllers/PcapController.cs | 26 ++++++++- Netch/Forms/LogForm.Designer.cs | 88 +++++++++++++++++++++++++++++ Netch/Forms/LogForm.cs | 78 +++++++++++++++++++++++++ Netch/Netch.csproj | 2 + 4 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 Netch/Forms/LogForm.Designer.cs create mode 100644 Netch/Forms/LogForm.cs diff --git a/Netch/Controllers/PcapController.cs b/Netch/Controllers/PcapController.cs index 1f791493ac..556d12fb7f 100644 --- a/Netch/Controllers/PcapController.cs +++ b/Netch/Controllers/PcapController.cs @@ -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 @@ -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() @@ -49,6 +67,8 @@ protected override void OnKeywordStopped() public override void Stop() { + Global.MainForm.Invoke(new Action(() => { _form!.Close(); })); + StopInstance(); } } diff --git a/Netch/Forms/LogForm.Designer.cs b/Netch/Forms/LogForm.Designer.cs new file mode 100644 index 0000000000..0c1ae8ba2a --- /dev/null +++ b/Netch/Forms/LogForm.Designer.cs @@ -0,0 +1,88 @@ +using System.ComponentModel; + +namespace Netch.Forms +{ + partial class LogForm + { + /// + /// Required designer variable. + /// + private IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.richTextBox1 = new System.Windows.Forms.RichTextBox(); + this.checkBox1 = new System.Windows.Forms.CheckBox(); + this.SuspendLayout(); + // + // richTextBox1 + // + this.richTextBox1.BackColor = System.Drawing.SystemColors.Control; + this.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top; + this.richTextBox1.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.richTextBox1.Location = new System.Drawing.Point(0, 0); + this.richTextBox1.Name = "richTextBox1"; + this.richTextBox1.ReadOnly = true; + this.richTextBox1.Size = new System.Drawing.Size(454, 288); + this.richTextBox1.TabIndex = 0; + this.richTextBox1.Text = ""; + this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged); + // + // checkBox1 + // + this.checkBox1.AutoSize = true; + this.checkBox1.Location = new System.Drawing.Point(12, 297); + this.checkBox1.Name = "checkBox1"; + this.checkBox1.Size = new System.Drawing.Size(102, 16); + this.checkBox1.TabIndex = 1; + this.checkBox1.Text = "Scroll to End"; + this.checkBox1.UseVisualStyleBackColor = true; + // + // LogForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(454, 318); + this.ControlBox = false; + this.Controls.Add(this.checkBox1); + this.Controls.Add(this.richTextBox1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "LogForm"; + this.ShowIcon = false; + this.ShowInTaskbar = false; + this.Text = "LogForm"; + this.Load += new System.EventHandler(this.Notifycation_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.CheckBox checkBox1; + public System.Windows.Forms.RichTextBox richTextBox1; + } +} \ No newline at end of file diff --git a/Netch/Forms/LogForm.cs b/Netch/Forms/LogForm.cs new file mode 100644 index 0000000000..24d01a93dc --- /dev/null +++ b/Netch/Forms/LogForm.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index a8fae9e0cd..9d82c5b314 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -49,6 +49,7 @@ + @@ -101,6 +102,7 @@ ResXFileCodeGenerator Resources.Designer.cs +