feat: add IP-ASN rule

This commit is contained in:
xishang0128
2024-03-12 03:14:25 +08:00
parent 7ad37ca0e3
commit 44d8a14629
15 changed files with 248 additions and 33 deletions

View File

@@ -1,10 +1,12 @@
package constant
var (
ASNEnable bool
GeodataMode bool
GeoAutoUpdate bool
GeoUpdateInterval int
GeoIpUrl string
MmdbUrl string
GeoSiteUrl string
ASNUrl string
)

View File

@@ -133,7 +133,8 @@ type Metadata struct {
Type Type `json:"type"`
SrcIP netip.Addr `json:"sourceIP"`
DstIP netip.Addr `json:"destinationIP"`
DstGeoIP []string `json:"destinationGeoIP"` // can be nil if never queried, empty slice if got no result
DstGeoIP []string `json:"destinationGeoIP"` // can be nil if never queried, empty slice if got no result
DstIPASN string `json:"destinationIPASN"`
SrcPort uint16 `json:"sourcePort,string"` // `,string` is used to compatible with old version json output
DstPort uint16 `json:"destinationPort,string"` // `,string` is used to compatible with old version json output
InIP netip.Addr `json:"inboundIP"`

View File

@@ -15,6 +15,7 @@ const Name = "mihomo"
var (
GeositeName = "GeoSite.dat"
GeoipName = "GeoIP.dat"
ASNName = "ASN.mmdb"
)
// Path is used to get the configuration path
@@ -112,6 +113,25 @@ func (p *path) MMDB() string {
return P.Join(p.homeDir, "geoip.metadb")
}
func (p *path) ASN() string {
files, err := os.ReadDir(p.homeDir)
if err != nil {
return ""
}
for _, fi := range files {
if fi.IsDir() {
// 目录则直接跳过
continue
} else {
if strings.EqualFold(fi.Name(), "ASN.mmdb") {
ASNName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
}
}
return P.Join(p.homeDir, ASNName)
}
func (p *path) OldCache() string {
return P.Join(p.homeDir, ".cache")
}

View File

@@ -9,6 +9,7 @@ const (
GEOSITE
GEOIP
IPCIDR
IPASN
SrcIPCIDR
IPSuffix
SrcIPSuffix
@@ -49,6 +50,8 @@ func (rt RuleType) String() string {
return "GeoIP"
case IPCIDR:
return "IPCIDR"
case IPASN:
return "IPASN"
case SrcIPCIDR:
return "SrcIPCIDR"
case IPSuffix: