Giter Club home page Giter Club logo

caddy-wol's Introduction

Automated Wake-on-Lan for Caddy

caddy-wol is a Caddy plugin that sends wake-on-lan magic packets to remote hosts to wake up e.g. reverse proxy targets when they can not be reached.

Installation

To add this module to your Caddy configuration, run the following xcaddy command:

xcaddy build \
    --with github.com/dulli/caddy-wol

Usage

The plugin implements a minimal HTTP handler directive that simply dispatches a WOL magic packet and then passes the request through unchanged to the next handler which may return a response to the client:

wake_on_lan <mac_address> [<broadcast_address>]

As with any such directive, you have to tell Caddy where to put in the execution order of all directives. This is done with a global configuration directive in your Caddyfile:

order wake_on_lan before respond

Internally, the magic packets are throttled to be sent at most once every 10 minutes per remote host, even if more frequent requests arrive at the server, as sending the packet once is of course enough to wake up the remote host.

Example Configuration

One possible example use case is to reverse proxy requests to e.g. a NAS (using the IP 192.168.0.100 and MAC address 00:11:22:33:44:55) on which a media server like Jellyfin is running (at port :8096). If the NAS is shut down when not in use to save energy, we want it to automatically boot up again, when somebody needs it again.

To do so, we first issue a standard reverse proxy directive and then register an error handler that is called when this proxy request goes unanswered because the target is not reachable. It then calls the caddy-wol plugin to wake up the NAS and re-issues the reverse proxy directive, but this time with a longer timeout to allow for the boot process to finish. The configuration to do all of this, is really straight forward using built-in Caddy directives:

{
    order wake_on_lan before respond
}

wol.example.com {
        reverse_proxy 192.168.0.100:8096
        handle_errors {
                @502 expression {err.status_code} == 502
                handle @502 {
                        wake_on_lan 00:11:22:33:44:55
                        reverse_proxy 192.168.0.100:8096 {
                                lb_try_duration 120s
                        }
                }
        }
}

For the client, this results in an as nice as possible user experience, where the page load simply takes a longer time and then automatically shows the requested resource as soon as it is available (if the wait time stays in the bounds of the clients request timeout). No error messages have to be presented and no manual or automatic page refreshes are required.

caddy-wol's People

Contributors

dependabot[bot] avatar dulli avatar francislavoie avatar michael94ellis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

caddy-wol's Issues

Caddy with Docker: sending WoL needs enhancements

I'm using Caddy in a Docker container which is not using host networking mode. This results in the WoL packet being eaten by some network layer (I am no expert in this).

I found this article to build a workaround: https://www.devwithimagination.com/2020/06/15/homebridge-docker-and-wake-on-lan/

Sadly this plugin is unable to use this workaround as the source and broadcast in the WoL packet can't be set using the Caddyfile but are rather calculated on the fly.

Can you add this option to the Caddyfile?

Failing to launch caddy with only caddy-wol plugin on caddy 2.7.5 in go 1.21.1 on debian 11

Hey, thanks for making this plugin! I happen to be experiencing an issue while trying to use caddy with caddy-wol as a debian 11 system service unit.

For reference I posted this first on the caddy forum and was recommended to come to you. I am a novice with Go and am not yet able to make out how to handle or investigate this error.
https://caddy.community/t/caddy-service-fails-when-not-logged-in/21532/3

Debian 11
Go 1.21.1
Caddy 2.7.5

When I run this as a system service while logged in caddy runs as expected, but the problem is that when I reboot it fails to run before I log in.

The helpful user that sent me here made this snippet from my logs when they directed me to reach out to you

ct 30 18:49:32 caddy caddy[473]: panic: runtime error: invalid memory address or nil pointer dereference
Oct 30 18:49:32 caddy caddy[473]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x12a2190]
Oct 30 18:49:32 caddy caddy[473]: goroutine 1 [running]:
Oct 30 18:49:32 caddy caddy[473]: github.com/dulli/caddy-wol.(*Middleware).Cleanup(0x3d40a40)
Oct 30 18:49:32 caddy caddy[473]:         github.com/dulli/[email protected]/middleware.go:113 +0x1c
Oct 30 18:49:32 caddy caddy[473]: github.com/caddyserver/caddy/v2.Context.LoadModuleByID({{0x1a411a4, 0x3c7e918}, 0x3afd0c0, 0x3b0a090, {0x0, 0x0, 0x0}, {0x3bbb820, 0x4, 0x4}}, ...)

service unit file

[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/caddy run --environ --config "/etc/caddy/Caddyfile"
ExecReload=/usr/local/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
ProtectSystem=true
PrivateTmp=true
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

Caddyfile[redacted]

{
	email '[email protected]'
	order wake_on_lan before respond
}

https://sub.mydomain.com {
	tls [email protected]
	reverse_proxy 192.168.1.254:8096
	handle_errors {
		@502 expression {err.status_code} == 502
		handle @502 {
			wake_on_lan 11-22-33-44-55-66 192.168.1.254:9
			reverse_proxy 192.168.1.254:8096 {
				lb_try_duration 30s
			}
		}
	}
}

There are also 3 more with the same config for different subdomains, https only

I just tried rebuilding caddy pointing to the main branch for this plugin and got the same error

I'm going to try to keep looking, but i have been looking for help with this error for a couple days now. I think maybe knowing it is not caddy that is failing might help me find more results though.

Let me know if I can provide any other information, thanks!

Error at start

Hi,

Thanks for your Caddy module.
I get this error at launch, any idea :

Error: adapting config using caddyfile: parsing caddyfile tokens for 'order': /etc/caddy/Caddyfile:17 - Error during parsing: wake_on_lan is not a registered directive

My caddyfile

{ order wake_on_lan before respond } jellyfin.bibitocarlos.eu { reverse_proxy 192.168.1.11:8096 handle_errors { @502 expression {err.status_code} == 502 handle @502 { wake_on_lan 70:85:c2:83:63:f5 reverse_proxy 192.168.1.11:8096 { lb_try_duration 120s } } } log { output file /var/log/caddy/access.log format console } header { Strict-Transport-Security "max-age=63072000;includeSubDomains;preload" X-Content-Type-Options nosniff X-XSS-Protection "1;mode=block" X-Robots-Tag none Content-Security-Policy "upgrade-insecure-requests" Cache-Control "public, max-age=15, must-revalidate" X-Frame-Options SAMEORIGIN Referrer-Policy no-referrer Permissions-Policy "geolocation=(self), microphone=()" } }

panic: internal error on xcaddy build

I tried with different plugins and only with this do I get:

`panic: internal error: can't find reason for requirement on github.com/google/pp [email protected]

goroutine 1 [running]:
cmd/go/internal/modget.(*resolver).updateBuildList.func1({{0x276a210, 0x17}, {0x 1822570, 0x22}})
/usr/local/go/src/cmd/go/internal/modget/get.go:1760 +0x10c
cmd/go/internal/modget.(*resolver).updateBuildList(0x1bda280, {0x71f020, 0x181c0 98}, {0x0, 0x0, 0x0})
/usr/local/go/src/cmd/go/internal/modget/get.go:1765 +0x498
cmd/go/internal/modget.(*resolver).applyUpgrades(0x1bda280, {0x71f020, 0x181c098 }, {0x0, 0x0, 0x0})
/usr/local/go/src/cmd/go/internal/modget/get.go:1312 +0x2fc
cmd/go/internal/modget.runGet({0x71f020, 0x181c098}, 0xa090c8, {0x1818080, 0x2, 0x2})
/usr/local/go/src/cmd/go/internal/modget/get.go:351 +0x444
main.invoke(0xa090c8, {0x1818068, 0x5, 0x5})
/usr/local/go/src/cmd/go/main.go:225 +0x400
main.main()
/usr/local/go/src/cmd/go/main.go:179 +0x974
2023/02/09 10:30:54 [FATAL] exit status 2`

I tried on Go 1.20, 19.5, 19, and 18.1 nothing changed, so its maybe this plugin?

Crash when starting the server

Hi,

I'm very interested in your module, but I'm getting a crash when starting the server:

{"level":"info","ts":1713386974.230629,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
{"level":"info","ts":1713386974.2570424,"logger":"admin","msg":"admin endpoint started","address":"localhost:2019","enforce_origin":false,"origins":["//localhost:2019","//[::1]:2019","//127.0.0.1:2019"]}
{"level":"info","ts":1713386974.2582517,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0x40005fb180"}
{"level":"info","ts":1713386974.258622,"logger":"http.auto_https","msg":"server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS","server_name":"srv0","https_port":443}
{"level":"info","ts":1713386974.2587116,"logger":"http.auto_https","msg":"enabling automatic HTTP->HTTPS redirects","server_name":"srv0"}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10b7920]

I'm using the following Docker file

FROM caddy:builder-alpine AS builder

RUN xcaddy build \
    --with github.com/dulli/caddy-wol

FROM caddy:alpine

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Any idea how I can fix this?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.