mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 06:43:14 +08:00
chore: Add GeoIP result to metadata
This commit is contained in:
parent
7eb16a098a
commit
d27340867f
@ -5,7 +5,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/oschwald/maxminddb-golang"
|
"github.com/oschwald/maxminddb-golang"
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type geoip2Country struct {
|
type geoip2Country struct {
|
||||||
@ -44,9 +43,11 @@ func (r Reader) LookupCode(ipAddress net.IP) []string {
|
|||||||
case string:
|
case string:
|
||||||
return []string{record}
|
return []string{record}
|
||||||
case []any: // lookup returned type of slice is []any
|
case []any: // lookup returned type of slice is []any
|
||||||
return common.Map(record, func(it any) string {
|
result := make([]string, 0, len(record))
|
||||||
return it.(string)
|
for _, item := range record {
|
||||||
})
|
result = append(result, item.(string))
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
return []string{}
|
return []string{}
|
||||||
|
|
||||||
|
@ -133,6 +133,7 @@ type Metadata struct {
|
|||||||
Type Type `json:"type"`
|
Type Type `json:"type"`
|
||||||
SrcIP netip.Addr `json:"sourceIP"`
|
SrcIP netip.Addr `json:"sourceIP"`
|
||||||
DstIP netip.Addr `json:"destinationIP"`
|
DstIP netip.Addr `json:"destinationIP"`
|
||||||
|
DstGeoIP []string `json:"destinationGeoIP"` // can be nil if never queried, empty slice if got no result
|
||||||
SrcPort uint16 `json:"sourcePort,string"` // `,string` is used to compatible with old version json output
|
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
|
DstPort uint16 `json:"destinationPort,string"` // `,string` is used to compatible with old version json output
|
||||||
InIP netip.Addr `json:"inboundIP"`
|
InIP netip.Addr `json:"inboundIP"`
|
||||||
|
@ -21,6 +21,8 @@ type GEOIP struct {
|
|||||||
recodeSize int
|
recodeSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ C.Rule = (*GEOIP)(nil)
|
||||||
|
|
||||||
func (g *GEOIP) RuleType() C.RuleType {
|
func (g *GEOIP) RuleType() C.RuleType {
|
||||||
return C.GEOIP
|
return C.GEOIP
|
||||||
}
|
}
|
||||||
@ -31,7 +33,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
|
|||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.EqualFold(g.country, "LAN") {
|
if g.country == "lan" {
|
||||||
return ip.IsPrivate() ||
|
return ip.IsPrivate() ||
|
||||||
ip.IsUnspecified() ||
|
ip.IsUnspecified() ||
|
||||||
ip.IsLoopback() ||
|
ip.IsLoopback() ||
|
||||||
@ -39,16 +41,31 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
|
|||||||
ip.IsLinkLocalUnicast() ||
|
ip.IsLinkLocalUnicast() ||
|
||||||
resolver.IsFakeBroadcastIP(ip), g.adapter
|
resolver.IsFakeBroadcastIP(ip), g.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, code := range metadata.DstGeoIP {
|
||||||
|
if g.country == code {
|
||||||
|
return true, g.adapter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !C.GeodataMode {
|
if !C.GeodataMode {
|
||||||
codes := mmdb.Instance().LookupCode(ip.AsSlice())
|
if metadata.DstGeoIP != nil {
|
||||||
for _, code := range codes {
|
return false, g.adapter
|
||||||
if strings.EqualFold(code, g.country) {
|
}
|
||||||
|
metadata.DstGeoIP = mmdb.Instance().LookupCode(ip.AsSlice())
|
||||||
|
for _, code := range metadata.DstGeoIP {
|
||||||
|
if g.country == code {
|
||||||
return true, g.adapter
|
return true, g.adapter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false, g.adapter
|
return false, g.adapter
|
||||||
}
|
}
|
||||||
return g.geoIPMatcher.Match(ip), g.adapter
|
|
||||||
|
match := g.geoIPMatcher.Match(ip)
|
||||||
|
if match {
|
||||||
|
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
|
||||||
|
}
|
||||||
|
return match, g.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GEOIP) Adapter() string {
|
func (g *GEOIP) Adapter() string {
|
||||||
@ -80,8 +97,9 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
|
|||||||
log.Errorln("can't initial GeoIP: %s", err)
|
log.Errorln("can't initial GeoIP: %s", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
country = strings.ToLower(country)
|
||||||
|
|
||||||
if !C.GeodataMode || strings.EqualFold(country, "LAN") {
|
if !C.GeodataMode || country == "lan" {
|
||||||
geoip := &GEOIP{
|
geoip := &GEOIP{
|
||||||
Base: &Base{},
|
Base: &Base{},
|
||||||
country: country,
|
country: country,
|
||||||
@ -93,7 +111,7 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
|
|||||||
|
|
||||||
geoIPMatcher, size, err := geodata.LoadGeoIPMatcher(country)
|
geoIPMatcher, size, err := geodata.LoadGeoIPMatcher(country)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("[GeoIP] %s", err.Error())
|
return nil, fmt.Errorf("[GeoIP] %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, size)
|
log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, size)
|
||||||
@ -107,5 +125,3 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
|
|||||||
}
|
}
|
||||||
return geoip, nil
|
return geoip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//var _ C.Rule = (*GEOIP)(nil)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user