diff options
| author | copilot-swe-agent[bot] <[email protected]> | 2026-03-09 06:35:47 +0000 |
|---|---|---|
| committer | copilot-swe-agent[bot] <[email protected]> | 2026-03-09 06:35:47 +0000 |
| commit | 2600825c50d7e7f2b4427674f1aefca270bcca27 (patch) | |
| tree | 18f216c6af28fd3975a0665d75ccf7b5c0078cb6 /plugin/v2ray/dat_out.go | |
| parent | 5a14eb90f575b983aa407341af91aa7654e65f12 (diff) | |
Refactor: all plugin subdirectories use option patterncopilot/modify-plugin-files-subdirectories
Apply the same functional options pattern from plugin/singbox to:
- plugin/mihomo (mrs_in.go, mrs_out.go)
- plugin/plaintext (text_in.go, common_in.go, common_out.go, text_out.go, clash_in.go, clash_out.go, json_in.go, surge_in.go, surge_out.go)
- plugin/maxmind (all input/output files)
- plugin/v2ray (dat_in.go, dat_out.go)
- plugin/special (cutter.go, lookup.go, private.go, stdin.go, stdout.go)
- lookup.go and merge.go updated to use new constructors
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/v2ray/dat_out.go')
| -rw-r--r-- | plugin/v2ray/dat_out.go | 136 |
1 files changed, 94 insertions, 42 deletions
diff --git a/plugin/v2ray/dat_out.go b/plugin/v2ray/dat_out.go index 5137e828..f2a28216 100644 --- a/plugin/v2ray/dat_out.go +++ b/plugin/v2ray/dat_out.go @@ -26,14 +26,88 @@ var ( func init() { lib.RegisterOutputConfigCreator(TypeGeoIPDatOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { - return newGeoIPDatOut(action, data) + return NewGeoIPDatOutFromBytes(action, data) }) - lib.RegisterOutputConverter(TypeGeoIPDatOut, &GeoIPDatOut{ + lib.RegisterOutputConverter(TypeGeoIPDatOut, &geoIPDatOut{ Description: DescGeoIPDatOut, }) } -func newGeoIPDatOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +type geoIPDatOut struct { + Type string + Action lib.Action + Description string + OutputName string + OutputDir string + Want []string + Exclude []string + OneFilePerList bool + OnlyIPType lib.IPType +} + +func NewGeoIPDatOut(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + g := &geoIPDatOut{ + Type: TypeGeoIPDatOut, + Action: action, + Description: DescGeoIPDatOut, + } + + for _, opt := range opts { + if opt != nil { + opt(g) + } + } + + return g +} + +func WithOutputName(name string) lib.OutputOption { + return func(g lib.OutputConverter) { + name = strings.TrimSpace(name) + if name == "" { + name = defaultOutputName + } + + g.(*geoIPDatOut).OutputName = name + } +} + +func WithOutputDir(dir string) lib.OutputOption { + return func(g lib.OutputConverter) { + dir = strings.TrimSpace(dir) + if dir == "" { + dir = defaultOutputDir + } + + g.(*geoIPDatOut).OutputDir = dir + } +} + +func WithOutputWantedList(lists []string) lib.OutputOption { + return func(g lib.OutputConverter) { + g.(*geoIPDatOut).Want = lists + } +} + +func WithOutputExcludedList(lists []string) lib.OutputOption { + return func(g lib.OutputConverter) { + g.(*geoIPDatOut).Exclude = lists + } +} + +func WithOneFilePerList(oneFilePerList bool) lib.OutputOption { + return func(g lib.OutputConverter) { + g.(*geoIPDatOut).OneFilePerList = oneFilePerList + } +} + +func WithOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(g lib.OutputConverter) { + g.(*geoIPDatOut).OnlyIPType = onlyIPType + } +} + +func NewGeoIPDatOutFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { OutputName string `json:"outputName"` OutputDir string `json:"outputDir"` @@ -49,52 +123,30 @@ func newGeoIPDatOut(action lib.Action, data json.RawMessage) (lib.OutputConverte } } - if tmp.OutputName == "" { - tmp.OutputName = defaultOutputName - } - - if tmp.OutputDir == "" { - tmp.OutputDir = defaultOutputDir - } - - return &GeoIPDatOut{ - Type: TypeGeoIPDatOut, - Action: action, - Description: DescGeoIPDatOut, - OutputName: tmp.OutputName, - OutputDir: tmp.OutputDir, - Want: tmp.Want, - Exclude: tmp.Exclude, - OneFilePerList: tmp.OneFilePerList, - OnlyIPType: tmp.OnlyIPType, - }, nil -} - -type GeoIPDatOut struct { - Type string - Action lib.Action - Description string - OutputName string - OutputDir string - Want []string - Exclude []string - OneFilePerList bool - OnlyIPType lib.IPType + return NewGeoIPDatOut( + action, + WithOutputName(tmp.OutputName), + WithOutputDir(tmp.OutputDir), + WithOutputWantedList(tmp.Want), + WithOutputExcludedList(tmp.Exclude), + WithOneFilePerList(tmp.OneFilePerList), + WithOutputOnlyIPType(tmp.OnlyIPType), + ), nil } -func (g *GeoIPDatOut) GetType() string { +func (g *geoIPDatOut) GetType() string { return g.Type } -func (g *GeoIPDatOut) GetAction() lib.Action { +func (g *geoIPDatOut) GetAction() lib.Action { return g.Action } -func (g *GeoIPDatOut) GetDescription() string { +func (g *geoIPDatOut) GetDescription() string { return g.Description } -func (g *GeoIPDatOut) Output(container lib.Container) error { +func (g *geoIPDatOut) Output(container lib.Container) error { geoIPList := new(GeoIPList) geoIPList.Entry = make([]*GeoIP, 0, 300) updated := false @@ -144,7 +196,7 @@ func (g *GeoIPDatOut) Output(container lib.Container) error { return nil } -func (g *GeoIPDatOut) filterAndSortList(container lib.Container) []string { +func (g *geoIPDatOut) filterAndSortList(container lib.Container) []string { excludeMap := make(map[string]bool) for _, exclude := range g.Exclude { if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { @@ -180,7 +232,7 @@ func (g *GeoIPDatOut) filterAndSortList(container lib.Container) []string { return list } -func (g *GeoIPDatOut) generateGeoIP(entry *lib.Entry) (*GeoIP, error) { +func (g *geoIPDatOut) generateGeoIP(entry *lib.Entry) (*GeoIP, error) { entryCidr, err := entry.MarshalPrefix(lib.GetIgnoreIPType(g.OnlyIPType)) if err != nil { return nil, err @@ -205,13 +257,13 @@ func (g *GeoIPDatOut) generateGeoIP(entry *lib.Entry) (*GeoIP, error) { } // Sort by country code to make reproducible builds -func (g *GeoIPDatOut) sort(list *GeoIPList) { +func (g *geoIPDatOut) sort(list *GeoIPList) { sort.SliceStable(list.Entry, func(i, j int) bool { return list.Entry[i].CountryCode < list.Entry[j].CountryCode }) } -func (g *GeoIPDatOut) writeFile(filename string, geoIPBytes []byte) error { +func (g *geoIPDatOut) writeFile(filename string, geoIPBytes []byte) error { if err := os.MkdirAll(g.OutputDir, 0755); err != nil { return err } |
