summaryrefslogtreecommitdiff
path: root/plugin/mihomo
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/mihomo')
-rw-r--r--plugin/mihomo/mrs_in.go139
-rw-r--r--plugin/mihomo/mrs_out.go107
2 files changed, 163 insertions, 83 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
diff --git a/plugin/mihomo/mrs_out.go b/plugin/mihomo/mrs_out.go
index e8d65e9f..ab425cec 100644
--- a/plugin/mihomo/mrs_out.go
+++ b/plugin/mihomo/mrs_out.go
@@ -27,14 +27,69 @@ var (
func init() {
lib.RegisterOutputConfigCreator(TypeMRSOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newMRSOut(action, data)
+ return NewMRSOutFromBytes(action, data)
})
- lib.RegisterOutputConverter(TypeMRSOut, &MRSOut{
+ lib.RegisterOutputConverter(TypeMRSOut, &mrs_out{
Description: DescMRSOut,
})
}
-func newMRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+type mrs_out struct {
+ Type string
+ Action lib.Action
+ Description string
+ OutputDir string
+ Want []string
+ Exclude []string
+ OnlyIPType lib.IPType
+}
+
+func NewMRSOut(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter {
+ m := &mrs_out{
+ Type: TypeMRSOut,
+ Action: action,
+ Description: DescMRSOut,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(m)
+ }
+ }
+
+ return m
+}
+
+func WithMRSOutOutputDir(dir string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" {
+ dir = defaultOutputDir
+ }
+
+ s.(*mrs_out).OutputDir = dir
+ }
+}
+
+func WithMRSOutWantedList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*mrs_out).Want = lists
+ }
+}
+
+func WithMRSOutExcludedList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*mrs_out).Exclude = lists
+ }
+}
+
+func WithMRSOutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*mrs_out).OnlyIPType = onlyIPType
+ }
+}
+
+func NewMRSOutFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) {
var tmp struct {
OutputDir string `json:"outputDir"`
Want []string `json:"wantedList"`
@@ -48,44 +103,28 @@ func newMRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er
}
}
- if tmp.OutputDir == "" {
- tmp.OutputDir = defaultOutputDir
- }
-
- return &MRSOut{
- Type: TypeMRSOut,
- Action: action,
- Description: DescMRSOut,
- OutputDir: tmp.OutputDir,
- Want: tmp.Want,
- Exclude: tmp.Exclude,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
-}
-
-type MRSOut struct {
- Type string
- Action lib.Action
- Description string
- OutputDir string
- Want []string
- Exclude []string
- OnlyIPType lib.IPType
+ return NewMRSOut(
+ action,
+ WithMRSOutOutputDir(tmp.OutputDir),
+ WithMRSOutWantedList(tmp.Want),
+ WithMRSOutExcludedList(tmp.Exclude),
+ WithMRSOutOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
-func (m *MRSOut) GetType() string {
+func (m *mrs_out) GetType() string {
return m.Type
}
-func (m *MRSOut) GetAction() lib.Action {
+func (m *mrs_out) GetAction() lib.Action {
return m.Action
}
-func (m *MRSOut) GetDescription() string {
+func (m *mrs_out) GetDescription() string {
return m.Description
}
-func (m *MRSOut) Output(container lib.Container) error {
+func (m *mrs_out) Output(container lib.Container) error {
for _, name := range m.filterAndSortList(container) {
entry, found := container.GetEntry(name)
if !found {
@@ -101,7 +140,7 @@ func (m *MRSOut) Output(container lib.Container) error {
return nil
}
-func (m *MRSOut) filterAndSortList(container lib.Container) []string {
+func (m *mrs_out) filterAndSortList(container lib.Container) []string {
excludeMap := make(map[string]bool)
for _, exclude := range m.Exclude {
if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" {
@@ -137,7 +176,7 @@ func (m *MRSOut) filterAndSortList(container lib.Container) []string {
return list
}
-func (m *MRSOut) generate(entry *lib.Entry) error {
+func (m *mrs_out) generate(entry *lib.Entry) error {
ipRanges, err := entry.MarshalIPRange(lib.GetIgnoreIPType(m.OnlyIPType))
if err != nil {
return err
@@ -155,7 +194,7 @@ func (m *MRSOut) generate(entry *lib.Entry) error {
return nil
}
-func (m *MRSOut) writeFile(filename string, ipRanges []netipx.IPRange) error {
+func (m *mrs_out) writeFile(filename string, ipRanges []netipx.IPRange) error {
if err := os.MkdirAll(m.OutputDir, 0755); err != nil {
return err
}
@@ -176,7 +215,7 @@ func (m *MRSOut) writeFile(filename string, ipRanges []netipx.IPRange) error {
return nil
}
-func (m *MRSOut) convertToMrs(ipRanges []netipx.IPRange, w io.Writer) (err error) {
+func (m *mrs_out) convertToMrs(ipRanges []netipx.IPRange, w io.Writer) (err error) {
encoder, err := zstd.NewWriter(w)
if err != nil {
return err