1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-14 17:49:15 -04:00

refactor common router structure into separate package

This commit is contained in:
Shelikhoo
2021-09-07 09:13:58 +01:00
parent fd63ad7160
commit 68f28b1913
17 changed files with 1323 additions and 1192 deletions

View File

@@ -3,14 +3,14 @@ package geodata
import (
"strings"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
)
type AttributeList struct {
matcher []AttributeMatcher
}
func (al *AttributeList) Match(domain *router.Domain) bool {
func (al *AttributeList) Match(domain *routercommon.Domain) bool {
for _, matcher := range al.matcher {
if !matcher.Match(domain) {
return false
@@ -36,12 +36,12 @@ func parseAttrs(attrs []string) *AttributeList {
}
type AttributeMatcher interface {
Match(*router.Domain) bool
Match(*routercommon.Domain) bool
}
type BooleanMatcher string
func (m BooleanMatcher) Match(domain *router.Domain) bool {
func (m BooleanMatcher) Match(domain *routercommon.Domain) bool {
for _, attr := range domain.Attribute {
if strings.EqualFold(attr.GetKey(), string(m)) {
return true

View File

@@ -1,20 +1,19 @@
package geodata
import (
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"strings"
"github.com/v2fly/v2ray-core/v4/app/router"
)
type loader struct {
LoaderImplementation
}
func (l *loader) LoadGeoSite(list string) ([]*router.Domain, error) {
func (l *loader) LoadGeoSite(list string) ([]*routercommon.Domain, error) {
return l.LoadGeoSiteWithAttr("geosite.dat", list)
}
func (l *loader) LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) {
func (l *loader) LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*routercommon.Domain, error) {
parts := strings.Split(siteWithAttr, "@")
if len(parts) == 0 {
return nil, newError("empty rule")
@@ -39,7 +38,7 @@ func (l *loader) LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*route
return domains, nil
}
filteredDomains := make([]*router.Domain, 0, len(domains))
filteredDomains := make([]*routercommon.Domain, 0, len(domains))
hasAttrMatched := false
for _, domain := range domains {
if attrs.Match(domain) {
@@ -54,7 +53,7 @@ func (l *loader) LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*route
return filteredDomains, nil
}
func (l *loader) LoadGeoIP(country string) ([]*router.CIDR, error) {
func (l *loader) LoadGeoIP(country string) ([]*routercommon.CIDR, error) {
return l.LoadIP("geoip.dat", country)
}

View File

@@ -1,17 +1,19 @@
package geodata
import "github.com/v2fly/v2ray-core/v4/app/router"
import (
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
)
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
type LoaderImplementation interface {
LoadSite(filename, list string) ([]*router.Domain, error)
LoadIP(filename, country string) ([]*router.CIDR, error)
LoadSite(filename, list string) ([]*routercommon.Domain, error)
LoadIP(filename, country string) ([]*routercommon.CIDR, error)
}
type Loader interface {
LoaderImplementation
LoadGeoSite(list string) ([]*router.Domain, error)
LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*router.Domain, error)
LoadGeoIP(country string) ([]*router.CIDR, error)
LoadGeoSite(list string) ([]*routercommon.Domain, error)
LoadGeoSiteWithAttr(file string, siteWithAttr string) ([]*routercommon.Domain, error)
LoadGeoIP(country string) ([]*routercommon.CIDR, error)
}

View File

@@ -1,36 +1,36 @@
package memconservative
import (
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"io/ioutil"
"strings"
"google.golang.org/protobuf/proto"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/common/platform"
)
type GeoIPCache map[string]*router.GeoIP
type GeoIPCache map[string]*routercommon.GeoIP
func (g GeoIPCache) Has(key string) bool {
return !(g.Get(key) == nil)
}
func (g GeoIPCache) Get(key string) *router.GeoIP {
func (g GeoIPCache) Get(key string) *routercommon.GeoIP {
if g == nil {
return nil
}
return g[key]
}
func (g GeoIPCache) Set(key string, value *router.GeoIP) {
func (g GeoIPCache) Set(key string, value *routercommon.GeoIP) {
if g == nil {
g = make(map[string]*router.GeoIP)
g = make(map[string]*routercommon.GeoIP)
}
g[key] = value
}
func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
func (g GeoIPCache) Unmarshal(filename, code string) (*routercommon.GeoIP, error) {
asset := platform.GetAssetLocation(filename)
idx := strings.ToLower(asset + ":" + code)
if g.Has(idx) {
@@ -40,7 +40,7 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
geoipBytes, err := Decode(asset, code)
switch err {
case nil:
var geoip router.GeoIP
var geoip routercommon.GeoIP
if err := proto.Unmarshal(geoipBytes, &geoip); err != nil {
return nil, err
}
@@ -57,7 +57,7 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
if err != nil {
return nil, err
}
var geoipList router.GeoIPList
var geoipList routercommon.GeoIPList
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
return nil, err
}
@@ -75,27 +75,27 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
return nil, newError("country code ", code, " not found in ", filename)
}
type GeoSiteCache map[string]*router.GeoSite
type GeoSiteCache map[string]*routercommon.GeoSite
func (g GeoSiteCache) Has(key string) bool {
return !(g.Get(key) == nil)
}
func (g GeoSiteCache) Get(key string) *router.GeoSite {
func (g GeoSiteCache) Get(key string) *routercommon.GeoSite {
if g == nil {
return nil
}
return g[key]
}
func (g GeoSiteCache) Set(key string, value *router.GeoSite) {
func (g GeoSiteCache) Set(key string, value *routercommon.GeoSite) {
if g == nil {
g = make(map[string]*router.GeoSite)
g = make(map[string]*routercommon.GeoSite)
}
g[key] = value
}
func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error) {
func (g GeoSiteCache) Unmarshal(filename, code string) (*routercommon.GeoSite, error) {
asset := platform.GetAssetLocation(filename)
idx := strings.ToLower(asset + ":" + code)
if g.Has(idx) {
@@ -105,7 +105,7 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
geositeBytes, err := Decode(asset, code)
switch err {
case nil:
var geosite router.GeoSite
var geosite routercommon.GeoSite
if err := proto.Unmarshal(geositeBytes, &geosite); err != nil {
return nil, err
}
@@ -122,7 +122,7 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
if err != nil {
return nil, err
}
var geositeList router.GeoSiteList
var geositeList routercommon.GeoSiteList
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
return nil, err
}

View File

@@ -1,9 +1,9 @@
package memconservative
import (
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"runtime"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
)
@@ -14,7 +14,7 @@ type memConservativeLoader struct {
geositecache GeoSiteCache
}
func (m *memConservativeLoader) LoadIP(filename, country string) ([]*router.CIDR, error) {
func (m *memConservativeLoader) LoadIP(filename, country string) ([]*routercommon.CIDR, error) {
defer runtime.GC()
geoip, err := m.geoipcache.Unmarshal(filename, country)
if err != nil {
@@ -23,7 +23,7 @@ func (m *memConservativeLoader) LoadIP(filename, country string) ([]*router.CIDR
return geoip.Cidr, nil
}
func (m *memConservativeLoader) LoadSite(filename, list string) ([]*router.Domain, error) {
func (m *memConservativeLoader) LoadSite(filename, list string) ([]*routercommon.Domain, error) {
defer runtime.GC()
geosite, err := m.geositecache.Unmarshal(filename, list)
if err != nil {
@@ -33,7 +33,7 @@ func (m *memConservativeLoader) LoadSite(filename, list string) ([]*router.Domai
}
func newMemConservativeLoader() geodata.LoaderImplementation {
return &memConservativeLoader{make(map[string]*router.GeoIP), make(map[string]*router.GeoSite)}
return &memConservativeLoader{make(map[string]*routercommon.GeoIP), make(map[string]*routercommon.GeoSite)}
}
func init() {

View File

@@ -1,23 +1,23 @@
package standard
import (
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"strings"
"google.golang.org/protobuf/proto"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
)
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
func loadIP(filename, country string) ([]*router.CIDR, error) {
func loadIP(filename, country string) ([]*routercommon.CIDR, error) {
geoipBytes, err := filesystem.ReadAsset(filename)
if err != nil {
return nil, newError("failed to open file: ", filename).Base(err)
}
var geoipList router.GeoIPList
var geoipList routercommon.GeoIPList
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
return nil, err
}
@@ -31,12 +31,12 @@ func loadIP(filename, country string) ([]*router.CIDR, error) {
return nil, newError("country not found in ", filename, ": ", country)
}
func loadSite(filename, list string) ([]*router.Domain, error) {
func loadSite(filename, list string) ([]*routercommon.Domain, error) {
geositeBytes, err := filesystem.ReadAsset(filename)
if err != nil {
return nil, newError("failed to open file: ", filename).Base(err)
}
var geositeList router.GeoSiteList
var geositeList routercommon.GeoSiteList
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
return nil, err
}
@@ -52,11 +52,11 @@ func loadSite(filename, list string) ([]*router.Domain, error) {
type standardLoader struct{}
func (d standardLoader) LoadSite(filename, list string) ([]*router.Domain, error) {
func (d standardLoader) LoadSite(filename, list string) ([]*routercommon.Domain, error) {
return loadSite(filename, list)
}
func (d standardLoader) LoadIP(filename, country string) ([]*router.CIDR, error) {
func (d standardLoader) LoadIP(filename, country string) ([]*routercommon.CIDR, error) {
return loadIP(filename, country)
}

View File

@@ -3,6 +3,7 @@ package rule
import (
"context"
"encoding/json"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"strconv"
"strings"
@@ -13,7 +14,7 @@ import (
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
func parseDomainRule(ctx context.Context, domain string) ([]*router.Domain, error) {
func parseDomainRule(ctx context.Context, domain string) ([]*routercommon.Domain, error) {
cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(ctx)
geoLoader := cfgEnv.GetGeoLoader()
@@ -57,14 +58,14 @@ func parseDomainRule(ctx context.Context, domain string) ([]*router.Domain, erro
return domains, nil
}
domainRule := new(router.Domain)
domainRule := new(routercommon.Domain)
switch {
case strings.HasPrefix(domain, "regexp:"):
regexpVal := domain[7:]
if len(regexpVal) == 0 {
return nil, newError("empty regexp type of rule: ", domain)
}
domainRule.Type = router.Domain_Regex
domainRule.Type = routercommon.Domain_Regex
domainRule.Value = regexpVal
case strings.HasPrefix(domain, "domain:"):
@@ -72,7 +73,7 @@ func parseDomainRule(ctx context.Context, domain string) ([]*router.Domain, erro
if len(domainName) == 0 {
return nil, newError("empty domain type of rule: ", domain)
}
domainRule.Type = router.Domain_RootDomain
domainRule.Type = routercommon.Domain_RootDomain
domainRule.Value = domainName
case strings.HasPrefix(domain, "full:"):
@@ -80,7 +81,7 @@ func parseDomainRule(ctx context.Context, domain string) ([]*router.Domain, erro
if len(fullVal) == 0 {
return nil, newError("empty full domain type of rule: ", domain)
}
domainRule.Type = router.Domain_Full
domainRule.Type = routercommon.Domain_Full
domainRule.Value = fullVal
case strings.HasPrefix(domain, "keyword:"):
@@ -88,11 +89,11 @@ func parseDomainRule(ctx context.Context, domain string) ([]*router.Domain, erro
if len(keywordVal) == 0 {
return nil, newError("empty keyword type of rule: ", domain)
}
domainRule.Type = router.Domain_Plain
domainRule.Type = routercommon.Domain_Plain
domainRule.Value = keywordVal
case strings.HasPrefix(domain, "dotless:"):
domainRule.Type = router.Domain_Regex
domainRule.Type = routercommon.Domain_Regex
switch substr := domain[8:]; {
case substr == "":
domainRule.Value = "^[^.]*$"
@@ -103,18 +104,18 @@ func parseDomainRule(ctx context.Context, domain string) ([]*router.Domain, erro
}
default:
domainRule.Type = router.Domain_Plain
domainRule.Type = routercommon.Domain_Plain
domainRule.Value = domain
}
return []*router.Domain{domainRule}, nil
return []*routercommon.Domain{domainRule}, nil
}
func toCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*router.GeoIP, error) {
func toCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*routercommon.GeoIP, error) {
cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(ctx)
geoLoader := cfgEnv.GetGeoLoader()
var geoipList []*router.GeoIP
var customCidrs []*router.CIDR
var geoipList []*routercommon.GeoIP
var customCidrs []*routercommon.CIDR
for _, ip := range ips {
if strings.HasPrefix(ip, "geoip:") {
@@ -132,7 +133,7 @@ func toCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*router.GeoIP,
return nil, newError("failed to load geoip: ", country).Base(err)
}
geoipList = append(geoipList, &router.GeoIP{
geoipList = append(geoipList, &routercommon.GeoIP{
CountryCode: strings.ToUpper(country),
Cidr: geoip,
InverseMatch: isReverseMatch,
@@ -175,7 +176,7 @@ func toCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*router.GeoIP,
return nil, newError("failed to load geoip: ", country, " from ", filename).Base(err)
}
geoipList = append(geoipList, &router.GeoIP{
geoipList = append(geoipList, &routercommon.GeoIP{
CountryCode: strings.ToUpper(filename + "_" + country),
Cidr: geoip,
InverseMatch: isInverseMatch,
@@ -192,7 +193,7 @@ func toCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*router.GeoIP,
}
if len(customCidrs) > 0 {
geoipList = append(geoipList, &router.GeoIP{
geoipList = append(geoipList, &routercommon.GeoIP{
Cidr: customCidrs,
})
}
@@ -329,7 +330,7 @@ func ParseRule(ctx context.Context, msg json.RawMessage) (*router.RoutingRule, e
return nil, newError("unknown router rule type: ", rawRule.Type)
}
func ParseIP(s string) (*router.CIDR, error) {
func ParseIP(s string) (*routercommon.CIDR, error) {
var addr, mask string
i := strings.Index(s, "/")
if i < 0 {
@@ -352,7 +353,7 @@ func ParseIP(s string) (*router.CIDR, error) {
if bits > 32 {
return nil, newError("invalid network mask for router: ", bits)
}
return &router.CIDR{
return &routercommon.CIDR{
Ip: []byte(ip.IP()),
Prefix: bits,
}, nil
@@ -368,7 +369,7 @@ func ParseIP(s string) (*router.CIDR, error) {
if bits > 128 {
return nil, newError("invalid network mask for router: ", bits)
}
return &router.CIDR{
return &routercommon.CIDR{
Ip: []byte(ip.IP()),
Prefix: bits,
}, nil
@@ -377,11 +378,11 @@ func ParseIP(s string) (*router.CIDR, error) {
}
}
func ParseDomainRule(ctx context.Context, domain string) ([]*router.Domain, error) {
func ParseDomainRule(ctx context.Context, domain string) ([]*routercommon.Domain, error) {
return parseDomainRule(ctx, domain)
}
func ToCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*router.GeoIP, error) {
func ToCidrList(ctx context.Context, ips cfgcommon.StringList) ([]*routercommon.GeoIP, error) {
return toCidrList(ctx, ips)
}

View File

@@ -5,11 +5,11 @@ package dns
import (
"context"
"encoding/json"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"sort"
"strings"
"github.com/v2fly/v2ray-core/v4/app/dns"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/platform"
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
@@ -56,15 +56,15 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
return newError("failed to parse name server: ", string(data))
}
func toDomainMatchingType(t router.Domain_Type) dns.DomainMatchingType {
func toDomainMatchingType(t routercommon.Domain_Type) dns.DomainMatchingType {
switch t {
case router.Domain_RootDomain:
case routercommon.Domain_RootDomain:
return dns.DomainMatchingType_Subdomain
case router.Domain_Full:
case routercommon.Domain_Full:
return dns.DomainMatchingType_Full
case router.Domain_Plain:
case routercommon.Domain_Plain:
return dns.DomainMatchingType_Keyword
case router.Domain_Regex:
case routercommon.Domain_Regex:
return dns.DomainMatchingType_Regex
default:
panic("unknown domain type")
@@ -131,11 +131,11 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
}, nil
}
var typeMap = map[router.Domain_Type]dns.DomainMatchingType{
router.Domain_Full: dns.DomainMatchingType_Full,
router.Domain_RootDomain: dns.DomainMatchingType_Subdomain,
router.Domain_Plain: dns.DomainMatchingType_Keyword,
router.Domain_Regex: dns.DomainMatchingType_Regex,
var typeMap = map[routercommon.Domain_Type]dns.DomainMatchingType{
routercommon.Domain_Full: dns.DomainMatchingType_Full,
routercommon.Domain_RootDomain: dns.DomainMatchingType_Subdomain,
routercommon.Domain_Plain: dns.DomainMatchingType_Keyword,
routercommon.Domain_Regex: dns.DomainMatchingType_Regex,
}
// DNSConfig is a JSON serializable object for dns.Config.