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 | |
| parent | 4cf50641990c7df79b1a03438dd9273532182e2f (diff) | |
Feat: support to exclude specified lists when output
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/maxmind/mmdb_out.go | 32 | ||||
| -rw-r--r-- | plugin/mihomo/mrs_out.go | 81 | ||||
| -rw-r--r-- | plugin/plaintext/common_out.go | 14 | ||||
| -rw-r--r-- | plugin/plaintext/text_out.go | 83 | ||||
| -rw-r--r-- | plugin/singbox/srs_out.go | 87 | ||||
| -rw-r--r-- | plugin/special/stdout.go | 58 | ||||
| -rw-r--r-- | plugin/v2ray/dat_out.go | 120 |
7 files changed, 260 insertions, 215 deletions
diff --git a/plugin/maxmind/mmdb_out.go b/plugin/maxmind/mmdb_out.go index 964971d7..3b8ba20c 100644 --- a/plugin/maxmind/mmdb_out.go +++ b/plugin/maxmind/mmdb_out.go @@ -2,7 +2,6 @@ package maxmind import ( "encoding/json" - "fmt" "log" "net" "os" @@ -40,6 +39,7 @@ func newMMDBOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, e OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` Overwrite []string `json:"overwriteList"` + Exclude []string `json:"excludedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -65,6 +65,7 @@ func newMMDBOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, e OutputDir: tmp.OutputDir, Want: tmp.Want, Overwrite: tmp.Overwrite, + Exclude: tmp.Exclude, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -77,6 +78,7 @@ type mmdbOut struct { OutputDir string Want []string Overwrite []string + Exclude []string OnlyIPType lib.IPType } @@ -106,30 +108,28 @@ func (m *mmdbOut) Output(container lib.Container) error { } updated := false - for _, name := range m.getEntryNameListInOrder(container) { + for _, name := range m.filterAndSortList(container) { entry, found := container.GetEntry(name) if !found { - log.Printf("❌ entry %s not found", name) + log.Printf("❌ entry %s not found\n", name) continue } + if err := m.marshalData(writer, entry); err != nil { return err } + updated = true } if updated { - if err := m.writeFile(m.OutputName, writer); err != nil { - return err - } - } else { - return fmt.Errorf("❌ [type %s | action %s] failed to write file", m.Type, m.Action) + return m.writeFile(m.OutputName, writer) } return nil } -func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string { +func (m *mmdbOut) filterAndSortList(container lib.Container) []string { /* Note: The IPs and/or CIDRs of the latter list will overwrite those of the former one when duplicated data found due to MaxMind mmdb file format constraint. @@ -140,9 +140,16 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string { The order of names in wantedList has a higher priority than which of the overwriteList. */ + excludeMap := make(map[string]bool) + for _, exclude := range m.Exclude { + if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { + excludeMap[exclude] = true + } + } + wantList := make([]string, 0, len(m.Want)) for _, want := range m.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] { wantList = append(wantList, want) } } @@ -154,7 +161,7 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string { 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 != "" { + if overwrite = strings.ToUpper(strings.TrimSpace(overwrite)); overwrite != "" && !excludeMap[overwrite] { overwriteList = append(overwriteList, overwrite) overwriteMap[overwrite] = true } @@ -163,8 +170,7 @@ func (m *mmdbOut) getEntryNameListInOrder(container lib.Container) []string { list := make([]string, 0, 300) for entry := range container.Loop() { name := entry.GetName() - _, found := overwriteMap[name] - if found { + if excludeMap[name] || overwriteMap[name] { continue } list = append(list, name) diff --git a/plugin/mihomo/mrs_out.go b/plugin/mihomo/mrs_out.go index fe9480da..28b922b3 100644 --- a/plugin/mihomo/mrs_out.go +++ b/plugin/mihomo/mrs_out.go @@ -38,6 +38,7 @@ func newMRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er var tmp struct { OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` + Exclude []string `json:"excludedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -51,20 +52,13 @@ 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: wantList, + Want: tmp.Want, + Exclude: tmp.Exclude, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -75,6 +69,7 @@ type mrsOut struct { Description string OutputDir string Want []string + Exclude []string OnlyIPType lib.IPType } @@ -91,45 +86,55 @@ func (m *mrsOut) GetDescription() string { } func (m *mrsOut) Output(container lib.Container) error { - switch len(m.Want) { - case 0: - list := make([]string, 0, 300) - for entry := range container.Loop() { - list = append(list, entry.GetName()) + for _, name := range m.filterAndSortList(container) { + entry, found := container.GetEntry(name) + if !found { + log.Printf("❌ entry %s not found\n", name) + continue } - // Sort the list - slices.Sort(list) + if err := m.generate(entry); err != nil { + return err + } + } + + return nil +} - for _, name := range list { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } - if err := m.generate(entry); err != nil { - return err - } +func (m *mrsOut) filterAndSortList(container lib.Container) []string { + excludeMap := make(map[string]bool) + for _, exclude := range m.Exclude { + if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { + excludeMap[exclude] = true } + } - default: - // Sort the list - slices.Sort(m.Want) + wantList := make([]string, 0, len(m.Want)) + for _, want := range m.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] { + wantList = append(wantList, want) + } + } - for _, name := range m.Want { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } + if len(wantList) > 0 { + // Sort the list + slices.Sort(wantList) + return wantList + } - if err := m.generate(entry); err != nil { - return err - } + list := make([]string, 0, 300) + for entry := range container.Loop() { + name := entry.GetName() + if excludeMap[name] { + continue } + list = append(list, name) } - return nil + // Sort the list + slices.Sort(list) + + return list } func (m *mrsOut) generate(entry *lib.Entry) error { diff --git a/plugin/plaintext/common_out.go b/plugin/plaintext/common_out.go index b8f1bb55..117d48c7 100644 --- a/plugin/plaintext/common_out.go +++ b/plugin/plaintext/common_out.go @@ -7,7 +7,6 @@ import ( "net" "os" "path/filepath" - "strings" "github.com/Loyalsoldier/geoip/lib" ) @@ -26,6 +25,7 @@ type textOut struct { OutputDir string OutputExt string Want []string + Exclude []string OnlyIPType lib.IPType AddPrefixInLine string @@ -37,6 +37,7 @@ func newTextOut(iType string, action lib.Action, data json.RawMessage) (lib.Outp OutputDir string `json:"outputDir"` OutputExt string `json:"outputExtension"` Want []string `json:"wantedList"` + Exclude []string `json:"excludedList"` OnlyIPType lib.IPType `json:"onlyIPType"` AddPrefixInLine string `json:"addPrefixInLine"` @@ -66,21 +67,14 @@ 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: wantList, + Want: tmp.Want, + Exclude: tmp.Exclude, OnlyIPType: tmp.OnlyIPType, AddPrefixInLine: tmp.AddPrefixInLine, diff --git a/plugin/plaintext/text_out.go b/plugin/plaintext/text_out.go index dce92ce8..cc51da95 100644 --- a/plugin/plaintext/text_out.go +++ b/plugin/plaintext/text_out.go @@ -36,52 +36,59 @@ func (t *textOut) GetDescription() string { } func (t *textOut) Output(container lib.Container) error { - switch len(t.Want) { - case 0: - list := make([]string, 0, 300) - for entry := range container.Loop() { - list = append(list, entry.GetName()) + for _, name := range t.filterAndSortList(container) { + entry, found := container.GetEntry(name) + if !found { + log.Printf("❌ entry %s not found\n", name) + continue } - // Sort the list - slices.Sort(list) + data, err := t.marshalBytes(entry) + if err != nil { + return err + } - for _, name := range list { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } - data, err := t.marshalBytes(entry) - if err != nil { - return err - } - filename := strings.ToLower(entry.GetName()) + t.OutputExt - if err := t.writeFile(filename, data); err != nil { - return err - } + filename := strings.ToLower(entry.GetName()) + t.OutputExt + if err := t.writeFile(filename, data); err != nil { + return err } + } + + return nil +} - default: +func (t *textOut) filterAndSortList(container lib.Container) []string { + excludeMap := make(map[string]bool) + for _, exclude := range t.Exclude { + if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { + excludeMap[exclude] = true + } + } + + wantList := make([]string, 0, len(t.Want)) + for _, want := range t.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] { + wantList = append(wantList, want) + } + } + + if len(wantList) > 0 { // Sort the list - slices.Sort(t.Want) + slices.Sort(wantList) + return wantList + } - for _, name := range t.Want { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } - data, err := t.marshalBytes(entry) - if err != nil { - return err - } - filename := strings.ToLower(entry.GetName()) + t.OutputExt - if err := t.writeFile(filename, data); err != nil { - return err - } + list := make([]string, 0, 300) + for entry := range container.Loop() { + name := entry.GetName() + if excludeMap[name] { + continue } + list = append(list, name) } - return nil + // Sort the list + slices.Sort(list) + + return list } diff --git a/plugin/singbox/srs_out.go b/plugin/singbox/srs_out.go index d2950ee5..4c4de1f7 100644 --- a/plugin/singbox/srs_out.go +++ b/plugin/singbox/srs_out.go @@ -37,6 +37,7 @@ func newSRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er var tmp struct { OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` + Exclude []string `json:"excludedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -50,20 +51,13 @@ 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: wantList, + Want: tmp.Want, + Exclude: tmp.Exclude, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -74,6 +68,7 @@ type srsOut struct { Description string OutputDir string Want []string + Exclude []string OnlyIPType lib.IPType } @@ -90,49 +85,59 @@ func (s *srsOut) GetDescription() string { } func (s *srsOut) Output(container lib.Container) error { - switch len(s.Want) { - case 0: - list := make([]string, 0, 300) - for entry := range container.Loop() { - list = append(list, entry.GetName()) + for _, name := range s.filterAndSortList(container) { + entry, found := container.GetEntry(name) + if !found { + log.Printf("❌ entry %s not found\n", name) + continue } - // Sort the list - slices.Sort(list) + if err := s.generate(entry); err != nil { + return err + } + } + + return nil +} - for _, name := range list { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } - if err := s.run(entry); err != nil { - return err - } +func (s *srsOut) filterAndSortList(container lib.Container) []string { + excludeMap := make(map[string]bool) + for _, exclude := range s.Exclude { + if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { + excludeMap[exclude] = true } + } - default: - // Sort the list - slices.Sort(s.Want) + wantList := make([]string, 0, len(s.Want)) + for _, want := range s.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] { + wantList = append(wantList, want) + } + } - for _, name := range s.Want { - entry, found := container.GetEntry(name) - if !found { - log.Printf("❌ entry %s not found", name) - continue - } + if len(wantList) > 0 { + // Sort the list + slices.Sort(wantList) + return wantList + } - if err := s.run(entry); err != nil { - return err - } + list := make([]string, 0, 300) + for entry := range container.Loop() { + name := entry.GetName() + if excludeMap[name] { + continue } + list = append(list, name) } - return nil + // Sort the list + slices.Sort(list) + + return list } -func (s *srsOut) run(entry *lib.Entry) error { - ruleset, err := s.generateRuleSet(entry) +func (s *srsOut) generate(entry *lib.Entry) error { + ruleset, err := s.marshalRuleSet(entry) if err != nil { return err } @@ -145,7 +150,7 @@ func (s *srsOut) run(entry *lib.Entry) error { return nil } -func (s *srsOut) generateRuleSet(entry *lib.Entry) (*option.PlainRuleSet, error) { +func (s *srsOut) marshalRuleSet(entry *lib.Entry) (*option.PlainRuleSet, error) { var entryCidr []string var err error switch s.OnlyIPType { diff --git a/plugin/special/stdout.go b/plugin/special/stdout.go index b0f9320a..8c57cb58 100644 --- a/plugin/special/stdout.go +++ b/plugin/special/stdout.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "slices" "strings" "github.com/Loyalsoldier/geoip/lib" @@ -27,6 +28,7 @@ func init() { func newStdout(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { var tmp struct { Want []string `json:"wantedList"` + Exclude []string `json:"excludedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -36,19 +38,12 @@ 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: wantList, + Want: tmp.Want, + Exclude: tmp.Exclude, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -57,7 +52,8 @@ type stdout struct { Type string Action lib.Action Description string - Want map[string]bool + Want []string + Exclude []string OnlyIPType lib.IPType } @@ -74,8 +70,9 @@ func (s *stdout) GetDescription() string { } func (s *stdout) Output(container lib.Container) error { - for entry := range container.Loop() { - if len(s.Want) > 0 && !s.Want[entry.GetName()] { + for _, name := range s.filterAndSortList(container) { + entry, found := container.GetEntry(name) + if !found { continue } @@ -83,6 +80,7 @@ func (s *stdout) Output(container lib.Container) error { if err != nil { continue } + for _, cidr := range cidrList { io.WriteString(os.Stdout, cidr+"\n") } @@ -91,6 +89,42 @@ func (s *stdout) Output(container lib.Container) error { return nil } +func (s *stdout) filterAndSortList(container lib.Container) []string { + excludeMap := make(map[string]bool) + for _, exclude := range s.Exclude { + if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { + excludeMap[exclude] = true + } + } + + wantList := make([]string, 0, len(s.Want)) + for _, want := range s.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 (s *stdout) generateCIDRList(entry *lib.Entry) ([]string, error) { var entryList []string var err error 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 |
