Hold Go's server privately in waf.Server and rproxy.Server (closes #437) #481

Merged
clawbot merged 1 commits from issue-437-private-http-server into next 2026-11-20 01:58:26 +01:00
15 changed files with 134 additions and 54 deletions
Showing only changes of commit ad7ea4608b - Show all commits

No files matched your search

+9 -1
View File
@@ -1,6 +1,14 @@
package rproxy
import "time"
import (
"net/http"
"time"
)
// HTTPServer is Go's server s holds, for tests that read its settings.
func (s *Server) HTTPServer() *http.Server {
return s.httpServer
}
// StopBreakerClock has the circuit breaker of s read at as the time, so that how
// long requests take cannot matter to it. It is called before s serves.
+5 -5
View File
@@ -56,7 +56,7 @@ func TestServerHasTheFixedLimits(t *testing.T) {
InstanceName: "test",
Level: slog.LevelInfo,
}),
})
}).HTTPServer()
if server.Addr != ":8080" || server.MaxHeaderBytes != 28<<10 ||
server.IdleTimeout != 2*time.Minute || server.ReadHeaderTimeout != time.Minute {
@@ -709,7 +709,7 @@ func TestABodyLeftUnreadIsReadOnlyUntilTheRequestTimeLimit(t *testing.T) {
before := time.Now()
server.Handler.ServeHTTP(w, req)
server.ServeHTTP(w, req)
if w.Code != http.StatusNoContent {
t.Errorf("status %d, want the app's 204", w.Code)
@@ -762,7 +762,7 @@ func TestABodyLeftUnreadIsReadNoMoreOnceTheTimeToSendTheAnswerRunsOut(
before := time.Now()
server.Handler.ServeHTTP(w, req)
server.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("status %d, want the app's 200", w.Code)
@@ -815,7 +815,7 @@ func TestAnAnswerRProxyGivesItselfHasTheResponseTimeToBeSent(t *testing.T) {
before := time.Now()
server.Handler.ServeHTTP(w, req)
server.ServeHTTP(w, req)
if w.Code != tc.status {
t.Errorf("status %d, want %d", w.Code, tc.status)
@@ -856,7 +856,7 @@ func TestAnAnswerRProxyGivesItselfHasTenSecondsToBeSentAtLeast(
before := time.Now()
server.Handler.ServeHTTP(w, req)
server.ServeHTTP(w, req)
if w.writeDeadline.Before(before.Add(least)) {
t.Errorf(
+1 -1
View File
@@ -583,7 +583,7 @@ func TestAQuestionNamesNoZone(t *testing.T) {
req.RemoteAddr = tc.remoteAddr
req.Header.Set(forwardedForHeader, tc.forwardedFor)
server.Handler.ServeHTTP(httptest.NewRecorder(), req)
server.ServeHTTP(httptest.NewRecorder(), req)
asked := standIn.question(t)
if asked.header.Get(wafprotocol.ClientHeader) != tc.client ||
+36 -13
View File
@@ -16,6 +16,7 @@ import (
"io"
"log"
"log/slog"
"net"
"net/http"
"net/url"
"slices"
@@ -63,9 +64,8 @@ type Params struct {
// Server is the server `sww rproxy` runs.
type Server struct {
*http.Server
h *handler
httpServer *http.Server
h *handler
}
// New returns the server `sww rproxy` runs. Go's server itself refuses
@@ -94,17 +94,40 @@ func New(params Params) *Server {
breakerClock: time.Now,
}
return &Server{
Server: &http.Server{
Addr: params.Config.ListenAddr,
Handler: h,
ErrorLog: errorLog,
ReadHeaderTimeout: params.Config.MaxRequestTime,
IdleTimeout: idleTimeout,
MaxHeaderBytes: requestHeaderMaxBytes - goServerExtraHeaderBytes,
},
h: h,
s := &Server{h: h}
s.httpServer = &http.Server{
Addr: params.Config.ListenAddr,
Handler: s,
ErrorLog: errorLog,
ReadHeaderTimeout: params.Config.MaxRequestTime,
IdleTimeout: idleTimeout,
MaxHeaderBytes: requestHeaderMaxBytes - goServerExtraHeaderBytes,
}
return s
}
// Serve serves requests on listener until Shutdown or Close, as Go's
// http.Server.Serve does.
func (s *Server) Serve(listener net.Listener) error {
return s.httpServer.Serve(listener)
}
// Shutdown stops serving once the requests in progress have finished, or
// when ctx is done, as Go's http.Server.Shutdown does.
func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}
// Close stops serving at once and closes every connection, as Go's
// http.Server.Close does.
func (s *Server) Close() error {
return s.httpServer.Close()
}
// ServeHTTP handles one request from a visitor, as handler.ServeHTTP says.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.h.ServeHTTP(w, r)
}
// WaitForReports returns once the reports being sent have gone or failed,
+1 -1
View File
@@ -710,7 +710,7 @@ func TestAnAnswerWithoutAContentTypeGetsNone(t *testing.T) {
)
w := httptest.NewRecorder()
server.Handler.ServeHTTP(w, req)
server.ServeHTTP(w, req)
// Go's server guesses a type when the first part of the body goes before the
// headers are flushed, which is a matter of timing here. So this looks for what
+9 -1
View File
@@ -95,12 +95,20 @@ func background(loop func(context.Context)) fx.Hook {
)
}
// daemonServer is the server a daemon runs, waf.Server or rproxy.Server: the
// methods of Go's http.Server that serve starts and stops it with.
type daemonServer interface {
Serve(listener net.Listener) error
Shutdown(ctx context.Context) error
Close() error
}
// serve serves requests on listener from the start. At the stop it gives the
// requests in progress shutdownTimeout to finish, then closes their
// connections. When serving fails, it logs why and sends the error to
// failed.
func serve(
lc fx.Lifecycle, server *http.Server, listener net.Listener,
lc fx.Lifecycle, server daemonServer, listener net.Listener,
processLog *slog.Logger, failed servingFailed,
) {
served := make(chan error, 1)
+7 -3
View File
@@ -4,7 +4,6 @@ import (
"context"
"log/slog"
"net"
"net/http"
"os"
"go.uber.org/fx"
@@ -44,8 +43,13 @@ func RunRProxy(ctx context.Context, params Params) int {
app := fx.New(
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
fx.Provide(
newRProxyServer,
func(server *rproxy.Server) *http.Server { return server.Server },
// run waits for rproxy.Server's reports, and serve takes it as a
// daemonServer.
fx.Annotate(
newRProxyServer,
fx.As(fx.Self()),
fx.As(new(daemonServer)),
),
newRProxyListener,
),
fx.Invoke(serve),
+2 -3
View File
@@ -6,7 +6,6 @@ import (
"io"
"log/slog"
"net"
"net/http"
"time"
"go.uber.org/fx"
@@ -97,8 +96,8 @@ func RunWAF(ctx context.Context, params Params) int {
newAlertQueue, newRuleFiles, newCoreRuleSet, newLookupFile,
newLimiter, newLedger, newAnomalyCounters,
newLists, newDNSBL, newAbuseIPDB, newRouteWatch,
newMetrics, newGeoJS, newWAFServer,
func(server *waf.Server) *http.Server { return server.Server },
newMetrics, newGeoJS,
fx.Annotate(newWAFServer, fx.As(new(daemonServer))),
newWAFListener, newStateFiles,
),
// fx stops hooks in the reverse of the order they were added in. The
+1 -1
View File
@@ -717,7 +717,7 @@ func askDirectly(
recorder := httptest.NewRecorder()
began := time.Now()
server.Handler.ServeHTTP(recorder, req)
server.ServeHTTP(recorder, req)
return recorder.Code, time.Since(began)
}
+1 -1
View File
@@ -592,7 +592,7 @@ func serveFromDE(
req.Header.Set(wafprotocol.ClientHeader, fromDE)
req.Header.Set(requestIDHeader, "the-id")
server.Handler.ServeHTTP(httptest.NewRecorder(), req)
server.ServeHTTP(httptest.NewRecorder(), req)
}
// wantPercent checks a limit percentage that a log line or a ban's notes
+7
View File
@@ -1,4 +1,11 @@
package waf
import "net/http"
// BesideLookupTimeLimit is besideLookupTimeLimit.
const BesideLookupTimeLimit = besideLookupTimeLimit
// HTTPServer is Go's server s holds, for tests that read its settings.
func (s *Server) HTTPServer() *http.Server {
return s.httpServer
}
+2 -1
View File
@@ -109,7 +109,8 @@ type CoreRuleSetInspector interface {
// Server is the server waf runs.
type Server struct {
*http.Server
httpServer *http.Server
h *handler
}
// newHandler returns the handler with what every request shares, the parts
+6 -3
View File
@@ -344,8 +344,8 @@ func startProxyWithAlerts(
return startRProxy(t, server.Addr, appURL, env), out, server, alertQueue
}
// serve serves server on a port of its own until the test ends, and sets its
// Addr to where it listens.
// serve serves server on a port of its own until the test ends, and sets
// server.Addr to where it listens.
func serve(t *testing.T, server *wafAndParts) {
t.Helper()
@@ -423,10 +423,13 @@ func startRProxy(
}
// wafAndParts is waf's server with the Params it was built from, so that a
// test can read the parts the server uses.
// test can read the parts the server uses. Addr is where it listens once serve
// has it serving.
type wafAndParts struct {
*waf.Server
waf.Params
Addr string
}
// banAbuseIPDBBlacklist does on w what a fetch of AbuseIPDB's blacklist with
+37 -12
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"log/slog"
"net"
"net/http"
"strings"
"time"
@@ -30,19 +31,43 @@ const (
// is far above rproxy's, and a question refused for its size would have rproxy
// let the request through.
func New(params Params) *Server {
return &Server{
Server: &http.Server{
Addr: params.Config.ListenAddr,
Handler: newHandler(params),
ErrorLog: slog.NewLogLogger(
params.ProcessLog.Handler(),
slog.LevelWarn,
),
ReadTimeout: wafTimeout,
WriteTimeout: wafTimeout,
IdleTimeout: wafIdleTimeout,
},
s := &Server{h: newHandler(params)}
s.httpServer = &http.Server{
Addr: params.Config.ListenAddr,
Handler: s,
ErrorLog: slog.NewLogLogger(
params.ProcessLog.Handler(),
slog.LevelWarn,
),
ReadTimeout: wafTimeout,
WriteTimeout: wafTimeout,
IdleTimeout: wafIdleTimeout,
}
return s
}
// Serve serves requests on listener until Shutdown or Close, as Go's
// http.Server.Serve does.
func (s *Server) Serve(listener net.Listener) error {
return s.httpServer.Serve(listener)
}
// Shutdown stops serving once the requests in progress have finished, or
// when ctx is done, as Go's http.Server.Shutdown does.
func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}
// Close stops serving at once and closes every connection, as Go's
// http.Server.Close does.
func (s *Server) Close() error {
return s.httpServer.Close()
}
// ServeHTTP handles one request to waf, as handler.ServeHTTP says.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.h.ServeHTTP(w, r)
}
// ServeHTTP handles one request to waf: a request carrying
+10 -8
View File
@@ -703,7 +703,9 @@ func TestQuestionWhoseBodyStopsArrivingGetsNoAnswer(t *testing.T) {
t,
"",
map[string]string{metricsToken: token},
func(server *wafAndParts) { server.ReadTimeout = readTimeout },
func(server *wafAndParts) {
server.HTTPServer().ReadTimeout = readTimeout
},
)
conn := dial(t, s.addr)
@@ -1834,16 +1836,16 @@ func TestLogLevelHoldsBackNoRequestLine(t *testing.T) {
func TestServerHasTheTimeLimitsSPECGives(t *testing.T) {
t.Parallel()
s := startWAF(t, "", nil)
server := startWAF(t, "", nil).server.HTTPServer()
if s.server.ReadTimeout != time.Minute ||
s.server.WriteTimeout != time.Minute ||
s.server.IdleTimeout != 2*time.Minute {
if server.ReadTimeout != time.Minute ||
server.WriteTimeout != time.Minute ||
server.IdleTimeout != 2*time.Minute {
t.Errorf(
"the read, write and idle limits are %v, %v and %v, want 1m, 1m and 2m",
s.server.ReadTimeout,
s.server.WriteTimeout,
s.server.IdleTimeout,
server.ReadTimeout,
server.WriteTimeout,
server.IdleTimeout,
)
}
}