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