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/singbox | |
| parent | 4cf50641990c7df79b1a03438dd9273532182e2f (diff) | |
Feat: support to exclude specified lists when output
Diffstat (limited to 'plugin/singbox')
| -rw-r--r-- | plugin/singbox/srs_out.go | 87 |
1 files changed, 46 insertions, 41 deletions
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 { |
