Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 60 additions & 71 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,38 +163,35 @@ func getInputForLookup(format, name, uri, dir string) lib.InputConverter {

switch strings.ToLower(format) {
case strings.ToLower(maxmind.TypeGeoLite2CountryMMDBIn):
input = &maxmind.GeoLite2CountryMMDBIn{
Type: maxmind.TypeGeoLite2CountryMMDBIn,
Action: lib.ActionAdd,
Description: maxmind.DescGeoLite2CountryMMDBIn,
URI: uri,
}
input = maxmind.NewGeoLite2CountryMMDBIn(
maxmind.TypeGeoLite2CountryMMDBIn,
maxmind.DescGeoLite2CountryMMDBIn,
lib.ActionAdd,
maxmind.WithURI(uri),
)

case strings.ToLower(maxmind.TypeDBIPCountryMMDBIn):
input = &maxmind.GeoLite2CountryMMDBIn{
Type: maxmind.TypeDBIPCountryMMDBIn,
Action: lib.ActionAdd,
Description: maxmind.DescDBIPCountryMMDBIn,
URI: uri,
}
input = maxmind.NewGeoLite2CountryMMDBIn(
maxmind.TypeDBIPCountryMMDBIn,
maxmind.DescDBIPCountryMMDBIn,
lib.ActionAdd,
maxmind.WithURI(uri),
)

case strings.ToLower(maxmind.TypeIPInfoCountryMMDBIn):
input = &maxmind.GeoLite2CountryMMDBIn{
Type: maxmind.TypeIPInfoCountryMMDBIn,
Action: lib.ActionAdd,
Description: maxmind.DescIPInfoCountryMMDBIn,
URI: uri,
}
input = maxmind.NewGeoLite2CountryMMDBIn(
maxmind.TypeIPInfoCountryMMDBIn,
maxmind.DescIPInfoCountryMMDBIn,
lib.ActionAdd,
maxmind.WithURI(uri),
)

case strings.ToLower(mihomo.TypeMRSIn):
input = &mihomo.MRSIn{
Type: mihomo.TypeMRSIn,
Action: lib.ActionAdd,
Description: mihomo.DescMRSIn,
Name: name,
URI: uri,
InputDir: dir,
}
input = mihomo.NewMRSIn(
lib.ActionAdd,
mihomo.WithNameAndURI(name, uri),
mihomo.WithInputDir(dir),
)

case strings.ToLower(singbox.TypeSRSIn):
input = singbox.NewSRSIn(
Expand All @@ -204,52 +201,46 @@ func getInputForLookup(format, name, uri, dir string) lib.InputConverter {
)

case strings.ToLower(v2ray.TypeGeoIPDatIn):
input = &v2ray.GeoIPDatIn{
Type: v2ray.TypeGeoIPDatIn,
Action: lib.ActionAdd,
Description: v2ray.DescGeoIPDatIn,
URI: uri,
}
input = v2ray.NewGeoIPDatIn(
lib.ActionAdd,
v2ray.WithURI(uri),
)

case strings.ToLower(plaintext.TypeTextIn):
input = &plaintext.TextIn{
Type: plaintext.TypeTextIn,
Action: lib.ActionAdd,
Description: plaintext.DescTextIn,
Name: name,
URI: uri,
InputDir: dir,
}
input = plaintext.NewTextIn(
plaintext.TypeTextIn,
plaintext.DescTextIn,
lib.ActionAdd,
plaintext.WithNameAndURI(name, uri),
plaintext.WithInputDir(dir),
)

case strings.ToLower(plaintext.TypeClashRuleSetIPCIDRIn):
input = &plaintext.TextIn{
Type: plaintext.TypeClashRuleSetIPCIDRIn,
Action: lib.ActionAdd,
Description: plaintext.DescClashRuleSetIPCIDRIn,
Name: name,
URI: uri,
InputDir: dir,
}
input = plaintext.NewTextIn(
plaintext.TypeClashRuleSetIPCIDRIn,
plaintext.DescClashRuleSetIPCIDRIn,
lib.ActionAdd,
plaintext.WithNameAndURI(name, uri),
plaintext.WithInputDir(dir),
)

case strings.ToLower(plaintext.TypeClashRuleSetClassicalIn):
input = &plaintext.TextIn{
Type: plaintext.TypeClashRuleSetClassicalIn,
Action: lib.ActionAdd,
Description: plaintext.DescClashRuleSetClassicalIn,
Name: name,
URI: uri,
InputDir: dir,
}
input = plaintext.NewTextIn(
plaintext.TypeClashRuleSetClassicalIn,
plaintext.DescClashRuleSetClassicalIn,
lib.ActionAdd,
plaintext.WithNameAndURI(name, uri),
plaintext.WithInputDir(dir),
)

case strings.ToLower(plaintext.TypeSurgeRuleSetIn):
input = &plaintext.TextIn{
Type: plaintext.TypeSurgeRuleSetIn,
Action: lib.ActionAdd,
Description: plaintext.DescSurgeRuleSetIn,
Name: name,
URI: uri,
InputDir: dir,
}
input = plaintext.NewTextIn(
plaintext.TypeSurgeRuleSetIn,
plaintext.DescSurgeRuleSetIn,
lib.ActionAdd,
plaintext.WithNameAndURI(name, uri),
plaintext.WithInputDir(dir),
)

default:
log.Fatal("unsupported input format")
Expand All @@ -259,11 +250,9 @@ func getInputForLookup(format, name, uri, dir string) lib.InputConverter {
}

func getOutputForLookup(search string, searchList ...string) lib.OutputConverter {
return &special.Lookup{
Type: special.TypeLookup,
Action: lib.ActionOutput,
Description: special.DescLookup,
Search: search,
SearchList: searchList,
}
return special.NewLookup(
lib.ActionOutput,
special.WithSearch(search),
special.WithSearchList(searchList),
)
}
38 changes: 15 additions & 23 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,30 @@ var mergeCmd = &cobra.Command{
}

func getInputForMerge() lib.InputConverter {
return &special.Stdin{
Type: special.TypeStdin,
Action: lib.ActionAdd,
Description: special.DescStdin,
Name: "temp",
}
return special.NewStdin(
lib.ActionAdd,
special.WithStdinName("temp"),
)
}

func getOutputForMerge(otype string) lib.OutputConverter {
switch lib.IPType(otype) {
case lib.IPv4:
return &special.Stdout{
Type: special.TypeStdout,
Action: lib.ActionOutput,
Description: special.DescStdout,
OnlyIPType: lib.IPv4,
}
return special.NewStdout(
lib.ActionOutput,
special.WithStdoutOnlyIPType(lib.IPv4),
)

case lib.IPv6:
return &special.Stdout{
Type: special.TypeStdout,
Action: lib.ActionOutput,
Description: special.DescStdout,
OnlyIPType: lib.IPv6,
}
return special.NewStdout(
lib.ActionOutput,
special.WithStdoutOnlyIPType(lib.IPv6),
)

default:
return &special.Stdout{
Type: special.TypeStdout,
Action: lib.ActionOutput,
Description: special.DescStdout,
}
return special.NewStdout(
lib.ActionOutput,
)
}

}
25 changes: 7 additions & 18 deletions plugin/maxmind/common_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package maxmind
import (
"encoding/json"
"path/filepath"
"strings"

"github.com/Loyalsoldier/geoip/lib"
)
Expand All @@ -14,7 +13,7 @@ var (
defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
)

func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
func NewGeoLite2CountryMMDBInFromBytes(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
URI string `json:"uri"`
Want []string `json:"wantedList"`
Expand All @@ -40,20 +39,10 @@ func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, dat
}
}

// 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 &GeoLite2CountryMMDBIn{
Type: iType,
Action: action,
Description: iDesc,
URI: tmp.URI,
Want: wantList,
OnlyIPType: tmp.OnlyIPType,
}, nil
return NewGeoLite2CountryMMDBIn(
iType, iDesc, action,
WithURI(tmp.URI),
WithInputWantedList(tmp.Want),
WithInputOnlyIPType(tmp.OnlyIPType),
), nil
}
44 changes: 12 additions & 32 deletions plugin/maxmind/common_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (d dbipCountry) HasData() bool {
return d != zeroDBIPCountry
}

func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
func NewGeoLite2CountryMMDBOutFromBytes(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
var tmp struct {
OutputName string `json:"outputName"`
OutputDir string `json:"outputDir"`
Expand All @@ -118,39 +118,19 @@ func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, da
}
}

if tmp.OutputName == "" {
tmp.OutputName = defaultGeoLite2CountryMMDBOutputName
}

if tmp.OutputDir == "" {
switch iType {
case TypeGeoLite2CountryMMDBOut:
tmp.OutputDir = defaultMaxmindOutputDir

case TypeDBIPCountryMMDBOut:
tmp.OutputDir = defaultDBIPOutputDir

case TypeIPInfoCountryMMDBOut:
tmp.OutputDir = defaultIPInfoOutputDir
}
}

return &GeoLite2CountryMMDBOut{
Type: iType,
Action: action,
Description: iDesc,
OutputName: tmp.OutputName,
OutputDir: tmp.OutputDir,
Want: tmp.Want,
Overwrite: tmp.Overwrite,
Exclude: tmp.Exclude,
OnlyIPType: tmp.OnlyIPType,

SourceMMDBURI: tmp.SourceMMDBURI,
}, nil
return NewGeoLite2CountryMMDBOut(
iType, iDesc, action,
WithOutputName(tmp.OutputName),
WithOutputDir(tmp.OutputDir, iType),
WithOutputWantedList(tmp.Want),
WithOutputOverwriteList(tmp.Overwrite),
WithOutputExcludedList(tmp.Exclude),
WithOutputOnlyIPType(tmp.OnlyIPType),
WithSourceMMDBURI(tmp.SourceMMDBURI),
), nil
}

func (g *GeoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
func (g *geoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
if strings.TrimSpace(g.SourceMMDBURI) == "" {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/maxmind/dbip_country_mmdb_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (

func init() {
lib.RegisterInputConfigCreator(TypeDBIPCountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
return newGeoLite2CountryMMDBIn(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
return NewGeoLite2CountryMMDBInFromBytes(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
})
lib.RegisterInputConverter(TypeDBIPCountryMMDBIn, &GeoLite2CountryMMDBIn{
lib.RegisterInputConverter(TypeDBIPCountryMMDBIn, &geoLite2CountryMMDBIn{
Description: DescDBIPCountryMMDBIn,
})
}
4 changes: 2 additions & 2 deletions plugin/maxmind/dbip_country_mmdb_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (

func init() {
lib.RegisterOutputConfigCreator(TypeDBIPCountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
return newGeoLite2CountryMMDBOut(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data)
return NewGeoLite2CountryMMDBOutFromBytes(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data)
})
lib.RegisterOutputConverter(TypeDBIPCountryMMDBOut, &GeoLite2CountryMMDBOut{
lib.RegisterOutputConverter(TypeDBIPCountryMMDBOut, &geoLite2CountryMMDBOut{
Description: DescDBIPCountryMMDBOut,
})
}
4 changes: 2 additions & 2 deletions plugin/maxmind/ipinfo_country_mmdb_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (

func init() {
lib.RegisterInputConfigCreator(TypeIPInfoCountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
return newGeoLite2CountryMMDBIn(TypeIPInfoCountryMMDBIn, DescIPInfoCountryMMDBIn, action, data)
return NewGeoLite2CountryMMDBInFromBytes(TypeIPInfoCountryMMDBIn, DescIPInfoCountryMMDBIn, action, data)
})
lib.RegisterInputConverter(TypeIPInfoCountryMMDBIn, &GeoLite2CountryMMDBIn{
lib.RegisterInputConverter(TypeIPInfoCountryMMDBIn, &geoLite2CountryMMDBIn{
Description: DescIPInfoCountryMMDBIn,
})
}
4 changes: 2 additions & 2 deletions plugin/maxmind/ipinfo_country_mmdb_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (

func init() {
lib.RegisterOutputConfigCreator(TypeIPInfoCountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
return newGeoLite2CountryMMDBOut(TypeIPInfoCountryMMDBOut, DescIPInfoCountryMMDBOut, action, data)
return NewGeoLite2CountryMMDBOutFromBytes(TypeIPInfoCountryMMDBOut, DescIPInfoCountryMMDBOut, action, data)
})
lib.RegisterOutputConverter(TypeIPInfoCountryMMDBOut, &GeoLite2CountryMMDBOut{
lib.RegisterOutputConverter(TypeIPInfoCountryMMDBOut, &geoLite2CountryMMDBOut{
Description: DescIPInfoCountryMMDBOut,
})
}
Loading