diff options
| author | Loyalsoldier <[email protected]> | 2024-08-31 11:03:59 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-08-31 11:03:59 +0800 |
| commit | deff06ac0f59751bb9857bc7d645b756b6133ddf (patch) | |
| tree | 318880aad86a2223ed0cddfbc4f4283929861102 /plugin | |
| parent | fca10e64304b331c56dafe307f4eddfb7614fbb2 (diff) | |
Feat: support to specify IP or CIDR in config file for text input format
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/plaintext/common_in.go | 1 | ||||
| -rw-r--r-- | plugin/plaintext/text_in.go | 50 |
2 files changed, 45 insertions, 6 deletions
diff --git a/plugin/plaintext/common_in.go b/plugin/plaintext/common_in.go index bf678e37..fbaf21ff 100644 --- a/plugin/plaintext/common_in.go +++ b/plugin/plaintext/common_in.go @@ -17,6 +17,7 @@ type textIn struct { Description string Name string URI string + IPOrCIDR []string InputDir string Want map[string]bool OnlyIPType lib.IPType diff --git a/plugin/plaintext/text_in.go b/plugin/plaintext/text_in.go index 037271c1..9bc0d73f 100644 --- a/plugin/plaintext/text_in.go +++ b/plugin/plaintext/text_in.go @@ -30,6 +30,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input var tmp struct { Name string `json:"name"` URI string `json:"uri"` + IPOrCIDR []string `json:"ipOrCIDR"` InputDir string `json:"inputDir"` Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` @@ -49,16 +50,23 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input } } - if tmp.Name == "" && tmp.URI == "" && tmp.InputDir == "" { - return nil, fmt.Errorf("type %s | action %s missing inputdir or name or uri", typeTextIn, action) + if iType != typeTextIn && len(tmp.IPOrCIDR) > 0 { + return nil, fmt.Errorf("❌ [type %s | action %s] ipOrCIDR is invalid for this input format", iType, action) } - if (tmp.Name != "" && tmp.URI == "") || (tmp.Name == "" && tmp.URI != "") { - return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeTextIn, action) + if iType == typeJSONIn && len(tmp.JSONPath) == 0 { + return nil, fmt.Errorf("❌ [type %s | action %s] missing jsonPath", typeJSONIn, action) } - if iType == typeJSONIn && len(tmp.JSONPath) == 0 { - return nil, fmt.Errorf("type %s | action %s missing jsonPath", typeJSONIn, action) + if tmp.InputDir == "" { + if tmp.Name == "" { + return nil, fmt.Errorf("❌ [type %s | action %s] missing inputDir or name", iType, action) + } + if tmp.URI == "" && len(tmp.IPOrCIDR) == 0 { + return nil, fmt.Errorf("❌ [type %s | action %s] missing uri or ipOrCIDR", iType, action) + } + } else if tmp.Name != "" || tmp.URI != "" || len(tmp.IPOrCIDR) > 0 { + return nil, fmt.Errorf("❌ [type %s | action %s] inputDir is not allowed to be used with name or uri or ipOrCIDR", iType, action) } // Filter want list @@ -75,6 +83,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input Description: descTextIn, Name: tmp.Name, URI: tmp.URI, + IPOrCIDR: tmp.IPOrCIDR, InputDir: tmp.InputDir, Want: wantList, OnlyIPType: tmp.OnlyIPType, @@ -104,6 +113,7 @@ func (t *textIn) Input(container lib.Container) (lib.Container, error) { switch { case t.InputDir != "": err = t.walkDir(t.InputDir, entries) + case t.Name != "" && t.URI != "": switch { case strings.HasPrefix(strings.ToLower(t.URI), "http://"), strings.HasPrefix(strings.ToLower(t.URI), "https://"): @@ -111,6 +121,15 @@ func (t *textIn) Input(container lib.Container) (lib.Container, error) { default: err = t.walkLocalFile(t.URI, t.Name, entries) } + if err != nil { + return nil, err + } + + fallthrough + + case t.Name != "" && len(t.IPOrCIDR) > 0: + err = t.appendIPOrCIDR(t.IPOrCIDR, t.Name, entries) + default: return nil, fmt.Errorf("config missing argument inputDir or name or uri") } @@ -238,3 +257,22 @@ func (t *textIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) return nil } + +func (t *textIn) appendIPOrCIDR(ipOrCIDR []string, name string, entries map[string]*lib.Entry) error { + name = strings.ToUpper(name) + + entry, found := entries[name] + if !found { + entry = lib.NewEntry(name) + } + + for _, cidr := range ipOrCIDR { + if err := entry.AddPrefix(strings.TrimSpace(cidr)); err != nil { + return err + } + } + + entries[name] = entry + + return nil +} |
