2021-11-17 16:03:47 +08:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-13 11:44:02 +08:00
|
|
|
"net/netip"
|
2021-11-17 16:03:47 +08:00
|
|
|
"strings"
|
|
|
|
|
2024-01-13 11:44:02 +08:00
|
|
|
"github.com/metacubex/mihomo/component/cidr"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/geodata/strmatcher"
|
2023-12-17 00:00:35 +08:00
|
|
|
"github.com/metacubex/mihomo/component/trie"
|
2021-11-17 16:03:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var matcherTypeMap = map[Domain_Type]strmatcher.Type{
|
|
|
|
Domain_Plain: strmatcher.Substr,
|
|
|
|
Domain_Regex: strmatcher.Regex,
|
|
|
|
Domain_Domain: strmatcher.Domain,
|
|
|
|
Domain_Full: strmatcher.Full,
|
|
|
|
}
|
|
|
|
|
|
|
|
func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
|
|
|
|
matcherType, f := matcherTypeMap[domain.Type]
|
|
|
|
if !f {
|
|
|
|
return nil, fmt.Errorf("unsupported domain type %v", domain.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
matcher, err := matcherType.New(domain.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create domain matcher, base error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return matcher, nil
|
|
|
|
}
|
|
|
|
|
2023-12-17 00:00:35 +08:00
|
|
|
type DomainMatcher interface {
|
|
|
|
ApplyDomain(string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type succinctDomainMatcher struct {
|
|
|
|
set *trie.DomainSet
|
|
|
|
otherMatchers []strmatcher.Matcher
|
|
|
|
not bool
|
|
|
|
}
|
|
|
|
|
2023-12-23 00:05:07 +08:00
|
|
|
func (m *succinctDomainMatcher) ApplyDomain(domain string) bool {
|
2023-12-17 00:00:35 +08:00
|
|
|
isMatched := m.set.Has(domain)
|
|
|
|
if !isMatched {
|
|
|
|
for _, matcher := range m.otherMatchers {
|
|
|
|
isMatched = matcher.Match(domain)
|
|
|
|
if isMatched {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.not {
|
|
|
|
isMatched = !isMatched
|
|
|
|
}
|
|
|
|
return isMatched
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSuccinctMatcherGroup(domains []*Domain, not bool) (DomainMatcher, error) {
|
|
|
|
t := trie.New[struct{}]()
|
|
|
|
m := &succinctDomainMatcher{
|
|
|
|
not: not,
|
|
|
|
}
|
|
|
|
for _, d := range domains {
|
|
|
|
switch d.Type {
|
|
|
|
case Domain_Plain, Domain_Regex:
|
|
|
|
matcher, err := matcherTypeMap[d.Type].New(d.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.otherMatchers = append(m.otherMatchers, matcher)
|
|
|
|
|
|
|
|
case Domain_Domain:
|
|
|
|
err := t.Insert("+."+d.Value, struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case Domain_Full:
|
|
|
|
err := t.Insert(d.Value, struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.set = t.NewDomainSet()
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type v2rayDomainMatcher struct {
|
2021-11-17 16:03:47 +08:00
|
|
|
matchers strmatcher.IndexMatcher
|
2022-05-15 13:16:45 +08:00
|
|
|
not bool
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
2023-12-17 00:00:35 +08:00
|
|
|
func NewMphMatcherGroup(domains []*Domain, not bool) (DomainMatcher, error) {
|
2021-11-17 16:03:47 +08:00
|
|
|
g := strmatcher.NewMphMatcherGroup()
|
|
|
|
for _, d := range domains {
|
|
|
|
matcherType, f := matcherTypeMap[d.Type]
|
|
|
|
if !f {
|
|
|
|
return nil, fmt.Errorf("unsupported domain type %v", d.Type)
|
|
|
|
}
|
|
|
|
_, err := g.AddPattern(d.Value, matcherType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Build()
|
2023-12-17 00:00:35 +08:00
|
|
|
return &v2rayDomainMatcher{
|
2021-11-17 16:03:47 +08:00
|
|
|
matchers: g,
|
2022-05-15 13:16:45 +08:00
|
|
|
not: not,
|
2021-11-17 16:03:47 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-17 00:00:35 +08:00
|
|
|
func (m *v2rayDomainMatcher) ApplyDomain(domain string) bool {
|
2022-05-15 13:16:45 +08:00
|
|
|
isMatched := len(m.matchers.Match(strings.ToLower(domain))) > 0
|
|
|
|
if m.not {
|
|
|
|
isMatched = !isMatched
|
|
|
|
}
|
|
|
|
return isMatched
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
2022-01-26 12:01:14 +08:00
|
|
|
|
2022-03-17 23:24:07 +08:00
|
|
|
type GeoIPMatcher struct {
|
|
|
|
countryCode string
|
|
|
|
reverseMatch bool
|
2024-01-13 11:44:02 +08:00
|
|
|
cidrSet *cidr.IpCidrSet
|
2022-03-17 23:24:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *GeoIPMatcher) Init(cidrs []*CIDR) error {
|
|
|
|
for _, cidr := range cidrs {
|
2024-01-13 11:44:02 +08:00
|
|
|
addr, ok := netip.AddrFromSlice(cidr.Ip)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("error when loading GeoIP: invalid IP: %s", cidr.Ip)
|
2022-03-17 23:24:07 +08:00
|
|
|
}
|
2024-01-13 11:44:02 +08:00
|
|
|
err := m.cidrSet.AddIpCidr(netip.PrefixFrom(addr, int(cidr.Prefix)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error when loading GeoIP: %w", err)
|
2022-03-17 23:24:07 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-13 11:44:02 +08:00
|
|
|
m.cidrSet.Merge()
|
2022-03-17 23:24:07 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
|
|
|
|
m.reverseMatch = isReverseMatch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if the given ip is included by the GeoIP.
|
2024-01-13 11:44:02 +08:00
|
|
|
func (m *GeoIPMatcher) Match(ip netip.Addr) bool {
|
|
|
|
match := m.cidrSet.IsContain(ip)
|
|
|
|
if m.reverseMatch {
|
|
|
|
return !match
|
2022-03-17 23:24:07 +08:00
|
|
|
}
|
2024-01-13 11:44:02 +08:00
|
|
|
return match
|
2022-03-17 23:24:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
|
|
|
|
type GeoIPMatcherContainer struct {
|
|
|
|
matchers []*GeoIPMatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a new GeoIP set into the container.
|
|
|
|
// If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
|
|
|
|
func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
|
|
|
|
if len(geoip.CountryCode) > 0 {
|
|
|
|
for _, m := range c.matchers {
|
|
|
|
if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.ReverseMatch {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &GeoIPMatcher{
|
|
|
|
countryCode: geoip.CountryCode,
|
|
|
|
reverseMatch: geoip.ReverseMatch,
|
2024-01-13 11:44:02 +08:00
|
|
|
cidrSet: cidr.NewIpCidrSet(),
|
2022-03-17 23:24:07 +08:00
|
|
|
}
|
|
|
|
if err := m.Init(geoip.Cidr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(geoip.CountryCode) > 0 {
|
|
|
|
c.matchers = append(c.matchers, m)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalGeoIPContainer GeoIPMatcherContainer
|
|
|
|
|
2022-01-26 12:01:14 +08:00
|
|
|
type MultiGeoIPMatcher struct {
|
|
|
|
matchers []*GeoIPMatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGeoIPMatcher(geoip *GeoIP) (*GeoIPMatcher, error) {
|
|
|
|
matcher, err := globalGeoIPContainer.Add(geoip)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return matcher, nil
|
|
|
|
}
|
|
|
|
|
2024-01-13 11:44:02 +08:00
|
|
|
func (m *MultiGeoIPMatcher) ApplyIp(ip netip.Addr) bool {
|
2022-01-26 12:01:14 +08:00
|
|
|
for _, matcher := range m.matchers {
|
|
|
|
if matcher.Match(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiGeoIPMatcher(geoips []*GeoIP) (*MultiGeoIPMatcher, error) {
|
|
|
|
var matchers []*GeoIPMatcher
|
|
|
|
for _, geoip := range geoips {
|
|
|
|
matcher, err := globalGeoIPContainer.Add(geoip)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
matchers = append(matchers, matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
matcher := &MultiGeoIPMatcher{
|
|
|
|
matchers: matchers,
|
|
|
|
}
|
|
|
|
|
|
|
|
return matcher, nil
|
|
|
|
}
|