chore: add sourceGeoIP and sourceIPASN to metadata

This commit is contained in:
wwqgtxx
2024-08-28 12:25:45 +08:00
parent 8483178524
commit 4fecf68b8b
17 changed files with 211 additions and 211 deletions

View File

@@ -3,6 +3,7 @@ package common
import (
"errors"
"fmt"
"net/netip"
"strings"
"github.com/metacubex/mihomo/component/geodata"
@@ -11,6 +12,8 @@ import (
"github.com/metacubex/mihomo/component/resolver"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
"golang.org/x/exp/slices"
)
type GEOIP struct {
@@ -41,52 +44,84 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
}
if g.country == "lan" {
return ip.IsPrivate() ||
ip.IsUnspecified() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
resolver.IsFakeBroadcastIP(ip), g.adapter
return g.isLan(ip), g.adapter
}
for _, code := range metadata.DstGeoIP {
if g.country == code {
return true, g.adapter
}
}
if !C.GeodataMode {
if C.GeodataMode {
if g.isSourceIP {
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
for _, code := range codes {
if g.country == code {
return true, g.adapter
}
if slices.Contains(metadata.SrcGeoIP, g.country) {
return true, g.adapter
}
return false, g.adapter
}
if metadata.DstGeoIP != nil {
return false, g.adapter
}
metadata.DstGeoIP = mmdb.IPInstance().LookupCode(ip.AsSlice())
for _, code := range metadata.DstGeoIP {
if g.country == code {
} else {
if slices.Contains(metadata.DstGeoIP, g.country) {
return true, g.adapter
}
}
return false, g.adapter
matcher, err := g.getIPMatcher()
if err != nil {
return false, ""
}
match := matcher.Match(ip)
if match {
if g.isSourceIP {
metadata.SrcGeoIP = append(metadata.SrcGeoIP, g.country)
} else {
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
}
}
return match, g.adapter
}
matcher, err := g.GetIPMatcher()
if err != nil {
return false, ""
if g.isSourceIP {
if metadata.SrcGeoIP != nil {
return slices.Contains(metadata.SrcGeoIP, g.country), g.adapter
}
} else {
if metadata.DstGeoIP != nil {
return slices.Contains(metadata.DstGeoIP, g.country), g.adapter
}
}
match := matcher.Match(ip)
if match && !g.isSourceIP {
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
if g.isSourceIP {
metadata.SrcGeoIP = codes
} else {
metadata.DstGeoIP = codes
}
return match, g.adapter
if slices.Contains(codes, g.country) {
return true, g.adapter
}
return false, ""
}
// MatchIp implements C.IpMatcher
func (g *GEOIP) MatchIp(ip netip.Addr) bool {
if !ip.IsValid() {
return false
}
if g.country == "lan" {
return g.isLan(ip)
}
if C.GeodataMode {
matcher, err := g.getIPMatcher()
if err != nil {
return false
}
return matcher.Match(ip)
}
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
return slices.Contains(codes, g.country)
}
func (g *GEOIP) isLan(ip netip.Addr) bool {
return ip.IsPrivate() ||
ip.IsUnspecified() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
resolver.IsFakeBroadcastIP(ip)
}
func (g *GEOIP) Adapter() string {
@@ -106,14 +141,19 @@ func (g *GEOIP) GetCountry() string {
}
func (g *GEOIP) GetIPMatcher() (router.IPMatcher, error) {
if g.geodata {
geoIPMatcher, err := geodata.LoadGeoIPMatcher(g.country)
if err != nil {
return nil, fmt.Errorf("[GeoIP] %w", err)
}
return geoIPMatcher, nil
if C.GeodataMode {
return g.getIPMatcher()
}
return nil, errors.New("geoip country not set")
return nil, errors.New("not geodata mode")
}
func (g *GEOIP) getIPMatcher() (router.IPMatcher, error) {
geoIPMatcher, err := geodata.LoadGeoIPMatcher(g.country)
if err != nil {
return nil, fmt.Errorf("[GeoIP] %w", err)
}
return geoIPMatcher, nil
}
func (g *GEOIP) GetRecodeSize() int {
@@ -141,12 +181,13 @@ func NewGEOIP(country string, adapter string, isSrc, noResolveIP bool) (*GEOIP,
return geoip, nil
}
geoip.geodata = true
geoIPMatcher, err := geoip.GetIPMatcher() // test load
if err != nil {
return nil, err
if C.GeodataMode {
geoIPMatcher, err := geoip.getIPMatcher() // test load
if err != nil {
return nil, err
}
log.Infoln("Finished initial GeoIP rule %s => %s, records: %d", country, adapter, geoIPMatcher.Count())
}
log.Infoln("Finished initial GeoIP rule %s => %s, records: %d", country, adapter, geoIPMatcher.Count())
return geoip, nil
}

View File

@@ -23,15 +23,19 @@ func (gs *GEOSITE) RuleType() C.RuleType {
}
func (gs *GEOSITE) Match(metadata *C.Metadata) (bool, string) {
domain := metadata.RuleHost()
return gs.MatchDomain(metadata.RuleHost()), gs.adapter
}
// MatchDomain implements C.DomainMatcher
func (gs *GEOSITE) MatchDomain(domain string) bool {
if len(domain) == 0 {
return false, ""
return false
}
matcher, err := gs.GetDomainMatcher()
if err != nil {
return false, ""
return false
}
return matcher.ApplyDomain(domain), gs.adapter
return matcher.ApplyDomain(domain)
}
func (gs *GEOSITE) Adapter() string {

View File

@@ -28,8 +28,11 @@ func (a *ASN) Match(metadata *C.Metadata) (bool, string) {
result := mmdb.ASNInstance().LookupASN(ip.AsSlice())
asnNumber := strconv.FormatUint(uint64(result.AutonomousSystemNumber), 10)
if !a.isSourceIP {
metadata.DstIPASN = asnNumber + " " + result.AutonomousSystemOrganization
ipASN := asnNumber + " " + result.AutonomousSystemOrganization
if a.isSourceIP {
metadata.SrcIPASN = ipASN
} else {
metadata.DstIPASN = ipASN
}
match := a.asn == asnNumber

View File

@@ -1,40 +0,0 @@
package provider
import (
"github.com/metacubex/mihomo/component/trie"
C "github.com/metacubex/mihomo/constant"
)
type DomainSet struct {
*domainStrategy
adapter string
}
func (d *DomainSet) ProviderNames() []string {
return nil
}
func (d *DomainSet) RuleType() C.RuleType {
return C.DomainSet
}
func (d *DomainSet) Match(metadata *C.Metadata) (bool, string) {
return d.domainStrategy.Match(metadata), d.adapter
}
func (d *DomainSet) Adapter() string {
return d.adapter
}
func (d *DomainSet) Payload() string {
return ""
}
func NewDomainSet(domainSet *trie.DomainSet, adapter string) *DomainSet {
return &DomainSet{
domainStrategy: &domainStrategy{domainSet: domainSet},
adapter: adapter,
}
}
var _ C.Rule = (*DomainSet)(nil)

View File

@@ -1,40 +0,0 @@
package provider
import (
"github.com/metacubex/mihomo/component/cidr"
C "github.com/metacubex/mihomo/constant"
)
type IpCidrSet struct {
*ipcidrStrategy
adapter string
}
func (d *IpCidrSet) ProviderNames() []string {
return nil
}
func (d *IpCidrSet) RuleType() C.RuleType {
return C.IpCidrSet
}
func (d *IpCidrSet) Match(metadata *C.Metadata) (bool, string) {
return d.ipcidrStrategy.Match(metadata), d.adapter
}
func (d *IpCidrSet) Adapter() string {
return d.adapter
}
func (d *IpCidrSet) Payload() string {
return ""
}
func NewIpCidrSet(cidrSet *cidr.IpCidrSet, adapter string) *IpCidrSet {
return &IpCidrSet{
ipcidrStrategy: &ipcidrStrategy{cidrSet: cidrSet},
adapter: adapter,
}
}
var _ C.Rule = (*IpCidrSet)(nil)

View File

@@ -1,6 +1,8 @@
package provider
import (
"net/netip"
C "github.com/metacubex/mihomo/constant"
P "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/rules/common"
@@ -35,6 +37,18 @@ func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
return false, ""
}
// MatchDomain implements C.DomainMatcher
func (rs *RuleSet) MatchDomain(domain string) bool {
ok, _ := rs.Match(&C.Metadata{Host: domain})
return ok
}
// MatchIp implements C.IpMatcher
func (rs *RuleSet) MatchIp(ip netip.Addr) bool {
ok, _ := rs.Match(&C.Metadata{DstIP: ip})
return ok
}
func (rs *RuleSet) Adapter() string {
return rs.adapter
}