Initial Commit
This commit is contained in:
25
WOLHelper.sln
Normal file
25
WOLHelper.sln
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.32126.317
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WOLHelper", "WOLHelper\WOLHelper.csproj", "{385E5B21-B758-46BC-BE41-0ADEBF9BD97B}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{385E5B21-B758-46BC-BE41-0ADEBF9BD97B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{385E5B21-B758-46BC-BE41-0ADEBF9BD97B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{385E5B21-B758-46BC-BE41-0ADEBF9BD97B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{385E5B21-B758-46BC-BE41-0ADEBF9BD97B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {1EE2A704-678F-4E80-A189-1466153695E2}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
36
WOLHelper/Properties/AssemblyInfo.cs
Normal file
36
WOLHelper/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||||
|
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||||
|
// die einer Assembly zugeordnet sind.
|
||||||
|
[assembly: AssemblyTitle("WOLHelper")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("Stadt Dortmund")]
|
||||||
|
[assembly: AssemblyProduct("WOLHelper")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © Stadt Dortmund 2022")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||||
|
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||||
|
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||||
|
[assembly: Guid("385e5b21-b758-46bc-be41-0adebf9bd97b")]
|
||||||
|
|
||||||
|
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||||
|
//
|
||||||
|
// Hauptversion
|
||||||
|
// Nebenversion
|
||||||
|
// Buildnummer
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||||
|
// indem Sie "*" wie unten gezeigt eingeben:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
171
WOLHelper/WOLHelper.cs
Normal file
171
WOLHelper/WOLHelper.cs
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WOLWin
|
||||||
|
{
|
||||||
|
public static class WOLHelper
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Liest MAC-Adresse des Host aus Liste
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="host">RQ-Nummer des Zielrechners</param>
|
||||||
|
/// <param name="pathToMacs">Dateipfad zur MAC-Liste</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<List<string>> GetMacAsync(string host, string pathToMacs)
|
||||||
|
{
|
||||||
|
List<string> macs = new List<string>();
|
||||||
|
|
||||||
|
using (StreamReader sr = new StreamReader(pathToMacs))
|
||||||
|
{
|
||||||
|
await sr.ReadLineAsync();
|
||||||
|
string line;
|
||||||
|
|
||||||
|
while ((line = await sr.ReadLineAsync()) != null)
|
||||||
|
{
|
||||||
|
string[] splitted = line.Split(';');
|
||||||
|
|
||||||
|
if (splitted[0] == host)
|
||||||
|
{
|
||||||
|
if (splitted[2] != "")
|
||||||
|
{
|
||||||
|
macs.Add(splitted[1] + ";" + splitted[2]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
macs.Add(splitted[1]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return macs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Weckt entfernten Rechner aus dem Energiesparmodus auf
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mac">MAC-Adresse des Zielrechners</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<int> WakeOnLan(string mac)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
byte[] magicPacket = CreateMagicPacket(mac);
|
||||||
|
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces().Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback && n.OperationalStatus == OperationalStatus.Up))
|
||||||
|
{
|
||||||
|
IPInterfaceProperties ipProps = networkInterface.GetIPProperties();
|
||||||
|
foreach (MulticastIPAddressInformation info in ipProps.MulticastAddresses)
|
||||||
|
{
|
||||||
|
IPAddress multiCastAdr = info.Address;
|
||||||
|
|
||||||
|
//IPv6
|
||||||
|
if (multiCastAdr.ToString().StartsWith("ff02::1%", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
UnicastIPAddressInformation uniInfo = ipProps.UnicastAddresses.Where(u => u.Address.AddressFamily == AddressFamily.InterNetworkV6 && !u.Address.IsIPv6LinkLocal).FirstOrDefault();
|
||||||
|
if (uniInfo != null)
|
||||||
|
{
|
||||||
|
result = await SendUdpAsync(uniInfo.Address, multiCastAdr, magicPacket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//IPv4
|
||||||
|
else if (multiCastAdr.ToString().Equals("224.0.0.1"))
|
||||||
|
{
|
||||||
|
UnicastIPAddressInformation unicastIPAddressInformation = ipProps.UnicastAddresses.Where(u => u.Address.AddressFamily == AddressFamily.InterNetwork && !ipProps.GetIPv4Properties().IsAutomaticPrivateAddressingActive).FirstOrDefault();
|
||||||
|
if (unicastIPAddressInformation != null)
|
||||||
|
{
|
||||||
|
result = await SendUdpAsync(unicastIPAddressInformation.Address, multiCastAdr, magicPacket);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Erstellt Magic Packet anhand MAC-Adresse des Zielrechners
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="macAdress">MAC-Adresse des Zielrechners</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static byte[] CreateMagicPacket(string macAdress)
|
||||||
|
{
|
||||||
|
var macAdressNorm = Regex.Replace(macAdress, "[: -]", "");
|
||||||
|
byte[] macBytes = new byte[6];
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
//i to hex to byte
|
||||||
|
macBytes[i] = Convert.ToByte(macAdressNorm.Substring(i * 2, 2), 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (MemoryStream memStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (BinaryWriter binWriter = new BinaryWriter(memStream))
|
||||||
|
{
|
||||||
|
//Beginne Paket mit 6x 255
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
binWriter.Write((byte)0xff);
|
||||||
|
|
||||||
|
}
|
||||||
|
//Füge Paket 16x MAC des Zielrechners hinzu
|
||||||
|
for (int i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
binWriter.Write(macBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return memStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sendet Magic Packet in UDP-Paket aus
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="adr">Lokale IP</param>
|
||||||
|
/// <param name="multicastIp">Multicast IP</param>
|
||||||
|
/// <param name="magicPacket">Magic Packet enthält MAC des Ziels</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static async Task<int> SendUdpAsync(IPAddress adr, IPAddress multicastIp, byte[] magicPacket)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
|
||||||
|
//Sendet UDP an Port 0
|
||||||
|
using (UdpClient client = new UdpClient(new IPEndPoint(adr, 0)))
|
||||||
|
{
|
||||||
|
count = await client.SendAsync(magicPacket, magicPacket.Length, multicastIp.ToString(), 9);
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prüft, ob Rechner erfolgreich aufgeweckt wurde
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="host">RQ-Nummer des Zielrechners</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool PingHost(string host)
|
||||||
|
{
|
||||||
|
bool pinged = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Ping p = new Ping();
|
||||||
|
PingReply reply = p.Send(host, 1000);
|
||||||
|
if (reply != null && reply.Status == IPStatus.Success)
|
||||||
|
{
|
||||||
|
pinged = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
pinged = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pinged;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
WOLHelper/WOLHelper.csproj
Normal file
48
WOLHelper/WOLHelper.csproj
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{385E5B21-B758-46BC-BE41-0ADEBF9BD97B}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>WOLHelper</RootNamespace>
|
||||||
|
<AssemblyName>WOLHelper</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<Deterministic>true</Deterministic>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>none</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="WOLHelper.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user