diff options
| author | Loyalsoldier <[email protected]> | 2024-08-13 08:36:12 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-08-13 10:24:46 +0800 |
| commit | 50ed45ced0f436f1a2817390df46f85a953af675 (patch) | |
| tree | 8e606bec1a72ec7a249e7fd3185c5afb8e3772dc /plugin | |
| parent | 56cd72d97f058fc6e931995039d69cd4b35084ec (diff) | |
Refine: wantedList in various formats
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/maxmind/country_csv.go | 42 | ||||
| -rw-r--r-- | plugin/maxmind/mmdb_in.go | 59 | ||||
| -rw-r--r-- | plugin/maxmind/mmdb_out.go | 4 | ||||
| -rw-r--r-- | plugin/mihomo/mrs_in.go | 24 | ||||
| -rw-r--r-- | plugin/mihomo/mrs_out.go | 24 | ||||
| -rw-r--r-- | plugin/plaintext/common_in.go | 1 | ||||
| -rw-r--r-- | plugin/plaintext/common_out.go | 11 | ||||
| -rw-r--r-- | plugin/plaintext/text_in.go | 19 | ||||
| -rw-r--r-- | plugin/plaintext/text_out.go | 14 | ||||
| -rw-r--r-- | plugin/singbox/srs_in.go | 24 | ||||
| -rw-r--r-- | plugin/singbox/srs_out.go | 24 | ||||
| -rw-r--r-- | plugin/special/cutter.go | 24 | ||||
| -rw-r--r-- | plugin/special/stdout.go | 22 | ||||
| -rw-r--r-- | plugin/v2ray/dat_in.go | 32 | ||||
| -rw-r--r-- | plugin/v2ray/dat_out.go | 24 |
15 files changed, 202 insertions, 146 deletions
diff --git a/plugin/maxmind/country_csv.go b/plugin/maxmind/country_csv.go index 15c80719..4aa02975 100644 --- a/plugin/maxmind/country_csv.go +++ b/plugin/maxmind/country_csv.go @@ -59,6 +59,14 @@ func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputCo tmp.IPv6File = defaultCountryIPv6File } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &geoLite2CountryCSV{ Type: typeCountryCSV, Action: action, @@ -66,7 +74,7 @@ func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputCo CountryCodeFile: tmp.CountryCodeFile, IPv4File: tmp.IPv4File, IPv6File: tmp.IPv6File, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -78,7 +86,7 @@ type geoLite2CountryCSV struct { CountryCodeFile string IPv4File string IPv6File string - Want []string + Want map[string]bool OnlyIPType lib.IPType } @@ -100,7 +108,7 @@ func (g *geoLite2CountryCSV) Input(container lib.Container) (lib.Container, erro return nil, err } - entries := make(map[string]*lib.Entry, 300) + entries := make(map[string]*lib.Entry, len(ccMap)) if g.IPv4File != "" { if err := g.process(g.IPv4File, ccMap, entries); err != nil { @@ -164,11 +172,16 @@ func (g *geoLite2CountryCSV) getCountryCode() (map[string]string, error) { } id := strings.TrimSpace(line[0]) - countryCode := strings.TrimSpace(line[4]) + countryCode := strings.ToUpper(strings.TrimSpace(line[4])) if id == "" || countryCode == "" { continue } - ccMap[id] = strings.ToUpper(countryCode) + + if len(g.Want) > 0 && !g.Want[countryCode] { + continue + } + + ccMap[id] = countryCode } if len(ccMap) == 0 { @@ -183,15 +196,7 @@ func (g *geoLite2CountryCSV) process(file string, ccMap map[string]string, entri return fmt.Errorf("❌ [type %s | action %s] invalid country code data", typeCountryCSV, g.Action) } if entries == nil { - entries = make(map[string]*lib.Entry, 300) - } - - // Filter want list - wantList := make(map[string]bool) - for _, want := range g.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } + entries = make(map[string]*lib.Entry, len(ccMap)) } fReader, err := os.Open(file) @@ -229,17 +234,16 @@ func (g *geoLite2CountryCSV) process(file string, ccMap map[string]string, entri } if countryCode, found := ccMap[ccID]; found { - if len(wantList) > 0 && !wantList[countryCode] { - continue - } cidrStr := strings.ToLower(strings.TrimSpace(record[0])) - entry, found := entries[countryCode] - if !found { + entry, got := entries[countryCode] + if !got { entry = lib.NewEntry(countryCode) } + if err := entry.AddPrefix(cidrStr); err != nil { return err } + entries[countryCode] = entry } } diff --git a/plugin/maxmind/mmdb_in.go b/plugin/maxmind/mmdb_in.go index 065f5601..3ee1eacb 100644 --- a/plugin/maxmind/mmdb_in.go +++ b/plugin/maxmind/mmdb_in.go @@ -46,12 +46,20 @@ func newMaxmindMMDBIn(action lib.Action, data json.RawMessage) (lib.InputConvert tmp.URI = defaultMMDBFile } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &maxmindMMDBIn{ Type: typeMaxmindMMDBIn, Action: action, Description: descMaxmindMMDBIn, URI: tmp.URI, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -61,68 +69,55 @@ type maxmindMMDBIn struct { Action lib.Action Description string URI string - Want []string + Want map[string]bool OnlyIPType lib.IPType } -func (g *maxmindMMDBIn) GetType() string { - return g.Type +func (m *maxmindMMDBIn) GetType() string { + return m.Type } -func (g *maxmindMMDBIn) GetAction() lib.Action { - return g.Action +func (m *maxmindMMDBIn) GetAction() lib.Action { + return m.Action } -func (g *maxmindMMDBIn) GetDescription() string { - return g.Description +func (m *maxmindMMDBIn) GetDescription() string { + return m.Description } -func (g *maxmindMMDBIn) Input(container lib.Container) (lib.Container, error) { +func (m *maxmindMMDBIn) Input(container lib.Container) (lib.Container, error) { var content []byte var err error switch { - case strings.HasPrefix(strings.ToLower(g.URI), "http://"), strings.HasPrefix(strings.ToLower(g.URI), "https://"): - content, err = lib.GetRemoteURLContent(g.URI) + case strings.HasPrefix(strings.ToLower(m.URI), "http://"), strings.HasPrefix(strings.ToLower(m.URI), "https://"): + content, err = lib.GetRemoteURLContent(m.URI) default: - content, err = os.ReadFile(g.URI) + content, err = os.ReadFile(m.URI) } if err != nil { return nil, err } entries := make(map[string]*lib.Entry, 300) - err = g.generateEntries(content, entries) + err = m.generateEntries(content, entries) if err != nil { return nil, err } if len(entries) == 0 { - return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeMaxmindMMDBIn, g.Action) + return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", typeMaxmindMMDBIn, m.Action) } var ignoreIPType lib.IgnoreIPOption - switch g.OnlyIPType { + switch m.OnlyIPType { case lib.IPv4: ignoreIPType = lib.IgnoreIPv6 case lib.IPv6: ignoreIPType = lib.IgnoreIPv4 } - // Filter want list - wantList := make(map[string]bool) - for _, want := range g.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } - } - for _, entry := range entries { - name := entry.GetName() - if len(wantList) > 0 && !wantList[name] { - continue - } - - switch g.Action { + switch m.Action { case lib.ActionAdd: if err := container.Add(entry, ignoreIPType); err != nil { return nil, err @@ -139,7 +134,7 @@ func (g *maxmindMMDBIn) Input(container lib.Container) (lib.Container, error) { return container, nil } -func (g *maxmindMMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error { +func (m *maxmindMMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error { db, err := maxminddb.FromBytes(content) if err != nil { return err @@ -177,6 +172,10 @@ func (g *maxmindMMDBIn) generateEntries(content []byte, entries map[string]*lib. continue } + if len(m.Want) > 0 && !m.Want[name] { + continue + } + entry, found := entries[name] if !found { entry = lib.NewEntry(name) diff --git a/plugin/maxmind/mmdb_out.go b/plugin/maxmind/mmdb_out.go index 5b838463..e42ea02d 100644 --- a/plugin/maxmind/mmdb_out.go +++ b/plugin/maxmind/mmdb_out.go @@ -140,7 +140,7 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string { The order of names in wantedList has a higher priority than which of the overwriteList. */ - wantList := make([]string, 0, 200) + wantList := make([]string, 0, len(m.Want)) for _, want := range m.Want { if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { wantList = append(wantList, want) @@ -151,7 +151,7 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string { return wantList } - overwriteList := make([]string, 0, 200) + overwriteList := make([]string, 0, len(m.Overwrite)) overwriteMap := make(map[string]bool) for _, overwrite := range m.Overwrite { if overwrite = strings.ToUpper(strings.TrimSpace(overwrite)); overwrite != "" { diff --git a/plugin/mihomo/mrs_in.go b/plugin/mihomo/mrs_in.go index 8b5eeefb..4976e071 100644 --- a/plugin/mihomo/mrs_in.go +++ b/plugin/mihomo/mrs_in.go @@ -39,6 +39,7 @@ func newMRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro Name string `json:"name"` URI string `json:"uri"` InputDir string `json:"inputDir"` + Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -56,6 +57,14 @@ func newMRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeMRSIn, action) } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &mrsIn{ Type: typeMRSIn, Action: action, @@ -63,6 +72,7 @@ func newMRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro Name: tmp.Name, URI: tmp.URI, InputDir: tmp.InputDir, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -74,6 +84,7 @@ type mrsIn struct { Name string URI string InputDir string + Want map[string]bool OnlyIPType lib.IPType } @@ -111,6 +122,10 @@ func (m *mrsIn) Input(container lib.Container) (lib.Container, error) { return nil, err } + if len(entries) == 0 { + return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", m.Type, m.Action) + } + var ignoreIPType lib.IgnoreIPOption switch m.OnlyIPType { case lib.IPv4: @@ -119,10 +134,6 @@ func (m *mrsIn) Input(container lib.Container) (lib.Container, error) { ignoreIPType = lib.IgnoreIPv4 } - if len(entries) == 0 { - return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", m.Type, m.Action) - } - for _, entry := range entries { switch m.Action { case lib.ActionAdd: @@ -218,6 +229,11 @@ func (m *mrsIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) func (m *mrsIn) generateEntries(name string, reader io.Reader, entries map[string]*lib.Entry) error { name = strings.ToUpper(name) + + if len(m.Want) > 0 && !m.Want[name] { + return nil + } + entry, found := entries[name] if !found { entry = lib.NewEntry(name) diff --git a/plugin/mihomo/mrs_out.go b/plugin/mihomo/mrs_out.go index 85717b30..14d48428 100644 --- a/plugin/mihomo/mrs_out.go +++ b/plugin/mihomo/mrs_out.go @@ -51,12 +51,20 @@ func newMRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er tmp.OutputDir = defaultOutputDir } + // Filter want list + wantList := make([]string, 0, len(tmp.Want)) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList = append(wantList, want) + } + } + return &mrsOut{ Type: typeMRSOut, Action: action, Description: descMRSOut, OutputDir: tmp.OutputDir, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -83,15 +91,7 @@ func (m *mrsOut) GetDescription() string { } func (m *mrsOut) Output(container lib.Container) error { - // Filter want list - wantList := make([]string, 0, len(m.Want)) - for _, want := range m.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList = append(wantList, want) - } - } - - switch len(wantList) { + switch len(m.Want) { case 0: list := make([]string, 0, 300) for entry := range container.Loop() { @@ -114,9 +114,9 @@ func (m *mrsOut) Output(container lib.Container) error { default: // Sort the list - slices.Sort(wantList) + slices.Sort(m.Want) - for _, name := range wantList { + for _, name := range m.Want { entry, found := container.GetEntry(name) if !found { log.Printf("❌ entry %s not found", name) diff --git a/plugin/plaintext/common_in.go b/plugin/plaintext/common_in.go index 4c8d48c6..bf678e37 100644 --- a/plugin/plaintext/common_in.go +++ b/plugin/plaintext/common_in.go @@ -18,6 +18,7 @@ type textIn struct { Name string URI string InputDir string + Want map[string]bool OnlyIPType lib.IPType JSONPath []string diff --git a/plugin/plaintext/common_out.go b/plugin/plaintext/common_out.go index ddc8d662..b8f1bb55 100644 --- a/plugin/plaintext/common_out.go +++ b/plugin/plaintext/common_out.go @@ -7,6 +7,7 @@ import ( "net" "os" "path/filepath" + "strings" "github.com/Loyalsoldier/geoip/lib" ) @@ -65,13 +66,21 @@ func newTextOut(iType string, action lib.Action, data json.RawMessage) (lib.Outp tmp.OutputExt = ".txt" } + // Filter want list + wantList := make([]string, 0, len(tmp.Want)) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList = append(wantList, want) + } + } + return &textOut{ Type: iType, Action: action, Description: descTextOut, OutputDir: tmp.OutputDir, OutputExt: tmp.OutputExt, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, AddPrefixInLine: tmp.AddPrefixInLine, diff --git a/plugin/plaintext/text_in.go b/plugin/plaintext/text_in.go index 4f2c2c71..037271c1 100644 --- a/plugin/plaintext/text_in.go +++ b/plugin/plaintext/text_in.go @@ -31,6 +31,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input Name string `json:"name"` URI string `json:"uri"` InputDir string `json:"inputDir"` + Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` JSONPath []string `json:"jsonPath"` @@ -60,6 +61,14 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input return nil, fmt.Errorf("type %s | action %s missing jsonPath", typeJSONIn, action) } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &textIn{ Type: iType, Action: action, @@ -67,6 +76,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input Name: tmp.Name, URI: tmp.URI, InputDir: tmp.InputDir, + Want: wantList, OnlyIPType: tmp.OnlyIPType, JSONPath: tmp.JSONPath, @@ -179,6 +189,10 @@ func (t *textIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) } entryName = strings.ToUpper(entryName) + + if len(t.Want) > 0 && !t.Want[entryName] { + return nil + } if _, found := entries[entryName]; found { return fmt.Errorf("found duplicated list %s", entryName) } @@ -210,6 +224,11 @@ func (t *textIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) } name = strings.ToUpper(name) + + if len(t.Want) > 0 && !t.Want[name] { + return nil + } + entry := lib.NewEntry(name) if err := t.scanFile(resp.Body, entry); err != nil { return err diff --git a/plugin/plaintext/text_out.go b/plugin/plaintext/text_out.go index bc8f904e..dce92ce8 100644 --- a/plugin/plaintext/text_out.go +++ b/plugin/plaintext/text_out.go @@ -36,15 +36,7 @@ func (t *textOut) GetDescription() string { } func (t *textOut) Output(container lib.Container) error { - // Filter want list - wantList := make([]string, 0, 50) - for _, want := range t.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList = append(wantList, want) - } - } - - switch len(wantList) { + switch len(t.Want) { case 0: list := make([]string, 0, 300) for entry := range container.Loop() { @@ -72,9 +64,9 @@ func (t *textOut) Output(container lib.Container) error { default: // Sort the list - slices.Sort(wantList) + slices.Sort(t.Want) - for _, name := range wantList { + for _, name := range t.Want { entry, found := container.GetEntry(name) if !found { log.Printf("❌ entry %s not found", name) diff --git a/plugin/singbox/srs_in.go b/plugin/singbox/srs_in.go index 8b664ce2..2a69e775 100644 --- a/plugin/singbox/srs_in.go +++ b/plugin/singbox/srs_in.go @@ -33,6 +33,7 @@ func newSRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro Name string `json:"name"` URI string `json:"uri"` InputDir string `json:"inputDir"` + Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -50,6 +51,14 @@ func newSRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeSRSIn, action) } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &srsIn{ Type: typeSRSIn, Action: action, @@ -57,6 +66,7 @@ func newSRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, erro Name: tmp.Name, URI: tmp.URI, InputDir: tmp.InputDir, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -68,6 +78,7 @@ type srsIn struct { Name string URI string InputDir string + Want map[string]bool OnlyIPType lib.IPType } @@ -105,6 +116,10 @@ func (s *srsIn) Input(container lib.Container) (lib.Container, error) { return nil, err } + if len(entries) == 0 { + return nil, fmt.Errorf("type %s | action %s no entry is generated", s.Type, s.Action) + } + var ignoreIPType lib.IgnoreIPOption switch s.OnlyIPType { case lib.IPv4: @@ -113,10 +128,6 @@ func (s *srsIn) Input(container lib.Container) (lib.Container, error) { ignoreIPType = lib.IgnoreIPv4 } - if len(entries) == 0 { - return nil, fmt.Errorf("type %s | action %s no entry is generated", s.Type, s.Action) - } - for _, entry := range entries { switch s.Action { case lib.ActionAdd: @@ -212,6 +223,11 @@ func (s *srsIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) func (s *srsIn) generateEntries(name string, reader io.Reader, entries map[string]*lib.Entry) error { name = strings.ToUpper(name) + + if len(s.Want) > 0 && !s.Want[name] { + return nil + } + entry, found := entries[name] if !found { entry = lib.NewEntry(name) diff --git a/plugin/singbox/srs_out.go b/plugin/singbox/srs_out.go index 7637d200..877e5acd 100644 --- a/plugin/singbox/srs_out.go +++ b/plugin/singbox/srs_out.go @@ -50,12 +50,20 @@ func newSRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er tmp.OutputDir = defaultOutputDir } + // Filter want list + wantList := make([]string, 0, len(tmp.Want)) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList = append(wantList, want) + } + } + return &srsOut{ Type: typeSRSOut, Action: action, Description: descSRSOut, OutputDir: tmp.OutputDir, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -82,15 +90,7 @@ func (s *srsOut) GetDescription() string { } func (s *srsOut) Output(container lib.Container) error { - // Filter want list - wantList := make([]string, 0, 50) - for _, want := range s.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList = append(wantList, want) - } - } - - switch len(wantList) { + switch len(s.Want) { case 0: list := make([]string, 0, 300) for entry := range container.Loop() { @@ -113,9 +113,9 @@ func (s *srsOut) Output(container lib.Container) error { default: // Sort the list - slices.Sort(wantList) + slices.Sort(s.Want) - for _, name := range wantList { + for _, name := range s.Want { entry, found := container.GetEntry(name) if !found { log.Printf("❌ entry %s not found", name) diff --git a/plugin/special/cutter.go b/plugin/special/cutter.go index d14e286d..fd4dcb59 100644 --- a/plugin/special/cutter.go +++ b/plugin/special/cutter.go @@ -38,11 +38,19 @@ func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, err return nil, fmt.Errorf("type %s only supports `remove` action", typeCutter) } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &cutter{ Type: typeCutter, Action: action, Description: descCutter, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -51,7 +59,7 @@ type cutter struct { Type string Action lib.Action Description string - Want []string + Want map[string]bool OnlyIPType lib.IPType } @@ -76,19 +84,11 @@ func (c *cutter) Input(container lib.Container) (lib.Container, error) { ignoreIPType = lib.IgnoreIPv4 } - // Filter want list - wantList := make(map[string]bool) - for _, want := range c.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } - } - for entry := range container.Loop() { - name := entry.GetName() - if len(wantList) > 0 && !wantList[name] { + if len(c.Want) > 0 && !c.Want[entry.GetName()] { continue } + if err := container.Remove(entry, lib.CaseRemoveEntry, ignoreIPType); err != nil { return nil, err } diff --git a/plugin/special/stdout.go b/plugin/special/stdout.go index 9437e597..b0f9320a 100644 --- a/plugin/special/stdout.go +++ b/plugin/special/stdout.go @@ -36,11 +36,19 @@ func newStdout(action lib.Action, data json.RawMessage) (lib.OutputConverter, er } } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &stdout{ Type: typeStdout, Action: action, Description: descStdout, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -49,7 +57,7 @@ type stdout struct { Type string Action lib.Action Description string - Want []string + Want map[string]bool OnlyIPType lib.IPType } @@ -66,16 +74,8 @@ func (s *stdout) GetDescription() string { } func (s *stdout) Output(container lib.Container) error { - // Filter want list - wantList := make(map[string]bool) - for _, want := range s.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } - } - for entry := range container.Loop() { - if len(wantList) > 0 && !wantList[entry.GetName()] { + if len(s.Want) > 0 && !s.Want[entry.GetName()] { continue } diff --git a/plugin/v2ray/dat_in.go b/plugin/v2ray/dat_in.go index 5baf32f2..4b2a98c0 100644 --- a/plugin/v2ray/dat_in.go +++ b/plugin/v2ray/dat_in.go @@ -45,12 +45,20 @@ func newGeoIPDatIn(action lib.Action, data json.RawMessage) (lib.InputConverter, return nil, fmt.Errorf("[type %s | action %s] uri must be specified in config", typeGeoIPdatIn, action) } + // Filter want list + wantList := make(map[string]bool) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + return &geoIPDatIn{ Type: typeGeoIPdatIn, Action: action, Description: descGeoIPdatIn, URI: tmp.URI, - Want: tmp.Want, + Want: wantList, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -60,7 +68,7 @@ type geoIPDatIn struct { Action lib.Action Description string URI string - Want []string + Want map[string]bool OnlyIPType lib.IPType } @@ -103,20 +111,7 @@ func (g *geoIPDatIn) Input(container lib.Container) (lib.Container, error) { ignoreIPType = lib.IgnoreIPv4 } - // Filter want list - wantList := make(map[string]bool) - for _, want := range g.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } - } - for _, entry := range entries { - name := entry.GetName() - if len(wantList) > 0 && !wantList[name] { - continue - } - switch g.Action { case lib.ActionAdd: if err := container.Add(entry, ignoreIPType); err != nil { @@ -178,7 +173,12 @@ func (g *geoIPDatIn) generateEntries(reader io.Reader, entries map[string]*lib.E } for _, geoip := range geoipList.Entry { - name := geoip.CountryCode + name := strings.ToUpper(strings.TrimSpace(geoip.CountryCode)) + + if len(g.Want) > 0 && !g.Want[name] { + continue + } + entry, found := entries[name] if !found { entry = lib.NewEntry(name) diff --git a/plugin/v2ray/dat_out.go b/plugin/v2ray/dat_out.go index 0dd9e23b..abb00ca4 100644 --- a/plugin/v2ray/dat_out.go +++ b/plugin/v2ray/dat_out.go @@ -57,13 +57,21 @@ func newGeoIPDat(action lib.Action, data json.RawMessage) (lib.OutputConverter, tmp.OutputDir = defaultOutputDir } + // Filter want list + wantList := make([]string, 0, len(tmp.Want)) + for _, want := range tmp.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList = append(wantList, want) + } + } + return &geoIPDatOut{ Type: typeGeoIPdatOut, Action: action, Description: descGeoIPdatOut, OutputName: tmp.OutputName, OutputDir: tmp.OutputDir, - Want: tmp.Want, + Want: wantList, OneFilePerList: tmp.OneFilePerList, OnlyIPType: tmp.OnlyIPType, }, nil @@ -93,19 +101,11 @@ func (g *geoIPDatOut) GetDescription() string { } func (g *geoIPDatOut) Output(container lib.Container) error { - // Filter want list - wantList := make([]string, 0, 50) - for _, want := range g.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList = append(wantList, want) - } - } - geoIPList := new(router.GeoIPList) geoIPList.Entry = make([]*router.GeoIP, 0, 300) updated := false - switch len(wantList) { + switch len(g.Want) { case 0: list := make([]string, 0, 300) for entry := range container.Loop() { @@ -143,9 +143,9 @@ func (g *geoIPDatOut) Output(container lib.Container) error { default: // Sort the list - sort.Strings(wantList) + sort.Strings(g.Want) - for _, name := range wantList { + for _, name := range g.Want { entry, found := container.GetEntry(name) if !found { log.Printf("❌ entry %s not found", name) |
