Skip to content

Commit

Permalink
Merge branch 'MetaCubeX:Alpha' into Alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
xishang0128 authored Mar 8, 2024
2 parents aec1584 + 6b74ade commit 43f4f31
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
9 changes: 4 additions & 5 deletions component/mmdb/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"

"github.com/oschwald/maxminddb-golang"
"github.com/sagernet/sing/common"
)

type geoip2Country struct {
Expand Down Expand Up @@ -43,11 +44,9 @@ func (r Reader) LookupCode(ipAddress net.IP) []string {
case string:
return []string{record}
case []any: // lookup returned type of slice is []any
result := make([]string, 0, len(record))
for _, item := range record {
result = append(result, item.(string))
}
return result
return common.Map(record, func(it any) string {
return it.(string)
})
}
return []string{}

Expand Down
1 change: 0 additions & 1 deletion constant/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ 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
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"`
Expand Down
34 changes: 9 additions & 25 deletions rules/common/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type GEOIP struct {
recodeSize int
}

var _ C.Rule = (*GEOIP)(nil)

func (g *GEOIP) RuleType() C.RuleType {
return C.GEOIP
}
Expand All @@ -33,39 +31,24 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
return false, ""
}

if g.country == "lan" {
if strings.EqualFold(g.country, "LAN") {
return ip.IsPrivate() ||
ip.IsUnspecified() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
resolver.IsFakeBroadcastIP(ip), g.adapter
}

for _, code := range metadata.DstGeoIP {
if g.country == code {
return true, g.adapter
}
}

if !C.GeodataMode {
if metadata.DstGeoIP != nil {
return false, g.adapter
}
metadata.DstGeoIP = mmdb.Instance().LookupCode(ip.AsSlice())
for _, code := range metadata.DstGeoIP {
if g.country == code {
codes := mmdb.Instance().LookupCode(ip.AsSlice())
for _, code := range codes {
if strings.EqualFold(code, g.country) {
return true, g.adapter
}
}
return false, g.adapter
}

match := g.geoIPMatcher.Match(ip)
if match {
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
}
return match, g.adapter
return g.geoIPMatcher.Match(ip), g.adapter
}

func (g *GEOIP) Adapter() string {
Expand Down Expand Up @@ -97,9 +80,8 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
log.Errorln("can't initial GeoIP: %s", err)
return nil, err
}
country = strings.ToLower(country)

if !C.GeodataMode || country == "lan" {
if !C.GeodataMode || strings.EqualFold(country, "LAN") {
geoip := &GEOIP{
Base: &Base{},
country: country,
Expand All @@ -111,7 +93,7 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)

geoIPMatcher, size, err := geodata.LoadGeoIPMatcher(country)
if err != nil {
return nil, fmt.Errorf("[GeoIP] %w", err)
return nil, fmt.Errorf("[GeoIP] %s", err.Error())
}

log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, size)
Expand All @@ -125,3 +107,5 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
}
return geoip, nil
}

//var _ C.Rule = (*GEOIP)(nil)

0 comments on commit 43f4f31

Please sign in to comment.