mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-03-05 05:27:31 +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
|
||||
}
|
||||
Reference in New Issue
Block a user