(config *firewallconfigs.FirewallActionConfig)
| 40 | } |
| 41 | |
| 42 | func (this *IPSetAction) Init(config *firewallconfigs.FirewallActionConfig) error { |
| 43 | this.config = &firewallconfigs.FirewallActionIPSetConfig{} |
| 44 | err := this.convertParams(config.Params, this.config) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | if len(this.config.WhiteName) == 0 { |
| 50 | return NewFataError("white list name should not be empty") |
| 51 | } |
| 52 | if len(this.config.BlackName) == 0 { |
| 53 | return NewFataError("black list name should not be empty") |
| 54 | } |
| 55 | |
| 56 | // 创建ipset |
| 57 | { |
| 58 | path, err := executils.LookPath("ipset") |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | // ipv4 |
| 64 | for _, listName := range []string{this.config.WhiteName, this.config.BlackName} { |
| 65 | if len(listName) == 0 { |
| 66 | continue |
| 67 | } |
| 68 | var cmd = executils.NewTimeoutCmd(30*time.Second, path, "create", listName, "hash:ip", "timeout", "0", "maxelem", "1000000") |
| 69 | cmd.WithStderr() |
| 70 | err := cmd.Run() |
| 71 | if err != nil { |
| 72 | var output = cmd.Stderr() |
| 73 | if !strings.Contains(output, "already exists") { |
| 74 | return fmt.Errorf("create ipset '%s': %w, output: %s", listName, err, output) |
| 75 | } else { |
| 76 | err = nil |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // ipv6 |
| 82 | for _, listName := range []string{this.config.WhiteNameIPv6, this.config.BlackNameIPv6} { |
| 83 | if len(listName) == 0 { |
| 84 | continue |
| 85 | } |
| 86 | var cmd = executils.NewTimeoutCmd(30*time.Second, path, "create", listName, "hash:ip", "family", "inet6", "timeout", "0", "maxelem", "1000000") |
| 87 | cmd.WithStderr() |
| 88 | err := cmd.Run() |
| 89 | if err != nil { |
| 90 | var output = cmd.Stderr() |
| 91 | if !strings.Contains(output, "already exists") { |
| 92 | return fmt.Errorf("create ipset '%s': %w, output: %s", listName, err, output) |
| 93 | } else { |
| 94 | err = nil |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 |
nothing calls this directly
no test coverage detected