chore: Add source matching for ip type rules

This commit is contained in:
xishang0128 2024-04-01 18:16:34 +08:00
parent f3743fc7f9
commit 3b472f786e
No known key found for this signature in database
GPG Key ID: 44A1E10B5ADF68CB
4 changed files with 50 additions and 12 deletions

View File

@ -8,8 +8,10 @@ const (
DomainRegex DomainRegex
GEOSITE GEOSITE
GEOIP GEOIP
IPCIDR SrcGEOIP
IPASN IPASN
SrcIPASN
IPCIDR
SrcIPCIDR SrcIPCIDR
IPSuffix IPSuffix
SrcIPSuffix SrcIPSuffix
@ -48,10 +50,14 @@ func (rt RuleType) String() string {
return "GeoSite" return "GeoSite"
case GEOIP: case GEOIP:
return "GeoIP" return "GeoIP"
case IPCIDR: case SrcGEOIP:
return "IPCIDR" return "SrcGeoIP"
case IPASN: case IPASN:
return "IPASN" return "IPASN"
case SrcIPASN:
return "SrcIPASN"
case IPCIDR:
return "IPCIDR"
case SrcIPCIDR: case SrcIPCIDR:
return "SrcIPCIDR" return "SrcIPCIDR"
case IPSuffix: case IPSuffix:

View File

@ -17,6 +17,7 @@ type GEOIP struct {
country string country string
adapter string adapter string
noResolveIP bool noResolveIP bool
isSourceIP bool
geoIPMatcher *router.GeoIPMatcher geoIPMatcher *router.GeoIPMatcher
recodeSize int recodeSize int
} }
@ -24,11 +25,17 @@ type GEOIP struct {
var _ C.Rule = (*GEOIP)(nil) var _ C.Rule = (*GEOIP)(nil)
func (g *GEOIP) RuleType() C.RuleType { func (g *GEOIP) RuleType() C.RuleType {
if g.isSourceIP {
return C.SrcGEOIP
}
return C.GEOIP return C.GEOIP
} }
func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) { func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP ip := metadata.DstIP
if g.isSourceIP {
ip = metadata.SrcIP
}
if !ip.IsValid() { if !ip.IsValid() {
return false, "" return false, ""
} }
@ -49,6 +56,16 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
} }
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
}
}
return false, g.adapter
}
if metadata.DstGeoIP != nil { if metadata.DstGeoIP != nil {
return false, g.adapter return false, g.adapter
} }
@ -62,7 +79,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
} }
match := g.geoIPMatcher.Match(ip) match := g.geoIPMatcher.Match(ip)
if match { if match && !g.isSourceIP {
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country) metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
} }
return match, g.adapter return match, g.adapter
@ -92,7 +109,7 @@ func (g *GEOIP) GetRecodeSize() int {
return g.recodeSize return g.recodeSize
} }
func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error) { func NewGEOIP(country string, adapter string, isSrc, noResolveIP bool) (*GEOIP, error) {
if err := geodata.InitGeoIP(); err != nil { if err := geodata.InitGeoIP(); err != nil {
log.Errorln("can't initial GeoIP: %s", err) log.Errorln("can't initial GeoIP: %s", err)
return nil, err return nil, err
@ -105,6 +122,7 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
country: country, country: country,
adapter: adapter, adapter: adapter,
noResolveIP: noResolveIP, noResolveIP: noResolveIP,
isSourceIP: isSrc,
} }
return geoip, nil return geoip, nil
} }
@ -120,6 +138,7 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
country: country, country: country,
adapter: adapter, adapter: adapter,
noResolveIP: noResolveIP, noResolveIP: noResolveIP,
isSourceIP: isSrc,
geoIPMatcher: geoIPMatcher, geoIPMatcher: geoIPMatcher,
recodeSize: size, recodeSize: size,
} }

View File

@ -14,24 +14,32 @@ type ASN struct {
asn string asn string
adapter string adapter string
noResolveIP bool noResolveIP bool
isSourceIP bool
} }
func (a *ASN) Match(metadata *C.Metadata) (bool, string) { func (a *ASN) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP ip := metadata.DstIP
if a.isSourceIP {
ip = metadata.SrcIP
}
if !ip.IsValid() { if !ip.IsValid() {
return false, "" return false, ""
} }
result := mmdb.ASNInstance().LookupASN(ip.AsSlice()) result := mmdb.ASNInstance().LookupASN(ip.AsSlice())
asnNumber := strconv.FormatUint(uint64(result.AutonomousSystemNumber), 10) asnNumber := strconv.FormatUint(uint64(result.AutonomousSystemNumber), 10)
metadata.DstIPASN = asnNumber + " " + result.AutonomousSystemOrganization if !a.isSourceIP {
metadata.DstIPASN = asnNumber + " " + result.AutonomousSystemOrganization
}
match := a.asn == asnNumber match := a.asn == asnNumber
return match, a.adapter return match, a.adapter
} }
func (a *ASN) RuleType() C.RuleType { func (a *ASN) RuleType() C.RuleType {
if a.isSourceIP {
return C.SrcIPASN
}
return C.IPASN return C.IPASN
} }
@ -51,7 +59,7 @@ func (a *ASN) GetASN() string {
return a.asn return a.asn
} }
func NewIPASN(asn string, adapter string, noResolveIP bool) (*ASN, error) { func NewIPASN(asn string, adapter string, isSrc, noResolveIP bool) (*ASN, error) {
C.ASNEnable = true C.ASNEnable = true
if err := geodata.InitASN(); err != nil { if err := geodata.InitASN(); err != nil {
log.Errorln("can't initial ASN: %s", err) log.Errorln("can't initial ASN: %s", err)
@ -63,5 +71,6 @@ func NewIPASN(asn string, adapter string, noResolveIP bool) (*ASN, error) {
asn: asn, asn: asn,
adapter: adapter, adapter: adapter,
noResolveIP: noResolveIP, noResolveIP: noResolveIP,
isSourceIP: isSrc,
}, nil }, nil
} }

View File

@ -23,13 +23,17 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
parsed, parseErr = RC.NewGEOSITE(payload, target) parsed, parseErr = RC.NewGEOSITE(payload, target)
case "GEOIP": case "GEOIP":
noResolve := RC.HasNoResolve(params) noResolve := RC.HasNoResolve(params)
parsed, parseErr = RC.NewGEOIP(payload, target, noResolve) parsed, parseErr = RC.NewGEOIP(payload, target, false, noResolve)
case "SRC-GEOIP":
parsed, parseErr = RC.NewGEOIP(payload, target, true, true)
case "IP-ASN":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RC.NewIPASN(payload, target, false, noResolve)
case "SRC-IP-ASN":
parsed, parseErr = RC.NewIPASN(payload, target, true, true)
case "IP-CIDR", "IP-CIDR6": case "IP-CIDR", "IP-CIDR6":
noResolve := RC.HasNoResolve(params) noResolve := RC.HasNoResolve(params)
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve)) parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve))
case "IP-ASN":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RC.NewIPASN(payload, target, noResolve)
case "SRC-IP-CIDR": case "SRC-IP-CIDR":
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true)) parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
case "IP-SUFFIX": case "IP-SUFFIX":