// 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" "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 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 } // 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 }