github.com/yaling888/clash@v1.53.0/component/geodata/utils.go (about)

     1  package geodata
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/yaling888/clash/component/geodata/router"
     7  )
     8  
     9  func loadGeoSiteMatcher(countryCode string, not bool) (*router.DomainMatcher, int, error) {
    10  	geoLoaderName := "standard"
    11  	geoLoader, err := GetGeoDataLoader(geoLoaderName)
    12  	if err != nil {
    13  		return nil, 0, err
    14  	}
    15  
    16  	domains, err := geoLoader.LoadGeoSite(countryCode)
    17  	if err != nil {
    18  		return nil, 0, err
    19  	}
    20  
    21  	/**
    22  	linear: linear algorithm
    23  	matcher, err := router.NewDomainMatcher(domains, not)
    24  	mph:minimal perfect hash algorithm
    25  	*/
    26  	matcher, err := router.NewMphMatcherGroup(domains, not)
    27  	if err != nil {
    28  		return nil, 0, err
    29  	}
    30  
    31  	return matcher, len(domains), nil
    32  }
    33  
    34  var ruleProviders map[string]*router.DomainMatcher
    35  
    36  func CleanGeoSiteCache() {
    37  	clear(ruleProviders)
    38  	ruleProviders = nil
    39  }
    40  
    41  func LoadProviderByCode(countryCode string) (matcher *router.DomainMatcher, count int, err error) {
    42  	countryCode = strings.ToLower(countryCode)
    43  	if ruleProviders == nil {
    44  		ruleProviders = make(map[string]*router.DomainMatcher)
    45  	}
    46  	var (
    47  		ok   bool
    48  		not  = strings.HasPrefix(countryCode, "!")
    49  		code = strings.TrimPrefix(countryCode, "!")
    50  	)
    51  	matcher, ok = ruleProviders[countryCode]
    52  	if !ok {
    53  		if matcher, count, err = loadGeoSiteMatcher(code, not); err == nil {
    54  			ruleProviders[countryCode] = matcher
    55  		}
    56  	}
    57  	return
    58  }