summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authoranthropic-code-agent[bot] <[email protected]>2026-04-28 17:46:32 +0000
committerGitHub <[email protected]>2026-04-28 17:46:32 +0000
commitc8d8eaa0a77322fde7774ba72a78dcfcfeeb3e1a (patch)
tree953c1c0d2884b13a5485d347fc753a4e33618ed5 /plugin
parent4f125e579472e5ed87fd052ef68ab80f5fe679b0 (diff)
Refactor v2ray and mihomo plugins to use functional options pattern
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/6ea417a5-8261-408f-8a04-7a3b485cc488 Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin')
-rw-r--r--plugin/mihomo/mrs_in.go139
-rw-r--r--plugin/mihomo/mrs_out.go107
-rw-r--r--plugin/v2ray/dat_in.go112
-rw-r--r--plugin/v2ray/dat_out.go136
4 files changed, 330 insertions, 164 deletions
diff --git a/plugin/mihomo/mrs_in.go b/plugin/mihomo/mrs_in.go
index cc80f245..4745701b 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 WithMihomoNameAndURI(name, uri string) lib.InputOption {
+ return func(m lib.InputConverter) {
+ name = strings.TrimSpace(name)
+ uri = strings.TrimSpace(uri)
+ if (name == "" || uri == "") && strings.TrimSpace(m.(*mrs_in).InputDir) == "" {
+ log.Fatalf("❌ [type %s | action %s] missing name or uri or inputDir", TypeMRSIn, m.(*mrs_in).Action)
+ }
+
+ m.(*mrs_in).Name = name
+ m.(*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 WithMihomoInputDir(dir string) lib.InputOption {
+ return func(m lib.InputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" && (strings.TrimSpace(m.(*mrs_in).Name) == "" || strings.TrimSpace(m.(*mrs_in).URI) == "") {
+ log.Fatalf("❌ [type %s | action %s] missing name or uri or inputDir", TypeMRSIn, m.(*mrs_in).Action)
+ }
+
+ m.(*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 WithMihomoInputWantedList(lists []string) lib.InputOption {
+ return func(m lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
}
+
+ m.(*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 WithMihomoInputOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(m lib.InputConverter) {
+ m.(*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,
+ WithMihomoNameAndURI(tmp.Name, tmp.URI),
+ WithMihomoInputDir(tmp.InputDir),
+ WithMihomoInputWantedList(tmp.Want),
+ WithMihomoInputOnlyIPType(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..71691b51 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 WithMihomoOutputDir(dir string) lib.OutputOption {
+ return func(m lib.OutputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" {
+ dir = defaultOutputDir
+ }
+
+ m.(*mrs_out).OutputDir = dir
+ }
+}
+
+func WithMihomoOutputWantedList(lists []string) lib.OutputOption {
+ return func(m lib.OutputConverter) {
+ m.(*mrs_out).Want = lists
+ }
+}
+
+func WithMihomoOutputExcludedList(lists []string) lib.OutputOption {
+ return func(m lib.OutputConverter) {
+ m.(*mrs_out).Exclude = lists
+ }
+}
+
+func WithMihomoOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
+ return func(m lib.OutputConverter) {
+ m.(*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,
+ WithMihomoOutputDir(tmp.OutputDir),
+ WithMihomoOutputWantedList(tmp.Want),
+ WithMihomoOutputExcludedList(tmp.Exclude),
+ WithMihomoOutputOnlyIPType(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
diff --git a/plugin/v2ray/dat_in.go b/plugin/v2ray/dat_in.go
index 33a24061..2d73ff97 100644
--- a/plugin/v2ray/dat_in.go
+++ b/plugin/v2ray/dat_in.go
@@ -9,6 +9,8 @@ import (
"os"
"strings"
+ "log"
+
"github.com/Loyalsoldier/geoip/lib"
"google.golang.org/protobuf/proto"
)
@@ -20,70 +22,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 WithV2rayURI(uri string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ uri = strings.TrimSpace(uri)
+ if uri == "" {
+ log.Fatalf("❌ [type %s | action %s] missing uri", 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 WithV2rayInputWantedList(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 WithV2rayInputOnlyIPType(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,
+ WithV2rayURI(tmp.URI),
+ WithV2rayInputWantedList(tmp.Want),
+ WithV2rayInputOnlyIPType(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 +156,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 +170,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 +188,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..9f6c7262 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 WithV2rayOutputName(name string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ name = strings.TrimSpace(name)
+ if name == "" {
+ name = defaultOutputName
+ }
+
+ g.(*geoIPDatOut).OutputName = name
+ }
+}
+
+func WithV2rayOutputDir(dir string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" {
+ dir = defaultOutputDir
+ }
+
+ g.(*geoIPDatOut).OutputDir = dir
+ }
+}
+
+func WithV2rayOutputWantedList(lists []string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoIPDatOut).Want = lists
+ }
+}
+
+func WithV2rayOutputExcludedList(lists []string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoIPDatOut).Exclude = lists
+ }
+}
+
+func WithV2rayOneFilePerList(oneFilePerList bool) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoIPDatOut).OneFilePerList = oneFilePerList
+ }
+}
+
+func WithV2rayOutputOnlyIPType(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,
+ WithV2rayOutputName(tmp.OutputName),
+ WithV2rayOutputDir(tmp.OutputDir),
+ WithV2rayOutputWantedList(tmp.Want),
+ WithV2rayOutputExcludedList(tmp.Exclude),
+ WithV2rayOneFilePerList(tmp.OneFilePerList),
+ WithV2rayOutputOnlyIPType(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
}