diff --git a/Forwarder/Forwarder/DBPanel.cs b/Forwarder/Forwarder/DBPanel.cs index e25f0e7..7ddc3e0 100644 --- a/Forwarder/Forwarder/DBPanel.cs +++ b/Forwarder/Forwarder/DBPanel.cs @@ -1,12 +1,4 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using System.Windows.Forms; namespace Forwarder { diff --git a/Forwarder/Forwarder/Extension.cs b/Forwarder/Forwarder/Extension.cs index b6b8b62..03a784c 100644 --- a/Forwarder/Forwarder/Extension.cs +++ b/Forwarder/Forwarder/Extension.cs @@ -99,7 +99,7 @@ string result = Encoding.ASCII.GetString(data, 0, Math.Min(max, data.Length)); StringBuilder sb = new StringBuilder(result.Trim()); for (int i = 0; i < sb.Length; i++) - if (Char.IsControl(sb[i]) && sb[i] != '\n' && sb[i] != '\r') + if (char.IsControl(sb[i]) && sb[i] != '\n' && sb[i] != '\r') sb[i] = '□'; result = sb.ToString(); if (data.Length > max) diff --git a/Forwarder/Forwarder/FAbout.cs b/Forwarder/Forwarder/FAbout.cs index 167ae9b..7c563b5 100644 --- a/Forwarder/Forwarder/FAbout.cs +++ b/Forwarder/Forwarder/FAbout.cs @@ -44,9 +44,7 @@ public static void Execute() { using (FAbout f = new FAbout()) - { f.ShowDialog(); - } } private void GetIp() @@ -59,7 +57,6 @@ output += ip.Address.ToString() + "\r\n"; output = output.Trim(); if (output.Length == 0) - lbIp.Visible = false; else { diff --git a/Forwarder/Forwarder/FTransmission.cs b/Forwarder/Forwarder/FTransmission.cs index ad7e2c3..22382cf 100644 --- a/Forwarder/Forwarder/FTransmission.cs +++ b/Forwarder/Forwarder/FTransmission.cs @@ -13,9 +13,9 @@ { public partial class FTransmission : Form { - private static Dictionary fmap = new Dictionary(); - private static Font fontMonospace = new Font("Lucida Console", 8f); - private static Font fontSmall = new Font(FontFamily.GenericSansSerif, 7f); + private static readonly Dictionary fmap = new Dictionary(); + private static readonly Font fontMonospace = new Font("Lucida Console", 8f); + private static readonly Font fontSmall = new Font(FontFamily.GenericSansSerif, 7f); private readonly Transmission trans; @@ -39,8 +39,7 @@ MessageBox.Show("Can't not load transmission, try again."); return; } - FTransmission f; - if (!fmap.TryGetValue(trans, out f)) + if (!fmap.TryGetValue(trans, out FTransmission f)) fmap[trans] = f = new FTransmission(trans); f.Show(); f.Focus(); @@ -48,8 +47,7 @@ public static FTransmission Get(Transmission trans) { - FTransmission f; - if (!fmap.TryGetValue(trans, out f)) + if (!fmap.TryGetValue(trans, out FTransmission f)) return null; return f; } @@ -63,9 +61,9 @@ { if (trans == null) return; - lbInfo.Text = "Transmission between " + trans.SourceIP + " and " + trans.DestinationIP; + lbInfo.Text = $"Transmission between {trans.SourceIP} and {trans.DestinationIP}"; lbAt.Text = trans.Date.ToLongDateString() + " " + trans.Date.ToLongTimeString(); - lbCtt.Text = "Uploaded: " + Utils.FBytes(trans.Uploaded) + " Downloaded: " + Utils.FBytes(trans.Downloaded); + lbCtt.Text = $"Uploaded: {Utils.FBytes(trans.Uploaded)} Downloaded: {Utils.FBytes(trans.Downloaded)}"; int cscroll = pTimeline.VerticalScroll.Value; pTimeline.VerticalScroll.Value = 0; pTimeline.Controls.Clear(); @@ -77,53 +75,63 @@ { if ((s.Time - tlast).TotalSeconds > 5) { - Label lbx = new Label(); - lbx.Parent = pTimeline; - lbx.Font = fontSmall; - lbx.Text = "On hold by " + Utils.FTime(s.Time - tlast); - lbx.TextAlign = ContentAlignment.MiddleCenter; - lbx.BackColor = Color.LightGray; - lbx.Top = y; - lbx.Left = 5; - lbx.Width = pTimeline.ClientSize.Width - 10; + Label lbx = new Label + { + Parent = pTimeline, + Font = fontSmall, + Text = "On hold by " + Utils.FTime(s.Time - tlast), + TextAlign = ContentAlignment.MiddleCenter, + BackColor = Color.LightGray, + Top = y, + Left = 5, + Width = pTimeline.ClientSize.Width - 10 + }; y += lbx.Height + 15; } - Label lbs = new Label(); - lbs.AutoSize = true; - lbs.MinimumSize = lbs.MaximumSize = new Size(w2, 0); - lbs.Padding = new Padding(3); - lbs.Top = y; - lbs.Width = pTimeline.ClientSize.Width / 2; - lbs.Left = s.FromMe ? 5 : pTimeline.ClientSize.Width - 5 - w2; - lbs.BackColor = s.FromMe ? Color.LightGreen : Color.LightBlue; - lbs.TextAlign = s.FromMe ? ContentAlignment.TopLeft : ContentAlignment.TopRight; - lbs.Text = s.Data.BinToTextSample(1024); - lbs.Font = fontMonospace; - lbs.Parent = pTimeline; + Label lbs = new Label + { + AutoSize = true, + Padding = new Padding(3), + Top = y, + Width = pTimeline.ClientSize.Width / 2, + Left = s.FromMe ? 5 : pTimeline.ClientSize.Width - 5 - w2, + BackColor = s.FromMe ? Color.LightGreen : Color.LightBlue, + TextAlign = s.FromMe ? ContentAlignment.TopLeft : ContentAlignment.TopRight, + Text = s.Data.BinToTextSample(1024), + Font = fontMonospace, + Parent = pTimeline, + Cursor = Cursors.Hand, + Tag = s + }; lbs.Click += Lbs_Click; - lbs.Cursor = Cursors.Hand; - lbs.Tag = s; - Label lbt = new Label(); - lbt.Parent = pTimeline; - lbt.Font = fontSmall; - lbt.Text = $"At: {Utils.FTime(s.Time - tstart)}"; - lbt.AutoSize = true; - lbt.Top = lbs.Top - 2; + lbs.MinimumSize = lbs.MaximumSize = new Size(w2, 0); + Label lbt = new Label + { + Parent = pTimeline, + Font = fontSmall, + Text = $"At: {Utils.FTime(s.Time - tstart)}", + AutoSize = true, + Top = lbs.Top - 2 + }; lbt.Left = s.FromMe ? w2 + 10 : lbs.Left - lbt.Width - 5; - Label lbd = new Label(); - lbd.Parent = pTimeline; - lbd.Font = fontSmall; - lbd.Text = $"Take: {Utils.FTime(s.Time - tlast)}"; - lbd.ForeColor = SystemColors.GrayText; - lbd.AutoSize = true; - lbd.Top = lbt.Top + lbt.Height - 1; + Label lbd = new Label + { + Parent = pTimeline, + Font = fontSmall, + Text = $"Take: {Utils.FTime(s.Time - tlast)}", + ForeColor = SystemColors.GrayText, + AutoSize = true, + Top = lbt.Top + lbt.Height - 1 + }; lbd.Left = s.FromMe ? w2 + 10 : lbs.Left - lbd.Width - 5; - Label lbz = new Label(); - lbz.Parent = pTimeline; - lbz.Font = fontSmall; - lbz.Text = Utils.FBytes(s.Data.Length); - lbz.AutoSize = true; - lbz.Top = lbd.Top + lbd.Height - 1; + Label lbz = new Label + { + Parent = pTimeline, + Font = fontSmall, + Text = Utils.FBytes(s.Data.Length), + AutoSize = true, + Top = lbd.Top + lbd.Height - 1 + }; lbz.Left = s.FromMe ? w2 + 10 : lbs.Left - lbz.Width - 5; y += lbs.Height + 15; tlast = s.Time; @@ -136,7 +144,7 @@ { if (!(sender is Label lb)) return; if (!(lb.Tag is Sentence s)) return; - FData.Execute(Utils.FTime(s.Time - trans.Date),s); + FData.Execute(Utils.FTime(s.Time - trans.Date), s); } private void FTransmission_FormClosed(object sender, FormClosedEventArgs e) @@ -158,14 +166,14 @@ private void btSaveAll_Click(object sender, EventArgs e) { - SaveFileDialog sfd = new SaveFileDialog(); - sfd.Filter = "*.zip|Zipped Transmission file"; - sfd.DefaultExt = ".zip"; - sfd.FileName = $"Transmission {trans.Id}.zip"; - if (sfd.ShowDialog() == DialogResult.OK) + SaveFileDialog sfd = new SaveFileDialog { + Filter = "*.zip|Zipped Transmission file", + DefaultExt = ".zip", + FileName = $"Transmission {trans.Id}.zip" + }; + if (sfd.ShowDialog() == DialogResult.OK) trans.SaveToFile(sfd.FileName); - } } } } diff --git a/Forwarder/Forwarder/Form1.cs b/Forwarder/Forwarder/Form1.cs index 3c4a0dd..55138aa 100644 --- a/Forwarder/Forwarder/Form1.cs +++ b/Forwarder/Forwarder/Form1.cs @@ -62,10 +62,10 @@ return false; try { - foreach(string line in File.ReadAllLines(cfgPath)) + foreach (string line in File.ReadAllLines(cfgPath)) { string[] lp = line.Split(':'); - if (lp.Length != 3 || lp[0].Length<1) + if (lp.Length != 3 || lp[0].Length < 1) continue; ForwarderControl fc = AddForwarder(); fc.SourceLocal = lp[0][0] == '-'; @@ -86,8 +86,8 @@ try { List fws = new List(); - foreach(ForwarderControl fc in lbForwarders.Items) - fws.Add( (fc.SourceLocal ? "-" : "*") + fc.SourcePort + ":" + fc.DestinationHost + ":" + fc.DestinationPort); + foreach (ForwarderControl fc in lbForwarders.Items) + fws.Add((fc.SourceLocal ? "-" : "*") + fc.SourcePort + ":" + fc.DestinationHost + ":" + fc.DestinationPort); File.WriteAllLines(cfgPath, fws.ToArray()); } catch { }; diff --git a/Forwarder/Forwarder/Forwarder.cs b/Forwarder/Forwarder/Forwarder.cs index 77a4cf5..595ed25 100644 --- a/Forwarder/Forwarder/Forwarder.cs +++ b/Forwarder/Forwarder/Forwarder.cs @@ -2,12 +2,10 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; -using System.Threading.Tasks; //Copyright(c) 2018 Iván Dominguez (XWolf Override) // @@ -173,10 +171,10 @@ private class TcpProcess { - private Forwarder fw; - private Socket scksrc; - private Socket sckdst; - private Transmission trans; + private readonly Forwarder fw; + private readonly Socket scksrc; + private readonly Socket sckdst; + private readonly Transmission trans; public TcpProcess(Forwarder forwarder, Socket source, IPEndPoint remote) { @@ -279,7 +277,6 @@ private ForwarderMessageType type; private string message; private Exception exception; - private DateTime time = DateTime.Now; private Transmission trans; private ForwarderMessage() @@ -289,77 +286,90 @@ public static ForwarderMessage FromException(Exception ex) { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.ERROR; - fw.message = ex.GetType().Name + ": " + ex.Message; - fw.exception = ex; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.ERROR, + message = ex.GetType().Name + ": " + ex.Message, + exception = ex + }; return fw; } public static ForwarderMessage FromActivation() { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.ACTIVATED; - fw.message = "Listener enabled"; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.ACTIVATED, + message = "Listener enabled" + }; return fw; } public static ForwarderMessage FromDeactivation() { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.DEACTIVATED; - fw.message = "Listener disabled"; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.DEACTIVATED, + message = "Listener disabled" + }; return fw; } public static ForwarderMessage FromNewTransmission(Transmission trans) { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.TSTART; - fw.trans = trans; - fw.message = "Transmission from " + trans.SourceIP; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.TSTART, + trans = trans, + message = "Transmission from " + trans.SourceIP + }; return fw; } public static ForwarderMessage FromSendTransmission(Transmission trans) { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.TSEND; - fw.trans = trans; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.TSEND, + trans = trans + }; return fw; } public static ForwarderMessage FromReceiveTransmission(Transmission trans) { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.TRECV; - fw.trans = trans; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.TRECV, + trans = trans + }; return fw; } public static ForwarderMessage FromEndTransmission(Transmission trans) { - ForwarderMessage fw = new ForwarderMessage(); - fw.type = ForwarderMessageType.TEND; - fw.trans = trans; + ForwarderMessage fw = new ForwarderMessage + { + type = ForwarderMessageType.TEND, + trans = trans + }; return fw; } public ForwarderMessageType Type => type; public string Message => message; - public DateTime TimeStamp => time; + public DateTime TimeStamp { get; } = DateTime.Now; public Exception Exception => exception; public Transmission Transmission => trans; } public class Transmission { - private IPEndPoint src; - private IPEndPoint dst; + private readonly IPEndPoint src; + private readonly IPEndPoint dst; private int uploaded; private int downloaded; - private DateTime date = DateTime.Now; - private List conversation = new List(); + private readonly List conversation = new List(); public Transmission(IPEndPoint src, IPEndPoint dst) { @@ -403,7 +413,7 @@ sw.WriteLine(); sw.WriteLine($"From={SourceIP}"); sw.WriteLine($"To={DestinationIP}"); - sw.WriteLine($"Time={date.ToUniversalTime().ToString("O")}"); + sw.WriteLine($"Time={Date.ToUniversalTime().ToString("O")}"); sw.WriteLine($"TotalSent={uploaded}"); sw.WriteLine($"TotalRecv={downloaded}"); for (int i = 0; i < conversation.Count; i++) @@ -429,7 +439,7 @@ private string GetId() { - return $"{src.Address}({src.Port})-{dst.Address}({dst.Port})@{date.ToComactString()}"; + return $"{src.Address}({src.Port})-{dst.Address}({dst.Port})@{Date.ToComactString()}"; } public IPEndPoint SourceIP => src; @@ -437,7 +447,7 @@ public object Tag { get; set; } public int Uploaded => uploaded; public int Downloaded => downloaded; - public DateTime Date => date; + public DateTime Date { get; } = DateTime.Now; public Sentence[] Conversation => conversation.ToArray(); public string Id => GetId(); } diff --git a/Forwarder/Forwarder/ForwarderControl.cs b/Forwarder/Forwarder/ForwarderControl.cs index 3cd1d2c..11a2259 100644 --- a/Forwarder/Forwarder/ForwarderControl.cs +++ b/Forwarder/Forwarder/ForwarderControl.cs @@ -252,8 +252,7 @@ { if (lvHistory.SelectedItems.Count < 0) return; - Transmission t = lvHistory.SelectedItems[0].Tag as Transmission; - if (t == null) + if (!(lvHistory.SelectedItems[0].Tag is Transmission t)) return; FTransmission.Execute(t); } diff --git a/Forwarder/Forwarder/Program.cs b/Forwarder/Forwarder/Program.cs index a2bce59..35c8b4e 100644 --- a/Forwarder/Forwarder/Program.cs +++ b/Forwarder/Forwarder/Program.cs @@ -41,8 +41,7 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { - Exception ex = e.ExceptionObject as Exception; - if (ex != null) + if (e.ExceptionObject is Exception ex) MessageBox.Show(ex.Message, "Critical " + ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); } }