All projects

Tool · Windows service

WolCatcher

A Wake-on-LAN app on the phone can turn a PC on but can't turn it off. WolCatcher closes that gap: it's a small Windows service that catches the magic packet while the PC is already on and shuts the PC down. The same WoL button now both wakes and turns off the computer.

  • C#
  • .NET 10
  • Generic Host
  • Windows Service
  • UDP
  • P/Invoke

It has been running as a service on my own PC since June.

Magic packet · 102 bytes

Phone: WoL button UDP 9 / 7 PC already on? Shut down
Six 0xFF bytes followed by the target MAC address repeated 16 times. When the PC is off, the network card wakes it; when it's on, WolCatcher hears the same packet.

How it works

A .NET Generic Host app that runs as a Windows service.

  1. Listen

    The service listens on UDP ports 9 and 7, broadcast included. Both ports can be changed in the settings.

  2. Find the magic packet anywhere

    It scans the whole payload for six 0xFF bytes followed by a MAC address repeated 16 times, so extra bytes at the start or a SecureOn password at the end don't stop the packet from being recognised.

  3. Check the MAC

    MAC addresses are read from the PC's network adapters automatically, or listed by hand in appsettings.json. Packets for other machines are ignored.

  4. Act

    Shut down with shutdown.exe, or sleep and hibernate through SetSuspendState in powrprof.dll, called with P/Invoke.

  5. Don't overreact

    After a WoL boot the phone keeps sending a few more packets, so anything that arrives in the first 45 seconds after the service starts is ignored. A separate cooldown stops repeated triggers in a short window.

MagicPacket.cs
for (int i = 0; i <= payload.Length - CoreLen; i++)
{
    if (!AllByte(payload.Slice(i, MacLen), 0xFF))
        continue;

    var mac = payload.Slice(i + MacLen, MacLen);

    bool valid = true;
    for (int r = 1; r < Repeat; r++)
    {
        if (!mac.SequenceEqual(payload.Slice(i + MacLen + r * MacLen, MacLen)))
        {
            valid = false;
            break;
        }
    }

    if (valid)
        return mac.ToArray();
}

Manages itself

The same exe installs the service with sc.exe, set to start automatically and restart after a crash. When it needs admin rights it relaunches itself through UAC. Logs go to %ProgramData%\WolCatcher\wolcatcher.log, which is moved to .1 once it passes 5 MB; start, stop and error events also appear in Event Viewer.

Commands and settings

Service commands need administrator rights.

CommandWhat it does
installInstalls and starts the service
uninstallStops and removes the service
start, stopStarts or stops the service
statusShows the service status
consoleRuns in the console instead of as a service
helpShows usage
appsettings.json
{
  "WolCatcher": {
    "Ports": [ 9, 7 ],
    "Action": "Shutdown",
    "StartupGraceSeconds": 45,
    "TargetMacs": [],
    "ActionDelaySeconds": 0,
    "Force": true,
    "CooldownSeconds": 10
  }
}
  • Action: Shutdown, Sleep or Hibernate.
  • TargetMacs: when empty, the MACs of this PC's adapters are used. AA:BB:CC:DD:EE:FF, aa-bb-cc-dd-ee-ff and plain hex all work.
  • Force: closes open apps when shutting down, so unsaved work can be lost.
  • Any setting can also come from an environment variable, for example WolCatcher__Action=Sleep. Restart the service after changing appsettings.json.

Setup

Windows 10/11 x64. Download the release, or build it with the .NET 10 SDK.

  1. Get a single-file exe

    Download the zip from Releases, or publish it yourself:

    PowerShell
    dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -o publish
  2. Install the service

    Copy WolCatcher.exe and appsettings.json to a permanent folder such as C:\Tools\WolCatcher. The service points at that path, so don't move the folder afterwards.

    PowerShell
    .\WolCatcher.exe install
  3. Set up the network

    Turn on Wake-on-LAN in the BIOS/UEFI and the network adapter settings. In the phone app, target the network's broadcast address (for example 192.168.1.255) on port 9. You may need to allow inbound UDP 9 and 7 in Windows Firewall.

Try it without shutting down

Stretch the startup grace and run it in the console; every packet is logged but the PC stays on.

PowerShell
$env:WolCatcher__StartupGraceSeconds = "99999"
dotnet run -- console

Known issues

More projects