diff options
| author | openai-code-agent[bot] <[email protected]> | 2026-04-28 17:44:54 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-28 17:44:54 +0000 |
| commit | e25f9fcf96d9971e90c3a5df9087d882e4a997a7 (patch) | |
| tree | 47ba52a559e962695a605c5faf14795c7f95bad2 /plugin | |
| parent | 60b366c1682009a246056aad7b4a9d30b2a3fd62 (diff) | |
refactor(v2ray): use functional options for GeoIP dat plugins
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/v2ray/dat_in.go | 71 | ||||
| -rw-r--r-- | plugin/v2ray/dat_out.go | 92 |
2 files changed, 124 insertions, 39 deletions
diff --git a/plugin/v2ray/dat_in.go b/plugin/v2ray/dat_in.go index 33a24061..53990274 100644 --- a/plugin/v2ray/dat_in.go +++ b/plugin/v2ray/dat_in.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net" "net/http" "os" @@ -20,14 +21,58 @@ const ( func init() { lib.RegisterInputConfigCreator(TypeGeoIPDatIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return newGeoIPDatIn(action, data) + return NewGeoIPDatInFromBytes(action, data) }) lib.RegisterInputConverter(TypeGeoIPDatIn, &GeoIPDatIn{ Description: DescGeoIPDatIn, }) } -func newGeoIPDatIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { +func NewGeoIPDatIn(action lib.Action, opts ...lib.InputOption) lib.InputConverter { + g := &GeoIPDatIn{ + Type: TypeGeoIPDatIn, + Action: action, + Description: DescGeoIPDatIn, + } + + for _, opt := range opts { + if opt != nil { + opt(g) + } + } + + return g +} + +func WithGeoIPDatURI(uri string) lib.InputOption { + return func(c lib.InputConverter) { + uri = strings.TrimSpace(uri) + if uri == "" { + log.Fatalf("❌ [type %s | action %s] uri must be specified in config", TypeGeoIPDatIn, c.(*GeoIPDatIn).Action) + } + c.(*GeoIPDatIn).URI = uri + } +} + +func WithGeoIPDatWantedList(lists []string) lib.InputOption { + return func(c lib.InputConverter) { + wantList := make(map[string]bool) + for _, want := range lists { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + c.(*GeoIPDatIn).Want = wantList + } +} + +func WithGeoIPDatOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(c lib.InputConverter) { + c.(*GeoIPDatIn).OnlyIPType = onlyIPType + } +} + +func NewGeoIPDatInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) { var tmp struct { URI string `json:"uri"` Want []string `json:"wantedList"` @@ -44,22 +89,12 @@ func newGeoIPDatIn(action lib.Action, data json.RawMessage) (lib.InputConverter, return nil, fmt.Errorf("❌ [type %s | action %s] uri must be specified in config", TypeGeoIPDatIn, 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 &GeoIPDatIn{ - Type: TypeGeoIPDatIn, - Action: action, - Description: DescGeoIPDatIn, - URI: tmp.URI, - Want: wantList, - OnlyIPType: tmp.OnlyIPType, - }, nil + return NewGeoIPDatIn( + action, + WithGeoIPDatURI(tmp.URI), + WithGeoIPDatWantedList(tmp.Want), + WithGeoIPDatOnlyIPType(tmp.OnlyIPType), + ), nil } type GeoIPDatIn struct { diff --git a/plugin/v2ray/dat_out.go b/plugin/v2ray/dat_out.go index 5137e828..76c8bb91 100644 --- a/plugin/v2ray/dat_out.go +++ b/plugin/v2ray/dat_out.go @@ -26,14 +26,74 @@ 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{ Description: DescGeoIPDatOut, }) } -func newGeoIPDatOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +func NewGeoIPDatOut(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + g := &GeoIPDatOut{ + Type: TypeGeoIPDatOut, + Action: action, + Description: DescGeoIPDatOut, + OutputName: defaultOutputName, + OutputDir: defaultOutputDir, + } + + for _, opt := range opts { + if opt != nil { + opt(g) + } + } + + return g +} + +func WithGeoIPDatOutputName(name string) lib.OutputOption { + return func(c lib.OutputConverter) { + name = strings.TrimSpace(name) + if name != "" { + c.(*GeoIPDatOut).OutputName = name + } + } +} + +func WithGeoIPDatOutputDir(dir string) lib.OutputOption { + return func(c lib.OutputConverter) { + dir = strings.TrimSpace(dir) + if dir != "" { + c.(*GeoIPDatOut).OutputDir = dir + } + } +} + +func WithGeoIPDatOutputWantedList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*GeoIPDatOut).Want = lists + } +} + +func WithGeoIPDatOutputExcludedList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*GeoIPDatOut).Exclude = lists + } +} + +func WithGeoIPDatOneFilePerList(enabled bool) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*GeoIPDatOut).OneFilePerList = enabled + } +} + +func WithGeoIPDatOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*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,25 +109,15 @@ 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 + return NewGeoIPDatOut( + action, + WithGeoIPDatOutputName(tmp.OutputName), + WithGeoIPDatOutputDir(tmp.OutputDir), + WithGeoIPDatOutputWantedList(tmp.Want), + WithGeoIPDatOutputExcludedList(tmp.Exclude), + WithGeoIPDatOneFilePerList(tmp.OneFilePerList), + WithGeoIPDatOutputOnlyIPType(tmp.OnlyIPType), + ), nil } type GeoIPDatOut struct { |
