-
Notifications
You must be signed in to change notification settings - Fork 112
cmd/wol-proxy: add wol-proxy #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+297
−1
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# wol-proxy | ||
|
||
wol-proxy automatically wakes up a suspended llama-swap server using Wake-on-LAN when requests are received. | ||
|
||
When a request arrives and llama-swap is unavailable, wol-proxy sends a WOL packet and holds the request until the server becomes available. If the server doesn't respond within the timeout period (default: 60 seconds), the request is dropped. | ||
|
||
This utility helps conserve energy by allowing GPU-heavy servers to remain suspended when idle, as they can consume hundreds of watts even when not actively processing requests. | ||
|
||
## Usage | ||
|
||
```shell | ||
# minimal | ||
$ ./wol-proxy -mac BA:DC:0F:FE:E0:00 -upstream http://192.168.1.13:8080 | ||
|
||
# everything | ||
$ ./wol-proxy -mac BA:DC:0F:FE:E0:00 -upstream http://192.168.1.13:8080 \ | ||
# use debug log level | ||
-log debug \ | ||
# altenerative listening port | ||
-listen localhost:9999 \ | ||
# seconds to hold requests waiting for upstream to be ready | ||
-timeout 30 | ||
``` | ||
|
||
## API | ||
|
||
`GET /status` - that's it. Everything else is proxied to the upstream server. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
flagMac = flag.String("mac", "", "mac address to send WoL packet to") | ||
flagUpstream = flag.String("upstream", "", "upstream proxy address to send requests to") | ||
flagListen = flag.String("listen", ":8080", "listen address to listen on") | ||
flagLog = flag.String("log", "info", "log level (debug, info, warn, error)") | ||
flagTimeout = flag.Int("timeout", 60, "seconds requests wait for upstream response before failing") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
switch *flagLog { | ||
case "debug": | ||
slog.SetLogLoggerLevel(slog.LevelDebug) | ||
case "info": | ||
slog.SetLogLoggerLevel(slog.LevelInfo) | ||
case "warn": | ||
slog.SetLogLoggerLevel(slog.LevelWarn) | ||
case "error": | ||
slog.SetLogLoggerLevel(slog.LevelError) | ||
default: | ||
slog.Error("invalid log level", "logLevel", *flagLog) | ||
} | ||
|
||
// Validate flags | ||
if *flagListen == "" { | ||
slog.Error("listen address is required") | ||
return | ||
} | ||
|
||
if *flagMac == "" { | ||
slog.Error("mac address is required") | ||
return | ||
} | ||
|
||
if *flagTimeout < 1 { | ||
slog.Error("timeout must be greater than 0") | ||
return | ||
} | ||
|
||
var upstreamURL *url.URL | ||
var err error | ||
// validate mac address | ||
if _, err = net.ParseMAC(*flagMac); err != nil { | ||
slog.Error("invalid mac address", "error", err) | ||
return | ||
} | ||
|
||
if *flagUpstream == "" { | ||
slog.Error("upstream proxy address is required") | ||
return | ||
} else { | ||
upstreamURL, err = url.ParseRequestURI(*flagUpstream) | ||
if err != nil { | ||
slog.Error("error parsing upstream url", "error", err) | ||
return | ||
} | ||
} | ||
|
||
proxy := newProxy(upstreamURL) | ||
server := &http.Server{ | ||
Addr: *flagListen, | ||
Handler: proxy, | ||
} | ||
|
||
// start the server | ||
go func() { | ||
slog.Info("server starting on", "address", *flagListen) | ||
if err := server.ListenAndServe(); err != nil { | ||
slog.Error("error starting server", "error", err) | ||
} | ||
}() | ||
|
||
mostlygeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// graceful shutdown | ||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) | ||
defer stop() | ||
<-ctx.Done() | ||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if err := server.Shutdown(shutdownCtx); err != nil { | ||
slog.Error("server shutdown error", "error", err) | ||
} | ||
} | ||
|
||
type upstreamStatus string | ||
|
||
const ( | ||
notready upstreamStatus = "not ready" | ||
ready upstreamStatus = "ready" | ||
) | ||
|
||
type proxyServer struct { | ||
upstreamProxy *httputil.ReverseProxy | ||
failCount int | ||
statusMutex sync.RWMutex | ||
status upstreamStatus | ||
} | ||
|
||
func newProxy(url *url.URL) *proxyServer { | ||
p := httputil.NewSingleHostReverseProxy(url) | ||
proxy := &proxyServer{ | ||
upstreamProxy: p, | ||
status: notready, | ||
failCount: 0, | ||
} | ||
|
||
// start a goroutien to check upstream status | ||
go func() { | ||
checkUrl := url.Scheme + "://" + url.Host + "/wol-health" | ||
client := &http.Client{Timeout: time.Second} | ||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
for range ticker.C { | ||
|
||
slog.Debug("checking upstream status at", "url", checkUrl) | ||
resp, err := client.Get(checkUrl) | ||
|
||
// drain the body | ||
if err == nil && resp != nil { | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
_ = resp.Body.Close() | ||
} | ||
|
||
if err == nil && resp != nil && resp.StatusCode == http.StatusOK { | ||
slog.Debug("upstream status: ready") | ||
proxy.setStatus(ready) | ||
proxy.statusMutex.Lock() | ||
proxy.failCount = 0 | ||
proxy.statusMutex.Unlock() | ||
} else { | ||
slog.Debug("upstream status: notready", "error", err) | ||
proxy.setStatus(notready) | ||
proxy.statusMutex.Lock() | ||
proxy.failCount++ | ||
proxy.statusMutex.Unlock() | ||
} | ||
|
||
} | ||
}() | ||
|
||
return proxy | ||
} | ||
|
||
func (p *proxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == "GET" && r.URL.Path == "/status" { | ||
w.Header().Set("Content-Type", "text/plain") | ||
w.WriteHeader(200) | ||
fmt.Fprintf(w, "status: %s\n", string(p.status)) | ||
fmt.Fprintf(w, "failures: %d\n", p.failCount) | ||
return | ||
} | ||
mostlygeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if p.getStatus() == notready { | ||
slog.Info("upstream not ready, sending magic packet", "mac", *flagMac) | ||
if err := sendMagicPacket(*flagMac); err != nil { | ||
slog.Warn("failed to send magic WoL packet", "error", err) | ||
} | ||
timeout, cancel := context.WithTimeout(context.Background(), time.Duration(*flagTimeout)*time.Second) | ||
defer cancel() | ||
for { | ||
select { | ||
case <-timeout.Done(): | ||
slog.Info("timeout waiting for upstream to be ready") | ||
http.Error(w, "timeout", http.StatusRequestTimeout) | ||
return | ||
default: | ||
if p.getStatus() == ready { | ||
break | ||
} | ||
// prevent busy waiting | ||
<-time.After(100 * time.Millisecond) | ||
} | ||
|
||
// break the loop | ||
if p.getStatus() == ready { | ||
break | ||
} | ||
} | ||
mostlygeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
p.upstreamProxy.ServeHTTP(w, r) | ||
} | ||
|
||
func (p *proxyServer) getStatus() upstreamStatus { | ||
p.statusMutex.RLock() | ||
defer p.statusMutex.RUnlock() | ||
return p.status | ||
} | ||
|
||
func (p *proxyServer) setStatus(status upstreamStatus) { | ||
p.statusMutex.Lock() | ||
defer p.statusMutex.Unlock() | ||
p.status = status | ||
} | ||
|
||
func sendMagicPacket(macAddr string) error { | ||
hwAddr, err := net.ParseMAC(macAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(hwAddr) != 6 { | ||
return errors.New("invalid MAC address") | ||
} | ||
|
||
// Create the magic packet. | ||
packet := make([]byte, 102) | ||
// Add 6 bytes of 0xFF. | ||
for i := 0; i < 6; i++ { | ||
packet[i] = 0xFF | ||
} | ||
// Repeat the MAC address 16 times. | ||
for i := 1; i <= 16; i++ { | ||
copy(packet[i*6:], hwAddr) | ||
} | ||
|
||
// Send the packet using UDP. | ||
addr := net.UDPAddr{ | ||
IP: net.IPv4bcast, | ||
Port: 9, | ||
} | ||
conn, err := net.DialUDP("udp", nil, &addr) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
_, err = conn.Write(packet) | ||
return err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.