summaryrefslogtreecommitdiff
path: root/plugin/mihomo/mrs_in.go
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-04-28 18:24:00 +0000
committerGitHub <[email protected]>2026-04-28 18:24:00 +0000
commit8834c0be63ee88e0ad23fe621d5f5fe344b32089 (patch)
tree49029d2fd927e8832d05420d1bf1cf850aa22325 /plugin/mihomo/mrs_in.go
parent4f125e579472e5ed87fd052ef68ab80f5fe679b0 (diff)
Refactor all plugins to use functional options patterncopilot/refactor-plugins-functional-options
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/e2b66c9a-3d01-490c-9b31-32109cfe4feb Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/mihomo/mrs_in.go')
-rw-r--r--plugin/mihomo/mrs_in.go139
1 files changed, 90 insertions, 49 deletions
diff --git a/plugin/mihomo/mrs_in.go b/plugin/mihomo/mrs_in.go
index cc80f245..e1fb5564 100644
--- a/plugin/mihomo/mrs_in.go
+++ b/plugin/mihomo/mrs_in.go
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
+ "log"
"net/http"
"net/netip"
"os"
@@ -27,80 +28,120 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeMRSIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newMRSIn(action, data)
+ return NewMRSInFromBytes(action, data)
})
- lib.RegisterInputConverter(TypeMRSIn, &MRSIn{
+ lib.RegisterInputConverter(TypeMRSIn, &mrs_in{
Description: DescMRSIn,
})
}
-func newMRSIn(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 mrs_in struct {
+ Type string
+ Action lib.Action
+ Description string
+ Name string
+ URI string
+ InputDir string
+ Want map[string]bool
+ OnlyIPType lib.IPType
+}
+
+func NewMRSIn(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ m := &mrs_in{
+ Type: TypeMRSIn,
+ Action: action,
+ Description: DescMRSIn,
}
- if len(data) > 0 {
- if err := json.Unmarshal(data, &tmp); err != nil {
- return nil, err
+ for _, opt := range opts {
+ if opt != nil {
+ opt(m)
}
}
- if tmp.Name == "" && tmp.URI == "" && tmp.InputDir == "" {
- return nil, fmt.Errorf("❌ [type %s | action %s] missing inputDir or name or uri", TypeMRSIn, action)
+ return m
+}
+
+func WithMRSInNameAndURI(name, uri string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ name = strings.TrimSpace(name)
+ uri = strings.TrimSpace(uri)
+ if (name == "" || uri == "") && strings.TrimSpace(s.(*mrs_in).InputDir) == "" {
+ log.Fatalf("❌ [type %s | action %s] missing name or uri or inputDir", TypeMRSIn, s.(*mrs_in).Action)
+ }
+
+ s.(*mrs_in).Name = name
+ s.(*mrs_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", TypeMRSIn, action)
+func WithMRSInInputDir(dir string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" && (strings.TrimSpace(s.(*mrs_in).Name) == "" || strings.TrimSpace(s.(*mrs_in).URI) == "") {
+ log.Fatalf("❌ [type %s | action %s] missing name or uri or inputDir", TypeMRSIn, s.(*mrs_in).Action)
+ }
+
+ s.(*mrs_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 WithMRSInWantedList(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.(*mrs_in).Want = wantList
}
+}
- return &MRSIn{
- Type: TypeMRSIn,
- Action: action,
- Description: DescMRSIn,
- Name: tmp.Name,
- URI: tmp.URI,
- InputDir: tmp.InputDir,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+func WithMRSInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*mrs_in).OnlyIPType = onlyIPType
+ }
}
-type MRSIn struct {
- Type string
- Action lib.Action
- Description string
- Name string
- URI string
- InputDir string
- Want map[string]bool
- OnlyIPType lib.IPType
+func NewMRSInFromBytes(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 NewMRSIn(
+ action,
+ WithMRSInNameAndURI(tmp.Name, tmp.URI),
+ WithMRSInInputDir(tmp.InputDir),
+ WithMRSInWantedList(tmp.Want),
+ WithMRSInOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
-func (m *MRSIn) GetType() string {
+func (m *mrs_in) GetType() string {
return m.Type
}
-func (m *MRSIn) GetAction() lib.Action {
+func (m *mrs_in) GetAction() lib.Action {
return m.Action
}
-func (m *MRSIn) GetDescription() string {
+func (m *mrs_in) GetDescription() string {
return m.Description
}
-func (m *MRSIn) Input(container lib.Container) (lib.Container, error) {
+func (m *mrs_in) Input(container lib.Container) (lib.Container, error) {
entries := make(map[string]*lib.Entry)
var err error
@@ -146,7 +187,7 @@ func (m *MRSIn) Input(container lib.Container) (lib.Container, error) {
return container, nil
}
-func (m *MRSIn) walkDir(dir string, entries map[string]*lib.Entry) error {
+func (m *mrs_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
@@ -165,7 +206,7 @@ func (m *MRSIn) walkDir(dir string, entries map[string]*lib.Entry) error {
return err
}
-func (m *MRSIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) error {
+func (m *mrs_in) walkLocalFile(path, name string, entries map[string]*lib.Entry) error {
entryName := ""
name = strings.TrimSpace(name)
if name != "" {
@@ -203,7 +244,7 @@ func (m *MRSIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)
return nil
}
-func (m *MRSIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error {
+func (m *mrs_in) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error {
resp, err := http.Get(url)
if err != nil {
return err
@@ -221,7 +262,7 @@ func (m *MRSIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry)
return nil
}
-func (m *MRSIn) generateEntries(name string, reader io.Reader, entries map[string]*lib.Entry) error {
+func (m *mrs_in) generateEntries(name string, reader io.Reader, entries map[string]*lib.Entry) error {
name = strings.ToUpper(name)
if len(m.Want) > 0 && !m.Want[name] {
@@ -247,7 +288,7 @@ func (m *MRSIn) generateEntries(name string, reader io.Reader, entries map[strin
return nil
}
-func (m *MRSIn) parseMRS(data []byte, entry *lib.Entry) error {
+func (m *mrs_in) parseMRS(data []byte, entry *lib.Entry) error {
reader, err := zstd.NewReader(bytes.NewReader(data))
if err != nil {
return err