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/singbox | |
| parent | 56cd72d97f058fc6e931995039d69cd4b35084ec (diff) | |
Refine: wantedList in various formats
Diffstat (limited to 'plugin/singbox')
| -rw-r--r-- | plugin/singbox/srs_in.go | 24 | ||||
| -rw-r--r-- | plugin/singbox/srs_out.go | 24 |
2 files changed, 32 insertions, 16 deletions
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) |
