diff options
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..c361bcd4 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 WithGeoIPDatOutOutputName(name string) lib.OutputOption { + return func(s lib.OutputConverter) { + name = strings.TrimSpace(name) + if name == "" { + name = defaultOutputName + } + + s.(*geoIPDatOut).OutputName = name + } +} + +func WithGeoIPDatOutOutputDir(dir string) lib.OutputOption { + return func(s lib.OutputConverter) { + dir = strings.TrimSpace(dir) + if dir == "" { + dir = defaultOutputDir + } + + s.(*geoIPDatOut).OutputDir = dir + } +} + +func WithGeoIPDatOutWantedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoIPDatOut).Want = lists + } +} + +func WithGeoIPDatOutExcludedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoIPDatOut).Exclude = lists + } +} + +func WithGeoIPDatOutOneFilePerList(oneFilePerList bool) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoIPDatOut).OneFilePerList = oneFilePerList + } +} + +func WithGeoIPDatOutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*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, + WithGeoIPDatOutOutputName(tmp.OutputName), + WithGeoIPDatOutOutputDir(tmp.OutputDir), + WithGeoIPDatOutWantedList(tmp.Want), + WithGeoIPDatOutExcludedList(tmp.Exclude), + WithGeoIPDatOutOneFilePerList(tmp.OneFilePerList), + WithGeoIPDatOutOnlyIPType(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 } |
