From 36f42fafff67a5f1fa978bed4c89f6cf087045ce Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 12 Jun 2022 16:09:01 -0700 Subject: [PATCH] Initial import. --- Dockerfile | 10 ++++++++ README.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ run.sh | 8 +++++++ 3 files changed, 87 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 run.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..115aa5b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM alpine:3.16.0 + +ENV PORT=18888 + +COPY . /app +RUN apk add --no-cache netcat-openbsd awake + +EXPOSE $PORT + +CMD ["/app/run.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..6749448 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# wake-on-lan-service + +Remotely wake up your servers ... as-a-service! + +**The problem:** Docker prevents bridge-networked containers from sending +wake-on-lan packets outside of the Docker network. That means standard Docker +containers can't wake up remote machines. As a work-around, you can put them +on the Docker host network, but that has security and interoperability +implications—you may not want to expose your containers that way. + +**The solution:** wake-on-lan-service runs on Docker's host network and sends +wake-on-lan packets *on behalf of* other containers running on a standard +bridge network. This allows them to wake up remote machines—without themselves +having to be on the host network. + +You might find this service useful for use cases like running [Home +Assistant](https://www.home-assistant.io/) in a Docker container without +putting it on the host network—while still allowing it to wake up remote +machines. + +Source at https://projects.torsion.org/witten/wake-on-lan-service + + +## How it works + +This service listens on a port for TCP requests containing a MAC address to +send a wake-on-lan packet to. It expects plain TCP requests, not HTTP. + + +## Environment variables + + * `PORT`: The TCP port to listen on for wake-on-lan requests, defaults to 18888. + + +## Example + +### Running the service + +``` +$ sudo docker run --detach --name wake-on-lan-service --host network \ + --env PORT=18888 + witten/wake-on-lan-service +``` + +### Using the service + +From within another container running on a Docker bridge network (so, no need +to run on the host network), execute: + +``` +echo "00:00:0A:BB:28:FC" | nc -N host.docker.internal 18888 +``` + +This example uses the OpenBSD netcat variant to send a TCP packet containing +the requested MAC address to wake. The packet is sent to port 18888 on the +host. + +For this to work, you may need to configure your client container to support +the special `host.docker.internal` host. E.g., `docker run --add-host +host.docker.internal:host-gateway ...` + +Alternatively, on Linux, you can try using the IP `172.17.0.1`. + + +## Security + +Note that no authorization is performed on the service request, so be aware +that anyone with network access to this service can wake arbitrary hosts by +MAC address. diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..7733c4e --- /dev/null +++ b/run.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -u + +while true; do + mac=$(nc -l -p "$PORT" | sed 's/[^a-f0-9:]//g') + awake "$mac" +done