Added NetworkResurrector.WakeOnLan.Inhouse project and new empty NetworkResurrector.WakeOnLan.Nikeee project
parent
d77e265357
commit
3ba1e271c1
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace NetworkResurrector.Api.Controllers
|
namespace NetworkResurrector.Api.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
// [Authorize]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("resurrector")]
|
[Route("resurrector")]
|
||||||
public class ResurrectorController : ControllerBase
|
public class ResurrectorController : ControllerBase
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using NetworkResurrector.WakeOnLan;
|
using NetworkResurrector.WakeOnLan.Inhouse;
|
||||||
|
|
||||||
namespace NetworkResurrector.Api.Extensions
|
namespace NetworkResurrector.Api.Extensions
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\NetworkResurrector.Application\NetworkResurrector.Application.csproj" />
|
<ProjectReference Include="..\NetworkResurrector.Application\NetworkResurrector.Application.csproj" />
|
||||||
<ProjectReference Include="..\NetworkResurrector.WakeOnLan\NetworkResurrector.WakeOnLan.csproj" />
|
<ProjectReference Include="..\NetworkResurrector.WakeOnLan.Inhouse\NetworkResurrector.WakeOnLan.Inhouse.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using NetworkResurrector.Abstractions;
|
||||||
|
|
||||||
|
namespace NetworkResurrector.WakeOnLan.Inhouse
|
||||||
|
{
|
||||||
|
public static class DependencyInjectionExtensions
|
||||||
|
{
|
||||||
|
public static void AddWakeOnLanService(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddScoped<WolClient>();
|
||||||
|
services.AddScoped<IWakeOnLanService, WolDriver>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\NetworkResurrector.Abstractions\NetworkResurrector.Abstractions.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,45 @@
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
|
namespace NetworkResurrector.WakeOnLan.Inhouse
|
||||||
|
{
|
||||||
|
internal class WolClient : UdpClient
|
||||||
|
{
|
||||||
|
private readonly ILogger<WolClient> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of <see cref="WolClient"/>.
|
||||||
|
/// </summary>
|
||||||
|
public WolClient(ILogger<WolClient> logger) : base()
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets up the UDP client to broadcast packets.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns><see langword="true"/> if the UDP client is set in
|
||||||
|
/// broadcast mode.</returns>
|
||||||
|
public bool SetClientInBrodcastMode()
|
||||||
|
{
|
||||||
|
bool broadcast = false;
|
||||||
|
if (this.Active)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 0);
|
||||||
|
_logger.LogInformation("WolClient => SetClientInBrodcastMode succedded.");
|
||||||
|
broadcast = true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
broadcast = false;
|
||||||
|
_logger.LogError("WolClient => SetClientInBrodcastMode failed.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return broadcast;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,92 @@
|
||||||
|
using NetworkResurrector.Abstractions;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace NetworkResurrector.WakeOnLan.Inhouse
|
||||||
|
{
|
||||||
|
internal class WolDriver : IWakeOnLanService
|
||||||
|
{
|
||||||
|
private readonly WolClient _client;
|
||||||
|
|
||||||
|
public WolDriver(WolClient client)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<(bool success, string message)> Wake(string macAddress)
|
||||||
|
{
|
||||||
|
//remove all non 0-9, A-F, a-f characters
|
||||||
|
macAddress = Regex.Replace(macAddress, @"[^0-9A-Fa-f]", "");
|
||||||
|
|
||||||
|
//check if mac adress length is valid
|
||||||
|
if (macAddress.Length != 12)
|
||||||
|
return (false, "Invalid MAC address.");
|
||||||
|
else
|
||||||
|
return await Wakeup(macAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Wakeup
|
||||||
|
/// <summary>
|
||||||
|
/// Wakes up the machine with the given <paramref name="macAddress"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// <note>
|
||||||
|
/// <list type="number">
|
||||||
|
/// <item> The motherboard must support Wake On LAN.</item>
|
||||||
|
/// <item> The NIC must support Wake On LAN.</item>
|
||||||
|
/// <item> There must be a wire connecting the motherboard's WOL port to
|
||||||
|
/// the NIC's WOL port. Usually there always is a connection on most of
|
||||||
|
/// the PCs.</item>
|
||||||
|
/// <item> The Wake On LAN feature must be enabled in the motherboard's
|
||||||
|
/// BIOS. Usually this is also enabled by default, but you might like to
|
||||||
|
/// check again.</item>
|
||||||
|
/// <item> The "Good Connection" light on the back of the NIC must be lit
|
||||||
|
/// when the machine is off. (By default always good if you are not
|
||||||
|
/// facing any network issues)</item>
|
||||||
|
/// <item> Port 12287 (0x2FFF) must be open. (By default it should be
|
||||||
|
/// open unless some antivirus or any other such program has changed
|
||||||
|
/// settings.)</item>
|
||||||
|
/// <item> Packets cannot be broadcast across the Internet. That's why
|
||||||
|
/// it's called Wake On Lan, not Wake On Internet.</item>
|
||||||
|
/// <item> To find your MAC address, run the MSINFO32.EXE tool that is a
|
||||||
|
/// part of Windows and navigate to Components > Network > Adapter
|
||||||
|
/// or simply type nbtstat -a <your hostname < at command prompt.
|
||||||
|
/// e.g. nbtstat -a mymachinename or nbtstat -A 10.2.100.213.
|
||||||
|
/// <param name="macAddress">The MAC address of the host which has to be
|
||||||
|
/// woken up.</param>
|
||||||
|
///
|
||||||
|
private async Task<(bool success, string message)> Wakeup(string macAddress)
|
||||||
|
{
|
||||||
|
_client.Connect(new IPAddress(0xffffffff) /*255.255.255.255 i.e broadcast*/, 0x2fff /*port = 12287*/);
|
||||||
|
|
||||||
|
if (!_client.SetClientInBrodcastMode())
|
||||||
|
return (false, "Remote client could not be set in broadcast mode!");
|
||||||
|
|
||||||
|
int byteCount = 0;
|
||||||
|
byte[] bytes = new byte[102];
|
||||||
|
|
||||||
|
for (int trailer = 0; trailer < 6; trailer++)
|
||||||
|
{
|
||||||
|
bytes[byteCount++] = 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int macPackets = 0; macPackets < 16; macPackets++)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
for (int macBytes = 0; macBytes < 6; macBytes++)
|
||||||
|
{
|
||||||
|
bytes[byteCount++] = byte.Parse(macAddress.Substring(i, 2), NumberStyles.HexNumber);
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int returnValue = await _client.SendAsync(bytes, byteCount);
|
||||||
|
|
||||||
|
return (true, $"{returnValue} bytes sent to {macAddress}");
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NetworkResurrector.WakeOnLan.Nikeee
|
||||||
|
{
|
||||||
|
public class Class1
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -22,6 +22,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.WakeOnLa
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Abstractions", "NetworkResurrector.Abstractions\NetworkResurrector.Abstractions.csproj", "{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Abstractions", "NetworkResurrector.Abstractions\NetworkResurrector.Abstractions.csproj", "{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.WakeOnLan.Nikeee", "NetworkResurrector.WakeOnLan.Nikeee\NetworkResurrector.WakeOnLan.Nikeee.csproj", "{59049C2B-CEFB-456D-B3D5-D2CF5325AEEB}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.WakeOnLan.Inhouse", "NetworkResurrector.WakeOnLan.Inhouse\NetworkResurrector.WakeOnLan.Inhouse.csproj", "{8A593A37-7ECA-4EDD-A0A7-86CA88ECC1BD}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -48,6 +52,14 @@ Global
|
||||||
{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}.Release|Any CPU.Build.0 = Release|Any CPU
|
{B7408385-ED73-4ED3-9654-9AFF8CDFDA8D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{59049C2B-CEFB-456D-B3D5-D2CF5325AEEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{59049C2B-CEFB-456D-B3D5-D2CF5325AEEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{59049C2B-CEFB-456D-B3D5-D2CF5325AEEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{59049C2B-CEFB-456D-B3D5-D2CF5325AEEB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{8A593A37-7ECA-4EDD-A0A7-86CA88ECC1BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{8A593A37-7ECA-4EDD-A0A7-86CA88ECC1BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{8A593A37-7ECA-4EDD-A0A7-86CA88ECC1BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{8A593A37-7ECA-4EDD-A0A7-86CA88ECC1BD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
Loading…
Reference in New Issue