ratelimit.New takes ratelimit.Params (Limits, MaxClients) in place of two arguments by position. The process logger moves out of requestlog into internal/processlog, whose New takes Params (Output, InstanceName, Level); its two tests move with it unchanged. geojs.GeoJS keeps its params whole, as DNSBL, AbuseIPDB and routewatch.Client do. SPEC and README list the new package. Model: opus-5-5
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package sww
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
|
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
|
)
|
|
|
|
// RunRProxy is `sww rproxy`: it reads rproxy's settings, then serves
|
|
// requests until ctx is done. Then it gives the requests in progress
|
|
// shutdownTimeout to finish, and the reports being sent
|
|
// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. It returns the process's exit status, 1
|
|
// when rproxy cannot start. Its process messages name the host's name as
|
|
// instance, and it writes those at info or more severe: rproxy has no
|
|
// setting for either.
|
|
func RunRProxy(ctx context.Context, params Params) int {
|
|
hostname, _ := os.Hostname() // "" when the host has no name to give
|
|
processLog := processlog.New(processlog.Params{
|
|
Output: params.Stdout,
|
|
InstanceName: hostname,
|
|
Level: slog.LevelInfo,
|
|
})
|
|
|
|
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
|
if err != nil {
|
|
return cannotStart(processLog, startError{"invalid setting", err})
|
|
}
|
|
|
|
var (
|
|
server *rproxy.Server
|
|
listener net.Listener
|
|
)
|
|
|
|
failed := make(servingFailed, 1)
|
|
|
|
app := fx.New(
|
|
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
|
|
fx.Provide(
|
|
newRProxyServer,
|
|
func(server *rproxy.Server) *http.Server { return server.Server },
|
|
newRProxyListener,
|
|
),
|
|
fx.Invoke(serve),
|
|
fx.Populate(&server, &listener),
|
|
fx.WithLogger(fxEventLogger(processLog)),
|
|
)
|
|
|
|
err = app.Err()
|
|
if err != nil {
|
|
return cannotStart(processLog, err)
|
|
}
|
|
|
|
processLog.Info("starting",
|
|
"version", params.Version,
|
|
"address", listener.Addr().String(),
|
|
"settings", cfg)
|
|
|
|
return run(ctx, app, failed, processLog, func() error {
|
|
server.WaitForReports()
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// newRProxyServer returns the server rproxy runs.
|
|
func newRProxyServer(
|
|
cfg *config.RProxy, out requestLog, processLog *slog.Logger,
|
|
) *rproxy.Server {
|
|
return rproxy.New(rproxy.Params{
|
|
Config: cfg,
|
|
RequestLog: out,
|
|
ProcessLog: processLog,
|
|
})
|
|
}
|
|
|
|
// newRProxyListener listens on SWW_RPROXY_LISTEN_ADDR.
|
|
func newRProxyListener(cfg *config.RProxy) (net.Listener, error) {
|
|
return listen(cfg.ListenAddr, "SWW_RPROXY_LISTEN_ADDR")
|
|
}
|