mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Fix match geoip
This commit is contained in:
parent
86ea035bdd
commit
960d04d172
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
N "github.com/sagernet/sing/common/network"
|
|
||||||
|
|
||||||
"github.com/oschwald/maxminddb-golang"
|
"github.com/oschwald/maxminddb-golang"
|
||||||
)
|
)
|
||||||
|
@ -31,8 +30,5 @@ func (r *Reader) Lookup(addr netip.Addr) string {
|
||||||
if code != "" {
|
if code != "" {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
if !N.IsPublicAddr(addr) {
|
|
||||||
return "private"
|
|
||||||
}
|
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package route
|
package route
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ RuleItem = (*GeoIPItem)(nil)
|
var _ RuleItem = (*GeoIPItem)(nil)
|
||||||
|
@ -32,29 +34,50 @@ func NewGeoIPItem(router adapter.Router, logger log.ContextLogger, isSource bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *GeoIPItem) Match(metadata *adapter.InboundContext) bool {
|
func (r *GeoIPItem) Match(metadata *adapter.InboundContext) bool {
|
||||||
geoReader := r.router.GeoIPReader()
|
var geoipCode string
|
||||||
if geoReader == nil {
|
if r.isSource && metadata.SourceGeoIPCode != "" {
|
||||||
return false
|
geoipCode = metadata.SourceGeoIPCode
|
||||||
|
} else if !r.isSource && metadata.GeoIPCode != "" {
|
||||||
|
geoipCode = metadata.GeoIPCode
|
||||||
}
|
}
|
||||||
|
if geoipCode != "" {
|
||||||
|
return r.codeMap[geoipCode]
|
||||||
|
}
|
||||||
|
var destination netip.Addr
|
||||||
if r.isSource {
|
if r.isSource {
|
||||||
if metadata.SourceGeoIPCode == "" {
|
destination = metadata.Source.Addr
|
||||||
metadata.SourceGeoIPCode = geoReader.Lookup(metadata.Source.Addr)
|
|
||||||
}
|
|
||||||
return r.codeMap[metadata.SourceGeoIPCode]
|
|
||||||
} else {
|
} else {
|
||||||
if metadata.Destination.IsIP() {
|
destination = metadata.Destination.Addr
|
||||||
if metadata.GeoIPCode == "" {
|
|
||||||
metadata.GeoIPCode = geoReader.Lookup(metadata.Destination.Addr)
|
|
||||||
}
|
}
|
||||||
return r.codeMap[metadata.GeoIPCode]
|
if destination.IsValid() {
|
||||||
|
return r.match(metadata, destination)
|
||||||
}
|
}
|
||||||
for _, address := range metadata.DestinationAddresses {
|
for _, destinationAddress := range metadata.DestinationAddresses {
|
||||||
if r.codeMap[geoReader.Lookup(address)] {
|
if r.match(metadata, destinationAddress) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *GeoIPItem) match(metadata *adapter.InboundContext, destination netip.Addr) bool {
|
||||||
|
var geoipCode string
|
||||||
|
geoReader := r.router.GeoIPReader()
|
||||||
|
if geoReader != nil {
|
||||||
|
geoipCode = geoReader.Lookup(destination)
|
||||||
|
}
|
||||||
|
if geoipCode == "" && !N.IsPublicAddr(destination) {
|
||||||
|
geoipCode = "private"
|
||||||
|
}
|
||||||
|
if geoipCode == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if r.isSource {
|
||||||
|
metadata.SourceGeoIPCode = geoipCode
|
||||||
|
} else {
|
||||||
|
metadata.GeoIPCode = geoipCode
|
||||||
|
}
|
||||||
|
return r.codeMap[geoipCode]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *GeoIPItem) String() string {
|
func (r *GeoIPItem) String() string {
|
||||||
|
|
Loading…
Reference in a new issue