remotes/origin/master
syneffort 3 years ago
parent 4abdf11c01
commit 02f6ade40c
  1. 51
      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,6 +29,7 @@ namespace Waker
/// </summary>
private void InitializeComponent()
{
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();
@ -37,6 +38,10 @@ namespace Waker
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
@ -75,8 +80,8 @@ namespace Waker
//
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.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);
@ -98,13 +103,47 @@ namespace Waker
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, 105);
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);
@ -113,7 +152,7 @@ namespace Waker
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.MaximumSize = new System.Drawing.Size(399, 173);
this.MinimumSize = new System.Drawing.Size(399, 144);
this.Name = "MainForm";
this.Text = "Waker";
@ -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;
}
}

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

@ -57,6 +57,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</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" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

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

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

Loading…
Cancel
Save