Skip to content

mad0x20wizard/Desomnia

Repository files navigation

https://raw.githubusercontent.com/mad0x20wizard/Desomnia/main/Desomnia.png

Desomnia

Build Status Documentation Status License Discord

Desomnia is an intelligent sleep management solution designed for use on local networks, ranging from home labs to small businesses. It monitors the usage of your machines locally and via network to keep them awake as long as they are in use and can wake them at the exact moment you open a connection to one of it's services β€” transparently, with minimal or no changes at all on the connecting sides. This is achieved through a smart use of standard network protocols and a little bit of IP spoofing.

If you have ever been frustrated by the built-in sleep management features of your operating system, Desomnia can be used as a drop-in replacement. You can configure it to monitor a variety of metrics ranging from network traffic, process activitiy, native power requests / inhibitor locks, among others, that should keep the system running and Desomnia enforces exactly that β€” across your entire network, automatically.

Why should I need this?

The aim is to reduce the energy consumption of your self-hosted services while maintaining availability.

But instead of requiring you to decide when to wake and suspend the servers manually, you can declare the hosts and services that need to be available in a unified configuration format. Desomnia will then take care of providing you with an integrated and frictionless experience.

Modes of operation

Desomnia can be deployed in three complementary roles and combined across multiple machines:

  1. Local Sleep Management – πŸͺŸ Windows 🐧 Linux

    Replaces the OS's built-in sleep management. Desomnia holds the system awake while any monitored resource is used β€” a user session, a running process, an open SMB share, an incoming network connection β€” and sends it to sleep once everything goes quiet.

  2. Wake-on-LAN client – πŸͺ platform-independent

    Runs on the machine you connect from. When you try to open a connection to a sleeping host, Desomnia detects the attempt, sends a Magic Packet, and waits for the host to come back online β€” while your application notices only a small delay.

  3. Wake-on-LAN proxy – πŸͺ platform-independent

    Runs on an always-on device (a Raspberry Pi, a NAS, a small server). It watches the network in promiscuous mode and sends Magic Packets on behalf of any client that tries to reach a sleeping host. Nor Servers or clients need any further configuration. The best thing: it does not create bottlenecks in your infrastructure; network traffic is only rerouted and intercepted when absolutely necessary.

Monitoring

In order to replace to built-in sleep management, Desomnia models your system as a tree of logical resources, which monitor and track specific types of activity. Only when all resources switch to an idle state, the system will be suspended.

The following activities can be tracked out of the box:

  • Network traffic – πŸͺ platform-independent

    Tracks incoming and outgoing connections using libpcap or Npcap. You declare which hosts and which services β€” by TCP or UDP port β€” should count as activity; anything else will be ignored. For remote hosts, network demand can also trigger actions: wake a sleeping machine, start a VM, or do a Single Packet Authorization before the connection attempt is made. In promiscuous mode, Desomnia watches the entire broadcast domain and can act on behalf of other hosts β€” this is the foundation of the Wake-on-LAN deployment.

  • Processes – πŸͺ platform-independent

    Watches running processes by name, with an optional CPU threshold to distinguish real activity from idle background processes. On Windows, Desomnia uses Event Trace for Windows (ETW) for near-instant start and stop notifications with no polling overhead; on other platforms it polls at a configurable interval.

  • Power requests – πŸͺŸ Windows 🐧 Linux

    Tracks and filters native power requests / inhibitor locks registered by processes and drivers β€” giving you selective control over which of these should be allowed to prevent the system from sleep.

  • SMB sessions – πŸͺŸ Windows

    Keeps the system awake while remote clients have open file-sharing sessions, with fine-grained filtering by username, client name, IP address, share name, or file path.

  • User sessions – πŸͺŸ Windows

    Tracks the activity of Windows user sessions, including Remote Desktop connections. Sessions can be filtered by user account, have individual idle thresholds, and trigger actions β€” lock, disconnect, logout, or run a script β€” when they start to idle.

Configuration examples

The following examples illustrate how you would configure the software for a selection of use cases, and should give you an idea of how Desomnia works. Read the docs to find out more about the available use-cases.

Wake-on-LAN proxy

Use this on an always-on device. Any client on the local network that tries to reach the server via RDP or SSH while it's suspended will trigger a Magic Packet automatically. On startup, IPv4 addresses will be resolved automatically via hostname resolution:

<?xml version="1.0" encoding="utf-8"?>
<SystemMonitor version="1">

  <NetworkMonitor watchMode="promiscuous" autoDetect="IPv4">
    <RemoteHost name="server" MAC="00:1A:2B:3C:4D:5E">
      <Service name="RDP" port="3389" />
      <Service name="SSH" port="22" />
    </RemoteHost>
  </NetworkMonitor>

</SystemMonitor>

Desomnia will automatically detect and temporarily claim the sleeping hosts IP address in order to filter incoming connection attempts. After a successful wake, the connection will be handed off to the target, transparently.

Server and VM sleep automation

This configuration could be used to automatically suspend a physical system unless there is an open SSH connection or a running backup plan. Additionally Desomnia will watch connections to a local VM named dev, running in bridged network mode:

<?xml version="1.0" encoding="utf-8"?>
<SystemMonitor version="1" timeout="2min" onIdle="sleep+20min" onDemand="sleepless">

  <NetworkMonitor>
    <Service name="SSH" port="22">

    <VirtualHost name="dev" IPv4="192.168.1.10" onDemand="start" onIdle="suspend+10min">
      <Service name="SSH" port="22" />
      <Service name="HTTP" port="80" />
    </VirtualHost>
  </NetworkMonitor>

  <PowerRequestManager>
    <RequestFilterRule type="Must" name="Backup">CBBackupPlan</RequestFilterRule>
  </PowerRequestManager>

</SystemMonitor>

The VM starts automatically on the first SSH or HTTP connection. Any live TCP or UDP connection to one of its services β€” from anywhere on the network β€” count as activity and keeps it running. Ten minutes after the last connection closes, the VM will be suspended. The physical host will also be suspended, once neither the VM or any of its own services are needed.

Additional Features

Thanks to its open architecture, Desomnia can be extended with plugins. A variety of optional features are already available:

  • DuoStreamMonitor – πŸͺŸ Windows

    If you use DuoStream to turn your computer in a multi-seat game streaming host, this plugin makes Desomnia aware of the configured streaming instances, starts them on demand when accessed by a Moonlight client and can stop them when they become idle, reducing GPU load and overall resource footprint

  • Hyper-V support – πŸͺŸ Windows

    This plugin lets Desomnia interact with virtual machines running on the Hyper-V platform. It resolves their MAC addresses directly from the hypervisor and can start, suspend, or stop VMs in response to actual network usage β€” no manual lifecycle management needed.

  • Firewall Knock Operator – πŸͺ Platform-independent

    Enhances Desomnia's Single Packet Authorization framework with strong cryptographic authentication, to allow you to temporarily authorise remote clients with dynamic IP addresses from the internet to wake hosts on your local network.

    🚧 This plugin aims to be fully interoperable with fwknop, the established open-source SPA tool. A future version will also allow you to configure the local system firewall, providing a full drop-in replacement for the tool's original use case.

Observability

Desomnia uses NLog for structured logging. Out of the box it runs quietly; a wide range of log files can be created to record why the system stayed awake, which requests triggered it to send a Magic Packet and to assist with debugging problems. A usage report can be created regularily, to summarise the daily sleep and wake activity.

Getting started

πŸͺŸ Windows

Download the latest release from the GitHub releases page and run the installer. It registers the service, installs all dependencies (including Npcap), and walks you through an initial configuration. Read the docs to discover everything Desomnia can do and how to configure it.

🍺 Homebrew – 🍎 macOS / 🐧 Linux

brew install mad0x20wizard/tools/desomnia
sudo brew services start mad0x20wizard/tools/desomnia

See the Homebrew installation guide for filesystem layout, plugin installation, and platform-specific notes.

πŸ‹ Docker – 🐧 Linux

A Docker image is available on Docker Hub. A ready-to-use docker-compose.yml is provided in the repository description there. See the Docker installation guide for volume layout and capability requirements.

Contributing

  • Bug reports: open an issue on GitHub.
  • Questions and feedback: join the community on Discord.
  • Feature requests: open a GitHub issue and describe your use case β€” all suggestions are welcome.

System requirements

  • Windows 8 / 10 / 11, Linux, or macOS
  • .NET Runtime 9 / 10, or Docker (Linux only)
  • Npcap on Windows or libpcap on Linux / macOS (optional, required for NetworkMonitor)

If you like this project, consider supporting it:

"Buy Me A Coffee"

About

This is a background service that automatically and transparently performs Wake-on-LAN and enhances the built-in sleep management system by adding filters and extra monitors.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors