Fix route

This commit is contained in:
世界 2022-07-04 19:34:45 +08:00
parent 792dc83778
commit 43cf0441db
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 69 additions and 27 deletions

View file

@ -19,6 +19,7 @@ import (
F "github.com/sagernet/sing/common/format"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
)
var _ adapter.Router = (*Router)(nil)
@ -82,7 +83,7 @@ func isGeoRule(rule option.DefaultRule) bool {
}
func notPrivateNode(code string) bool {
return code == "private"
return code != "private"
}
func (r *Router) Initialize(outbounds []adapter.Outbound, defaultOutbound func() adapter.Outbound) error {
@ -156,7 +157,10 @@ func (r *Router) Initialize(outbounds []adapter.Outbound, defaultOutbound func()
func (r *Router) Start() error {
if r.needGeoDatabase {
go r.prepareGeoIPDatabase()
err := r.prepareGeoIPDatabase()
if err != nil {
return err
}
}
return nil
}
@ -171,15 +175,17 @@ func (r *Router) GeoIPReader() *geoip2.Reader {
return r.geoReader
}
func (r *Router) prepareGeoIPDatabase() {
func (r *Router) prepareGeoIPDatabase() error {
var geoPath string
if r.geoOptions.Path != "" {
geoPath = r.geoOptions.Path
} else {
geoPath = "Country.mmdb"
if foundPath, loaded := C.Find(geoPath); loaded {
geoPath = foundPath
}
}
geoPath, loaded := C.Find(geoPath)
if !loaded {
if !rw.FileExists(geoPath) {
r.logger.Warn("geoip database not exists: ", geoPath)
var err error
for attempts := 0; attempts < 3; attempts++ {
@ -192,7 +198,7 @@ func (r *Router) prepareGeoIPDatabase() {
time.Sleep(10 * time.Second)
}
if err != nil {
return
return err
}
}
geoReader, err := geoip2.Open(geoPath)
@ -200,9 +206,9 @@ func (r *Router) prepareGeoIPDatabase() {
r.logger.Info("loaded geoip database")
r.geoReader = geoReader
} else {
r.logger.Error("open geoip database: ", err)
return
return E.Cause(err, "open geoip database")
}
return nil
}
func (r *Router) downloadGeoIPDatabase(savePath string) error {

View file

@ -41,9 +41,10 @@ func NewRule(router adapter.Router, logger log.Logger, options option.Rule) (ada
var _ adapter.Rule = (*DefaultRule)(nil)
type DefaultRule struct {
index int
outbound string
items []RuleItem
items []RuleItem
sourceAddressItems []RuleItem
destinationAddressItems []RuleItem
outbound string
}
type RuleItem interface {
@ -78,37 +79,37 @@ func NewDefaultRule(router adapter.Router, logger log.Logger, options option.Def
rule.items = append(rule.items, NewProtocolItem(options.Protocol))
}
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
rule.items = append(rule.items, NewDomainItem(options.Domain, options.DomainSuffix))
rule.destinationAddressItems = append(rule.destinationAddressItems, NewDomainItem(options.Domain, options.DomainSuffix))
}
if len(options.DomainKeyword) > 0 {
rule.items = append(rule.items, NewDomainKeywordItem(options.DomainKeyword))
rule.destinationAddressItems = append(rule.destinationAddressItems, NewDomainKeywordItem(options.DomainKeyword))
}
if len(options.DomainRegex) > 0 {
item, err := NewDomainRegexItem(options.DomainRegex)
if err != nil {
return nil, E.Cause(err, "domain_regex")
}
rule.items = append(rule.items, item)
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
}
if len(options.SourceGeoIP) > 0 {
rule.items = append(rule.items, NewGeoIPItem(router, logger, true, options.SourceGeoIP))
rule.sourceAddressItems = append(rule.sourceAddressItems, NewGeoIPItem(router, logger, true, options.SourceGeoIP))
}
if len(options.GeoIP) > 0 {
rule.items = append(rule.items, NewGeoIPItem(router, logger, false, options.GeoIP))
rule.destinationAddressItems = append(rule.destinationAddressItems, NewGeoIPItem(router, logger, false, options.GeoIP))
}
if len(options.SourceIPCIDR) > 0 {
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
if err != nil {
return nil, E.Cause(err, "source_ipcidr")
}
rule.items = append(rule.items, item)
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
}
if len(options.IPCIDR) > 0 {
item, err := NewIPCIDRItem(false, options.IPCIDR)
if err != nil {
return nil, E.Cause(err, "ipcidr")
}
rule.items = append(rule.items, item)
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
}
if len(options.SourcePort) > 0 {
rule.items = append(rule.items, NewPortItem(true, options.SourcePort))
@ -121,11 +122,38 @@ func NewDefaultRule(router adapter.Router, logger log.Logger, options option.Def
func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
for _, item := range r.items {
if item.Match(metadata) {
return true
if !item.Match(metadata) {
return false
}
}
return false
if len(r.sourceAddressItems) > 0 {
var sourceAddressMatch bool
for _, item := range r.sourceAddressItems {
if item.Match(metadata) {
sourceAddressMatch = true
break
}
}
if !sourceAddressMatch {
return false
}
}
if len(r.destinationAddressItems) > 0 {
var destinationAddressMatch bool
for _, item := range r.destinationAddressItems {
if item.Match(metadata) {
destinationAddressMatch = true
break
}
}
if !destinationAddressMatch {
return false
}
}
return true
}
func (r *DefaultRule) Outbound() string {