diff --git a/WoL/Waker/Designcontest-Ecommerce-Business-Idea.ico b/WoL/Waker/Designcontest-Ecommerce-Business-Idea.ico new file mode 100644 index 0000000..65a60da Binary files /dev/null and b/WoL/Waker/Designcontest-Ecommerce-Business-Idea.ico differ diff --git a/WoL/Waker/MainForm.Designer.cs b/WoL/Waker/MainForm.Designer.cs index 45f8ea9..5283973 100644 --- a/WoL/Waker/MainForm.Designer.cs +++ b/WoL/Waker/MainForm.Designer.cs @@ -29,75 +29,96 @@ 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.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, 54); - this.WakeButton.TabIndex = 2; - this.WakeButton.Text = "WoL"; - this.WakeButton.UseVisualStyleBackColor = true; - this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click); - // - // 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, 79); - 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, 118); - this.MinimumSize = new System.Drawing.Size(399, 118); - this.Name = "MainForm"; - this.Text = "Waker"; - this.ResumeLayout(false); - this.PerformLayout(); + 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(); } @@ -108,6 +129,8 @@ namespace Waker private System.Windows.Forms.TextBox MacTextBox; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button WakeButton; - } + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox PortTextBox; + } } diff --git a/WoL/Waker/MainForm.cs b/WoL/Waker/MainForm.cs index 78140f4..14a044f 100644 --- a/WoL/Waker/MainForm.cs +++ b/WoL/Waker/MainForm.cs @@ -16,6 +16,7 @@ namespace Waker { private string ip; private string mac; + private string ports; public MainForm() { @@ -33,25 +34,33 @@ namespace Waker if (!string.IsNullOrEmpty(Settings.Default.mac)) mac = Settings.Default.mac; + if (!string.IsNullOrEmpty(Settings.Default.ports)) + ports = Settings.Default.ports; + FillInformation(); AddressTextBox.KeyUp += TextBox_KeyUp; MacTextBox.KeyUp += TextBox_KeyUp; + PortTextBox.KeyUp += TextBox_KeyUp; } private bool GetInformation() { ip = AddressTextBox.Text.Replace(" ", ""); mac = MacTextBox.Text.Replace(" ", ""); + ports = PortTextBox.Text.Replace(" ", ""); if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(mac)) return false; AddressTextBox.Text = ip; MacTextBox.Text = mac; + PortTextBox.Text = ports; Settings.Default.ip = ip; Settings.Default.mac = mac; + Settings.Default.ports = ports; + Settings.Default.Save(); return true; @@ -64,6 +73,9 @@ namespace Waker if (!string.IsNullOrEmpty(mac)) MacTextBox.Text = mac; + + if (!string.IsNullOrEmpty(ports)) + PortTextBox.Text = ports; } private void WakeButton_Click(object sender, EventArgs e) @@ -76,7 +88,32 @@ namespace Waker try { - WoL.Wake(ip, mac); + string portsString = ports.Replace(" ", ""); + + if (string.IsNullOrEmpty(portsString)) + { + WoL.Wake(ip, mac); + return; + } + + + string[] portStringArray = portsString.Split(','); + if (portStringArray == null || portStringArray.Length < 1 || string.IsNullOrEmpty(portStringArray[0])) + { + WoL.Wake(ip, mac); + return; + } + + + int[] portArray = portStringArray.Select(int.Parse).ToArray(); + if (portArray == null || portArray.Length< 1) + { + WoL.Wake(ip, mac); + return; + } + + + WoL.Wake(ip, mac, portArray); } catch (Exception ex) { MessageBox.Show(ex.Message); diff --git a/WoL/Waker/Settings.Designer.cs b/WoL/Waker/Settings.Designer.cs index f4e2dca..86cbe4e 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", "16.10.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -46,5 +46,17 @@ namespace Waker { this["mac"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("7, 9")] + public string ports { + get { + return ((string)(this["ports"])); + } + set { + this["ports"] = value; + } + } } } diff --git a/WoL/Waker/Settings.settings b/WoL/Waker/Settings.settings index ab38a05..a944aed 100644 --- a/WoL/Waker/Settings.settings +++ b/WoL/Waker/Settings.settings @@ -8,5 +8,8 @@ + + 7, 9 + \ No newline at end of file diff --git a/WoL/Waker/Waker.csproj b/WoL/Waker/Waker.csproj index 0e86d44..337f031 100644 --- a/WoL/Waker/Waker.csproj +++ b/WoL/Waker/Waker.csproj @@ -4,8 +4,16 @@ WinExe netcoreapp3.1 true + False + 1.0.0.0 + Waker.Program + Designcontest-Ecommerce-Business-Idea.ico + + + + diff --git a/WoL/WoL/WoL.cs b/WoL/WoL/WoL.cs index 2b316a7..035dae8 100644 --- a/WoL/WoL/WoL.cs +++ b/WoL/WoL/WoL.cs @@ -9,18 +9,37 @@ namespace WakeOnLan { public class WoL { - private const int WOL_PACKET_LEN = 102; + private static readonly int WOL_PACKET_LEN = 102; + private static readonly int[] DEFAULT_PORTS = new int[] { 7, 9 }; - public static void Wake(string ip, string macAddress) + public static void Wake(string ip, string macAddress, int[] ports = null) { byte[] wolBuffer = GetWolPacket(macAddress); UdpClient udp = new UdpClient(); udp.EnableBroadcast = true; - IPAddress ipAddress = IPAddress.Parse(ip); - udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), 7); - udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), 9); + IPAddress ipAddress = GetIpAddress(ip); + if (ports == null || ports.Length < 1) + ports = DEFAULT_PORTS; + + foreach (int port in ports) + { + udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), port); + } + } + + 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)