2023-09-03 12:13:18 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func addStringFlag(names []string, defvalue string, f *flag.FlagSet) *string {
|
|
|
|
var value string
|
|
|
|
for _, name := range names {
|
|
|
|
f.StringVar(&value, name, defvalue, "")
|
|
|
|
}
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
|
|
|
func addBoolFlag(names []string, f *flag.FlagSet) *bool {
|
|
|
|
var value bool
|
|
|
|
for _, name := range names {
|
|
|
|
f.BoolVar(&value, name, false, "")
|
|
|
|
}
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
|
|
|
var SocketPath *string
|
|
|
|
|
|
|
|
func addSocketFlag(f *flag.FlagSet) *string {
|
2023-09-05 16:44:40 +02:00
|
|
|
return addStringFlag([]string{"s", "socket"}, "/run/reaction/reaction.sock", f)
|
2023-09-03 12:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func addConfFlag(f *flag.FlagSet) *string {
|
2023-09-05 16:44:40 +02:00
|
|
|
return addStringFlag([]string{"c", "config"}, "", f)
|
2023-09-03 12:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func addFormatFlag(f *flag.FlagSet) *string {
|
2023-09-05 16:44:40 +02:00
|
|
|
return addStringFlag([]string{"f", "format"}, "yaml", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addLimitFlag(f *flag.FlagSet) *string {
|
|
|
|
return addStringFlag([]string{"l", "limit"}, "", f)
|
2023-09-03 12:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
|
|
|
|
help := addBoolFlag([]string{"h", "help"}, f)
|
|
|
|
f.Parse(os.Args[2:])
|
|
|
|
if *help {
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if len(f.Args()) > maxRemainingArgs {
|
|
|
|
fmt.Printf("ERROR unrecognized argument(s): %v\n", f.Args()[maxRemainingArgs:])
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:00 +02:00
|
|
|
// FIXME add this options for show & flush
|
|
|
|
// -l/--limit .STREAM[.FILTER] # limit to stream and filter
|
|
|
|
// -f/--format yaml|json # (default: yaml)
|
2023-09-03 12:13:18 +02:00
|
|
|
func basicUsage() {
|
|
|
|
const (
|
|
|
|
bold = "\033[1m"
|
|
|
|
reset = "\033[0m"
|
|
|
|
)
|
|
|
|
fmt.Print(`usage:
|
|
|
|
|
|
|
|
` + bold + `reaction start` + reset + `
|
|
|
|
# start the daemon
|
|
|
|
|
|
|
|
# options:
|
|
|
|
-c/--config CONFIG_FILE # configuration file (required)
|
|
|
|
-s/--socket SOCKET # path to the client-daemon communication socket
|
|
|
|
# (default: /run/reaction/reaction.sock)
|
|
|
|
|
|
|
|
` + bold + `reaction example-conf` + reset + `
|
|
|
|
# print a configuration file example
|
|
|
|
|
2023-09-05 16:44:40 +02:00
|
|
|
` + bold + `reaction show` + reset + `
|
2023-10-04 12:00:00 +02:00
|
|
|
# show current matches and which actions are still to be run
|
2023-09-03 12:13:18 +02:00
|
|
|
# (e.g know what is currenly banned)
|
|
|
|
|
|
|
|
# options:
|
|
|
|
-s/--socket SOCKET # path to the client-daemon communication socket
|
|
|
|
|
2023-09-05 16:44:40 +02:00
|
|
|
` + bold + `reaction flush` + reset + ` TARGET
|
2023-10-04 12:00:00 +02:00
|
|
|
# run currently active matches and pending actions for the specified TARGET
|
|
|
|
# (then show flushed matches and actions)
|
2023-09-03 12:13:18 +02:00
|
|
|
|
|
|
|
# options:
|
|
|
|
-s/--socket SOCKET # path to the client-daemon communication socket
|
|
|
|
|
|
|
|
` + bold + `reaction test-regex` + reset + ` REGEX LINE # test REGEX against LINE
|
|
|
|
cat FILE | ` + bold + `reaction test-regex` + reset + ` REGEX # test REGEX against each line of FILE
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:embed reaction.yml
|
|
|
|
var exampleConf string
|
|
|
|
|
|
|
|
func Main() {
|
|
|
|
if len(os.Args) <= 1 {
|
|
|
|
fmt.Println("No argument provided")
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
} else if os.Args[1] == "-h" || os.Args[1] == "--help" {
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
f := flag.NewFlagSet(os.Args[1], flag.ContinueOnError)
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "help", "-h", "--help":
|
|
|
|
basicUsage()
|
|
|
|
|
|
|
|
case "example-conf":
|
|
|
|
subCommandParse(f, 0)
|
|
|
|
fmt.Print(exampleConf)
|
|
|
|
|
|
|
|
case "start":
|
|
|
|
SocketPath = addSocketFlag(f)
|
|
|
|
confFilename := addConfFlag(f)
|
|
|
|
subCommandParse(f, 0)
|
|
|
|
if *confFilename == "" {
|
|
|
|
fmt.Println("no configuration file provided")
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
Daemon(*confFilename)
|
|
|
|
|
|
|
|
case "show":
|
|
|
|
SocketPath = addSocketFlag(f)
|
|
|
|
queryFormat := addFormatFlag(f)
|
2023-09-05 16:44:40 +02:00
|
|
|
limit := addLimitFlag(f)
|
|
|
|
subCommandParse(f, 0)
|
2023-09-03 12:13:18 +02:00
|
|
|
// if *queryFormat != "yaml" && *queryFormat != "json" {
|
|
|
|
// fmt.Println("only `yaml` and `json` formats are supported.")
|
|
|
|
// f.PrintDefaults()
|
|
|
|
// os.Exit(1)
|
|
|
|
// }
|
|
|
|
if *queryFormat != "yaml" {
|
|
|
|
fmt.Println("for now, only `yaml` format is supported.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-09-05 16:44:40 +02:00
|
|
|
if *limit != "" {
|
|
|
|
fmt.Println("for now, -l/--limit is not supported")
|
2023-09-03 12:13:18 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
// f.Arg(0) is "" if there is no remaining argument
|
2023-09-24 16:01:56 +02:00
|
|
|
ClientShow(*limit)
|
2023-09-03 12:13:18 +02:00
|
|
|
|
|
|
|
case "flush":
|
|
|
|
SocketPath = addSocketFlag(f)
|
2023-09-05 16:44:40 +02:00
|
|
|
limit := addLimitFlag(f)
|
|
|
|
subCommandParse(f, 1)
|
2023-09-03 12:13:18 +02:00
|
|
|
if f.Arg(0) == "" {
|
2023-09-05 16:44:40 +02:00
|
|
|
fmt.Println("subcommand flush takes one TARGET argument")
|
|
|
|
basicUsage()
|
2023-09-03 12:13:18 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-09-05 16:44:40 +02:00
|
|
|
if *limit != "" {
|
|
|
|
fmt.Println("for now, -l/--limit is not supported")
|
2023-09-03 12:13:18 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
ClientFlush(f.Arg(0), f.Arg(1))
|
|
|
|
|
|
|
|
case "test-regex":
|
|
|
|
// socket not needed, no interaction with the daemon
|
|
|
|
subCommandParse(f, 2)
|
|
|
|
if f.Arg(0) == "" {
|
|
|
|
fmt.Println("subcommand test-regex takes at least one REGEX argument")
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
regex, err := regexp.Compile(f.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR the specified regex is invalid: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if f.Arg(1) == "" {
|
|
|
|
fmt.Println("INFO no second argument. reading from stdin.")
|
|
|
|
|
|
|
|
MatchStdin(regex)
|
|
|
|
} else {
|
|
|
|
Match(regex, f.Arg(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Println("subcommand not recognized")
|
|
|
|
basicUsage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|