Skip to content

Add --log-level, proper exit statuses and SIGTERM handling - #234

Open
dividedby wants to merge 8 commits into
windtf:masterfrom
dividedby:feat/logging-cli
Open

dividedby wants to merge 8 commits into
windtf:masterfrom
dividedby:feat/logging-cli

Conversation

@dividedby

Copy link
Copy Markdown

Summary

  • --log-level error|info|debug (default info). info keeps the startup messages and sets the wireguard device log to errors only; debug adds per-request lines (Resolving address for ..., Health metric request, closed idle UDP sessions) and the verbose wireguard log. -s/--silent is now an alias for error. Previously -s only silenced wireguard-go, and every health-check hit logged a line (container healthchecks every few seconds).
  • Exit statuses. Argument errors, including a missing config path or a malformed -i address, print the usage to stderr and exit 2 (previously usage went to stdout with exit 0, and a bad -i panicked). A failed daemon start or an -i listener that can't bind exits 1 with a log message. --help still exits 0.
  • SIGTERM is handled alongside SIGINT, so docker stop shuts down cleanly.

README documents the flag, the level table and the exit statuses.

This PR touches cmd/wireproxy/main.go and adds cmd/wireproxy/main_test.go, which may conflict with the Landlock PR from the same set. Happy to rebase.

Test plan

  • go test -race ./..., go vet ./..., gofmt -l ., golangci-lint (fork CI green).
  • New tests: level gating and call sites, level mapping, subprocess tests for usage errors (exit 2, stderr), --help (exit 0), a busy -i port (exit 1, no stack trace), a malformed -i, and SIGTERM (exit 0).

🤖 Generated with Claude Code

dividedby and others added 8 commits November 19, 2026 21:33
-s only silenced wireguard-go; the device defaulted to Verbose, which logs
every keepalive and handshake, and wireproxy's own per-request lines could not
be turned off. Every /readyz hit printed "Health metric request", so
container healthchecks filled the log, and every SOCKS5 name lookup printed
"Resolving address for".

--log-level takes error, info (default) or debug:

- error: wireguard logs errors only; wireproxy prints errors only.
- info: wireguard logs errors only; wireproxy also prints startup and
  lifecycle lines.
- debug: wireguard logs verbosely; wireproxy also prints per-request lines.

-s/--silent stays and is an alias for --log-level error. It now maps to the
wireguard error level instead of fully silent, so device errors still show.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every argument parse error printed the usage to stdout and returned from
main, so wireproxy exited 0. A missing option value (for example -i with no
argument) or an unknown flag looked like a successful run to supervisors and
scripts.

Print the error and usage to stderr and exit with status 2. --help is
handled by the argument parser and still prints to stdout and exits 0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Only SIGINT and SIGQUIT were passed to signal.Notify. SIGTERM, which is what
docker stop and systemd send, fell through to the Go runtime's default and
killed the process by signal instead of leaving main through the normal
shutdown path (as PID 1 in a container this shows up as exit status 2).

Add syscall.SIGTERM to the notified signals.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…start

Two more error paths in main returned normally and exited 0: no configuration
path given (and none at a default location), and a failure to spawn the
background process with -d. Both printed the message to stdout.

A missing configuration path is an argument error: print it with the usage to
stderr and exit 2. A daemon spawn failure is a runtime error: print it to
stderr and exit 1. Config parse failures already exit 1 through log.Fatal and
are now covered by a test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
golangci-lint (errcheck) flagged the unchecked Close calls on the reserved
listener and the health response body. Check the listener close and discard
the response body close explicitly.

The test reserved a port by listening and closing, then passed it to the
child, so another process could take it in between. Start the child again
with a fresh port (up to three attempts) when it exits before its health
endpoint answers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
If the -i address could not be bound (for example the port is already in
use), the listener goroutine panicked, which dumps a stack trace and exits
with status 2. This is a runtime error, not a usage error: log it and exit 1
like the other startup failures.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A malformed -i value (for example "nohostport", or a non-numeric or
out-of-range port) was only noticed in extractPort while applying the
Landlock rules, which panics with a stack trace. Check it with the other
arguments instead: it must be empty or host:port with a port in 0-65535.
A bad value now prints the usage to stderr and exits 2.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
"closing inactive session" is printed once per client session, so it belongs
with the other per-request lines at debug level. The "listening" line stays
at info.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant