mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 16:57:08 +00:00
feat: add IP-ASN rule
This commit is contained in:
@@ -52,7 +52,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
|
||||
if metadata.DstGeoIP != nil {
|
||||
return false, g.adapter
|
||||
}
|
||||
metadata.DstGeoIP = mmdb.Instance().LookupCode(ip.AsSlice())
|
||||
metadata.DstGeoIP = mmdb.IPInstance().LookupCode(ip.AsSlice())
|
||||
for _, code := range metadata.DstGeoIP {
|
||||
if g.country == code {
|
||||
return true, g.adapter
|
||||
|
||||
67
rules/common/ipasn.go
Normal file
67
rules/common/ipasn.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/metacubex/mihomo/component/geodata"
|
||||
"github.com/metacubex/mihomo/component/mmdb"
|
||||
C "github.com/metacubex/mihomo/constant"
|
||||
"github.com/metacubex/mihomo/log"
|
||||
)
|
||||
|
||||
type ASN struct {
|
||||
*Base
|
||||
asn string
|
||||
adapter string
|
||||
noResolveIP bool
|
||||
}
|
||||
|
||||
func (a *ASN) Match(metadata *C.Metadata) (bool, string) {
|
||||
ip := metadata.DstIP
|
||||
if !ip.IsValid() {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
result := mmdb.ASNInstance().LookupASN(ip.AsSlice())
|
||||
|
||||
asnNumber := strconv.FormatUint(uint64(result.AutonomousSystemNumber), 10)
|
||||
metadata.DstIPASN = asnNumber + " " + result.AutonomousSystemOrganization
|
||||
|
||||
match := a.asn == asnNumber
|
||||
return match, a.adapter
|
||||
}
|
||||
|
||||
func (a *ASN) RuleType() C.RuleType {
|
||||
return C.IPASN
|
||||
}
|
||||
|
||||
func (a *ASN) Adapter() string {
|
||||
return a.adapter
|
||||
}
|
||||
|
||||
func (a *ASN) Payload() string {
|
||||
return a.asn
|
||||
}
|
||||
|
||||
func (a *ASN) ShouldResolveIP() bool {
|
||||
return !a.noResolveIP
|
||||
}
|
||||
|
||||
func (a *ASN) GetASN() string {
|
||||
return a.asn
|
||||
}
|
||||
|
||||
func NewIPASN(asn string, adapter string, noResolveIP bool) (*ASN, error) {
|
||||
C.ASNEnable = true
|
||||
if err := geodata.InitASN(); err != nil {
|
||||
log.Errorln("can't initial ASN: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ASN{
|
||||
Base: &Base{},
|
||||
asn: asn,
|
||||
adapter: adapter,
|
||||
noResolveIP: noResolveIP,
|
||||
}, nil
|
||||
}
|
||||
@@ -27,6 +27,9 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
|
||||
case "IP-CIDR", "IP-CIDR6":
|
||||
noResolve := RC.HasNoResolve(params)
|
||||
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":
|
||||
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
|
||||
case "IP-SUFFIX":
|
||||
|
||||
Reference in New Issue
Block a user