remotes/origin/master
syneffort 3 years ago
parent 4abdf11c01
commit 02f6ade40c
  1. 225
      WoL/Waker/MainForm.Designer.cs
  2. 103
      WoL/Waker/MainForm.cs
  3. 3
      WoL/Waker/MainForm.resx
  4. 14
      WoL/Waker/Settings.Designer.cs
  5. 3
      WoL/Waker/Settings.settings
  6. 48
      WoL/WoL/PingCheck.cs
  7. 24
      WoL/WoL/Utils.cs
  8. 15
      WoL/WoL/WoL.cs

@ -29,96 +29,135 @@ namespace Waker
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); this.components = new System.ComponentModel.Container();
this.AddressTextBox = new System.Windows.Forms.TextBox(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.label1 = new System.Windows.Forms.Label(); this.AddressTextBox = new System.Windows.Forms.TextBox();
this.MacTextBox = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label(); this.MacTextBox = new System.Windows.Forms.TextBox();
this.WakeButton = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label(); this.WakeButton = new System.Windows.Forms.Button();
this.PortTextBox = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout(); this.PortTextBox = new System.Windows.Forms.TextBox();
// this.PingPortTextBox = new System.Windows.Forms.TextBox();
// AddressTextBox this.label4 = new System.Windows.Forms.Label();
// this.PingStatusPanel = new System.Windows.Forms.Panel();
this.AddressTextBox.Location = new System.Drawing.Point(67, 14); this.pingTimer = new System.Windows.Forms.Timer(this.components);
this.AddressTextBox.Name = "AddressTextBox"; this.SuspendLayout();
this.AddressTextBox.Size = new System.Drawing.Size(198, 23); //
this.AddressTextBox.TabIndex = 0; // AddressTextBox
// //
// label1 this.AddressTextBox.Location = new System.Drawing.Point(67, 14);
// this.AddressTextBox.Name = "AddressTextBox";
this.label1.AutoSize = true; this.AddressTextBox.Size = new System.Drawing.Size(198, 23);
this.label1.Location = new System.Drawing.Point(12, 18); this.AddressTextBox.TabIndex = 0;
this.label1.Name = "label1"; //
this.label1.Size = new System.Drawing.Size(49, 15); // label1
this.label1.TabIndex = 1; //
this.label1.Text = "Address"; this.label1.AutoSize = true;
// this.label1.Location = new System.Drawing.Point(12, 18);
// MacTextBox this.label1.Name = "label1";
// this.label1.Size = new System.Drawing.Size(49, 15);
this.MacTextBox.Location = new System.Drawing.Point(67, 43); this.label1.TabIndex = 1;
this.MacTextBox.Name = "MacTextBox"; this.label1.Text = "Address";
this.MacTextBox.Size = new System.Drawing.Size(198, 23); //
this.MacTextBox.TabIndex = 1; // MacTextBox
// //
// label2 this.MacTextBox.Location = new System.Drawing.Point(67, 43);
// this.MacTextBox.Name = "MacTextBox";
this.label2.AutoSize = true; this.MacTextBox.Size = new System.Drawing.Size(198, 23);
this.label2.Location = new System.Drawing.Point(21, 47); this.MacTextBox.TabIndex = 1;
this.label2.Name = "label2"; //
this.label2.Size = new System.Drawing.Size(30, 15); // label2
this.label2.TabIndex = 1; //
this.label2.Text = "mac"; this.label2.AutoSize = true;
// this.label2.Location = new System.Drawing.Point(21, 47);
// WakeButton this.label2.Name = "label2";
// this.label2.Size = new System.Drawing.Size(30, 15);
this.WakeButton.Location = new System.Drawing.Point(271, 12); this.label2.TabIndex = 1;
this.WakeButton.Name = "WakeButton"; this.label2.Text = "mac";
this.WakeButton.Size = new System.Drawing.Size(102, 83); //
this.WakeButton.TabIndex = 3; // WakeButton
this.WakeButton.Text = "WoL"; //
this.WakeButton.UseVisualStyleBackColor = true; this.WakeButton.Location = new System.Drawing.Point(271, 12);
this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click); this.WakeButton.Name = "WakeButton";
// this.WakeButton.Size = new System.Drawing.Size(102, 112);
// label3 this.WakeButton.TabIndex = 4;
// this.WakeButton.Text = "WoL";
this.label3.AutoSize = true; this.WakeButton.UseVisualStyleBackColor = true;
this.label3.Location = new System.Drawing.Point(19, 76); this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click);
this.label3.Name = "label3"; //
this.label3.Size = new System.Drawing.Size(34, 15); // label3
this.label3.TabIndex = 3; //
this.label3.Text = "ports"; this.label3.AutoSize = true;
// this.label3.Location = new System.Drawing.Point(19, 76);
// PortTextBox this.label3.Name = "label3";
// this.label3.Size = new System.Drawing.Size(34, 15);
this.PortTextBox.Location = new System.Drawing.Point(67, 72); this.label3.TabIndex = 3;
this.PortTextBox.Name = "PortTextBox"; this.label3.Text = "ports";
this.PortTextBox.Size = new System.Drawing.Size(198, 23); //
this.PortTextBox.TabIndex = 2; // PortTextBox
this.PortTextBox.Text = "7, 9"; //
// this.PortTextBox.Location = new System.Drawing.Point(67, 72);
// MainForm this.PortTextBox.Name = "PortTextBox";
// this.PortTextBox.Size = new System.Drawing.Size(198, 23);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.PortTextBox.TabIndex = 2;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.PortTextBox.Text = "7, 9";
this.BackColor = System.Drawing.Color.White; //
this.ClientSize = new System.Drawing.Size(383, 105); // PingPortTextBox
this.Controls.Add(this.label3); //
this.Controls.Add(this.PortTextBox); this.PingPortTextBox.Location = new System.Drawing.Point(138, 101);
this.Controls.Add(this.WakeButton); this.PingPortTextBox.Name = "PingPortTextBox";
this.Controls.Add(this.label2); this.PingPortTextBox.Size = new System.Drawing.Size(98, 23);
this.Controls.Add(this.label1); this.PingPortTextBox.TabIndex = 3;
this.Controls.Add(this.MacTextBox); this.PingPortTextBox.TextChanged += new System.EventHandler(this.PingPortTextBox_TextChanged);
this.Controls.Add(this.AddressTextBox); //
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; // label4
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); //
this.MaximumSize = new System.Drawing.Size(399, 144); this.label4.AutoSize = true;
this.MinimumSize = new System.Drawing.Size(399, 144); this.label4.Location = new System.Drawing.Point(19, 105);
this.Name = "MainForm"; this.label4.Name = "label4";
this.Text = "Waker"; this.label4.Size = new System.Drawing.Size(113, 15);
this.ResumeLayout(false); this.label4.TabIndex = 3;
this.PerformLayout(); 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.Button WakeButton;
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox PortTextBox; 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;
}
} }

@ -9,14 +9,22 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using WakeOnLan; using WakeOnLan;
using System.Configuration; using System.Configuration;
using System.Threading;
namespace Waker namespace Waker
{ {
public partial class MainForm : Form 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 ip;
private string mac; private string mac;
private string ports; private string ports;
private string pingPort;
private Thread pingThread;
public MainForm() public MainForm()
{ {
@ -37,14 +45,37 @@ namespace Waker
if (!string.IsNullOrEmpty(Settings.Default.ports)) if (!string.IsNullOrEmpty(Settings.Default.ports))
ports = Settings.Default.ports; ports = Settings.Default.ports;
if (!string.IsNullOrEmpty(Settings.Default.pingPort))
pingPort = Settings.Default.pingPort;
FillInformation(); FillInformation();
AddressTextBox.TextChanged += AddressTextBox_TextChanged;
AddressTextBox.KeyUp += TextBox_KeyUp; AddressTextBox.KeyUp += TextBox_KeyUp;
MacTextBox.KeyUp += TextBox_KeyUp; MacTextBox.KeyUp += TextBox_KeyUp;
PortTextBox.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(" ", ""); ip = AddressTextBox.Text.Replace(" ", "");
mac = MacTextBox.Text.Replace(" ", ""); mac = MacTextBox.Text.Replace(" ", "");
@ -76,11 +107,14 @@ namespace Waker
if (!string.IsNullOrEmpty(ports)) if (!string.IsNullOrEmpty(ports))
PortTextBox.Text = ports; PortTextBox.Text = ports;
if (!string.IsNullOrEmpty(pingPort))
PingPortTextBox.Text = pingPort;
} }
private void WakeButton_Click(object sender, EventArgs e) private void WakeButton_Click(object sender, EventArgs e)
{ {
if (!GetInformation()) if (!SetInformation())
{ {
MessageBox.Show("Invalid information"); MessageBox.Show("Invalid information");
return; return;
@ -127,5 +161,70 @@ namespace Waker
WakeButton_Click(sender, e); 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;
}
} }
} }

@ -57,6 +57,9 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="pingTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>

@ -12,7 +12,7 @@ namespace Waker {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [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 { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
@ -58,5 +58,17 @@ namespace Waker {
this["ports"] = value; 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;
}
}
} }
} }

@ -11,5 +11,8 @@
<Setting Name="ports" Type="System.String" Scope="User"> <Setting Name="ports" Type="System.String" Scope="User">
<Value Profile="(Default)">7, 9</Value> <Value Profile="(Default)">7, 9</Value>
</Setting> </Setting>
<Setting Name="pingPort" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

@ -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;
}
}
}

@ -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];
}
}
}

@ -19,7 +19,7 @@ namespace WakeOnLan
UdpClient udp = new UdpClient(); UdpClient udp = new UdpClient();
udp.EnableBroadcast = true; udp.EnableBroadcast = true;
IPAddress ipAddress = GetIpAddress(ip); IPAddress ipAddress = Utils.GetIpAddress(ip);
if (ports == null || ports.Length < 1) if (ports == null || ports.Length < 1)
ports = DEFAULT_PORTS; 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) private static byte[] GetWolPacket(string macAddress)
{ {
byte[] datagram = new byte[WOL_PACKET_LEN]; byte[] datagram = new byte[WOL_PACKET_LEN];

Loading…
Cancel
Save