Skip to content

Commit

Permalink
add clash rule-set support, from v2ray only
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Nov 15, 2023
1 parent 30080c4 commit 2825b6f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ Available types:

Supported conversion pairs: (Column=From, Row=To)

| | MaxMind | V2Ray-geoip | sing-geoip | Meta-geoip|
|------------|:-------:|:-----------:|:----------:|:---------:|
| MaxMind | - | | | |
| V2Ray | | - | | |
| sing-geoip | | | - | |
| Meta-geoip | | | | - |
| | MaxMind | V2Ray-geoip | sing-geoip | Meta-geoip |
| ---------- | :-----: | :---------: | :--------: | :--------: |
| MaxMind | - | | | |
| V2Ray | | - | | |
| sing-geoip | | | - | |
| Meta-geoip | | | | - |

Conversion to MaxMind is not available for legal reasons.
Conversion to V2Ray is on the TODO list.

#### Site

```shell
geo convert site -i <input_type> -o <output_type> -f [output_filename] input_filename
geo convert site -i <input_type> -o <output_type> -f [output_filename] -c [country code] input_filename
```

```shell
Expand Down
1 change: 1 addition & 0 deletions cmd/geo/internal/convert/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ var (
fromType string
toType string
output string
code string
)
8 changes: 7 additions & 1 deletion cmd/geo/internal/convert/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func init() {
CommandSite.PersistentFlags().StringVarP(&fromType, "from-type", "i", "", "specify input database type")
CommandSite.PersistentFlags().StringVarP(&toType, "to-type", "o", "meta", "set output database type")
CommandSite.PersistentFlags().StringVarP(&output, "output-name", "f", "", "specify output filename")
CommandSite.PersistentFlags().StringVarP(&code, "code", "c", "", "specify output code")
}

var CommandSite = &cobra.Command{
Expand Down Expand Up @@ -55,7 +56,12 @@ func site(cmd *cobra.Command, args []string) error {
return err
}
filename += ".db"

case "clash":
err = convert.V2RayToYamlByCode(geositeList, &buffer, code)
if err != nil {
return err
}
filename += ".yaml"
default:
return E.New("unsupported output GeoSite database type: ", toType)
}
Expand Down
46 changes: 46 additions & 0 deletions convert/v2ray_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package convert

import (
"io"
"sort"
"strings"

"github.com/metacubex/geo/encoding/clashrule"
"github.com/metacubex/geo/encoding/singgeo"
"github.com/metacubex/geo/encoding/v2raygeo"

"github.com/sagernet/sing/common"
"gopkg.in/yaml.v3"
)

// V2RaySiteToSing is modified from https://github.com/SagerNet/sing-geosite
Expand Down Expand Up @@ -90,3 +93,46 @@ func V2RaySiteToSing(geositeList []*v2raygeo.GeoSite, output io.Writer) error {
}
return singgeo.Write(output, domainMap)
}

func V2RayToYamlByCode(geositeList []*v2raygeo.GeoSite, output io.Writer, targetCode string) error {
domainMap := make(map[string][]string, len(geositeList))
for _, vGeositeEntry := range geositeList {
code := strings.ToLower(vGeositeEntry.CountryCode)
if strings.EqualFold(code, targetCode) {
domains := make([]string, 0, len(vGeositeEntry.Domain)*2)
attributes := make(map[string][]*v2raygeo.Domain)
for _, domain := range vGeositeEntry.Domain {
if len(domain.Attribute) > 0 {
for _, attribute := range domain.Attribute {
attributes[attribute.Key] = append(attributes[attribute.Key], domain)
}
}
ruleType := clashrule.GetClashRule(domain.Type)
switch domain.Type {
case v2raygeo.Domain_Plain:
domains = append(domains, ruleType+domain.Value)
case v2raygeo.Domain_Domain:
domains = append(domains, ruleType+domain.Value)
case v2raygeo.Domain_Full:
domains = append(domains, ruleType+domain.Value)
case v2raygeo.Domain_Regex:
continue
}
}
sort.Strings(domains)
domainMap[targetCode] = common.Uniq(domains)
}

}

yamlOutput := map[string]interface{}{
"payload": domainMap[targetCode],
}
yamlBytes, err := yaml.Marshal(yamlOutput)
if err != nil {
return err
}
_, err = output.Write(yamlBytes)
return err

}
18 changes: 18 additions & 0 deletions encoding/clashrule/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package clashrule

import "github.com/metacubex/geo/encoding/v2raygeo"

func GetClashRule(from v2raygeo.Domain_Type) string {

switch from {
case v2raygeo.Domain_Plain:
return "DOMAIN-KEYWORD,"
case v2raygeo.Domain_Full:
return "DOMAIN,"
case v2raygeo.Domain_Domain:
return "DOMAIN-SUFFIX,"
default:
return ""
}

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/sagernet/sing v0.2.17
github.com/spf13/cobra v1.8.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
14 changes: 1 addition & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/maxmind/mmdbwriter v0.0.0-20230619164437-bb691ac3530c h1:0nrmOQhyoZ/qSRAhK+daN3UqxKtPYQbFneGHtqNlT3Y=
github.com/maxmind/mmdbwriter v0.0.0-20230619164437-bb691ac3530c/go.mod h1:1RuJQ3t4j6AAczNy9GfoA1U8glYwwIwrNh5WHXatOPE=
github.com/maxmind/mmdbwriter v1.0.0 h1:bieL4P6yaYaHvbtLSwnKtEvScUKKD6jcKaLiTM3WSMw=
github.com/maxmind/mmdbwriter v1.0.0/go.mod h1:noBMCUtyN5PUQ4H8ikkOvGSHhzhLok51fON2hcrpKj8=
github.com/oschwald/maxminddb-golang v1.11.0 h1:aSXMqYR/EPNjGE8epgqwDay+P30hCBZIveY0WZbAWh0=
github.com/oschwald/maxminddb-golang v1.11.0/go.mod h1:YmVI+H0zh3ySFR3w+oz8PCfglAFj3PuCmui13+P9zDg=
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagernet/sing v0.2.7 h1:cOy0FfPS8q7m0aJ51wS7LRQAGc9wF+fWhHtBDj99wy8=
github.com/sagernet/sing v0.2.7/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
github.com/sagernet/sing v0.2.17 h1:vMPKb3MV0Aa5ws4dCJkRI8XEjrsUcDn810czd0FwmzI=
github.com/sagernet/sing v0.2.17/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d h1:ggxwEf5eu0l8v+87VhX1czFh8zJul3hK16Gmruxn7hw=
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d/go.mod h1:tgPU4N2u9RByaTN3NC2p9xOzyFpte4jYwsIIRF7XlSc=
go4.org/netipx v0.0.0-20230824141953-6213f710f925 h1:eeQDDVKFkx0g4Hyy8pHgmZaK0EqB4SD6rvKbUdN3ziQ=
go4.org/netipx v0.0.0-20230824141953-6213f710f925/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 2825b6f

Please sign in to comment.