diff options
| -rw-r--r-- | lib/lib.go | 4 | ||||
| -rw-r--r-- | lookup.go | 13 | ||||
| -rw-r--r-- | plugin/singbox/srs_in.go | 137 | ||||
| -rw-r--r-- | plugin/singbox/srs_out.go | 107 |
4 files changed, 171 insertions, 90 deletions
@@ -50,6 +50,10 @@ type OutputConverter interface { Output(Container) error } +type InputOption func(InputConverter) + +type OutputOption func(OutputConverter) + type IgnoreIPOption func() IPType func IgnoreIPv4() IPType { @@ -197,14 +197,11 @@ func getInputForLookup(format, name, uri, dir string) lib.InputConverter { } case strings.ToLower(singbox.TypeSRSIn): - input = &singbox.SRSIn{ - Type: singbox.TypeSRSIn, - Action: lib.ActionAdd, - Description: singbox.DescSRSIn, - Name: name, - URI: uri, - InputDir: dir, - } + input = singbox.NewSRSIn( + lib.ActionAdd, + singbox.WithNameAndURI(name, uri), + singbox.WithInputDir(dir), + ) case strings.ToLower(v2ray.TypeGeoIPDatIn): input = &v2ray.GeoIPDatIn{ diff --git a/plugin/singbox/srs_in.go b/plugin/singbox/srs_in.go index 348f4563..22e29897 100644 --- a/plugin/singbox/srs_in.go +++ b/plugin/singbox/srs_in.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "os" "path/filepath" @@ -21,80 +22,120 @@ const ( func init() { lib.RegisterInputConfigCreator(TypeSRSIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return newSRSIn(action, data) + return NewSRSInFromBytes(action, data) }) - lib.RegisterInputConverter(TypeSRSIn, &SRSIn{ + lib.RegisterInputConverter(TypeSRSIn, &srs_in{ Description: DescSRSIn, }) } -func newSRSIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - var tmp struct { - Name string `json:"name"` - URI string `json:"uri"` - InputDir string `json:"inputDir"` - Want []string `json:"wantedList"` - OnlyIPType lib.IPType `json:"onlyIPType"` +type srs_in struct { + Type string + Action lib.Action + Description string + Name string + URI string + InputDir string + Want map[string]bool + OnlyIPType lib.IPType +} + +func NewSRSIn(action lib.Action, opts ...lib.InputOption) lib.InputConverter { + s := &srs_in{ + Type: TypeSRSIn, + Action: action, + Description: DescSRSIn, } - if len(data) > 0 { - if err := json.Unmarshal(data, &tmp); err != nil { - return nil, err + for _, opt := range opts { + if opt != nil { + opt(s) } } - if tmp.Name == "" && tmp.URI == "" && tmp.InputDir == "" { - return nil, fmt.Errorf("❌ [type %s | action %s] missing inputdir or name or uri", TypeSRSIn, action) + return s +} + +func WithNameAndURI(name, uri string) lib.InputOption { + return func(s lib.InputConverter) { + name = strings.TrimSpace(name) + uri = strings.TrimSpace(uri) + if (name == "" || uri == "") && strings.TrimSpace(s.(*srs_in).InputDir) == "" { + log.Fatalf("❌ [type %s | action %s] missing name or uri or inputDir", TypeSRSIn, s.(*srs_in).Action) + } + + s.(*srs_in).Name = name + s.(*srs_in).URI = uri } +} - if (tmp.Name != "" && tmp.URI == "") || (tmp.Name == "" && tmp.URI != "") { - return nil, fmt.Errorf("❌ [type %s | action %s] name & uri must be specified together", TypeSRSIn, action) +func WithInputDir(dir string) lib.InputOption { + return func(s lib.InputConverter) { + dir = strings.TrimSpace(dir) + if dir == "" && (strings.TrimSpace(s.(*srs_in).Name) == "" || strings.TrimSpace(s.(*srs_in).URI) == "") { + log.Fatalf("❌ [type %s | action %s] missing name or uri or inputDir", TypeSRSIn, s.(*srs_in).Action) + } + + s.(*srs_in).InputDir = dir } +} - // Filter want list - wantList := make(map[string]bool) - for _, want := range tmp.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true +func WithInputWantedList(lists []string) lib.InputOption { + return func(s lib.InputConverter) { + wantList := make(map[string]bool) + for _, want := range lists { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } } + + s.(*srs_in).Want = wantList } +} - return &SRSIn{ - Type: TypeSRSIn, - Action: action, - Description: DescSRSIn, - Name: tmp.Name, - URI: tmp.URI, - InputDir: tmp.InputDir, - Want: wantList, - OnlyIPType: tmp.OnlyIPType, - }, nil +func WithInputOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(s lib.InputConverter) { + s.(*srs_in).OnlyIPType = onlyIPType + } } -type SRSIn struct { - Type string - Action lib.Action - Description string - Name string - URI string - InputDir string - Want map[string]bool - OnlyIPType lib.IPType +func NewSRSInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) { + var tmp struct { + Name string `json:"name"` + URI string `json:"uri"` + InputDir string `json:"inputDir"` + Want []string `json:"wantedList"` + OnlyIPType lib.IPType `json:"onlyIPType"` + } + + if len(data) > 0 { + if err := json.Unmarshal(data, &tmp); err != nil { + return nil, err + } + } + + return NewSRSIn( + action, + WithNameAndURI(tmp.Name, tmp.URI), + WithInputDir(tmp.InputDir), + WithInputWantedList(tmp.Want), + WithInputOnlyIPType(tmp.OnlyIPType), + ), nil } -func (s *SRSIn) GetType() string { +func (s *srs_in) GetType() string { return s.Type } -func (s *SRSIn) GetAction() lib.Action { +func (s *srs_in) GetAction() lib.Action { return s.Action } -func (s *SRSIn) GetDescription() string { +func (s *srs_in) GetDescription() string { return s.Description } -func (s *SRSIn) Input(container lib.Container) (lib.Container, error) { +func (s *srs_in) Input(container lib.Container) (lib.Container, error) { entries := make(map[string]*lib.Entry) var err error @@ -140,7 +181,7 @@ func (s *SRSIn) Input(container lib.Container) (lib.Container, error) { return container, nil } -func (s *SRSIn) walkDir(dir string, entries map[string]*lib.Entry) error { +func (s *srs_in) walkDir(dir string, entries map[string]*lib.Entry) error { err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -159,7 +200,7 @@ func (s *SRSIn) walkDir(dir string, entries map[string]*lib.Entry) error { return err } -func (s *SRSIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) error { +func (s *srs_in) walkLocalFile(path, name string, entries map[string]*lib.Entry) error { entryName := "" name = strings.TrimSpace(name) if name != "" { @@ -197,7 +238,7 @@ func (s *SRSIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) return nil } -func (s *SRSIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error { +func (s *srs_in) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error { resp, err := http.Get(url) if err != nil { return err @@ -215,7 +256,7 @@ func (s *SRSIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) return nil } -func (s *SRSIn) generateEntries(name string, reader io.Reader, entries map[string]*lib.Entry) error { +func (s *srs_in) generateEntries(name string, reader io.Reader, entries map[string]*lib.Entry) error { name = strings.ToUpper(name) if len(s.Want) > 0 && !s.Want[name] { diff --git a/plugin/singbox/srs_out.go b/plugin/singbox/srs_out.go index 535eb6b8..386a656d 100644 --- a/plugin/singbox/srs_out.go +++ b/plugin/singbox/srs_out.go @@ -26,14 +26,69 @@ var ( func init() { lib.RegisterOutputConfigCreator(TypeSRSOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { - return newSRSOut(action, data) + return NewSRSOutFromBytes(action, data) }) - lib.RegisterOutputConverter(TypeSRSOut, &SRSOut{ + lib.RegisterOutputConverter(TypeSRSOut, &srs_out{ Description: DescSRSOut, }) } -func newSRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +type srs_out struct { + Type string + Action lib.Action + Description string + OutputDir string + Want []string + Exclude []string + OnlyIPType lib.IPType +} + +func NewSRSOut(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + s := &srs_out{ + Type: TypeSRSOut, + Action: action, + Description: DescSRSOut, + } + + for _, opt := range opts { + if opt != nil { + opt(s) + } + } + + return s +} + +func WithOutputDir(dir string) lib.OutputOption { + return func(s lib.OutputConverter) { + dir = strings.TrimSpace(dir) + if dir == "" { + dir = defaultOutputDir + } + + s.(*srs_out).OutputDir = dir + } +} + +func WithOutputWantedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*srs_out).Want = lists + } +} + +func WithOutputExcludedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*srs_out).Exclude = lists + } +} + +func WithOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*srs_out).OnlyIPType = onlyIPType + } +} + +func NewSRSOutFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` @@ -47,44 +102,28 @@ func newSRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er } } - if tmp.OutputDir == "" { - tmp.OutputDir = defaultOutputDir - } - - return &SRSOut{ - Type: TypeSRSOut, - Action: action, - Description: DescSRSOut, - OutputDir: tmp.OutputDir, - Want: tmp.Want, - Exclude: tmp.Exclude, - OnlyIPType: tmp.OnlyIPType, - }, nil -} - -type SRSOut struct { - Type string - Action lib.Action - Description string - OutputDir string - Want []string - Exclude []string - OnlyIPType lib.IPType + return NewSRSOut( + action, + WithOutputDir(tmp.OutputDir), + WithOutputWantedList(tmp.Want), + WithOutputExcludedList(tmp.Exclude), + WithOutputOnlyIPType(tmp.OnlyIPType), + ), nil } -func (s *SRSOut) GetType() string { +func (s *srs_out) GetType() string { return s.Type } -func (s *SRSOut) GetAction() lib.Action { +func (s *srs_out) GetAction() lib.Action { return s.Action } -func (s *SRSOut) GetDescription() string { +func (s *srs_out) GetDescription() string { return s.Description } -func (s *SRSOut) Output(container lib.Container) error { +func (s *srs_out) Output(container lib.Container) error { for _, name := range s.filterAndSortList(container) { entry, found := container.GetEntry(name) if !found { @@ -100,7 +139,7 @@ func (s *SRSOut) Output(container lib.Container) error { return nil } -func (s *SRSOut) filterAndSortList(container lib.Container) []string { +func (s *srs_out) filterAndSortList(container lib.Container) []string { excludeMap := make(map[string]bool) for _, exclude := range s.Exclude { if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { @@ -136,7 +175,7 @@ func (s *SRSOut) filterAndSortList(container lib.Container) []string { return list } -func (s *SRSOut) generate(entry *lib.Entry) error { +func (s *srs_out) generate(entry *lib.Entry) error { ruleset, err := s.marshalRuleSet(entry) if err != nil { return err @@ -150,7 +189,7 @@ func (s *SRSOut) generate(entry *lib.Entry) error { return nil } -func (s *SRSOut) marshalRuleSet(entry *lib.Entry) (*option.PlainRuleSet, error) { +func (s *srs_out) marshalRuleSet(entry *lib.Entry) (*option.PlainRuleSet, error) { entryCidr, err := entry.MarshalText(lib.GetIgnoreIPType(s.OnlyIPType)) if err != nil { return nil, err @@ -174,7 +213,7 @@ func (s *SRSOut) marshalRuleSet(entry *lib.Entry) (*option.PlainRuleSet, error) return nil, fmt.Errorf("❌ [type %s | action %s] entry %s has no CIDR", s.Type, s.Action, entry.GetName()) } -func (s *SRSOut) writeFile(filename string, ruleset *option.PlainRuleSet) error { +func (s *srs_out) writeFile(filename string, ruleset *option.PlainRuleSet) error { if err := os.MkdirAll(s.OutputDir, 0755); err != nil { return err } |
