summaryrefslogtreecommitdiff
path: root/plugin/v2ray/dat_in.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/v2ray/dat_in.go')
-rw-r--r--plugin/v2ray/dat_in.go111
1 files changed, 72 insertions, 39 deletions
diff --git a/plugin/v2ray/dat_in.go b/plugin/v2ray/dat_in.go
index 33a24061..dede0165 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 WithGeoIPDatInURI(uri string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ uri = strings.TrimSpace(uri)
+ if uri == "" {
+ log.Fatalf("❌ [type %s | action %s] uri must be specified in config", TypeGeoIPDatIn, s.(*geoIPDatIn).Action)
+ }
+
+ s.(*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 WithGeoIPDatInWantedList(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.(*geoIPDatIn).Want = wantList
}
+}
- return &GeoIPDatIn{
- Type: TypeGeoIPDatIn,
- Action: action,
- Description: DescGeoIPDatIn,
- URI: tmp.URI,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+func WithGeoIPDatInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*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,
+ WithGeoIPDatInURI(tmp.URI),
+ WithGeoIPDatInWantedList(tmp.Want),
+ WithGeoIPDatInOnlyIPType(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