check / check (push) Waiting to run
go.uber.org/fx builds the parts of waf and rproxy in internal/sww, one provider per part. Each background loop is a lifecycle hook added where its part is built. waf.Params takes every part built, and waf.New builds none. GeoJS calls back into waf, so waf exports AddLookup and GeoJSFailed, and clientGroup and banNetblock take the settings. newServer, loadStateFiles, the inBackground list and serveRProxy are gone. serve is the one hook that serves both daemons, and reports a failure to run on a channel of its own; fx's signal handling is not used. Once fx has stopped, run takes each daemon's last step: waf writes the state files, and rproxy waits for its reports. Model: opus-5-5
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
// Package sww runs the sww process, as the command it is given.
|
|
// `sww waf` reads the settings, the rule files, the lookup database
|
|
// and the state and cache files, answers rproxy's questions until it is told to
|
|
// stop, and then stops in an orderly way, writing the state and cache files.
|
|
// `sww rproxy` reads rproxy's settings alone, and runs the reverse proxy in
|
|
// front of one app that asks waf about each request. `sww healthcheck` is the
|
|
// container's health check. fx builds the parts of each daemon, starts their
|
|
// background work and stops it.
|
|
package sww
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
)
|
|
|
|
// usageStatus is the exit status with no command, or one sww does not
|
|
// know.
|
|
const usageStatus = 2
|
|
|
|
// usage is what sww prints then.
|
|
const usage = `usage: sww <command>
|
|
|
|
commands:
|
|
rproxy the reverse proxy in front of one app
|
|
waf decides for rproxy, and keeps bans, counters and history
|
|
healthcheck the container's health check
|
|
`
|
|
|
|
// Params are what RunWAF and RunRProxy need from the process.
|
|
type Params struct {
|
|
// Version is the version of the binary, set when it is built.
|
|
Version string
|
|
// Viper reads the settings, normally the viper config.NewViper returns
|
|
// for the process's environment.
|
|
Viper *viper.Viper
|
|
// Stdout receives the request log and the process's own messages.
|
|
Stdout io.Writer
|
|
// BesideLookupTimeLimit is the waf.Params.BesideLookupTimeLimit RunWAF
|
|
// gives waf. Main leaves it zero, waf's own limit; tests lengthen it.
|
|
BesideLookupTimeLimit time.Duration
|
|
}
|
|
|
|
// Main runs the command os.Args names, and returns the process's exit status.
|
|
// `sww rproxy` and `sww waf` run until SIGTERM or SIGINT.
|
|
// With no command, or one it does not know, it prints the commands and returns
|
|
// usageStatus.
|
|
func Main(version string) int {
|
|
args := os.Args[1:]
|
|
if len(args) == 0 {
|
|
return printUsage(os.Stderr)
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(),
|
|
syscall.SIGTERM, os.Interrupt)
|
|
defer stop()
|
|
|
|
params := Params{
|
|
Version: version,
|
|
Viper: config.NewViper(),
|
|
Stdout: os.Stdout,
|
|
}
|
|
|
|
switch args[0] {
|
|
case "rproxy":
|
|
return RunRProxy(ctx, params)
|
|
case "waf":
|
|
return RunWAF(ctx, params)
|
|
case "healthcheck":
|
|
return HealthCheck(ctx, args[1:], params.Viper, os.Stderr)
|
|
default:
|
|
return printUsage(os.Stderr)
|
|
}
|
|
}
|
|
|
|
// printUsage writes the commands to w, and returns usageStatus.
|
|
func printUsage(w io.Writer) int {
|
|
_, _ = fmt.Fprint(w, usage)
|
|
|
|
return usageStatus
|
|
}
|