chore: Increase support for other format of ASN
Some checks are pending
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run

This commit is contained in:
xishang0128 2024-11-04 19:31:43 +08:00
parent 3e966e82c7
commit a86c562852
2 changed files with 25 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import (
"net" "net"
"strings" "strings"
"github.com/metacubex/mihomo/log"
"github.com/oschwald/maxminddb-golang" "github.com/oschwald/maxminddb-golang"
) )
@ -23,11 +24,16 @@ type ASNReader struct {
*maxminddb.Reader *maxminddb.Reader
} }
type ASNResult struct { type GeoLite2 struct {
AutonomousSystemNumber uint32 `maxminddb:"autonomous_system_number"` AutonomousSystemNumber uint32 `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
} }
type IPInfo struct {
ASN string `maxminddb:"asn"`
Name string `maxminddb:"name"`
}
func (r IPReader) LookupCode(ipAddress net.IP) []string { func (r IPReader) LookupCode(ipAddress net.IP) []string {
switch r.databaseType { switch r.databaseType {
case typeMaxmind: case typeMaxmind:
@ -66,8 +72,18 @@ func (r IPReader) LookupCode(ipAddress net.IP) []string {
} }
} }
func (r ASNReader) LookupASN(ip net.IP) ASNResult { func (r ASNReader) LookupASN(ip net.IP) (string, string) {
var result ASNResult switch r.Metadata.DatabaseType {
r.Lookup(ip, &result) case "GeoLite2-ASN", "DBIP-ASN-Lite (compat=GeoLite2-ASN)":
return result var result GeoLite2
_ = r.Lookup(ip, &result)
return fmt.Sprint(result.AutonomousSystemNumber), result.AutonomousSystemOrganization
case "ipinfo generic_asn_free.mmdb":
var result IPInfo
_ = r.Lookup(ip, &result)
return result.ASN[2:], result.Name
default:
log.Warnln("Unsupported ASN type: %s", r.Metadata.DatabaseType)
}
return "", ""
} }

View File

@ -1,8 +1,6 @@
package common package common
import ( import (
"strconv"
"github.com/metacubex/mihomo/component/geodata" "github.com/metacubex/mihomo/component/geodata"
"github.com/metacubex/mihomo/component/mmdb" "github.com/metacubex/mihomo/component/mmdb"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
@ -26,17 +24,14 @@ func (a *ASN) Match(metadata *C.Metadata) (bool, string) {
return false, "" return false, ""
} }
result := mmdb.ASNInstance().LookupASN(ip.AsSlice()) asn, aso := mmdb.ASNInstance().LookupASN(ip.AsSlice())
asnNumber := strconv.FormatUint(uint64(result.AutonomousSystemNumber), 10)
ipASN := asnNumber + " " + result.AutonomousSystemOrganization
if a.isSourceIP { if a.isSourceIP {
metadata.SrcIPASN = ipASN metadata.SrcIPASN = asn + " " + aso
} else { } else {
metadata.DstIPASN = ipASN metadata.DstIPASN = asn + " " + aso
} }
match := a.asn == asnNumber return a.asn == asn, a.adapter
return match, a.adapter
} }
func (a *ASN) RuleType() C.RuleType { func (a *ASN) RuleType() C.RuleType {