|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/TecharoHQ/anubis/lib/config" |
| 13 | + "github.com/facebookgo/flagenv" |
| 14 | + "sigs.k8s.io/yaml" |
| 15 | +) |
| 16 | + |
| 17 | +type Rule struct { |
| 18 | + Name string `yaml:"name" json:"name"` |
| 19 | + Action config.Rule `yaml:"action" json:"action"` |
| 20 | + RemoteAddr []string `json:"remote_addresses,omitempty" yaml:"remote_addresses,omitempty"` |
| 21 | + Weight *config.Weight `json:"weight,omitempty" yaml:"weight,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +func init() { |
| 25 | + flag.Usage = func() { |
| 26 | + fmt.Printf(`Usage of %[1]s: |
| 27 | +
|
| 28 | + %[1]s [flags] <blocklist-url> <filename> |
| 29 | +
|
| 30 | +Grabs the contents of the blocklist, converts it to an Anubis ruleset, and writes it to filename. |
| 31 | +
|
| 32 | +Flags: |
| 33 | +`, filepath.Base(os.Args[0])) |
| 34 | + |
| 35 | + flag.PrintDefaults() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +var ( |
| 40 | + action = flag.String("action", "DENY", "Anubis action to take (ALLOW / DENY / WEIGH)") |
| 41 | + manualRuleName = flag.String("rule-name", "", "If set, prefer this name over inferring from filename") |
| 42 | + weight = flag.Int("weight", 0, "If set to any number, add/subtract this many weight points when --action=WEIGH") |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + flagenv.Parse() |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + if flag.NArg() != 2 { |
| 50 | + flag.Usage() |
| 51 | + os.Exit(2) |
| 52 | + } |
| 53 | + |
| 54 | + blocklistURL := flag.Arg(0) |
| 55 | + foutName := flag.Arg(1) |
| 56 | + ruleName := strings.TrimSuffix(foutName, filepath.Ext(foutName)) |
| 57 | + |
| 58 | + if *manualRuleName != "" { |
| 59 | + ruleName = *manualRuleName |
| 60 | + } |
| 61 | + |
| 62 | + ruleAction := config.Rule(*action) |
| 63 | + if err := ruleAction.Valid(); err != nil { |
| 64 | + log.Fatalf("--action=%q is invalid: %v", *action, err) |
| 65 | + } |
| 66 | + |
| 67 | + result := &Rule{ |
| 68 | + Name: ruleName, |
| 69 | + Action: ruleAction, |
| 70 | + } |
| 71 | + |
| 72 | + if *weight != 0 { |
| 73 | + if ruleAction != config.RuleWeigh { |
| 74 | + log.Fatalf("used --weight=%d but --action=%s", *weight, *action) |
| 75 | + } |
| 76 | + |
| 77 | + result.Weight = &config.Weight{ |
| 78 | + Adjust: *weight, |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + ips, err := FetchBlocklist(blocklistURL) |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("can't fetch blocklist %s: %v", blocklistURL, err) |
| 85 | + } |
| 86 | + |
| 87 | + result.RemoteAddr = ips |
| 88 | + |
| 89 | + fout, err := os.Create(foutName) |
| 90 | + if err != nil { |
| 91 | + log.Fatalf("can't create output file %q: %v", foutName, err) |
| 92 | + } |
| 93 | + defer fout.Close() |
| 94 | + |
| 95 | + fmt.Fprintf(fout, "# Generated by %s on %s from %s\n\n", filepath.Base(os.Args[0]), time.Now().Format(time.RFC3339), blocklistURL) |
| 96 | + |
| 97 | + data, err := yaml.Marshal([]*Rule{result}) |
| 98 | + if err != nil { |
| 99 | + log.Fatalf("can't marshal yaml") |
| 100 | + } |
| 101 | + |
| 102 | + fout.Write(data) |
| 103 | +} |
0 commit comments