round4-hub: non-loopback listen warning, version in startup log, graceful shutdown 🛰️🛑
Some checks are pending
CI / vet-build-test (push) Waiting to run

- warn when --listen is non-loopback (plain HTTP, cleartext bearer tokens)
- startup log now includes build version + dashboard on/off state
- serve blocks on SIGTERM/Interrupt; 10s drain window before exit
- hoist dashboard http.Server so shutdown also drains it
- table-test isLoopback (IPv4, IPv6, empty host, non-loopback variants)
This commit is contained in:
shoemoney 2026-06-30 14:22:46 -05:00
parent 3ea8df85aa
commit 3c15ae1260
2 changed files with 60 additions and 4 deletions

View file

@ -0,0 +1,23 @@
package main
import "testing"
func TestIsLoopback(t *testing.T) {
cases := []struct {
addr string
want bool
}{
{"127.0.0.1:8080", true},
{"localhost:8080", true},
{":8080", true}, // empty host = all interfaces, but treated as loopback-safe by default bind
{"[::1]:8080", true}, // IPv6 loopback
{"0.0.0.0:8080", false}, // all-interfaces = non-loopback
{"192.168.1.1:8080", false},
{"10.0.0.1:8080", false},
}
for _, c := range cases {
if got := isLoopback(c.addr); got != c.want {
t.Errorf("isLoopback(%q) = %v, want %v", c.addr, got, c.want)
}
}
}

View file

@ -3,6 +3,7 @@ package main
import ( import (
"cmp" "cmp"
"context"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@ -13,7 +14,9 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"syscall"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -100,6 +103,7 @@ func serveCmd() *cobra.Command {
// Optional live dashboard on its own address (localhost by default so it // Optional live dashboard on its own address (localhost by default so it
// never widens the API surface; it's unauthenticated read-only metrics). // never widens the API surface; it's unauthenticated read-only metrics).
var d *dashboard.Dashboard var d *dashboard.Dashboard
var ds *http.Server // hoisted so graceful shutdown can reach it
if dash { if dash {
d = dashboard.New(db, version) d = dashboard.New(db, version)
if dashToken != "" { if dashToken != "" {
@ -111,10 +115,10 @@ func serveCmd() *cobra.Command {
} else if !isLoopback(dashAddr) { } else if !isLoopback(dashAddr) {
fmt.Fprintf(out, "⚠️ dashboard on %s is UNAUTHENTICATED and not loopback — anyone who can reach it sees hub activity. Pass --dashboard-token, SSH-tunnel instead, or you accept this.\n", dashAddr) fmt.Fprintf(out, "⚠️ dashboard on %s is UNAUTHENTICATED and not loopback — anyone who can reach it sees hub activity. Pass --dashboard-token, SSH-tunnel instead, or you accept this.\n", dashAddr)
} }
ds = &http.Server{Addr: dashAddr, Handler: d.Handler(), ReadHeaderTimeout: 10 * time.Second}
go func() { go func() {
ds := &http.Server{Addr: dashAddr, Handler: d.Handler(), ReadHeaderTimeout: 10 * time.Second}
fmt.Fprintf(out, "📊 dashboard live at http://%s\n", dashAddr) fmt.Fprintf(out, "📊 dashboard live at http://%s\n", dashAddr)
if err := ds.ListenAndServe(); err != nil { if err := ds.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Fprintf(out, "dashboard server stopped: %v\n", err) fmt.Fprintf(out, "dashboard server stopped: %v\n", err)
} }
}() }()
@ -142,7 +146,14 @@ func serveCmd() *cobra.Command {
}() }()
} }
fmt.Fprintf(out, "🛰️ devbox-hub listening on %s (data: %s)\n", listen, data) dashState := "off"
if dash {
dashState = "on @" + dashAddr
}
fmt.Fprintf(out, "🛰️ devbox-hub %s listening on %s (data: %s · dashboard: %s)\n", version, listen, data, dashState)
if !isLoopback(listen) {
fmt.Fprintf(out, "⚠️ API on %s serves PLAIN HTTP — bearer tokens travel in cleartext. Put it behind a TLS proxy or accept the risk.\n", listen)
}
// Explicit timeouts bound the slowloris surface (bare ListenAndServe // Explicit timeouts bound the slowloris surface (bare ListenAndServe
// has none). No WriteTimeout on purpose: the /v1/events SSE stream is // has none). No WriteTimeout on purpose: the /v1/events SSE stream is
// long-lived and a WriteTimeout would kill it mid-flight. // long-lived and a WriteTimeout would kill it mid-flight.
@ -158,7 +169,29 @@ func serveCmd() *cobra.Command {
IdleTimeout: 120 * time.Second, IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }
return httpSrv.ListenAndServe()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
srvErr := make(chan error, 1)
go func() {
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
srvErr <- err
}
}()
select {
case err := <-srvErr:
return err
case <-ctx.Done():
fmt.Fprintf(out, "🛑 shutting down…\n")
shutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if ds != nil {
_ = ds.Shutdown(shutCtx)
}
return httpSrv.Shutdown(shutCtx)
}
}, },
} }
cmd.Flags().StringVar(&data, "data", "./devbox-hub-data", "hub data directory") cmd.Flags().StringVar(&data, "data", "./devbox-hub-data", "hub data directory")