diff --git a/src/App.config b/src/App.config new file mode 100755 index 0000000..5754728 --- /dev/null +++ b/src/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Form1.Designer.cs b/src/Form1.Designer.cs new file mode 100755 index 0000000..e8417b5 --- /dev/null +++ b/src/Form1.Designer.cs @@ -0,0 +1,199 @@ +namespace TCPCommander +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.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.components = new System.ComponentModel.Container(); + this.timer1 = new System.Windows.Forms.Timer(this.components); + this.StartButton = new System.Windows.Forms.Button(); + this.TCP_PortTextBox = new System.Windows.Forms.TextBox(); + this.IPLabel = new System.Windows.Forms.Label(); + this.TCP_PortLabel = new System.Windows.Forms.Label(); + this.TCP_AvailablePortsLabel = new System.Windows.Forms.Label(); + this.DebugButton = new System.Windows.Forms.Button(); + this.UDP_AvailablePortsLabel = new System.Windows.Forms.Label(); + this.UDP_PortLabel = new System.Windows.Forms.Label(); + this.UDP_PortTextBox = new System.Windows.Forms.TextBox(); + this.TCP_label = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // timer1 + // + this.timer1.Tick += new System.EventHandler(this.timer1_Tick); + // + // StartButton + // + this.StartButton.Location = new System.Drawing.Point(385, 40); + this.StartButton.Name = "StartButton"; + this.StartButton.Size = new System.Drawing.Size(93, 38); + this.StartButton.TabIndex = 2; + this.StartButton.Text = "Start Server"; + this.StartButton.UseVisualStyleBackColor = true; + this.StartButton.Click += new System.EventHandler(this.StartButton_Click); + // + // TCP_PortTextBox + // + this.TCP_PortTextBox.Location = new System.Drawing.Point(59, 92); + this.TCP_PortTextBox.MaxLength = 65535; + this.TCP_PortTextBox.Name = "TCP_PortTextBox"; + this.TCP_PortTextBox.Size = new System.Drawing.Size(100, 20); + this.TCP_PortTextBox.TabIndex = 0; + this.TCP_PortTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TCP_PortTextBox_KeyPress); + this.TCP_PortTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.TCP_PortTextBox_KeyUp); + // + // IPLabel + // + this.IPLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.IPLabel.Location = new System.Drawing.Point(112, 5); + this.IPLabel.Name = "IPLabel"; + this.IPLabel.Size = new System.Drawing.Size(174, 65); + this.IPLabel.TabIndex = 2; + this.IPLabel.Text = "IP Address Here"; + this.IPLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // TCP_PortLabel + // + this.TCP_PortLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.TCP_PortLabel.Location = new System.Drawing.Point(19, 143); + this.TCP_PortLabel.Name = "TCP_PortLabel"; + this.TCP_PortLabel.Size = new System.Drawing.Size(174, 53); + this.TCP_PortLabel.TabIndex = 3; + this.TCP_PortLabel.Text = "Port Here"; + this.TCP_PortLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // TCP_AvailablePortsLabel + // + this.TCP_AvailablePortsLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.TCP_AvailablePortsLabel.Location = new System.Drawing.Point(59, 115); + this.TCP_AvailablePortsLabel.Name = "TCP_AvailablePortsLabel"; + this.TCP_AvailablePortsLabel.Size = new System.Drawing.Size(100, 23); + this.TCP_AvailablePortsLabel.TabIndex = 4; + this.TCP_AvailablePortsLabel.Text = "(1 - 65535)"; + this.TCP_AvailablePortsLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // DebugButton + // + this.DebugButton.Location = new System.Drawing.Point(385, 105); + this.DebugButton.Name = "DebugButton"; + this.DebugButton.Size = new System.Drawing.Size(93, 38); + this.DebugButton.TabIndex = 3; + this.DebugButton.Text = "Debug Info"; + this.DebugButton.UseVisualStyleBackColor = true; + this.DebugButton.Click += new System.EventHandler(this.DebugButton_Click); + // + // UDP_AvailablePortsLabel + // + this.UDP_AvailablePortsLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.UDP_AvailablePortsLabel.Location = new System.Drawing.Point(241, 115); + this.UDP_AvailablePortsLabel.Name = "UDP_AvailablePortsLabel"; + this.UDP_AvailablePortsLabel.Size = new System.Drawing.Size(100, 23); + this.UDP_AvailablePortsLabel.TabIndex = 8; + this.UDP_AvailablePortsLabel.Text = "(1 - 65535)"; + this.UDP_AvailablePortsLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // UDP_PortLabel + // + this.UDP_PortLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.UDP_PortLabel.Location = new System.Drawing.Point(199, 143); + this.UDP_PortLabel.Name = "UDP_PortLabel"; + this.UDP_PortLabel.Size = new System.Drawing.Size(174, 53); + this.UDP_PortLabel.TabIndex = 7; + this.UDP_PortLabel.Text = "Port Here"; + this.UDP_PortLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // UDP_PortTextBox + // + this.UDP_PortTextBox.Location = new System.Drawing.Point(241, 92); + this.UDP_PortTextBox.MaxLength = 65535; + this.UDP_PortTextBox.Name = "UDP_PortTextBox"; + this.UDP_PortTextBox.Size = new System.Drawing.Size(100, 20); + this.UDP_PortTextBox.TabIndex = 1; + this.UDP_PortTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.UDP_PortTextBox_KeyPress); + this.UDP_PortTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.UDP_PortTextBox_KeyUp); + // + // TCP_label + // + this.TCP_label.AutoSize = true; + this.TCP_label.Location = new System.Drawing.Point(95, 76); + this.TCP_label.Name = "TCP_label"; + this.TCP_label.Size = new System.Drawing.Size(28, 13); + this.TCP_label.TabIndex = 9; + this.TCP_label.Text = "TCP"; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(280, 76); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(30, 13); + this.label4.TabIndex = 10; + this.label4.Text = "UDP"; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(490, 200); + this.Controls.Add(this.label4); + this.Controls.Add(this.TCP_label); + this.Controls.Add(this.UDP_AvailablePortsLabel); + this.Controls.Add(this.UDP_PortLabel); + this.Controls.Add(this.UDP_PortTextBox); + this.Controls.Add(this.DebugButton); + this.Controls.Add(this.TCP_AvailablePortsLabel); + this.Controls.Add(this.TCP_PortLabel); + this.Controls.Add(this.IPLabel); + this.Controls.Add(this.TCP_PortTextBox); + this.Controls.Add(this.StartButton); + this.Name = "Form1"; + this.Text = "TCPCommander Launcher"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); + this.Load += new System.EventHandler(this.Form1_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Timer timer1; + private System.Windows.Forms.Button StartButton; + private System.Windows.Forms.TextBox TCP_PortTextBox; + private System.Windows.Forms.Label IPLabel; + private System.Windows.Forms.Label TCP_PortLabel; + private System.Windows.Forms.Label TCP_AvailablePortsLabel; + private System.Windows.Forms.Button DebugButton; + private System.Windows.Forms.Label UDP_AvailablePortsLabel; + private System.Windows.Forms.Label UDP_PortLabel; + private System.Windows.Forms.TextBox UDP_PortTextBox; + private System.Windows.Forms.Label TCP_label; + private System.Windows.Forms.Label label4; + } +} + diff --git a/src/Form1.cs b/src/Form1.cs new file mode 100755 index 0000000..fa583ef --- /dev/null +++ b/src/Form1.cs @@ -0,0 +1,861 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using System.Windows.Forms; +using System.Management; +using Microsoft.Win32; +using System.Runtime.InteropServices; +using System.IO; +using System.Drawing.Imaging; +using System.Threading; +using Open.Nat; +using WinForms = System.Windows.Forms; + +namespace TCPCommander +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + WinForms.Timer ScreenshotTimer = new WinForms.Timer(); + + IPEndPoint TCP_ipEP; + public static TcpClient TCP_client; + public static TcpListener TCP_listener; + + IPEndPoint UDP_ipEP; + public static UdpClient UDP_client; + + public static string IPstring; + + [DllImport("user32.dll")] + public static extern bool GetCursorPos(out Point lpPoint); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool SetCursorPos(int X, int Y); + + + ImageCodecInfo jpgEncoder; + System.Drawing.Imaging.Encoder encoder; + EncoderParameters encoderparams; + EncoderParameter encoderparam; + + Form3 f3; + private void Form1_Load(object sender, EventArgs e) + { + + StartButton.Enabled = false; + IPLabel.Text = ""; + TCP_PortLabel.Text = "Port: (Not Selected)"; + + f2 = new Form2(); + f3 = new Form3(); + + f2.Hide(); + f2.Text = "Server Status: Offline"; + f2.IPLabel.Text = "Server Offline"; + f2.LogTextBox.Location = new Point((this.Width) - (f2.LogTextBox.Size.Width / 2), f2.LogTextBox.Location.Y); + f2.IPLabel.Location = new Point((this.Width) - (f2.IPLabel.Size.Width / 2), f2.IPLabel.Location.Y); + f2.SessionTimeLabel.Location = new Point((this.Width) - (f2.IPLabel.Size.Width / 2), f2.SessionTimeLabel.Location.Y); + + f2.SessionTimeLabel.Text = "00:00:00"; + + f2.LogTextBox.ReadOnly = true; + f2.LogTextBox.BackColor = System.Drawing.SystemColors.Window; + + + this.MinimumSize = this.Size; + this.MaximumSize = this.Size; + + f2.MinimumSize = f2.Size; + f2.MaximumSize = f2.Size; + f3.Opacity = 0; + f3.Show(); + f3.Hide(); + + + Initialize(); + timer1.Interval = 1000; + timer1.Enabled = true; + + + + jpgEncoder = GetEncoder(ImageFormat.Jpeg); + encoder = System.Drawing.Imaging.Encoder.Quality; + encoderparams = new EncoderParameters(1); + encoderparam = new EncoderParameter(encoder, 100L); + + encoderparams.Param[0] = encoderparam; + } + + bool TCP_clientIsOpen; + bool UDP_clientIsOpen; + byte[] buffer = new byte[1]; + + int SessionTime = 0; + bool ServerStatusChanged; + private void timer1_Tick(object sender, EventArgs e) + { + if (StartServer) + { + SessionTime++; + } + else if (SessionTime > 0) + { + LogAdd("Session Ended @ " + DateTime.Now.ToString("MM/dd/yy") + " - " + DateTime.Now.ToString("HH:mm:ss")); + LogAdd("Session Lasted: " + Hour + " Hours : " + Minute + " Minutes : " + Second + " Seconds"); + SessionTime = 0; + } + + if (!(ServerStatusChanged == StartServer && !StartServer)) + { + f2.SessionTimeLabel.Text = GetSessionTime(); + } + + ServerStatusChanged = StartServer; + + if (TCP_client != null) + { + if (TCP_clientIsOpen) + { + if (TCP_client.Client.Poll(0, SelectMode.SelectRead)) + { + try + { + + if (TCP_client.Client.Receive(buffer, SocketFlags.Peek) == 0) + { + ResetServer(StartServer); + } + } catch (Exception ex) + { + Debug.WriteLine("EXCEPTION OCCURED: Type: " + ex.GetType().ToString() + "Message: " + ex.Message); + ResetServer(StartServer); + LogAdd("Exception Thrown: " + ex.GetType() + ":" + ex.Message); + } + } + } + } + + } + + public async void PortForward(bool forward, bool alert = false) + { + if (!forward) + { + if (alert) + MessageBox.Show("Alert: TCPCommander Server currently has UPnP disabled, so you cannot connect from outside the network", "TCPCommander Alert", MessageBoxButtons.OK, MessageBoxIcon.Information); + + return; + } + + try + { + var discoverer = new NatDiscoverer(); + var device = await discoverer.DiscoverDeviceAsync(); + await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, int.Parse(TCP_PortTextBox.Text), int.Parse(TCP_PortTextBox.Text), "TCPCommander Server Map"));//"Inner Router")); + + var sb = new StringBuilder(); + var ip = await device.GetExternalIPAsync(); + + sb.AppendFormat("\n+------+-------------------------------+--------------------------------+------------------------------------+-------------------------+"); + sb.AppendFormat("\n| PROT | PUBLIC (Reacheable) | PRIVATE (Your computer) | Description | |"); + sb.AppendFormat("\n+------+----------------------+--------+-----------------------+--------+------------------------------------+-------------------------+"); + sb.AppendFormat("\n| | IP Address | Port | IP Address | Port | | Expires |"); + sb.AppendFormat("\n+------+----------------------+--------+-----------------------+--------+------------------------------------+-------------------------+"); + foreach (var mapping in await device.GetAllMappingsAsync()) + { + sb.AppendFormat("\n| {5} | {0,-20} | {1,6} | {2,-21} | {3,6} | {4,-35}|{6,25}|", + ip, mapping.PublicPort, mapping.PrivateIP, mapping.PrivatePort, mapping.Description, mapping.Protocol == Protocol.Tcp ? "TCP" : "UDP", mapping.Expiration.ToLocalTime()); + } + sb.AppendFormat("\n+------+----------------------+--------+-----------------------+--------+------------------------------------+-------------------------+"); + Console.WriteLine(sb.ToString()); + } + catch (MappingException ex) + { + Console.WriteLine(ex.Message); + Console.ReadLine(); + } + catch (WebException ex) + { + Console.WriteLine(ex.Message); + Console.ReadLine(); + } + + } + + public void ResetServer(bool ServerIsStarted) + { + if (ServerIsStarted) + { + TCP_listener.Stop(); + + TCP_client.Close(); + TCP_clientIsOpen = false; + + UDP_client.Close(); + UDP_clientIsOpen = false; + + Console.WriteLine(@" +=================================================== +Client has been detected as being disconnected! +==================================================="); + worker.WorkerSupportsCancellation = true; + worker.CancelAsync(); + + worker.Dispose(); + LogAdd("Client Disconnected"); + LogAdd("Restarting Server"); + } + else + { + TCP_listener.Stop(); + + try + { + if (TCP_client != null) + { + TCP_client.GetStream().Close(); + TCP_client.Close(); + TCP_client = null; + } + } + catch (Exception ex) + { + Debug.WriteLine("Exception Thrown: " + ex.GetType() + ":" + ex.Message); + LogAdd("Exception Thrown: " + ex.GetType() + ":" + ex.Message); + } + + TCP_clientIsOpen = false; + Console.WriteLine(@" +========================================================== +Server has been shutdown by the user and is now offline! +=========================================================="); + worker.WorkerSupportsCancellation = true; + worker.CancelAsync(); + + + worker.DoWork -= ListenToClient; + worker.RunWorkerCompleted -= Worker_RunWorkerCompleted; + + worker.Dispose(); + LogAdd("Server Shut Down By Host"); + LogAdd("Worker Temporarily Deactivated"); + } + } + + BackgroundWorker worker; + public /*async*/ void Initialize() + { + try + { + IPstring = GetLocalNetworkIPV4(); + IPLabel.Text = "IP: " + IPstring; + + } + catch (Exception ex) + { + Debug.WriteLine("Error Occured: " + ex.GetType().ToString() + " : " + ex.Message); + LogAdd("Exception Thrown: " + ex.GetType() + ":" + ex.Message); + } + + + + private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + worker.DoWork -= ListenToClient; + worker.RunWorkerCompleted -= Worker_RunWorkerCompleted; + worker.Dispose(); + Debug.WriteLine("The Worker has stopped"); + LogAdd("Worker Stopping And Restarting - Connection Temporarily Disabled"); + worker = new BackgroundWorker(); + worker.DoWork += ListenToClient; + worker.RunWorkerCompleted += Worker_RunWorkerCompleted; + worker.RunWorkerAsync(); + } + + bool PCLocked = false; + public void ListenToClient(object sender, DoWorkEventArgs e) + { + LogAdd("Listener Started - Connection Enabled"); + try + { + TCP_listener.Start(); + Console.WriteLine(@" +=================================================== +Started listening requests at: {0}:{1} +===================================================", + TCP_ipEP.Address, TCP_ipEP.Port); + TCP_client = TCP_listener.AcceptTcpClient(); + TCP_clientIsOpen = true; + + + + Console.WriteLine("Connected toTCP_client!" + " \n"); + + + Console.WriteLine("Client: " + (TCP_client.Client.RemoteEndPoint as IPEndPoint).Address + ":" + (TCP_client.Client.RemoteEndPoint as IPEndPoint).Port); //IMPORTANT FOR GETTINGTCP_client IP AND PORT!!! + LogAdd("Client: " + (TCP_client.Client.RemoteEndPoint as IPEndPoint).Address + ":" + (TCP_client.Client.RemoteEndPoint as IPEndPoint).Port + " Connected to the Server"); + } + catch (Exception ex) + { + if (ex.Message == "A blocking operation was interrupted by a call to WSACancelBlockingCall") + { + Debug.WriteLine("Alert: An exception has occured that is thrown when waiting for theTCP_client to accept is stopped by the server.\nThis error is normal and shouldn't impact performance, but is possible to fix with another method"); + Debug.WriteLine("Same Thing sortof goes for the read in the whileTCP_listener"); + } + + Debug.WriteLine(ex.Message); + LogAdd("Exception Thrown: " + ex.GetType() + ":" + ex.Message); + } + + while (TCP_client.Connected) + { + try + { + int bytesize = 1024; + byte[] buffer = new byte[bytesize]; + string networkRead =TCP_client.GetStream().Read(buffer, 0, bytesize).ToString(); + string data = ASCIIEncoding.ASCII.GetString(buffer); + Debug.WriteLine(data); + + LogAdd("Incoming Data: " + data); + + if (data.ToUpper().Contains("{TEST}")) + { + sendData(getBytes("{TEST_RESPOND}"),TCP_client.GetStream()); + Console.WriteLine("DETECTED \"TEST\""); + } + else if (data.ToUpper().Contains("{SHUTDOWN}")) + { + Shutdown(); + LogAdd("Shutting Down"); + } + else if (data.ToUpper().Contains("{MONITOR_ON}")) + { + Debug.WriteLine("the monitor has been turned on"); + + SetMonitorState(MonitorState.ON); + + mouse_event(MOUSEEVENTF_MOVE, 0, 0, 0, UIntPtr.Zero); + + sendData(getBytes("{MONITOR_TURNED_ON}"),TCP_client.GetStream()); + LogAdd("Turning Monitor ON"); + } + else if (data.ToUpper().Contains("{MONITOR_OFF}")) + { + Debug.WriteLine("the monitor has been turned off"); + + SetMonitorState(MonitorState.OFF); + + sendData(getBytes("{MONITOR_TURNED_OFF}"),TCP_client.GetStream()); + + LogAdd("Turning Mointor OFF"); + } + else if (data.ToUpper().Contains("{TAKE_SCREENSHOT}")) + { + ScreenshotTimer.Interval = 1000; + ScreenshotTimer.Tick += SendScreenshotTick; + + } + else if (data.ToString().Contains("{SPEED_TEST}") && data.ToString().Contains("{/SPEED_TEST}")) + { + //sendData(new byte[1024 * 1024 * 5],TCP_client.GetStream()); + Console.WriteLine("Speed Test: Size = " + data.Length); + LogAdd("Completing Speed Test"); + } + else if (data.ToString().Contains("{LOCK_PC}")) + { + + if (PCLocked) + { + f3.Invoke(new Action(() => f3.UnlockPC())); + } + else + { + f3.Invoke(new Action(() => f3.LockPC())); + } + + PCLocked = !PCLocked; + + sendData(getBytes("{PC_LOCKED}"),TCP_client.GetStream()); + LogAdd("PC Locked Set -> " + PCLocked); + } + else if (data.ToString().Contains("{PC_LOCK_STATUS}")) + { + sendData(getBytes("{PC_LOCK_STATUS}" + PCLocked.ToString().ToLower() + "{/PC_LOCK_STATUS}"),TCP_client.GetStream()); + LogAdd("Getting & Sending PC Lock Status"); + } + } + catch (Exception ex) + { + Debug.WriteLine("Exception WHILE Listening: " + ex.Message); + TCP_client.GetStream().Close(); + TCP_client.Close(); + TCP_clientIsOpen = false; + LogAdd("Exception Thrown: " + ex.GetType() + ":" + ex.Message); + } + } + } + + public void SendScreenshotTick(object sender, EventArgs e) + { + + Debug.WriteLine("a screenshot was taken"); + + var bitmap = SaveScreenshotWithMousePointer(); + bitmap = new Bitmap(bitmap, new Size(1280, 720)); + + var stream = new MemoryStream(); + bitmap.Save(stream, jpgEncoder, encoderparams); + Debug.WriteLine("Getting stream size: " + stream.Length); + + sendData(stream.ToArray(),TCP_client.GetStream()); + + bitmap.Dispose(); + LogAdd("Taking & Sending Screenshot"); + } + + private ImageCodecInfo GetEncoder(ImageFormat format) + { + + ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); + + foreach (ImageCodecInfo codec in codecs) + { + if (codec.FormatID == format.Guid) + { + return codec; + } + } + return null; + } + + void Shutdown() + { + sendData(getBytes("{BEGAN_SHUTDOWN}"),TCP_client.GetStream()); + ProcessStartInfo psi = new ProcessStartInfo("shutdown", "/s /f /t 0"); + psi.CreateNoWindow = true; + psi.UseShellExecute = false; + Process.Start(psi); + } + + #region Screenshot Capture + public static class User32 + { + public const Int32 CURSOR_SHOWING = 0x00000001; + + [StructLayout(LayoutKind.Sequential)] + public struct ICONINFO + { + public bool fIcon; + public Int32 xHotspot; + public Int32 yHotspot; + public IntPtr hbmMask; + public IntPtr hbmColor; + } + + [StructLayout(LayoutKind.Sequential)] + public struct POINT + { + public Int32 x; + public Int32 y; + } + + [StructLayout(LayoutKind.Sequential)] + public struct CURSORINFO + { + public Int32 cbSize; + public Int32 flags; + public IntPtr hCursor; + public POINT ptScreenPos; + } + + [DllImport("user32.dll")] + public static extern bool GetCursorInfo(out CURSORINFO pci); + + [DllImport("user32.dll")] + public static extern IntPtr CopyIcon(IntPtr hIcon); + + [DllImport("user32.dll")] + public static extern bool DrawIcon(IntPtr hdc, int x, int y, IntPtr hIcon); + + [DllImport("user32.dll")] + public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo); + } + + Bitmap SaveScreenshotWithMousePointer() + { + var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); + // Create a graphics object from the bitmap. + var gfxScreenshot = Graphics.FromImage(bmpScreenshot); + // Take the screenshot from the upper left corner to the right + gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); + + User32.CURSORINFO cursorInfo; + cursorInfo.cbSize = Marshal.SizeOf(typeof(User32.CURSORINFO)); + + if (User32.GetCursorInfo(out cursorInfo)) + { + // if the cursor is showing draw it on the screen shot + if (cursorInfo.flags == User32.CURSOR_SHOWING) + { + // we need to get hotspot so we can draw the cursor in the correct position + var iconPointer = User32.CopyIcon(cursorInfo.hCursor); + User32.ICONINFO iconInfo; + int iconX, iconY; + + if (User32.GetIconInfo(iconPointer, out iconInfo)) + { + // calculate the correct position of the cursor + iconX = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot); + iconY = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot); + + // draw the cursor icon on top of the captured screen image + User32.DrawIcon(gfxScreenshot.GetHdc(), iconX, iconY, cursorInfo.hCursor); + + // release the handle created by call to g.GetHdc() + gfxScreenshot.ReleaseHdc(); + } + } + } + + return bmpScreenshot; + } + + public Bitmap SaveScreenshot() + { + var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); + // Create a graphics object from the bitmap. + var gfxScreenshot = Graphics.FromImage(bmpScreenshot); + // Take the screenshot from the upper left corner to the right + gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); + return bmpScreenshot; + } + #endregion + + [DllImport("user32.dll")] + static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo); + private const int MOUSEEVENTF_MOVE = 0x0001; + + #region Monitor Command + [DllImport("user32.dll")] + static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); + + private int SC_MONITORPOWER = 0xF170; + private int WM_SYSCOMMAND = 0x0112; + + public enum MonitorState + { + ON = -1, + OFF = 2, + STANDBY = 1 + } + + public void SetMonitorState(MonitorState state) + { + this.Invoke(new Action(() => SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, (int)state))); + } + #endregion + + byte[] getBytes(string input) + { + byte[] bytes = Encoding.ASCII.GetBytes(input); + return bytes; + } + + void sendData(byte[] data, NetworkStream stream) + { + int bufferSize = 1024; + byte[] dataLength = BitConverter.GetBytes(data.Length); + stream.Write(dataLength, 0, 4); //send to the other side the size of data! + int bytesSent = 0; + int bytesLeft = data.Length; + while (bytesLeft > 0) + { + int curDataSize = Math.Min(bufferSize, bytesLeft); + stream.Write(data, bytesSent, curDataSize); + bytesSent += curDataSize; + bytesLeft -= curDataSize; + } + } + + void sendScreenshot(byte[] data, NetworkStream stream) + { + int bufferSize = (1024 * 1024) / 2; + byte[] dataLength = BitConverter.GetBytes(data.Length); + stream.Write(dataLength, 0, 4); //send to the other side the size of data! + int bytesSent = 0; + int bytesLeft = data.Length; + while (bytesLeft > 0) + { + int curDataSize = Math.Min(bufferSize, bytesLeft); + stream.Write(data, bytesSent, curDataSize); + bytesSent += curDataSize; + bytesLeft -= curDataSize; + } + } + + public static string getBetween(string strSource, string strStart, string strEnd) + { + int Start, End; + if (strSource.Contains(strStart) && strSource.Contains(strEnd)) + { + Start = strSource.IndexOf(strStart, 0) + strStart.Length; + End = strSource.IndexOf(strEnd, Start); + return strSource.Substring(Start, End - Start); + } + else + { + return ""; + } + } + + public static string GetLocalNetworkIPV4() + { + string localIP = ""; + bool OpenPort = false; + using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) + { + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); + + for (int i = 60000; i < 65535; i++) + { + if (OpenPort) + { + Debug.WriteLine("Working Port Found"); + break; + } + + foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) + { + if (tcpi.LocalEndPoint.Port == i) + { + Debug.WriteLine(i + " Is In Use"); + break; + } + else + { + OpenPort = true; + socket.Connect("8.8.8.8", i); + IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; + localIP = endPoint.Address.ToString(); + break; + } + } + + } + } + + return localIP; + } + + Form2 f2; + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + try + { + if (TCP_client != null) + { + TCP_client.GetStream().Close(); + TCP_client.Close(); + } + + if (UDP_client != null) + { + UDP_client.Close(); + } + } + catch (Exception) { } + + if (TCP_listener != null) + { + if (TCP_listener.Server.IsBound) + { + TCP_listener.Stop(); + } + } + } + + bool StartServer = false; + private async void StartButton_Click(object sender, EventArgs e) + { + StartServer = !StartServer; + + if (StartServer) + { + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); + + foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) + { + if (tcpi.LocalEndPoint.Port == int.Parse(TCP_PortTextBox.Text)) + { + MessageBox.Show("Could not open port \"" + TCP_PortTextBox.Text + "\" as it is already in use!", "Error Opening Port", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + StartServer = false; + return; + } else if (tcpi.LocalEndPoint.Port == int.Parse(UDP_PortTextBox.Text)) + { + MessageBox.Show("Could not open port \"" + TCP_PortTextBox.Text + "\" as it is already in use!", "Error Opening Port", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + StartServer = false; + return; + } + } + + TCP_PortTextBox.ReadOnly = true; + TCP_PortTextBox.BackColor = System.Drawing.SystemColors.Window; + + StartButton.Text = "Stop Server"; + + PortForward(false, true); + + f2.Text = "Server Status: Online"; + f2.IPLabel.Text = "Connect: " + IPstring + ", " + TCP_PortTextBox.Text + ", " + UDP_PortTextBox.Text; + f2.SessionTimeLabel.Text = GetSessionTime(); + LogAdd("Session Started @ " + DateTime.Now.ToString("MM/dd/yy") + " - " + DateTime.Now.ToString("HH:mm:ss")); + + TCP_ipEP = new IPEndPoint(IPAddress.Parse(IPstring), int.Parse(TCP_PortTextBox.Text)); //allow a way to set the port in the future + TCP_listener = new TcpListener(TCP_ipEP); + + UDP_ipEP = new IPEndPoint(IPAddress.Parse(IPstring), int.Parse(UDP_PortTextBox.Text)); + UDP_client = new UdpClient(UDP_ipEP); + + worker = new BackgroundWorker(); + worker.DoWork += ListenToClient; + worker.RunWorkerCompleted += Worker_RunWorkerCompleted; + worker.RunWorkerAsync(); + TCP_PortLabel.Text = TCP_PortTextBox.Text; + + //await Task.Run(() => SetUpPortMap()); + } + else + { + TCP_PortTextBox.ReadOnly = false; + StartButton.Text = "Start Server"; + TCP_PortLabel.Text = "Port: (Not Selected)"; + f2.Text = "Server Status: Offline"; + f2.IPLabel.Text = "Server Offline"; + f2.SessionTimeLabel.Text = "00:00:00"; + ResetServer(StartServer); + } + } + + private void TCP_PortTextBox_KeyPress(object sender, KeyPressEventArgs e) + { + if (TCP_PortTextBox.TextLength >= 5 && e.KeyChar != (char)8) + { + e.Handled = true; + } + + if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)8)) + { + e.Handled = true; + } + } + + private void TCP_PortTextBox_KeyUp(object sender, EventArgs e) + { + + if (TCP_PortTextBox.TextLength > 0 && UDP_PortTextBox.TextLength > 0) + { + if (int.Parse(TCP_PortTextBox.Text) > 0 && int.Parse(TCP_PortTextBox.Text) < 65536 && int.Parse(UDP_PortTextBox.Text) > 0 && int.Parse(UDP_PortTextBox.Text) < 65536) + { + StartButton.Enabled = false; + } + else + StartButton.Enabled = true; + } + else + StartButton.Enabled = false; + } + + private void UDP_PortTextBox_KeyPress(object sender, KeyPressEventArgs e) + { + if (UDP_PortTextBox.TextLength >= 5 && e.KeyChar != (char)8) + { + e.Handled = true; + } + + if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)8)) + { + e.Handled = true; + } + } + + private void UDP_PortTextBox_KeyUp(object sender, EventArgs e) + { + + if (TCP_PortTextBox.TextLength > 0 && UDP_PortTextBox.TextLength > 0) + { + if (int.Parse(TCP_PortTextBox.Text) > 0 && int.Parse(TCP_PortTextBox.Text) < 65536 && int.Parse(UDP_PortTextBox.Text) > 0 && int.Parse(UDP_PortTextBox.Text) < 65536) + { + StartButton.Enabled = true; + } + else + StartButton.Enabled = false; + } + else + StartButton.Enabled = false; + } + + + public static bool DebugButtonEnabled = false; + private void DebugButton_Click(object sender, EventArgs e) + { + DebugButtonEnabled = !DebugButtonEnabled; + if (DebugButtonEnabled) + { + f2.BringToFront(); + f2.Show(); + } + else + { + f2.Hide(); + } + } + + int Hour; + int Minute; + int Second; + public string GetSessionTime() + { + Hour = SessionTime / 3600; + Minute = (SessionTime % 3600) / 60; + Second = SessionTime % 60; + return String.Format("{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second); + } + + public void LogAdd(string input) + { + if (f2.LogTextBox.InvokeRequired) + { + f2.LogTextBox.Invoke(new Action(() => { + f2.LogTextBox.Text += DateTime.Now.ToString("[MM/dd - HH:mm:ss]") + " - " + input + "\r\n\r\n"; + f2.LogTextBox.SelectionStart = f2.LogTextBox.TextLength; + f2.LogTextBox.ScrollToCaret(); + })); + } + else + { + f2.LogTextBox.Text += DateTime.Now.ToString("[MM/dd - HH:mm:ss]") + " - " + input + "\r\n\r\n"; + f2.LogTextBox.SelectionStart = f2.LogTextBox.TextLength; + f2.LogTextBox.ScrollToCaret(); + } + //f2.LogTextBox.ScrollToCaret(); + } + } +} diff --git a/src/Form1.resx b/src/Form1.resx new file mode 100755 index 0000000..aac33d5 --- /dev/null +++ b/src/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/src/Form2.Designer.cs b/src/Form2.Designer.cs new file mode 100755 index 0000000..daf4e20 --- /dev/null +++ b/src/Form2.Designer.cs @@ -0,0 +1,87 @@ +namespace TCPCommander +{ + partial class Form2 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.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.IPLabel = new System.Windows.Forms.Label(); + this.LogTextBox = new System.Windows.Forms.TextBox(); + this.SessionTimeLabel = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // IPLabel + // + this.IPLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.IPLabel.Location = new System.Drawing.Point(314, 45); + this.IPLabel.Name = "IPLabel"; + this.IPLabel.Size = new System.Drawing.Size(278, 49); + this.IPLabel.TabIndex = 1; + this.IPLabel.Text = "Status"; + this.IPLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // LogTextBox + // + this.LogTextBox.Location = new System.Drawing.Point(88, 179); + this.LogTextBox.Multiline = true; + this.LogTextBox.Name = "LogTextBox"; + this.LogTextBox.Size = new System.Drawing.Size(738, 259); + this.LogTextBox.TabIndex = 2; + // + // SessionTimeLabel + // + this.SessionTimeLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.SessionTimeLabel.Location = new System.Drawing.Point(314, 94); + this.SessionTimeLabel.Name = "SessionTimeLabel"; + this.SessionTimeLabel.Size = new System.Drawing.Size(278, 49); + this.SessionTimeLabel.TabIndex = 3; + this.SessionTimeLabel.Text = "Session Time"; + this.SessionTimeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // Form2 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(936, 450); + this.Controls.Add(this.SessionTimeLabel); + this.Controls.Add(this.LogTextBox); + this.Controls.Add(this.IPLabel); + this.Name = "Form2"; + this.Text = "Server Status:"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing); + this.Load += new System.EventHandler(this.Form2_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + public System.Windows.Forms.Label IPLabel; + public System.Windows.Forms.TextBox LogTextBox; + public System.Windows.Forms.Label SessionTimeLabel; + } +} \ No newline at end of file diff --git a/src/Form2.cs b/src/Form2.cs new file mode 100755 index 0000000..e8c3385 --- /dev/null +++ b/src/Form2.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace TCPCommander +{ + public partial class Form2 : Form + { + public Form2() + { + InitializeComponent(); + } + + private void Form2_Load(object sender, EventArgs e) + { + + } + + private void Form2_FormClosing(object sender, FormClosingEventArgs e) + { + Form1.DebugButtonEnabled = false; + this.Hide(); + e.Cancel = true; + } + } +} diff --git a/src/Form2.resx b/src/Form2.resx new file mode 100755 index 0000000..29dcb1b --- /dev/null +++ b/src/Form2.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/src/Form3.Designer.cs b/src/Form3.Designer.cs new file mode 100755 index 0000000..5aa60cb --- /dev/null +++ b/src/Form3.Designer.cs @@ -0,0 +1,103 @@ +namespace TCPCommander +{ + partial class Form3 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.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.components = new System.ComponentModel.Container(); + this.timer1 = new System.Windows.Forms.Timer(this.components); + this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // timer1 + // + this.timer1.Interval = 10; + this.timer1.Tick += new System.EventHandler(this.Timer1_Tick); + // + // notifyIcon1 + // + this.notifyIcon1.Text = "Computer Lock"; + this.notifyIcon1.Visible = true; + // + // label1 + // + this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 21F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.label1.ForeColor = System.Drawing.Color.White; + this.label1.Location = new System.Drawing.Point(12, 20); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(770, 65); + this.label1.TabIndex = 0; + this.label1.Text = "Your computer is currently locked by the Administrator"; + this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // label2 + // + this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.label2.ForeColor = System.Drawing.Color.White; + this.label2.Location = new System.Drawing.Point(12, 110); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(770, 50); + this.label2.TabIndex = 1; + this.label2.Text = "Please use your phone to unlock your computer."; + this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // Form3 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoSize = true; + this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(6)))), ((int)(((byte)(28)))), ((int)(((byte)(74))))); + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; + this.Name = "Form3"; + this.ShowIcon = false; + this.ShowInTaskbar = false; + this.Text = "Computer Lock"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); + this.Load += new System.EventHandler(this.Form1_Load); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Timer timer1; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + public System.Windows.Forms.NotifyIcon notifyIcon1; + } +} + diff --git a/src/Form3.cs b/src/Form3.cs new file mode 100755 index 0000000..01e4857 --- /dev/null +++ b/src/Form3.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Windows.Input; +using System.Threading; +using System.Management; +using System.IO; +using Microsoft.Win32; +using KeyboardHookMain; +using System.Security.Permissions; +//This project is using JSON.NET +namespace TCPCommander //need to make login form (form1), form for keybinding (form3), form for attempts and at what time +{ + public partial class Form3 : Form + { + public static bool isLocked = false; + + private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); + private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); + private const UInt32 SWP_NOSIZE = 0x0001; + private const UInt32 SWP_NOMOVE = 0x0002; + private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE; + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + public static extern bool ShutdownBlockReasonCreate(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string pwszReason); + + [DllImport("user32.dll")] + public static extern bool ShutdownBlockReasonDestroy(IntPtr hWnd); + + //[DllImport("user32.dll", SetLastError = true)] + //public static extern bool BlockInput(bool fBlockIt); + + protected override void WndProc(ref Message m) + { + const int WM_QUERYENDSESSION = 0x0011; + const int WM_ENDSESSION = 0x0016; + + if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION) + return; + + base.WndProc(ref m); + } + + public Form3() + { + InitializeComponent(); + } + + Form[] z; + private void Form1_Load(object sender, EventArgs e) + { + + //BlockInput(true); + AutoScaleMode = AutoScaleMode.Font; + WindowState = FormWindowState.Maximized; + notifyIcon1.Icon = SystemIcons.Shield; + + SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanging; + } + + int screenCount; + private void SystemEvents_DisplaySettingsChanging(object sender, EventArgs e) + { + if (isLocked) + { + screenCount = Screen.AllScreens.Length; + + for (int a = 0; a < z.Length; a++) + { + if (z[a] != null) + { + z[a].Close(); + z[a].Dispose(); + } + } + z = new Form[screenCount - 1]; + for (int a = 0; a < z.Length; a++) + { + z[a] = new Form(); + z[a].FormBorderStyle = FormBorderStyle.None; + z[a].BackColor = Color.Black; + z[a].Location = Screen.AllScreens[a + 1].WorkingArea.Location; + + z[a].Show(); + + Rectangle bounds = Screen.AllScreens[a + 1].Bounds; + z[a].SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); + } + } + } + + private void MenuItem4_Click(object sender, EventArgs e) + { + Application.Exit(); + } + + private void Timer1_Tick(object sender, EventArgs e) + { + + } + + public bool PreFilterMessage(ref Message m) + { + if (m.Msg == 0x201 || m.Msg == 0x202 || m.Msg == 0x203) return true; + if (m.Msg == 0x204 || m.Msg == 0x205 || m.Msg == 0x206) return true; + return false; + } + + public void LockPC() + { + this.Show(); + SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); + + Cursor.Hide(); + + + + KeyboardHookMain.KeyboardHook.EngageFullKeyboardLockdown(); + //mmf.DisableMouse(); + + StopShutdown("(Shutdown Disabled)\nReason: Lock Exploitation Protection"); + + this.Opacity = 1; + + notifyIcon1.Visible = true; + notifyIcon1.BalloonTipTitle = ("Computer Lock"); + notifyIcon1.BalloonTipText = ("Your computer has been locked."); + notifyIcon1.ShowBalloonTip(5000); + isLocked = true; + + screenCount = Screen.AllScreens.Length; + z = new Form[screenCount - 1]; + if (screenCount > 1) + { + for (int a = 0; a < z.Length; a++) + { + Debug.WriteLine(Application.OpenForms.Count); + + z[a] = new Form(); + z[a].FormBorderStyle = FormBorderStyle.None; + z[a].BackColor = Color.Black; + z[a].Location = Screen.AllScreens[a + 1].WorkingArea.Location; + + z[a].Show(); + + Rectangle bounds = Screen.AllScreens[a + 1].Bounds; + z[a].SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); + } + } + } + + public void UnlockPC() + { + this.Hide(); + SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); + + Cursor.Show(); + ResetShutdown(); + KeyboardHookMain.KeyboardHook.ReleaseFullKeyboardLockdown(); + //mmf.EnableMouse(); + + this.Opacity = 0.0; + + notifyIcon1.BalloonTipTitle = "Computer Lock"; + notifyIcon1.BalloonTipText = "Your computer has been unlocked."; + notifyIcon1.ShowBalloonTip(5000); + isLocked = false; + + for (int a = 0; a < z.Length; a++) + { + z[a].Close(); + z[a].Dispose(); + } + + } + + private void StopShutdown(string strMsg) + { + try + { + if (ShutdownBlockReasonCreate(this.Handle, strMsg)) + { + Debug.WriteLine("Shutdown aborted."); + } + else + { + Debug.WriteLine("Shutdown couldn't be aborted."); + } + } + catch (Exception e) + { + Console.WriteLine("StopShutdown Error: " + e.Message + " " + e.StackTrace); + } + } + + private void ResetShutdown() + { + try + { + if (ShutdownBlockReasonDestroy(this.Handle)) + { + Debug.WriteLine("Shutdown Reset"); + } + else + { + Debug.WriteLine("Shutdown couldn't be reset"); + } + } + catch (Exception e) + { + Debug.WriteLine("ResetShutdown Error: " + e.Message + " " + e.StackTrace); + } + } + + private void Form1_Paint(object sender, PaintEventArgs e) + { + Pen p = new Pen(Color.White); / + p.Width = 5; + Graphics g = e.Graphics; + g.DrawRectangle(p, new Rectangle(label1.Location.X - 5, label1.Location.Y - 5, label1.Width + 10, label1.Height + 10)); + g.DrawRectangle(p, new Rectangle(label2.Location.X - 5, label2.Location.Y - 5, label2.Width + 10, label2.Height + 10)); + } + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + notifyIcon1.BalloonTipClosed += (s, args) => + { + var thisIcon = (NotifyIcon)sender; + thisIcon.Visible = false; + thisIcon.Dispose(); + SystemEvents.DisplaySettingsChanging -= SystemEvents_DisplaySettingsChanging; + }; + } + } +} diff --git a/src/Form3.resx b/src/Form3.resx new file mode 100755 index 0000000..18a13d0 --- /dev/null +++ b/src/Form3.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + 104, 17 + + \ No newline at end of file diff --git a/src/KeyboardLock.cs b/src/KeyboardLock.cs new file mode 100755 index 0000000..3ee2db4 --- /dev/null +++ b/src/KeyboardLock.cs @@ -0,0 +1,1733 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; +using System.Linq; +using System.Runtime.InteropServices; +using System.Security.Permissions; +using System.Text; +using System.Windows.Forms; + + +namespace KeyboardHookMain //Was first created by refactorsaurusrex +{ + /// + /// Equivalent of the Keys enumerations, but includes a value allowing Windows Logo keys to act as a modifer key. + /// + [Flags] + public enum KeysEx + { + /// + /// The bitmask to extract modifiers from a key value. + /// + Modifiers = -65536, + /// + /// No key pressed. + /// + None = 0, + // + // Summary: + // The left mouse button. + LButton = 1, + // + // Summary: + // The right mouse button. + RButton = 2, + // + // Summary: + // The CANCEL key. + Cancel = 3, + // + // Summary: + // The middle mouse button (three-button mouse). + MButton = 4, + // + // Summary: + // The first x mouse button (five-button mouse). + XButton1 = 5, + // + // Summary: + // The second x mouse button (five-button mouse). + XButton2 = 6, + // + // Summary: + // The BACKSPACE key. + Back = 8, + // + // Summary: + // The TAB key. + Tab = 9, + // + // Summary: + // The LINEFEED key. + LineFeed = 10, + // + // Summary: + // The CLEAR key. + Clear = 12, + // + // Summary: + // The ENTER key. + Enter = 13, + // + // Summary: + // The RETURN key. + Return = 13, + // + // Summary: + // The SHIFT key. + ShiftKey = 16, + // + // Summary: + // The CTRL key. + ControlKey = 17, + // + // Summary: + // The ALT key. + Menu = 18, + // + // Summary: + // The PAUSE key. + Pause = 19, + // + // Summary: + // The CAPS LOCK key. + CapsLock = 20, + // + // Summary: + // The CAPS LOCK key. + Capital = 20, + // + // Summary: + // The IME Kana mode key. + KanaMode = 21, + // + // Summary: + // The IME Hanguel mode key. (maintained for compatibility; use HangulMode) + HanguelMode = 21, + // + // Summary: + // The IME Hangul mode key. + HangulMode = 21, + // + // Summary: + // The IME Junja mode key. + JunjaMode = 23, + // + // Summary: + // The IME final mode key. + FinalMode = 24, + // + // Summary: + // The IME Kanji mode key. + KanjiMode = 25, + // + // Summary: + // The IME Hanja mode key. + HanjaMode = 25, + // + // Summary: + // The ESC key. + Escape = 27, + // + // Summary: + // The IME convert key. + IMEConvert = 28, + // + // Summary: + // The IME nonconvert key. + IMENonconvert = 29, + // + // Summary: + // The IME accept key. Obsolete, use System.Windows.Forms.Keys.IMEAccept instead. + IMEAceept = 30, + // + // Summary: + // The IME accept key, replaces System.Windows.Forms.Keys.IMEAceept. + IMEAccept = 30, + // + // Summary: + // The IME mode change key. + IMEModeChange = 31, + // + // Summary: + // The SPACEBAR key. + Space = 32, + // + // Summary: + // The PAGE UP key. + Prior = 33, + // + // Summary: + // The PAGE UP key. + PageUp = 33, + // + // Summary: + // The PAGE DOWN key. + Next = 34, + // + // Summary: + // The PAGE DOWN key. + PageDown = 34, + // + // Summary: + // The END key. + End = 35, + // + // Summary: + // The HOME key. + Home = 36, + // + // Summary: + // The LEFT ARROW key. + Left = 37, + // + // Summary: + // The UP ARROW key. + Up = 38, + // + // Summary: + // The RIGHT ARROW key. + Right = 39, + // + // Summary: + // The DOWN ARROW key. + Down = 40, + // + // Summary: + // The SELECT key. + Select = 41, + // + // Summary: + // The PRINT key. + Print = 42, + // + // Summary: + // The EXECUTE key. + Execute = 43, + // + // Summary: + // The PRINT SCREEN key. + PrintScreen = 44, + // + // Summary: + // The PRINT SCREEN key. + Snapshot = 44, + // + // Summary: + // The INS key. + Insert = 45, + // + // Summary: + // The DEL key. + Delete = 46, + // + // Summary: + // The HELP key. + Help = 47, + // + // Summary: + // The 0 key. + D0 = 48, + // + // Summary: + // The 1 key. + D1 = 49, + // + // Summary: + // The 2 key. + D2 = 50, + // + // Summary: + // The 3 key. + D3 = 51, + // + // Summary: + // The 4 key. + D4 = 52, + // + // Summary: + // The 5 key. + D5 = 53, + // + // Summary: + // The 6 key. + D6 = 54, + // + // Summary: + // The 7 key. + D7 = 55, + // + // Summary: + // The 8 key. + D8 = 56, + // + // Summary: + // The 9 key. + D9 = 57, + // + // Summary: + // The A key. + A = 65, + // + // Summary: + // The B key. + B = 66, + // + // Summary: + // The C key. + C = 67, + // + // Summary: + // The D key. + D = 68, + // + // Summary: + // The E key. + E = 69, + // + // Summary: + // The F key. + F = 70, + // + // Summary: + // The G key. + G = 71, + // + // Summary: + // The H key. + H = 72, + // + // Summary: + // The I key. + I = 73, + // + // Summary: + // The J key. + J = 74, + // + // Summary: + // The K key. + K = 75, + // + // Summary: + // The L key. + L = 76, + // + // Summary: + // The M key. + M = 77, + // + // Summary: + // The N key. + N = 78, + // + // Summary: + // The O key. + O = 79, + // + // Summary: + // The P key. + P = 80, + // + // Summary: + // The Q key. + Q = 81, + // + // Summary: + // The R key. + R = 82, + // + // Summary: + // The S key. + S = 83, + // + // Summary: + // The T key. + T = 84, + // + // Summary: + // The U key. + U = 85, + // + // Summary: + // The V key. + V = 86, + // + // Summary: + // The W key. + W = 87, + // + // Summary: + // The X key. + X = 88, + // + // Summary: + // The Y key. + Y = 89, + // + // Summary: + // The Z key. + Z = 90, + // + // Summary: + // The left Windows logo key (Microsoft Natural Keyboard). + LWin = 91, + // + // Summary: + // The right Windows logo key (Microsoft Natural Keyboard). + RWin = 92, + // + // Summary: + // The application key (Microsoft Natural Keyboard). + Apps = 93, + // + // Summary: + // The computer sleep key. + Sleep = 95, + // + // Summary: + // The 0 key on the numeric keypad. + NumPad0 = 96, + // + // Summary: + // The 1 key on the numeric keypad. + NumPad1 = 97, + // + // Summary: + // The 2 key on the numeric keypad. + NumPad2 = 98, + // + // Summary: + // The 3 key on the numeric keypad. + NumPad3 = 99, + // + // Summary: + // The 4 key on the numeric keypad. + NumPad4 = 100, + // + // Summary: + // The 5 key on the numeric keypad. + NumPad5 = 101, + // + // Summary: + // The 6 key on the numeric keypad. + NumPad6 = 102, + // + // Summary: + // The 7 key on the numeric keypad. + NumPad7 = 103, + // + // Summary: + // The 8 key on the numeric keypad. + NumPad8 = 104, + // + // Summary: + // The 9 key on the numeric keypad. + NumPad9 = 105, + // + // Summary: + // The multiply key. + Multiply = 106, + // + // Summary: + // The add key. + Add = 107, + // + // Summary: + // The separator key. + Separator = 108, + // + // Summary: + // The subtract key. + Subtract = 109, + // + // Summary: + // The decimal key. + Decimal = 110, + // + // Summary: + // The divide key. + Divide = 111, + // + // Summary: + // The F1 key. + F1 = 112, + // + // Summary: + // The F2 key. + F2 = 113, + // + // Summary: + // The F3 key. + F3 = 114, + // + // Summary: + // The F4 key. + F4 = 115, + // + // Summary: + // The F5 key. + F5 = 116, + // + // Summary: + // The F6 key. + F6 = 117, + // + // Summary: + // The F7 key. + F7 = 118, + // + // Summary: + // The F8 key. + F8 = 119, + // + // Summary: + // The F9 key. + F9 = 120, + // + // Summary: + // The F10 key. + F10 = 121, + // + // Summary: + // The F11 key. + F11 = 122, + // + // Summary: + // The F12 key. + F12 = 123, + // + // Summary: + // The F13 key. + F13 = 124, + // + // Summary: + // The F14 key. + F14 = 125, + // + // Summary: + // The F15 key. + F15 = 126, + // + // Summary: + // The F16 key. + F16 = 127, + // + // Summary: + // The F17 key. + F17 = 128, + // + // Summary: + // The F18 key. + F18 = 129, + // + // Summary: + // The F19 key. + F19 = 130, + // + // Summary: + // The F20 key. + F20 = 131, + // + // Summary: + // The F21 key. + F21 = 132, + // + // Summary: + // The F22 key. + F22 = 133, + // + // Summary: + // The F23 key. + F23 = 134, + // + // Summary: + // The F24 key. + F24 = 135, + // + // Summary: + // The NUM LOCK key. + NumLock = 144, + // + // Summary: + // The SCROLL LOCK key. + Scroll = 145, + // + // Summary: + // The left SHIFT key. + LShiftKey = 160, + // + // Summary: + // The right SHIFT key. + RShiftKey = 161, + // + // Summary: + // The left CTRL key. + LControlKey = 162, + // + // Summary: + // The right CTRL key. + RControlKey = 163, + // + // Summary: + // The left ALT key. + LMenu = 164, + // + // Summary: + // The right ALT key. + RMenu = 165, + // + // Summary: + // The browser back key (Windows 2000 or later). + BrowserBack = 166, + // + // Summary: + // The browser forward key (Windows 2000 or later). + BrowserForward = 167, + // + // Summary: + // The browser refresh key (Windows 2000 or later). + BrowserRefresh = 168, + // + // Summary: + // The browser stop key (Windows 2000 or later). + BrowserStop = 169, + // + // Summary: + // The browser search key (Windows 2000 or later). + BrowserSearch = 170, + // + // Summary: + // The browser favorites key (Windows 2000 or later). + BrowserFavorites = 171, + // + // Summary: + // The browser home key (Windows 2000 or later). + BrowserHome = 172, + // + // Summary: + // The volume mute key (Windows 2000 or later). + VolumeMute = 173, + // + // Summary: + // The volume down key (Windows 2000 or later). + VolumeDown = 174, + // + // Summary: + // The volume up key (Windows 2000 or later). + VolumeUp = 175, + /// + /// The media next track key (Windows 2000 or later). + /// + MediaNextTrack = 176, + /// + /// The media previous track key (Windows 2000 or later). + /// + MediaPreviousTrack = 177, + /// + /// The media Stop key (Windows 2000 or later). + /// + MediaStop = 178, + /// + /// The media play pause key (Windows 2000 or later). + /// + MediaPlayPause = 179, + /// + /// The launch mail key (Windows 2000 or later). + /// + LaunchMail = 180, + /// + /// The select media key (Windows 2000 or later). + /// + SelectMedia = 181, + /// + /// The start application one key (Windows 2000 or later). + /// + LaunchApplication1 = 182, + /// + /// The start application two key (Windows 2000 or later). + /// + LaunchApplication2 = 183, + /// + /// The OEM 1 key. + /// + Oem1 = 186, + /// + /// The OEM Semicolon key on a US standard keyboard (Windows 2000 or later). + /// + OemSemicolon = 186, + /// + /// The OEM plus key on any country/region keyboard (Windows 2000 or later). + /// + Oemplus = 187, + /// + /// The OEM comma key on any country/region keyboard (Windows 2000 or later). + /// + Oemcomma = 188, + /// + /// The OEM minus key on any country/region keyboard (Windows 2000 or later). + /// + OemMinus = 189, + /// + /// The OEM period key on any country/region keyboard (Windows 2000 or later). + /// + OemPeriod = 190, + /// + /// The OEM question mark key on a US standard keyboard (Windows 2000 or later). + /// + OemQuestion = 191, + /// + /// The OEM 2 key. + /// + Oem2 = 191, + /// + /// The OEM tilde key on a US standard keyboard (Windows 2000 or later). + /// + Oemtilde = 192, + /// + /// The OEM 3 key. + /// + Oem3 = 192, + /// + /// The OEM 4 key. + /// + Oem4 = 219, + /// + /// The OEM open bracket key on a US standard keyboard (Windows 2000 or later). + /// + OemOpenBrackets = 219, + /// + /// The OEM pipe key on a US standard keyboard (Windows 2000 or later). + /// + OemPipe = 220, + /// + /// The OEM 5 key. + /// + Oem5 = 220, + /// + /// The OEM 6 key. + /// + Oem6 = 221, + /// + /// The OEM close bracket key on a US standard keyboard (Windows 2000 or later). + /// + OemCloseBrackets = 221, + /// + /// The OEM 7 key. + /// + Oem7 = 222, + /// + /// The OEM singled/double quote key on a US standard keyboard (Windows 2000 + /// or later). + /// + OemQuotes = 222, + /// + /// The OEM 8 key. + /// + Oem8 = 223, + /// + /// The OEM 102 key. + /// + Oem102 = 226, + /// + /// The OEM angle bracket or backslash key on the RT 102 key keyboard (Windows + /// 2000 or later). + /// + OemBackslash = 226, + /// + /// The PROCESS KEY key. + /// + ProcessKey = 229, + /// + /// Used to pass Unicode characters as if they were keystrokes. The Packet key + /// value is the low word of a 32-bit virtual-key value used for non-keyboard + /// input methods. + /// + Packet = 231, + /// + /// The ATTN key. + /// + Attn = 246, + /// + /// The CRSEL key. + /// + Crsel = 247, + /// + /// The EXSEL key. + /// + Exsel = 248, + /// + /// The ERASE EOF key. + /// + EraseEof = 249, + /// + /// The PLAY key. + /// + Play = 250, + /// + /// The ZOOM key. + /// + Zoom = 251, + /// + /// A constant reserved for future use. + /// + NoName = 252, + /// + /// The PA1 key. + /// + Pa1 = 253, + /// + /// The CLEAR key. + /// + OemClear = 254, + /// + /// The bitmask to extract a key code from a key value. + /// + KeyCode = 65535, + /// + /// The SHIFT modifier key. + /// + Shift = 65536, + /// + /// The CTRL modifier key. + /// + Control = 131072, + /// + /// The ALT modifier key. + /// + Alt = 262144, + /// + /// The WINLOGO modifier key. + /// + WinLogo = 524288 + } + + public enum KeyState + { + /// + /// Indicates that the key is pressed down. + /// + Down, + /// + /// Indicates that the key has been released. + /// + Up + } + + static class NativeMethods + { + internal delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + internal static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool UnhookWindowsHookEx(IntPtr hhk); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + internal static extern short GetKeyState(int nVirtKey); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + internal static extern uint MapVirtualKey(uint uCode, uint uMapType); + + internal static class KeyStateConstants + { + internal const int KEY_PRESSED = 0x8000; + internal const int WM_KEYDOWN = 0x0100; + internal const int WM_KEYUP = 0x0101; + internal const int WM_SYSKEYDOWN = 0x0104; + internal const int WM_SYSKEYUP = 0x0105; + internal const int WH_KEYBOARD_LL = 13; + } + } + + public class KeyCombination : IEquatable + { + /// + /// The backing keys for this combination. + /// + KeysEx keyChain; + + /// + /// Creates a new KeyCombination based on the provided archetype. The new combination is a deep copy + /// of the original. + /// + /// The combination to copy from. + /// A new KeyCombination object. + public static KeyCombination CopyFrom(KeyCombination combo) + { + return new KeyCombination(combo.Keys); + } + + /// + /// Initializes a new instance of the class. + /// + /// The keys that constitute a keyboard combination. + public KeyCombination(Keys keys) + { + keyChain = keys.ToKeysEx(); + } + + /// + /// Initializes a new instance of the class. + /// + /// The keys that constitute a keyboard combination. + public KeyCombination(KeysEx keys) + { + keyChain = keys; + } + + /// + /// Initializes a new instance of the class. + /// + /// The keys that constitute a keyboard combination, express in the following + /// format: "Modifer + Modifier + Key". + public KeyCombination(string keys) + { + if (!string.IsNullOrEmpty(keys)) + FromStringToKeys(keys); + } + + /// + /// Returns true if the values of its operands are equal, otherwise false. + /// + public static bool operator ==(KeyCombination x, KeyCombination y) + { + if (ReferenceEquals(null, x)) + return ReferenceEquals(null, y); + + return x.Equals(y); + } + + /// + /// returns false if its operands are equal, otherwise true. + /// + public static bool operator !=(KeyCombination x, KeyCombination y) + { + return !(x == y); + } + + /// + /// Adds the specified keys to this combination. Note that a combination can only contain a single + /// non-modifier key. If one already exists, it will be overwritten. + /// + /// The keys to add. + public void Add(KeysEx keys) + { + KeysEx keyCode = keys & KeysEx.KeyCode; + + if (keyCode != KeysEx.None) + keyChain = keyCode | keyChain & KeysEx.Modifiers; + + KeysEx modifiers = keys & KeysEx.Modifiers; + + if (modifiers != KeysEx.None) + keyChain = modifiers | keyChain & KeysEx.KeyCode | keyChain & KeysEx.Modifiers; + } + + /// + /// Removes the specified key from this combination. Must be called once for each key to be removed. + /// + /// The key to remove. + public void Remove(KeysEx key) + { + if (Contains(key)) + keyChain ^= key; + + if (key == KeysEx.LWin || key == KeysEx.RWin) + { + if (Contains(KeysEx.WinLogo)) + keyChain ^= KeysEx.WinLogo; + } + } + + /// + /// Determines whether the specified key exists in this combination. + /// + /// The key. + /// True if the specified key exists; otherwise, false. + public bool Contains(KeysEx key) + { + switch (key) + { + case KeysEx.Control: + case KeysEx.Shift: + case KeysEx.Alt: + case KeysEx.WinLogo: + return (keyChain & KeysEx.Modifiers) == key; + + default: + return (keyChain & KeysEx.KeyCode) == key; + } + } + + /// + /// Gets the keys that constitute this combination. + /// + public KeysEx Keys + { + get { return keyChain; } + } + + /// + /// Gets a value indicating whether this instance is empty. + /// + public bool IsEmpty + { + get { return Keys == KeysEx.None; } + } + + /// + /// Gets the unmodified key for this combination. + /// + public KeysEx UnmodifiedKey + { + get { return keyChain & KeysEx.KeyCode; } + } + + /// + /// Returns a that represents this instance. + /// + /// A that represents this instance. + public override string ToString() + { + return keyChain.ToFriendlyString(); + } + + /// + /// Gets a value indicating whether this combination is valid. + /// + /// A valid combination is one which can be used as a keyboard shortcut. Certain + /// keys such as Enter or CapsLock cannot be used for a shortcut. A combination also cannot + /// consist solely of modifier keys. + public bool IsValid + { + get + { + KeysEx keyCode = keyChain & KeysEx.KeyCode; + switch (keyCode) + { + case KeysEx.Enter: + case KeysEx.CapsLock: + case KeysEx.NumLock: + case KeysEx.Tab: + case KeysEx.None: + case KeysEx.ShiftKey: + case KeysEx.LShiftKey: + case KeysEx.RShiftKey: + case KeysEx.ControlKey: + case KeysEx.LControlKey: + case KeysEx.RControlKey: + case KeysEx.Menu: + case KeysEx.LMenu: + case KeysEx.RMenu: + case KeysEx.LWin: + case KeysEx.RWin: + return false; + + case KeysEx.Delete: + if ((keyChain & KeysEx.Modifiers) == (KeysEx.Control | KeysEx.Alt)) + return false; + break; + } + + return true; + } + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with the current . + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) + { + return Equals(obj as KeyCombination); + } + + /// + /// Returns a hash code for this instance. + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() + { + unchecked + { + return 17 * 23 + ToString().GetHashCode(); + } + } + + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// An object to compare with this object. + /// true if the current object is equal to the parameter; otherwise, false. + public bool Equals(KeyCombination other) + { + if (ReferenceEquals(null, other)) + return false; + + return ToString() == other.ToString(); + } + + /// + /// Froms the string to keys. + /// + /// The keys. + void FromStringToKeys(string keys) + { + IEnumerable segments = keys.Split(new[] { '+' }); + + foreach (string segment in segments) + { + string modifiedSegment = segment.ToLowerInvariant().Trim() == "ctrl" ? "Control" : segment; + keyChain |= EnumParser.Parse(modifiedSegment.Trim()); + } + } + } + + public static class EnumParser + { + /// + /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. + /// + /// The enum type. + /// A string containing the name or value to convert. + public static T Parse(string value) where T : struct + { + return (T)Enum.Parse(typeof(T), value); + } + + /// + /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. + /// + /// The enum type. + /// A string containing the name or value to convert. + /// If true, ignore case; otherwise, regard case. + public static T Parse(string value, bool ignoreCase) where T : struct + { + return (T)Enum.Parse(typeof(T), value, ignoreCase); + } + } + + public static class Extensions + { + /// + /// Converts the specified Keys to a KeysEx enumeration. + /// + /// The keys to convert. + public static KeysEx ToKeysEx(this Keys keys) + { + return EnumParser.Parse(keys.ToString()); + } + } + + public static class KeysExtensions + { + /// + /// Returns a friendly string representation of this KeysEx instance. + /// + /// The keys to convert to a friendly string. + public static string ToFriendlyString(this KeysEx keys) + { + var friendlyString = new StringBuilder(); + + if (IsWinLogoModified(keys)) + friendlyString.Append("WinLogo + "); + + if (IsControlModified(keys)) + friendlyString.Append("Ctrl + "); + + if (IsShiftModified(keys)) + friendlyString.Append("Shift + "); + + if (IsAltModified(keys)) + friendlyString.Append("Alt + "); + + string unmodifiedKey = UnmodifiedKey(keys); + + if (string.IsNullOrEmpty(unmodifiedKey) && friendlyString.Length >= 3) + friendlyString.Remove(friendlyString.Length - 3, 3); + else + friendlyString.Append(unmodifiedKey); + + return friendlyString.ToString(); + } + + /// + /// Returns KeyCode portion of this instance. That is, the key stripped of modifiers. + /// + /// The keys to remove modifier keys from. + static string UnmodifiedKey(KeysEx keys) + { + KeysEx keyCode = keys & KeysEx.KeyCode; + + switch (keyCode) + { + case KeysEx.Menu: + case KeysEx.LMenu: + case KeysEx.RMenu: + case KeysEx.ShiftKey: + case KeysEx.LShiftKey: + case KeysEx.RShiftKey: + case KeysEx.ControlKey: + case KeysEx.LControlKey: + case KeysEx.RControlKey: + case KeysEx.LWin: + case KeysEx.RWin: + return string.Empty; + } + + switch (keyCode) + { + case KeysEx.D0: + case KeysEx.D1: + case KeysEx.D2: + case KeysEx.D3: + case KeysEx.D4: + case KeysEx.D5: + case KeysEx.D6: + case KeysEx.D7: + case KeysEx.D8: + case KeysEx.D9: + return keyCode.ToString().Remove(0, 1); + } + + if (keyCode.ToString().ToUpperInvariant().StartsWith("OEM")) + { + const uint convertToChar = 2; + uint keyLiteral = NativeMethods.MapVirtualKey((uint)keyCode, convertToChar); + return Convert.ToChar(keyLiteral).ToString(CultureInfo.InvariantCulture); + } + + return keyCode.ToString(); + } + + /// + /// Determines whether the specified keys contains the WinLogo modifier, the LWin key, or the RWin key. + /// + /// The keys to check for logo key modifiers. + /// True if this instance contains a logo key modifier; otherwise, false. + static bool IsWinLogoModified(KeysEx keys) + { + if ((keys & KeysEx.WinLogo) == KeysEx.WinLogo) + return true; + + KeysEx keyCode = keys & KeysEx.KeyCode; + switch (keyCode) + { + case KeysEx.LWin: + case KeysEx.RWin: + return true; + } + + return false; + } + + /// + /// Determines whether this enumeration contains the Alt modifier. + /// + /// The keys to check for the Alt modifier. + /// True if this enumeration contains the Alt modifier; otherwise false. + static bool IsAltModified(KeysEx keys) + { + if ((keys & KeysEx.Alt) == KeysEx.Alt) + return true; + + KeysEx keyCode = keys & KeysEx.KeyCode; + switch (keyCode) + { + case KeysEx.Menu: + case KeysEx.LMenu: + case KeysEx.RMenu: + return true; + } + + return false; + } + + /// + /// Determines whether this enumeration contains the Shift modifier. + /// + /// The keys to check for the Shift modifier. + /// True if this enumeration contains the Shift modifier; otherwise false. + static bool IsShiftModified(KeysEx keys) + { + if ((keys & KeysEx.Shift) == KeysEx.Shift) + return true; + + KeysEx keyCode = keys & KeysEx.KeyCode; + switch (keyCode) + { + case KeysEx.ShiftKey: + case KeysEx.LShiftKey: + case KeysEx.RShiftKey: + return true; + } + + return false; + } + + /// + /// Determines whether this enumeration contains the Control modifier. + /// + /// The keys to check for the Control modifier. + /// True if this enumeration contains the Control modifier; otherwise false. + static bool IsControlModified(KeysEx keys) + { + if ((keys & KeysEx.Control) == KeysEx.Control) + return true; + + KeysEx keyCode = keys & KeysEx.KeyCode; + switch (keyCode) + { + case KeysEx.ControlKey: + case KeysEx.LControlKey: + case KeysEx.RControlKey: + return true; + } + + return false; + } + } + + public class KeyboardHook : IDisposable + { + const int keyUp = NativeMethods.KeyStateConstants.WM_KEYUP; + const int systemKeyUp = NativeMethods.KeyStateConstants.WM_SYSKEYUP; + const int keyDown = NativeMethods.KeyStateConstants.WM_KEYDOWN; + const int systemKeyDown = NativeMethods.KeyStateConstants.WM_SYSKEYDOWN; + + static readonly HashSet currentHookKeys = new HashSet(); + static readonly NativeMethods.LowLevelKeyboardProc lockdownHookCallBack = LockdownHookCallBack; + static bool isKeyboardLockedDown; + static IntPtr lockdownHookId; + static KeysEx lockedDownKeys; + + IntPtr hookId; + bool isDisposed; + bool keyReleased; + readonly NativeMethods.LowLevelKeyboardProc hookCallback; + + /// + /// Occurs when a key is pressed while keyboard lockdown is engaged. + /// + public static event EventHandler LockedDownKeyboardKeyPressed = delegate { }; + + /// + /// Engages a full keyboard lockdown, which disables all keyboard processing beyond the LockedDownKeyboardKeyPressed event. + /// + public static void EngageFullKeyboardLockdown() + { + lockdownHookId = NativeMethods.SetWindowsHookEx( + NativeMethods.KeyStateConstants.WH_KEYBOARD_LL, + lockdownHookCallBack, + IntPtr.Zero, + 0); + + if (lockdownHookId == IntPtr.Zero) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + isKeyboardLockedDown = true; + } + + /// + /// Releases keyboard lockdown and allows keyboard processing as normal. + /// + public static void ReleaseFullKeyboardLockdown() + { + if (lockdownHookId != IntPtr.Zero) + { + NativeMethods.UnhookWindowsHookEx(lockdownHookId); + lockdownHookId = IntPtr.Zero; + } + + isKeyboardLockedDown = false; + } + + /// + /// Determines whether the specified key combination is already in use. + /// + /// The combination to check. + /// True if the key combination is already in use; otherwise, false. + public static bool IsKeyCombinationTaken(KeyCombination combination) + { + return currentHookKeys.Contains(combination); + } + + /// + /// Raises the KeyboardLockDownKeyPressed event by invoking each subscribed delegate asynchronously. + /// + /// The keys that are pressed. + /// The state of the keys. + static void OnLockedDownKeyboardKeyPressed(KeysEx pressedKeys, KeyState state) + { + foreach (EventHandler pressedEvent in LockedDownKeyboardKeyPressed.GetInvocationList()) + { + var args = new KeyboardLockDownKeyPressedEventArgs(pressedKeys, state); + AsyncCallback callback = ar => ((EventHandler)ar.AsyncState).EndInvoke(ar); + pressedEvent.BeginInvoke(null, args, callback, pressedEvent); + } + } + + /// + /// Returns a value indicating whether a non-modifier key is currently pressed. + /// + /// The key to check. Key must not be a modifier bit mask. + /// True if the key is currently pressed; otherwise false. + /// Occurs if key is a modifier bit mask. + static bool IsKeyPressed(KeysEx key) + { + if ((key & KeysEx.Modifiers) == KeysEx.Modifiers) + { + throw new ArgumentException("Key cannot contain any modifiers.", "key"); + } + + const int keyPressed = NativeMethods.KeyStateConstants.KEY_PRESSED; + return (NativeMethods.GetKeyState((int)key) & keyPressed) == keyPressed; + } + + /// + /// Callback handler for keyboard lockdowns. + /// + static IntPtr LockdownHookCallBack(int nCode, IntPtr wParam, IntPtr lParam) + { + int keyStateParam = (int)wParam; + KeysEx pressedKey = (KeysEx)Marshal.ReadInt32(lParam); + + switch (pressedKey) + { + case KeysEx.LControlKey: + case KeysEx.RControlKey: + pressedKey = KeysEx.Control; + break; + + case KeysEx.RMenu: + case KeysEx.LMenu: + pressedKey = KeysEx.Alt; + break; + + case KeysEx.LShiftKey: + case KeysEx.RShiftKey: + pressedKey = KeysEx.Shift; + break; + + case KeysEx.LWin: + case KeysEx.RWin: + pressedKey = KeysEx.WinLogo; + break; + } + + KeyState keyState; + if (keyStateParam == keyUp || keyStateParam == systemKeyUp) + { + keyState = KeyState.Up; + lockedDownKeys ^= pressedKey; + } + else if (keyStateParam == keyDown || keyStateParam == systemKeyDown) + { + keyState = KeyState.Down; + lockedDownKeys |= pressedKey; + } + else + { + throw new ArgumentOutOfRangeException("wParam", "Invalid key state detected."); + } + + OnLockedDownKeyboardKeyPressed(lockedDownKeys, keyState); + return new IntPtr(1); + } + + /// + /// Initializes a new instance of the KeyboardHook class. + /// + public KeyboardHook() + { + hookCallback = HookCallBack; + Combination = new KeyCombination(KeysEx.None); + } + + /// + /// Finalizes an instance of the KeyboardHook class. + /// + ~KeyboardHook() + { + Dispose(false); + } + + /// + /// Occurs when the KeyboardHook's assigned key combination is pressed. + /// + public event EventHandler Pressed = delegate { }; + + /// + /// Gets the key combination for this hook. + /// + public KeyCombination Combination { get; private set; } + + /// + /// Gets or sets a value indicating whether to allow normal processing of key strokes after + /// the hook has finished processing it. + /// + public bool AllowPassThrough { get; set; } + + /// + /// Gets or sets a value indicating whether the KeyboardHook Pressed event is fired repeatedly for the duration + /// of the key press or only once per key press. + /// + public bool AutoRepeat { get; set; } + + /// + /// Gets a value indicating whether the KeyboardHook is currently active. + /// + public bool IsEngaged + { + get { return hookId != IntPtr.Zero; } + } + + /// + /// Removes the keys associated with this hook. This can only be performed when the hook is not active. + /// + /// Occurs if the hook is currently engaged. + public void RemoveKeys() //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Was only public void before + { + if (Combination.Keys == KeysEx.None) + return; + + if (IsEngaged) + throw new InvalidOperationException("Cannot remove keys while hook is engaged."); + + if (Combination != null && currentHookKeys.Contains(Combination)) + currentHookKeys.Remove(Combination); + + Combination = new KeyCombination(KeysEx.None); + } + + /// + /// Associates the specified key combination with this hook. This can only be performed when the hook is not active. + /// + /// The key combination. + /// Occurs if this hook is currently engaged -OR- if the key combination is invalid + /// -OR- if the key combination is already in use by another hook. + public void SetKeys(KeyCombination combination) + { + if (Combination == combination) + return; + + if (IsEngaged) + throw new InvalidOperationException("Cannot set keys while hook is engaged."); + + if (!combination.IsValid) + throw new InvalidOperationException("Key combination is not valid."); + + if (currentHookKeys.Contains(combination)) + throw new InvalidOperationException(string.Format("The combination '{0}' is already in use.", combination)); + + if (Combination != null && currentHookKeys.Contains(Combination)) + currentHookKeys.Remove(Combination); + + currentHookKeys.Add(combination); + Combination = combination; + } + + /// + /// Activates the KeyboardHook. + /// + /// Occurs if the KeyboardHook is empty OR if the KeyboardHook is already engaged. + /// Occurs if the KeyboardHook has been disposed. + [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] + public void Engage() + { + if (isDisposed) + throw new ObjectDisposedException("KeyboardHook"); + + if (Combination == null) + throw new InvalidOperationException("Cannot engage hook when Combination is null."); + + if (IsEngaged) + return; + + hookId = NativeMethods.SetWindowsHookEx( + NativeMethods.KeyStateConstants.WH_KEYBOARD_LL, + hookCallback, + IntPtr.Zero, + 0); + + if (hookId == IntPtr.Zero) + throw new Win32Exception(Marshal.GetLastWin32Error()); + } + + /// + /// Removes the KeyboardHook from the system. + /// + /// Disengage removes the hook from the system, but maintains all its data. Use Engage to re-install the hook. To + /// discard the hook and all its data, use Dispose. It is not necessary to call Disengage prior to Dispose. + public void Disengage() + { + if (hookId != IntPtr.Zero) + { + NativeMethods.UnhookWindowsHookEx(hookId); + hookId = IntPtr.Zero; + } + } + + /// + /// Returns the string representation of the KeyboardHook. + /// + /// A string representing the KeyboardHook. + public override string ToString() + { + return Combination == null ? string.Empty : Combination.ToString(); + } + + /// + /// Disengages the KeyboardHook and releases all associated resources. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disengages the KeyboardHook, releases all unmanaged resources, and optionally releases + /// all managed resources. + /// + /// True to release both managed and unmanaged resources; false to + /// release only unmanaged resources. + protected virtual void Dispose(bool isDisposing) + { + if (isDisposed) + return; + + if (isDisposing) + { + // No managed resources to dispose. + } + + Disengage(); + + if (Combination != null) + currentHookKeys.Remove(Combination); + + isDisposed = true; + } + + /// + /// Raises the Pressed event. + /// + /// The Pressed event is executed asynchronously. + protected virtual void OnPressed() + { + // Invoke these asychronously in case they're slow. (Testing revealed that Windows will reassert + // responsibility for the key stroke(s) if the hookcallback doesn't immediately return.) + foreach (EventHandler pressedEvent in Pressed.GetInvocationList()) + pressedEvent.BeginInvoke(this, EventArgs.Empty, ar => ((EventHandler)ar.AsyncState).EndInvoke(ar), pressedEvent); + } + + /// + /// Determines whether the specified key, combined with all currently pressed modifier keys, matches this hook's key combination. + /// + /// The unmodified key to combine with all currently pressed keyboard modifier keys. + /// True if unmodifiedKey matches Combination.UnmodifiedKey and all modifier keys contained in the + /// Combination property are currently pressed; otherwise, false. + protected bool CheckIsHookKeyCombination(KeysEx unmodifiedKey) + { + KeyCombination pressedKeyCombo = new KeyCombination(unmodifiedKey); + + if (IsKeyPressed(KeysEx.ControlKey)) + pressedKeyCombo.Add(KeysEx.Control); + + if (IsKeyPressed(KeysEx.ShiftKey)) + pressedKeyCombo.Add(KeysEx.Shift); + + if (IsKeyPressed(KeysEx.LMenu) || IsKeyPressed(KeysEx.RMenu)) + pressedKeyCombo.Add(KeysEx.Alt); + + if ((IsKeyPressed(KeysEx.LWin) || IsKeyPressed(KeysEx.RWin))) + pressedKeyCombo.Add(KeysEx.WinLogo); + + return Combination == pressedKeyCombo; + } + + /// + /// The callback proceedure for the installed KeyboardHook. + /// + /// A code the KeyboardHook procedure uses to determine how to process the message. + /// The key state. + /// The key pressed. + /// A value indicating whether or not to process additional hooks in the current hook chain. + IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr lParam) + { + if (isKeyboardLockedDown) + return new IntPtr(1); // A non-zero return value blocks additional processing of key strokes. + + // MSDN documentation indicates that nCodes less than 0 should always only invoke CallNextHookEx. + if (nCode < 0) + return NativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam); + + int keyStateParam = (int)wParam; + KeyState keyState; + + if (keyStateParam == keyUp || keyStateParam == systemKeyUp) + keyState = KeyState.Up; + else if (keyStateParam == keyDown || keyStateParam == systemKeyDown) + keyState = KeyState.Down; + else + throw new ArgumentOutOfRangeException("wParam", "Invalid key state detected."); + + var pressedKey = (KeysEx)Marshal.ReadInt32(lParam); + + if (!CheckIsHookKeyCombination(pressedKey)) + return NativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam); + + if (keyState == KeyState.Up) + { + keyReleased = true; + return NativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam); + } + + // If AutoRepeat is on, always process hook; otherwise, only process hook if they key has been released and re-pressed. + if (AutoRepeat || keyReleased) + { + keyReleased = false; + OnPressed(); + } + + if (AllowPassThrough) + return NativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam); + + return new IntPtr(1); + } + } + + public class KeyboardLockDownKeyPressedEventArgs : EventArgs + { + /// + /// Initializes a new instance of the class. + /// + /// The keys that were pressed. + /// The state of the pressed keys. + /// + public KeyboardLockDownKeyPressedEventArgs(KeysEx keys, KeyState state) + { + Keys = keys; + State = state; + } + + /// + /// Gets the keys that were pressed. + /// + public KeysEx Keys { get; private set; } + + /// + /// Gets the state of the pressed keys. + /// + public KeyState State { get; private set; } + } +} \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs new file mode 100755 index 0000000..f409eea --- /dev/null +++ b/src/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace TCPCommander +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs new file mode 100755 index 0000000..ca7e21e --- /dev/null +++ b/src/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TCPCommander")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TCPCommander")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("f924fba5-b17d-4e39-8e5e-de79969c2bfe")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/Properties/Resources.Designer.cs b/src/Properties/Resources.Designer.cs new file mode 100755 index 0000000..0b21f0b --- /dev/null +++ b/src/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TCPCommander.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TCPCommander.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/src/Properties/Resources.resx b/src/Properties/Resources.resx new file mode 100755 index 0000000..ffecec8 --- /dev/null +++ b/src/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/src/Properties/Settings.Designer.cs b/src/Properties/Settings.Designer.cs new file mode 100755 index 0000000..05de363 --- /dev/null +++ b/src/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TCPCommander.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/src/Properties/Settings.settings b/src/Properties/Settings.settings new file mode 100755 index 0000000..abf36c5 --- /dev/null +++ b/src/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/TCPCommander.csproj b/src/TCPCommander.csproj new file mode 100755 index 0000000..1f46144 --- /dev/null +++ b/src/TCPCommander.csproj @@ -0,0 +1,120 @@ + + + + + Debug + AnyCPU + {F924FBA5-B17D-4E39-8E5E-DE79969C2BFE} + WinExe + TCPCommander + TCPCommander + v4.7.2 + 512 + true + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + packages\Open.NAT.2.1.0.0\lib\net45\Open.Nat.dll + + + + + + packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + True + True + + + + + + + + + + + + + + + Form + + + Form1.cs + + + Form + + + Form3.cs + + + Form + + + Form2.cs + + + + + + Form1.cs + + + Form3.cs + + + Form2.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + + + + + \ No newline at end of file diff --git a/src/TCPCommander.sln b/src/TCPCommander.sln new file mode 100755 index 0000000..2c2b0af --- /dev/null +++ b/src/TCPCommander.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.87 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TCPCommander", "TCPCommander.csproj", "{F924FBA5-B17D-4E39-8E5E-DE79969C2BFE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F924FBA5-B17D-4E39-8E5E-DE79969C2BFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F924FBA5-B17D-4E39-8E5E-DE79969C2BFE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F924FBA5-B17D-4E39-8E5E-DE79969C2BFE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F924FBA5-B17D-4E39-8E5E-DE79969C2BFE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {85233985-BFA9-4BFC-9ED6-E281E2F662A0} + EndGlobalSection +EndGlobal diff --git a/src/packages.config b/src/packages.config new file mode 100755 index 0000000..537740f --- /dev/null +++ b/src/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file