diff --git a/WoL/Waker/MainForm.Designer.cs b/WoL/Waker/MainForm.Designer.cs index 5283973..8d2bb4b 100644 --- a/WoL/Waker/MainForm.Designer.cs +++ b/WoL/Waker/MainForm.Designer.cs @@ -29,96 +29,135 @@ namespace Waker /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); - this.AddressTextBox = new System.Windows.Forms.TextBox(); - this.label1 = new System.Windows.Forms.Label(); - this.MacTextBox = new System.Windows.Forms.TextBox(); - this.label2 = new System.Windows.Forms.Label(); - this.WakeButton = new System.Windows.Forms.Button(); - this.label3 = new System.Windows.Forms.Label(); - this.PortTextBox = new System.Windows.Forms.TextBox(); - this.SuspendLayout(); - // - // AddressTextBox - // - this.AddressTextBox.Location = new System.Drawing.Point(67, 14); - this.AddressTextBox.Name = "AddressTextBox"; - this.AddressTextBox.Size = new System.Drawing.Size(198, 23); - this.AddressTextBox.TabIndex = 0; - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(12, 18); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(49, 15); - this.label1.TabIndex = 1; - this.label1.Text = "Address"; - // - // MacTextBox - // - this.MacTextBox.Location = new System.Drawing.Point(67, 43); - this.MacTextBox.Name = "MacTextBox"; - this.MacTextBox.Size = new System.Drawing.Size(198, 23); - this.MacTextBox.TabIndex = 1; - // - // label2 - // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(21, 47); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(30, 15); - this.label2.TabIndex = 1; - this.label2.Text = "mac"; - // - // WakeButton - // - this.WakeButton.Location = new System.Drawing.Point(271, 12); - this.WakeButton.Name = "WakeButton"; - this.WakeButton.Size = new System.Drawing.Size(102, 83); - this.WakeButton.TabIndex = 3; - this.WakeButton.Text = "WoL"; - this.WakeButton.UseVisualStyleBackColor = true; - this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click); - // - // label3 - // - this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(19, 76); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(34, 15); - this.label3.TabIndex = 3; - this.label3.Text = "ports"; - // - // PortTextBox - // - this.PortTextBox.Location = new System.Drawing.Point(67, 72); - this.PortTextBox.Name = "PortTextBox"; - this.PortTextBox.Size = new System.Drawing.Size(198, 23); - this.PortTextBox.TabIndex = 2; - this.PortTextBox.Text = "7, 9"; - // - // MainForm - // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(383, 105); - this.Controls.Add(this.label3); - this.Controls.Add(this.PortTextBox); - this.Controls.Add(this.WakeButton); - this.Controls.Add(this.label2); - this.Controls.Add(this.label1); - this.Controls.Add(this.MacTextBox); - this.Controls.Add(this.AddressTextBox); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.MaximumSize = new System.Drawing.Size(399, 144); - this.MinimumSize = new System.Drawing.Size(399, 144); - this.Name = "MainForm"; - this.Text = "Waker"; - this.ResumeLayout(false); - this.PerformLayout(); + this.components = new System.ComponentModel.Container(); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); + this.AddressTextBox = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.MacTextBox = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.WakeButton = new System.Windows.Forms.Button(); + this.label3 = new System.Windows.Forms.Label(); + this.PortTextBox = new System.Windows.Forms.TextBox(); + this.PingPortTextBox = new System.Windows.Forms.TextBox(); + this.label4 = new System.Windows.Forms.Label(); + this.PingStatusPanel = new System.Windows.Forms.Panel(); + this.pingTimer = new System.Windows.Forms.Timer(this.components); + this.SuspendLayout(); + // + // AddressTextBox + // + this.AddressTextBox.Location = new System.Drawing.Point(67, 14); + this.AddressTextBox.Name = "AddressTextBox"; + this.AddressTextBox.Size = new System.Drawing.Size(198, 23); + this.AddressTextBox.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 18); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(49, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Address"; + // + // MacTextBox + // + this.MacTextBox.Location = new System.Drawing.Point(67, 43); + this.MacTextBox.Name = "MacTextBox"; + this.MacTextBox.Size = new System.Drawing.Size(198, 23); + this.MacTextBox.TabIndex = 1; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(21, 47); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(30, 15); + this.label2.TabIndex = 1; + this.label2.Text = "mac"; + // + // WakeButton + // + this.WakeButton.Location = new System.Drawing.Point(271, 12); + this.WakeButton.Name = "WakeButton"; + this.WakeButton.Size = new System.Drawing.Size(102, 112); + this.WakeButton.TabIndex = 4; + this.WakeButton.Text = "WoL"; + this.WakeButton.UseVisualStyleBackColor = true; + this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click); + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(19, 76); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(34, 15); + this.label3.TabIndex = 3; + this.label3.Text = "ports"; + // + // PortTextBox + // + this.PortTextBox.Location = new System.Drawing.Point(67, 72); + this.PortTextBox.Name = "PortTextBox"; + this.PortTextBox.Size = new System.Drawing.Size(198, 23); + this.PortTextBox.TabIndex = 2; + this.PortTextBox.Text = "7, 9"; + // + // PingPortTextBox + // + this.PingPortTextBox.Location = new System.Drawing.Point(138, 101); + this.PingPortTextBox.Name = "PingPortTextBox"; + this.PingPortTextBox.Size = new System.Drawing.Size(98, 23); + this.PingPortTextBox.TabIndex = 3; + this.PingPortTextBox.TextChanged += new System.EventHandler(this.PingPortTextBox_TextChanged); + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(19, 105); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(113, 15); + this.label4.TabIndex = 3; + this.label4.Text = "ping port (optional)"; + // + // PingStatusPanel + // + this.PingStatusPanel.BackColor = System.Drawing.Color.Gray; + this.PingStatusPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.PingStatusPanel.Location = new System.Drawing.Point(242, 101); + this.PingStatusPanel.Name = "PingStatusPanel"; + this.PingStatusPanel.Size = new System.Drawing.Size(23, 23); + this.PingStatusPanel.TabIndex = 4; + // + // pingTimer + // + this.pingTimer.Interval = 5000; + this.pingTimer.Tick += new System.EventHandler(this.pingTimer_Tick); + // + // MainForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.BackColor = System.Drawing.Color.White; + this.ClientSize = new System.Drawing.Size(383, 134); + this.Controls.Add(this.PingStatusPanel); + this.Controls.Add(this.label4); + this.Controls.Add(this.label3); + this.Controls.Add(this.PingPortTextBox); + this.Controls.Add(this.PortTextBox); + this.Controls.Add(this.WakeButton); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.MacTextBox); + this.Controls.Add(this.AddressTextBox); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.MaximumSize = new System.Drawing.Size(399, 173); + this.MinimumSize = new System.Drawing.Size(399, 144); + this.Name = "MainForm"; + this.Text = "Waker"; + this.ResumeLayout(false); + this.PerformLayout(); } @@ -131,6 +170,10 @@ namespace Waker private System.Windows.Forms.Button WakeButton; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox PortTextBox; - } + private System.Windows.Forms.TextBox PingPortTextBox; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Panel PingStatusPanel; + private System.Windows.Forms.Timer pingTimer; + } } diff --git a/WoL/Waker/MainForm.cs b/WoL/Waker/MainForm.cs index 14a044f..8a8692f 100644 --- a/WoL/Waker/MainForm.cs +++ b/WoL/Waker/MainForm.cs @@ -9,14 +9,22 @@ using System.Threading.Tasks; using System.Windows.Forms; using WakeOnLan; using System.Configuration; +using System.Threading; namespace Waker { public partial class MainForm : Form { + private readonly Color PING_SUCCESS = Color.Green; + private readonly Color PING_FAIL = Color.Red; + private readonly Color PING_UNKNOWN = Color.Gray; + private string ip; private string mac; private string ports; + private string pingPort; + + private Thread pingThread; public MainForm() { @@ -37,14 +45,37 @@ namespace Waker if (!string.IsNullOrEmpty(Settings.Default.ports)) ports = Settings.Default.ports; + if (!string.IsNullOrEmpty(Settings.Default.pingPort)) + pingPort = Settings.Default.pingPort; + FillInformation(); + AddressTextBox.TextChanged += AddressTextBox_TextChanged; + AddressTextBox.KeyUp += TextBox_KeyUp; MacTextBox.KeyUp += TextBox_KeyUp; PortTextBox.KeyUp += TextBox_KeyUp; } - private bool GetInformation() + private void AddressTextBox_TextChanged(object sender, EventArgs e) + { + SetIp(); + } + + private bool SetIp() + { + ip = AddressTextBox.Text.Replace(" ", ""); + if (string.IsNullOrEmpty(ip)) + return false; + + AddressTextBox.Text = ip; + Settings.Default.ip = ip; + Settings.Default.Save(); + + return true; + } + + private bool SetInformation() { ip = AddressTextBox.Text.Replace(" ", ""); mac = MacTextBox.Text.Replace(" ", ""); @@ -76,11 +107,14 @@ namespace Waker if (!string.IsNullOrEmpty(ports)) PortTextBox.Text = ports; + + if (!string.IsNullOrEmpty(pingPort)) + PingPortTextBox.Text = pingPort; } private void WakeButton_Click(object sender, EventArgs e) { - if (!GetInformation()) + if (!SetInformation()) { MessageBox.Show("Invalid information"); return; @@ -127,5 +161,70 @@ namespace Waker WakeButton_Click(sender, e); } + + private void PingPortTextBox_TextChanged(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(PingPortTextBox.Text)) + return; + + pingPort = PingPortTextBox.Text; + + if (!pingTimer.Enabled) + pingTimer.Enabled = true; + } + + private void pingTimer_Tick(object sender, EventArgs e) + { + try + { + int targetPort = 0; + if (!int.TryParse(pingPort, out targetPort)) + { + PingStatusPanel.BackColor = PING_UNKNOWN; + return; + } + + if (targetPort < 1) + { + PingStatusPanel.BackColor = PING_UNKNOWN; + return; + } + + if (string.IsNullOrEmpty(ip)) + return; + + Settings.Default.pingPort = pingPort; + Settings.Default.Save(); + + object[] parameters = new object[2] { ip, targetPort }; + pingThread = new Thread(new ParameterizedThreadStart(SendPing)); + pingThread.Start(parameters); + + } catch (Exception ex) + { + pingTimer.Enabled = false; + MessageBox.Show(ex.Message); + } + } + + public void SendPing(object parameters) + { + object[] src = parameters as object[]; + if (src == null || src.Length < 2) + return; + + string ip = src[0] as string; + int? port = src[1] as int?; + + if (ip == null || port == null) + return; + + if (PingCheck.Test(ip, port == null ? default(int) : port.Value)) + { + PingStatusPanel.BackColor = PING_SUCCESS; + } + else + PingStatusPanel.BackColor = PING_FAIL; + } } } diff --git a/WoL/Waker/MainForm.resx b/WoL/Waker/MainForm.resx index 2f0e34a..b725ce6 100644 --- a/WoL/Waker/MainForm.resx +++ b/WoL/Waker/MainForm.resx @@ -57,6 +57,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + diff --git a/WoL/Waker/Settings.Designer.cs b/WoL/Waker/Settings.Designer.cs index 86cbe4e..7d04a7a 100644 --- a/WoL/Waker/Settings.Designer.cs +++ b/WoL/Waker/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace Waker { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -58,5 +58,17 @@ namespace Waker { this["ports"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string pingPort { + get { + return ((string)(this["pingPort"])); + } + set { + this["pingPort"] = value; + } + } } } diff --git a/WoL/Waker/Settings.settings b/WoL/Waker/Settings.settings index a944aed..0f49671 100644 --- a/WoL/Waker/Settings.settings +++ b/WoL/Waker/Settings.settings @@ -11,5 +11,8 @@ 7, 9 + + + \ No newline at end of file diff --git a/WoL/WoL/PingCheck.cs b/WoL/WoL/PingCheck.cs new file mode 100644 index 0000000..2cc2c83 --- /dev/null +++ b/WoL/WoL/PingCheck.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; + +namespace WakeOnLan +{ + public class PingCheck + { + public static bool Test(string ip, int port, int timeout = 5 * 1000) + { + Socket socket = null; + try + { + socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, false); + + IAsyncResult result = socket.BeginConnect(ip, port, null, null); + return result.AsyncWaitHandle.WaitOne(timeout, true); + } + catch (Exception ex) + { + throw ex; + } + finally + { + if (socket != null) + socket.Close(); + } + + + //Ping ping = new Ping(); + //PingOptions options = new PingOptions(); + //options.DontFragment = true; + + //string data = "123123123"; + //byte[] buffer = ASCIIEncoding.ASCII.GetBytes(data); + + //PingReply reply = ping.Send(ipAddress, , timeout, buffer); + //if (reply.Status == IPStatus.Success) + // return true; + + //return false; + } + } +} diff --git a/WoL/WoL/Utils.cs b/WoL/WoL/Utils.cs new file mode 100644 index 0000000..82fde55 --- /dev/null +++ b/WoL/WoL/Utils.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; + +namespace WakeOnLan +{ + class Utils + { + public static IPAddress GetIpAddress(string ip) + { + Regex regex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); + if (regex.IsMatch(ip)) + return IPAddress.Parse(ip); + + IPHostEntry hostEntry = Dns.GetHostEntry(ip); + if (hostEntry.AddressList.Length < 1) + throw new Exception("invalid ip address"); + + return hostEntry.AddressList[0]; + } + } +} diff --git a/WoL/WoL/WoL.cs b/WoL/WoL/WoL.cs index 035dae8..60f4885 100644 --- a/WoL/WoL/WoL.cs +++ b/WoL/WoL/WoL.cs @@ -19,7 +19,7 @@ namespace WakeOnLan UdpClient udp = new UdpClient(); udp.EnableBroadcast = true; - IPAddress ipAddress = GetIpAddress(ip); + IPAddress ipAddress = Utils.GetIpAddress(ip); if (ports == null || ports.Length < 1) ports = DEFAULT_PORTS; @@ -29,19 +29,6 @@ namespace WakeOnLan } } - private static IPAddress GetIpAddress(string ip) - { - Regex regex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); - if (regex.IsMatch(ip)) - return IPAddress.Parse(ip); - - IPHostEntry hostEntry = Dns.GetHostEntry(ip); - if (hostEntry.AddressList.Length < 1) - throw new Exception("invalid ip address"); - - return hostEntry.AddressList[0]; - } - private static byte[] GetWolPacket(string macAddress) { byte[] datagram = new byte[WOL_PACKET_LEN];