summaryrefslogtreecommitdiff
path: root/plugin/v2ray
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-03-09 06:35:47 +0000
committercopilot-swe-agent[bot] <[email protected]>2026-03-09 06:35:47 +0000
commit2600825c50d7e7f2b4427674f1aefca270bcca27 (patch)
tree18f216c6af28fd3975a0665d75ccf7b5c0078cb6 /plugin/v2ray
parent5a14eb90f575b983aa407341af91aa7654e65f12 (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')
-rw-r--r--plugin/v2ray/dat_in.go111
-rw-r--r--plugin/v2ray/dat_out.go136
2 files changed, 166 insertions, 81 deletions
diff --git a/plugin/v2ray/dat_in.go b/plugin/v2ray/dat_in.go
index 33a24061..93c52a51 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,70 +21,102 @@ 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{
+ lib.RegisterInputConverter(TypeGeoIPDatIn, &geoIPDatIn{
Description: DescGeoIPDatIn,
})
}
-func newGeoIPDatIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- var tmp struct {
- URI string `json:"uri"`
- Want []string `json:"wantedList"`
- OnlyIPType lib.IPType `json:"onlyIPType"`
+type geoIPDatIn struct {
+ Type string
+ Action lib.Action
+ Description string
+ URI string
+ Want map[string]bool
+ OnlyIPType lib.IPType
+}
+
+func NewGeoIPDatIn(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ g := &geoIPDatIn{
+ Type: TypeGeoIPDatIn,
+ Action: action,
+ Description: DescGeoIPDatIn,
}
- if len(data) > 0 {
- if err := json.Unmarshal(data, &tmp); err != nil {
- return nil, err
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
}
}
- if tmp.URI == "" {
- return nil, fmt.Errorf("❌ [type %s | action %s] uri must be specified in config", TypeGeoIPDatIn, action)
+ return g
+}
+
+func WithURI(uri string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ uri = strings.TrimSpace(uri)
+ if uri == "" {
+ log.Fatalf("❌ [type %s | action %s] uri must be specified", TypeGeoIPDatIn, g.(*geoIPDatIn).Action)
+ }
+
+ g.(*geoIPDatIn).URI = uri
}
+}
- // 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(g lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
}
+
+ g.(*geoIPDatIn).Want = wantList
}
+}
- return &GeoIPDatIn{
- Type: TypeGeoIPDatIn,
- Action: action,
- Description: DescGeoIPDatIn,
- URI: tmp.URI,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+func WithInputOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoIPDatIn).OnlyIPType = onlyIPType
+ }
}
-type GeoIPDatIn struct {
- Type string
- Action lib.Action
- Description string
- URI string
- Want map[string]bool
- OnlyIPType lib.IPType
+func NewGeoIPDatInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
+ var tmp struct {
+ URI string `json:"uri"`
+ 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 NewGeoIPDatIn(
+ action,
+ WithURI(tmp.URI),
+ WithInputWantedList(tmp.Want),
+ WithInputOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
-func (g *GeoIPDatIn) GetType() string {
+func (g *geoIPDatIn) GetType() string {
return g.Type
}
-func (g *GeoIPDatIn) GetAction() lib.Action {
+func (g *geoIPDatIn) GetAction() lib.Action {
return g.Action
}
-func (g *GeoIPDatIn) GetDescription() string {
+func (g *geoIPDatIn) GetDescription() string {
return g.Description
}
-func (g *GeoIPDatIn) Input(container lib.Container) (lib.Container, error) {
+func (g *geoIPDatIn) Input(container lib.Container) (lib.Container, error) {
entries := make(map[string]*lib.Entry)
var err error
@@ -122,7 +155,7 @@ func (g *GeoIPDatIn) Input(container lib.Container) (lib.Container, error) {
return container, nil
}
-func (g *GeoIPDatIn) walkLocalFile(path string, entries map[string]*lib.Entry) error {
+func (g *geoIPDatIn) walkLocalFile(path string, entries map[string]*lib.Entry) error {
file, err := os.Open(path)
if err != nil {
return err
@@ -136,7 +169,7 @@ func (g *GeoIPDatIn) walkLocalFile(path string, entries map[string]*lib.Entry) e
return nil
}
-func (g *GeoIPDatIn) walkRemoteFile(url string, entries map[string]*lib.Entry) error {
+func (g *geoIPDatIn) walkRemoteFile(url string, entries map[string]*lib.Entry) error {
resp, err := http.Get(url)
if err != nil {
return err
@@ -154,7 +187,7 @@ func (g *GeoIPDatIn) walkRemoteFile(url string, entries map[string]*lib.Entry) e
return nil
}
-func (g *GeoIPDatIn) generateEntries(reader io.Reader, entries map[string]*lib.Entry) error {
+func (g *geoIPDatIn) generateEntries(reader io.Reader, entries map[string]*lib.Entry) error {
geoipBytes, err := io.ReadAll(reader)
if err != nil {
return err
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
}