diff options
| author | Loyalsoldier <[email protected]> | 2024-10-24 06:01:54 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-10-24 06:01:54 +0800 |
| commit | 40e0ad6895d62ad12ea1b1da4293dbcc05ea1d29 (patch) | |
| tree | a3b3310824baa53ff4d7dad417a90a769386269d /plugin/v2ray | |
| parent | 4cf50641990c7df79b1a03438dd9273532182e2f (diff) | |
Feat: support to exclude specified lists when output
Diffstat (limited to 'plugin/v2ray')
| -rw-r--r-- | plugin/v2ray/dat_out.go | 120 |
1 files changed, 57 insertions, 63 deletions
diff --git a/plugin/v2ray/dat_out.go b/plugin/v2ray/dat_out.go index 1e64ede3..3b120277 100644 --- a/plugin/v2ray/dat_out.go +++ b/plugin/v2ray/dat_out.go @@ -7,6 +7,7 @@ import ( "net/netip" "os" "path/filepath" + "slices" "sort" "strings" @@ -38,6 +39,7 @@ func newGeoIPDat(action lib.Action, data json.RawMessage) (lib.OutputConverter, OutputName string `json:"outputName"` OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` + Exclude []string `json:"excludedList"` OneFilePerList bool `json:"oneFilePerList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -56,21 +58,14 @@ 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: wantList, + Want: tmp.Want, + Exclude: tmp.Exclude, OneFilePerList: tmp.OneFilePerList, OnlyIPType: tmp.OnlyIPType, }, nil @@ -83,6 +78,7 @@ type geoIPDatOut struct { OutputName string OutputDir string Want []string + Exclude []string OneFilePerList bool OnlyIPType lib.IPType } @@ -104,70 +100,32 @@ func (g *geoIPDatOut) Output(container lib.Container) error { geoIPList.Entry = make([]*GeoIP, 0, 300) updated := false - switch len(g.Want) { - case 0: - list := make([]string, 0, 300) - for entry := range container.Loop() { - list = append(list, entry.GetName()) + for _, name := range g.filterAndSortList(container) { + entry, found := container.GetEntry(name) + if !found { + log.Printf("❌ entry %s not found\n", name) + continue } - // Sort the list - sort.Strings(list) + geoIP, err := g.generateGeoIP(entry) + if err != nil { + return err + } + geoIPList.Entry = append(geoIPList.Entry, geoIP) + updated = true - for _, name := range list { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } - geoIP, err := g.generateGeoIP(entry) + if g.OneFilePerList { + geoIPBytes, err := proto.Marshal(geoIPList) if err != nil { return err } - geoIPList.Entry = append(geoIPList.Entry, geoIP) - updated = true - if g.OneFilePerList { - geoIPBytes, err := proto.Marshal(geoIPList) - if err != nil { - return err - } - filename := strings.ToLower(entry.GetName()) + ".dat" - if err := g.writeFile(filename, geoIPBytes); err != nil { - return err - } - geoIPList.Entry = nil - } - } - - default: - // Sort the list - sort.Strings(g.Want) - - for _, name := range g.Want { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } - geoIP, err := g.generateGeoIP(entry) - if err != nil { + filename := strings.ToLower(entry.GetName()) + ".dat" + if err := g.writeFile(filename, geoIPBytes); err != nil { return err } - geoIPList.Entry = append(geoIPList.Entry, geoIP) - updated = true - if g.OneFilePerList { - geoIPBytes, err := proto.Marshal(geoIPList) - if err != nil { - return err - } - filename := strings.ToLower(entry.GetName()) + ".dat" - if err := g.writeFile(filename, geoIPBytes); err != nil { - return err - } - geoIPList.Entry = nil - } + geoIPList.Entry = nil } } @@ -187,6 +145,42 @@ func (g *geoIPDatOut) Output(container lib.Container) error { return nil } +func (g *geoIPDatOut) filterAndSortList(container lib.Container) []string { + excludeMap := make(map[string]bool) + for _, exclude := range g.Exclude { + if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { + excludeMap[exclude] = true + } + } + + wantList := make([]string, 0, len(g.Want)) + for _, want := range g.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] { + wantList = append(wantList, want) + } + } + + if len(wantList) > 0 { + // Sort the list + slices.Sort(wantList) + return wantList + } + + list := make([]string, 0, 300) + for entry := range container.Loop() { + name := entry.GetName() + if excludeMap[name] { + continue + } + list = append(list, name) + } + + // Sort the list + slices.Sort(list) + + return list +} + func (g *geoIPDatOut) generateGeoIP(entry *lib.Entry) (*GeoIP, error) { var entryCidr []netip.Prefix var err error |
